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