• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/gpu/ganesh/gl/GrGLTexture.h"
9 
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "include/gpu/ganesh/SkImageGanesh.h"
12 #include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
13 #include "src/core/SkTraceEvent.h"
14 #include "src/gpu/ganesh/GrSemaphore.h"
15 #include "src/gpu/ganesh/GrShaderCaps.h"
16 #include "src/gpu/ganesh/GrTexture.h"
17 #include "src/gpu/ganesh/gl/GrGLBackendSurfacePriv.h"
18 #include "src/gpu/ganesh/gl/GrGLGpu.h"
19 
20 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
21 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
22 
TextureTypeFromTarget(GrGLenum target)23 GrTextureType GrGLTexture::TextureTypeFromTarget(GrGLenum target) {
24     switch (target) {
25         case GR_GL_TEXTURE_2D:
26             return GrTextureType::k2D;
27         case GR_GL_TEXTURE_RECTANGLE:
28             return GrTextureType::kRectangle;
29         case GR_GL_TEXTURE_EXTERNAL:
30             return GrTextureType::kExternal;
31     }
32     SK_ABORT("Unexpected texture target");
33 }
34 
target_from_texture_type(GrTextureType type)35 static inline GrGLenum target_from_texture_type(GrTextureType type) {
36     switch (type) {
37         case GrTextureType::k2D:
38             return GR_GL_TEXTURE_2D;
39         case GrTextureType::kRectangle:
40             return GR_GL_TEXTURE_RECTANGLE;
41         case GrTextureType::kExternal:
42             return GR_GL_TEXTURE_EXTERNAL;
43         default:
44             SK_ABORT("Unexpected texture target");
45     }
46     SK_ABORT("Unexpected texture type");
47 }
48 
49 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
GrGLTexture(GrGLGpu * gpu,skgpu::Budgeted budgeted,const Desc & desc,GrMipmapStatus mipmapStatus,std::string_view label)50 GrGLTexture::GrGLTexture(GrGLGpu* gpu,
51                          skgpu::Budgeted budgeted,
52                          const Desc& desc,
53                          GrMipmapStatus mipmapStatus,
54                          std::string_view label)
55         : GrSurface(gpu, desc.fSize, desc.fIsProtected, label)
56         , GrTexture(gpu,
57                     desc.fSize,
58                     desc.fIsProtected,
59                     TextureTypeFromTarget(desc.fTarget),
60                     mipmapStatus,
61                     label)
62         , fParameters(sk_make_sp<GrGLTextureParameters>()) {
63     this->init(desc);
64     this->registerWithCache(budgeted);
65     if (GrGLFormatIsCompressed(desc.fFormat)) {
66         this->setReadOnly();
67     }
68 }
69 
GrGLTexture(GrGLGpu * gpu,const Desc & desc,GrMipmapStatus mipmapStatus,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType,std::string_view label)70 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipmapStatus mipmapStatus,
71                          sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable,
72                          GrIOType ioType, std::string_view label)
73         : GrSurface(gpu, desc.fSize, desc.fIsProtected, label)
74         , GrTexture(gpu,
75                     desc.fSize,
76                     desc.fIsProtected,
77                     TextureTypeFromTarget(desc.fTarget),
78                     mipmapStatus,
79                     label)
80         , fParameters(std::move(parameters)) {
81     SkASSERT(fParameters);
82     this->init(desc);
83     this->registerWithCacheWrapped(cacheable);
84     if (ioType == kRead_GrIOType) {
85         this->setReadOnly();
86     }
87 }
88 
GrGLTexture(GrGLGpu * gpu,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrMipmapStatus mipmapStatus,std::string_view label)89 GrGLTexture::GrGLTexture(GrGLGpu* gpu,
90                          const Desc& desc,
91                          sk_sp<GrGLTextureParameters> parameters,
92                          GrMipmapStatus mipmapStatus,
93                          std::string_view label)
94         : GrSurface(gpu, desc.fSize, desc.fIsProtected, label)
95         , GrTexture(gpu,
96                     desc.fSize,
97                     desc.fIsProtected,
98                     TextureTypeFromTarget(desc.fTarget),
99                     mipmapStatus,
100                     label) {
101     SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned);
102     fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
103     this->init(desc);
104 }
105 
init(const Desc & desc)106 void GrGLTexture::init(const Desc& desc) {
107     SkASSERT(0 != desc.fID);
108     SkASSERT(GrGLFormat::kUnknown != desc.fFormat);
109     fID = desc.fID;
110     fFormat = desc.fFormat;
111     fTextureIDOwnership = desc.fOwnership;
112 }
113 
target() const114 GrGLenum GrGLTexture::target() const { return target_from_texture_type(this->textureType()); }
115 
onRelease()116 void GrGLTexture::onRelease() {
117     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
118     ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture release(%u)", this->uniqueID().asUInt());
119 
120     if (fID) {
121         if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
122             GL_CALL(DeleteTextures(1, &fID));
123         }
124         fID = 0;
125     }
126     INHERITED::onRelease();
127 }
128 
onAbandon()129 void GrGLTexture::onAbandon() {
130     fID = 0;
131     INHERITED::onAbandon();
132 }
133 
getBackendTexture() const134 GrBackendTexture GrGLTexture::getBackendTexture() const {
135     GrGLTextureInfo info;
136     info.fTarget = target_from_texture_type(this->textureType());
137     info.fID = fID;
138     info.fFormat = GrGLFormatToEnum(fFormat);
139     info.fProtected = skgpu::Protected(this->isProtected());
140 
141     return GrBackendTextures::MakeGL(
142             this->width(), this->height(), this->mipmapped(), info, fParameters);
143 }
144 
backendFormat() const145 GrBackendFormat GrGLTexture::backendFormat() const {
146     return GrBackendFormats::MakeGL(GrGLFormatToEnum(fFormat),
147                                     target_from_texture_type(this->textureType()));
148 }
149 
MakeWrapped(GrGLGpu * gpu,GrMipmapStatus mipmapStatus,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType,std::string_view label)150 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
151                                             GrMipmapStatus mipmapStatus,
152                                             const Desc& desc,
153                                             sk_sp<GrGLTextureParameters> parameters,
154                                             GrWrapCacheable cacheable,
155                                             GrIOType ioType,
156                                             std::string_view label) {
157     return sk_sp<GrGLTexture>(new GrGLTexture(
158             gpu, desc, mipmapStatus, std::move(parameters), cacheable, ioType, label));
159 }
160 
onStealBackendTexture(GrBackendTexture * backendTexture,SkImages::BackendTextureReleaseProc * releaseProc)161 bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
162                                         SkImages::BackendTextureReleaseProc* releaseProc) {
163     *backendTexture = this->getBackendTexture();
164     // Set the release proc to a no-op function. GL doesn't require any special cleanup.
165     *releaseProc = [](GrBackendTexture){};
166 
167     // It's important that we only abandon this texture's objects, not subclass objects such as
168     // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
169     // cleaned up by us.
170     this->GrGLTexture::onAbandon();
171     return true;
172 }
173 
onSetLabel()174 void GrGLTexture::onSetLabel() {
175     SkASSERT(fID);
176     SkASSERT(fTextureIDOwnership == GrBackendObjectOwnership::kOwned);
177     if (!this->getLabel().empty()) {
178         const std::string label = "_Skia_" + this->getLabel();
179         GrGLGpu* glGpu = static_cast<GrGLGpu*>(this->getGpu());
180         if (glGpu->glCaps().debugSupport()) {
181             GR_GL_CALL(glGpu->glInterface(), ObjectLabel(GR_GL_TEXTURE, fID, -1, label.c_str()));
182         }
183     }
184 }
185 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const186 void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
187     // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
188     // which is multiply inherited from both ourselves and a texture. In these cases, one part
189     // (texture, rt) may be wrapped, while the other is owned by Skia.
190     bool refsWrappedTextureObjects =
191             this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
192     if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
193         return;
194     }
195 
196     size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), 1,
197                                          this->mipmapped());
198 
199     // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
200     // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
201     // has no downside in the normal case.
202     SkString resourceName = this->getResourceName();
203     resourceName.append("/texture");
204 
205     // As we are only dumping our texture memory (not any additional memory tracked by classes
206     // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
207     // hitting an override.
208     this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", size);
209 
210     SkString texture_id;
211     texture_id.appendU32(this->textureID());
212     traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
213 }
214