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