1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef GrTexture_DEFINED
10#define GrTexture_DEFINED
11
12#include "include/core/SkImage.h"
13#include "include/core/SkPoint.h"
14#include "include/core/SkRefCnt.h"
15#include "include/gpu/GrBackendSurface.h"
16#include "include/private/GrTypesPriv.h"
17#include "src/gpu/GrSurface.h"
18
19class GrTexture : virtual public GrSurface {
20public:
21 GrTexture* asTexture() override { return this; }
22 const GrTexture* asTexture() const override { return this; }
23
24 virtual GrBackendTexture getBackendTexture() const = 0;
25
26 /**
27 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
28 * changed externally to Skia.
29 */
30 virtual void textureParamsModified() = 0;
31
32 /**
33 * This function steals the backend texture from a uniquely owned GrTexture with no pending
34 * IO, passing it out to the caller. The GrTexture is deleted in the process.
35 *
36 * Note that if the GrTexture is not uniquely owned (no other refs), or has pending IO, this
37 * function will fail.
38 */
39 static bool StealBackendTexture(sk_sp<GrTexture>,
40 GrBackendTexture*,
41 SkImage::BackendTextureReleaseProc*);
42
43 /** See addIdleProc. */
44 enum class IdleState {
45 kFlushed,
46 kFinished
47 };
48 /**
49 * Installs a proc on this texture. It will be called when the texture becomes "idle". There
50 * are two types of idle states as indicated by IdleState. For managed backends (e.g. GL where
51 * a driver typically handles CPU/GPU synchronization of resource access) there is no difference
52 * between the two. They both mean "all work related to the resource has been flushed to the
53 * backend API and the texture is not owned outside the resource cache".
54 *
55 * If the API is unmanaged (e.g. Vulkan) then kFinished has the additional constraint that the
56 * work flushed to the GPU is finished.
57 */
58 virtual void addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState) {
59 // This is the default implementation for the managed case where the IdleState can be
60 // ignored. Unmanaged backends, e.g. Vulkan, must override this to consider IdleState.
61 fIdleProcs.push_back(std::move(idleProc));
62 }
63 /** Helper version of addIdleProc that creates the ref-counted wrapper. */
64 void addIdleProc(GrRefCntedCallback::Callback callback,
65 GrRefCntedCallback::Context context,
66 IdleState state) {
67 this->addIdleProc(sk_make_sp<GrRefCntedCallback>(callback, context), state);
68 }
69
70 GrTextureType textureType() const { return fTextureType; }
71 bool hasRestrictedSampling() const {
72 return GrTextureTypeHasRestrictedSampling(this->textureType());
73 }
74
75 void markMipmapsDirty();
76 void markMipmapsClean();
77 GrMipmapped mipmapped() const {
78 return GrMipmapped(fMipmapStatus != GrMipmapStatus::kNotAllocated);
79 }
80 bool mipmapsAreDirty() const { return fMipmapStatus != GrMipmapStatus::kValid; }
81 GrMipmapStatus mipmapStatus() const { return fMipmapStatus; }
82 int maxMipmapLevel() const { return fMaxMipmapLevel; }
83
84 static void ComputeScratchKey(const GrCaps& caps,
85 const GrBackendFormat& format,
86 SkISize dimensions,
87 GrRenderable,
88 int sampleCnt,
89 GrMipmapped,
90 GrProtected,
91 GrScratchKey* key);
92
93protected:
94 GrTexture(GrGpu*, const SkISize&, GrProtected, GrTextureType, GrMipmapStatus);
95
96 virtual bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) = 0;
97
98 SkTArray<sk_sp<GrRefCntedCallback>> fIdleProcs;
99
100 void willRemoveLastRef() override {
101 // We're about to be idle in the resource cache. Do our part to trigger the idle callbacks.
102 fIdleProcs.reset();
103 }
104 virtual void callIdleProcsOnBehalfOfResource() {}
105 void computeScratchKey(GrScratchKey*) const override;
106
107private:
108 size_t onGpuMemorySize() const override;
109
110 GrTextureType fTextureType;
111 GrMipmapStatus fMipmapStatus;
112 int fMaxMipmapLevel;
113 friend class GrTextureResource;
114
115 typedef GrSurface INHERITED;
116};
117
118#endif
119