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