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 #ifdef SKIA_OHOS
129 HITRACE_OHOS_NAME_FMT_LEVEL(DebugTraceLevel::DETAIL, "Texture release(%u)", this->uniqueID().asUInt());
130 #else
131 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
132 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture release(%u)", this->uniqueID().asUInt());
133 #endif
134
135 if (fID) {
136 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
137 GL_CALL(DeleteTextures(1, &fID));
138 }
139 fID = 0;
140 }
141 INHERITED::onRelease();
142 }
143
onAbandon()144 void GrGLTexture::onAbandon() {
145 fID = 0;
146 INHERITED::onAbandon();
147 }
148
getBackendTexture() const149 GrBackendTexture GrGLTexture::getBackendTexture() const {
150 GrGLTextureInfo info;
151 info.fTarget = target_from_texture_type(this->textureType());
152 info.fID = fID;
153 info.fFormat = GrGLFormatToEnum(fFormat);
154 info.fProtected = skgpu::Protected(this->isProtected());
155
156 return GrBackendTextures::MakeGL(
157 this->width(), this->height(), this->mipmapped(), info, fParameters);
158 }
159
backendFormat() const160 GrBackendFormat GrGLTexture::backendFormat() const {
161 return GrBackendFormats::MakeGL(GrGLFormatToEnum(fFormat),
162 target_from_texture_type(this->textureType()));
163 }
164
MakeWrapped(GrGLGpu * gpu,GrMipmapStatus mipmapStatus,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType,std::string_view label)165 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
166 GrMipmapStatus mipmapStatus,
167 const Desc& desc,
168 sk_sp<GrGLTextureParameters> parameters,
169 GrWrapCacheable cacheable,
170 GrIOType ioType,
171 std::string_view label) {
172 return sk_sp<GrGLTexture>(new GrGLTexture(
173 gpu, desc, mipmapStatus, std::move(parameters), cacheable, ioType, label));
174 }
175
onStealBackendTexture(GrBackendTexture * backendTexture,SkImages::BackendTextureReleaseProc * releaseProc)176 bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
177 SkImages::BackendTextureReleaseProc* releaseProc) {
178 *backendTexture = this->getBackendTexture();
179 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
180 *releaseProc = [](GrBackendTexture){};
181
182 // It's important that we only abandon this texture's objects, not subclass objects such as
183 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
184 // cleaned up by us.
185 this->GrGLTexture::onAbandon();
186 return true;
187 }
188
onSetLabel()189 void GrGLTexture::onSetLabel() {
190 SkASSERT(fID);
191 SkASSERT(fTextureIDOwnership == GrBackendObjectOwnership::kOwned);
192 if (!this->getLabel().empty()) {
193 const std::string label = "_Skia_" + this->getLabel();
194 GrGLGpu* glGpu = static_cast<GrGLGpu*>(this->getGpu());
195 if (glGpu->glCaps().debugSupport()) {
196 GR_GL_CALL(glGpu->glInterface(), ObjectLabel(GR_GL_TEXTURE, fID, -1, label.c_str()));
197 }
198 }
199 }
200
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const201 void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
202 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
203 // which is multiply inherited from both ourselves and a texture. In these cases, one part
204 // (texture, rt) may be wrapped, while the other is owned by Skia.
205 bool refsWrappedTextureObjects =
206 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
207 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
208 return;
209 }
210
211 size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), 1,
212 this->mipmapped());
213
214 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
215 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
216 // has no downside in the normal case.
217 SkString resourceName = this->getResourceName();
218 resourceName.append("/texture");
219
220 // As we are only dumping our texture memory (not any additional memory tracked by classes
221 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
222 // hitting an override.
223 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", size);
224
225 SkString texture_id;
226 texture_id.appendU32(this->textureID());
227 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
228 }
229