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