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 
97     if (fID) {
98         if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
99             GL_CALL(DeleteTextures(1, &fID));
100         }
101         fID = 0;
102     }
103     INHERITED::onRelease();
104 }
105 
onAbandon()106 void GrGLTexture::onAbandon() {
107     fID = 0;
108     INHERITED::onAbandon();
109 }
110 
getBackendTexture() const111 GrBackendTexture GrGLTexture::getBackendTexture() const {
112     GrGLTextureInfo info;
113     info.fTarget = target_from_texture_type(this->textureType());
114     info.fID = fID;
115     info.fFormat = GrGLFormatToEnum(fFormat);
116     return GrBackendTexture(this->width(), this->height(), this->mipmapped(), info, fParameters);
117 }
118 
backendFormat() const119 GrBackendFormat GrGLTexture::backendFormat() const {
120     return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat),
121                                    target_from_texture_type(this->textureType()));
122 }
123 
MakeWrapped(GrGLGpu * gpu,GrMipmapStatus mipmapStatus,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType)124 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
125                                             GrMipmapStatus mipmapStatus,
126                                             const Desc& desc,
127                                             sk_sp<GrGLTextureParameters> parameters,
128                                             GrWrapCacheable cacheable,
129                                             GrIOType ioType) {
130     return sk_sp<GrGLTexture>(
131             new GrGLTexture(gpu, desc, mipmapStatus, std::move(parameters), cacheable, ioType));
132 }
133 
onStealBackendTexture(GrBackendTexture * backendTexture,SkImage::BackendTextureReleaseProc * releaseProc)134 bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
135                                         SkImage::BackendTextureReleaseProc* releaseProc) {
136     *backendTexture = this->getBackendTexture();
137     // Set the release proc to a no-op function. GL doesn't require any special cleanup.
138     *releaseProc = [](GrBackendTexture){};
139 
140     // It's important that we only abandon this texture's objects, not subclass objects such as
141     // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
142     // cleaned up by us.
143     this->GrGLTexture::onAbandon();
144     return true;
145 }
146 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const147 void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
148     // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
149     // which is multiply inherited from both ourselves and a texture. In these cases, one part
150     // (texture, rt) may be wrapped, while the other is owned by Skia.
151     bool refsWrappedTextureObjects =
152             this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
153     if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
154         return;
155     }
156 
157     // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
158     // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
159     // has no downside in the normal case.
160     SkString resourceName = this->getResourceName();
161     resourceName.append("/texture");
162 
163     // As we are only dumping our texture memory (not any additional memory tracked by classes
164     // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
165     // hitting an override.
166     this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
167                                    GrGLTexture::gpuMemorySize());
168 
169     SkString texture_id;
170     texture_id.appendU32(this->textureID());
171     traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
172 }
173