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/gl/GrGLTexture.h"
9
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "src/core/SkTraceEvent.h"
12 #include "src/gpu/GrSemaphore.h"
13 #include "src/gpu/GrShaderCaps.h"
14 #include "src/gpu/GrTexture.h"
15 #include "src/gpu/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,SkBudgeted budgeted,const Desc & desc,GrMipmapStatus mipmapStatus)47 GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const Desc& desc,
48 GrMipmapStatus mipmapStatus)
49 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
50 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
51 TextureTypeFromTarget(desc.fTarget), mipmapStatus)
52 , fParameters(sk_make_sp<GrGLTextureParameters>()) {
53 this->init(desc);
54 this->registerWithCache(budgeted);
55 if (GrGLFormatIsCompressed(desc.fFormat)) {
56 this->setReadOnly();
57 }
58 }
59
GrGLTexture(GrGLGpu * gpu,const Desc & desc,GrMipmapStatus mipmapStatus,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType)60 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipmapStatus mipmapStatus,
61 sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable,
62 GrIOType ioType)
63 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
64 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
65 TextureTypeFromTarget(desc.fTarget), mipmapStatus)
66 , fParameters(std::move(parameters)) {
67 SkASSERT(fParameters);
68 this->init(desc);
69 this->registerWithCacheWrapped(cacheable);
70 if (ioType == kRead_GrIOType) {
71 this->setReadOnly();
72 }
73 }
74
GrGLTexture(GrGLGpu * gpu,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrMipmapStatus mipmapStatus)75 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, sk_sp<GrGLTextureParameters> parameters,
76 GrMipmapStatus mipmapStatus)
77 : GrSurface(gpu, desc.fSize, GrProtected::kNo)
78 , INHERITED(gpu, desc.fSize, GrProtected::kNo,
79 TextureTypeFromTarget(desc.fTarget), mipmapStatus) {
80 SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned);
81 fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
82 this->init(desc);
83 }
84
init(const Desc & desc)85 void GrGLTexture::init(const Desc& desc) {
86 SkASSERT(0 != desc.fID);
87 SkASSERT(GrGLFormat::kUnknown != desc.fFormat);
88 fID = desc.fID;
89 fFormat = desc.fFormat;
90 fTextureIDOwnership = desc.fOwnership;
91 }
92
target() const93 GrGLenum GrGLTexture::target() const { return target_from_texture_type(this->textureType()); }
94
onRelease()95 void GrGLTexture::onRelease() {
96 #ifdef SKIA_OHOS
97 HITRACE_OHOS_NAME_FMT_LEVEL(DebugTraceLevel::DETAIL, "Texture release(%u)", this->uniqueID().asUInt());
98 #else
99 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
100 ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture release(%u)", this->uniqueID().asUInt());
101 #endif
102
103 if (fID) {
104 if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
105 GL_CALL(DeleteTextures(1, &fID));
106 }
107 fID = 0;
108 }
109 INHERITED::onRelease();
110 }
111
onAbandon()112 void GrGLTexture::onAbandon() {
113 fID = 0;
114 INHERITED::onAbandon();
115 }
116
getBackendTexture() const117 GrBackendTexture GrGLTexture::getBackendTexture() const {
118 GrGLTextureInfo info;
119 info.fTarget = target_from_texture_type(this->textureType());
120 info.fID = fID;
121 info.fFormat = GrGLFormatToEnum(fFormat);
122 return GrBackendTexture(this->width(), this->height(), this->mipmapped(), info, fParameters);
123 }
124
backendFormat() const125 GrBackendFormat GrGLTexture::backendFormat() const {
126 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat),
127 target_from_texture_type(this->textureType()));
128 }
129
MakeWrapped(GrGLGpu * gpu,GrMipmapStatus mipmapStatus,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType)130 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
131 GrMipmapStatus mipmapStatus,
132 const Desc& desc,
133 sk_sp<GrGLTextureParameters> parameters,
134 GrWrapCacheable cacheable,
135 GrIOType ioType) {
136 return sk_sp<GrGLTexture>(
137 new GrGLTexture(gpu, desc, mipmapStatus, std::move(parameters), cacheable, ioType));
138 }
139
onStealBackendTexture(GrBackendTexture * backendTexture,SkImage::BackendTextureReleaseProc * releaseProc)140 bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
141 SkImage::BackendTextureReleaseProc* releaseProc) {
142 *backendTexture = this->getBackendTexture();
143 // Set the release proc to a no-op function. GL doesn't require any special cleanup.
144 *releaseProc = [](GrBackendTexture){};
145
146 // It's important that we only abandon this texture's objects, not subclass objects such as
147 // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
148 // cleaned up by us.
149 this->GrGLTexture::onAbandon();
150 return true;
151 }
152
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const153 void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
154 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
155 // which is multiply inherited from both ourselves and a texture. In these cases, one part
156 // (texture, rt) may be wrapped, while the other is owned by Skia.
157 bool refsWrappedTextureObjects =
158 this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
159 if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
160 return;
161 }
162
163 size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), 1,
164 this->mipmapped());
165
166 // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
167 // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
168 // has no downside in the normal case.
169 SkString resourceName = this->getResourceName();
170 resourceName.append("/texture");
171
172 // As we are only dumping our texture memory (not any additional memory tracked by classes
173 // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
174 // hitting an override.
175 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", size);
176
177 SkString texture_id;
178 texture_id.appendU32(this->textureID());
179 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
180 }
181