• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrGLTexture.h"
9 #include "GrGLGpu.h"
10 #include "GrSemaphore.h"
11 #include "GrShaderCaps.h"
12 #include "GrTexturePriv.h"
13 #include "SkTraceMemoryDump.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     return GrTextureType::k2D;
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     }
40     SK_ABORT("Unexpected texture type");
41     return GR_GL_TEXTURE_2D;
42 }
43 
44 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
GrGLTexture(GrGLGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,const IDDesc & idDesc,GrMipMapsStatus mipMapsStatus)45 GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
46                          const IDDesc& idDesc, GrMipMapsStatus mipMapsStatus)
47         : GrSurface(gpu, desc)
48         , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
49     this->init(desc, idDesc);
50     this->registerWithCache(budgeted);
51     if (GrPixelConfigIsCompressed(desc.fConfig)) {
52         this->setReadOnly();
53     }
54 }
55 
GrGLTexture(GrGLGpu * gpu,const GrSurfaceDesc & desc,GrMipMapsStatus mipMapsStatus,const IDDesc & idDesc,GrWrapCacheable cacheable,GrIOType ioType)56 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
57                          const IDDesc& idDesc, GrWrapCacheable cacheable, GrIOType ioType)
58         : GrSurface(gpu, desc)
59         , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
60     this->init(desc, idDesc);
61     this->registerWithCacheWrapped(cacheable);
62     if (ioType == kRead_GrIOType) {
63         this->setReadOnly();
64     }
65 }
66 
GrGLTexture(GrGLGpu * gpu,const GrSurfaceDesc & desc,const IDDesc & idDesc,GrMipMapsStatus mipMapsStatus)67 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
68                          GrMipMapsStatus mipMapsStatus)
69         : GrSurface(gpu, desc)
70         , INHERITED(gpu, desc, TextureTypeFromTarget(idDesc.fInfo.fTarget), mipMapsStatus) {
71     this->init(desc, idDesc);
72 }
73 
init(const GrSurfaceDesc & desc,const IDDesc & idDesc)74 void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
75     SkASSERT(0 != idDesc.fInfo.fID);
76     SkASSERT(0 != idDesc.fInfo.fFormat);
77     fParamsTimestamp = GrGpu::kExpiredTimestamp;
78     fID = idDesc.fInfo.fID;
79     fFormat = idDesc.fInfo.fFormat;
80     fTextureIDOwnership = idDesc.fOwnership;
81 }
82 
target() const83 GrGLenum GrGLTexture::target() const {
84     return target_from_texture_type(this->texturePriv().textureType());
85 }
86 
onRelease()87 void GrGLTexture::onRelease() {
88     if (fID) {
89         if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
90             GL_CALL(DeleteTextures(1, &fID));
91         }
92         fID = 0;
93     }
94     this->invokeReleaseProc();
95     INHERITED::onRelease();
96 }
97 
onAbandon()98 void GrGLTexture::onAbandon() {
99     fID = 0;
100     this->invokeReleaseProc();
101     INHERITED::onAbandon();
102 }
103 
getBackendTexture() const104 GrBackendTexture GrGLTexture::getBackendTexture() const {
105     GrGLTextureInfo info;
106     info.fTarget = target_from_texture_type(this->texturePriv().textureType());
107     info.fID = fID;
108     info.fFormat = fFormat;
109     return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), info);
110 }
111 
backendFormat() const112 GrBackendFormat GrGLTexture::backendFormat() const {
113     return GrBackendFormat::MakeGL(fFormat,
114                                    target_from_texture_type(this->texturePriv().textureType()));
115 }
116 
MakeWrapped(GrGLGpu * gpu,const GrSurfaceDesc & desc,GrMipMapsStatus mipMapsStatus,const IDDesc & idDesc,GrWrapCacheable cacheable,GrIOType ioType)117 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
118                                             GrMipMapsStatus mipMapsStatus, const IDDesc& idDesc,
119                                             GrWrapCacheable cacheable, GrIOType ioType) {
120     return sk_sp<GrGLTexture>(new GrGLTexture(gpu, desc, mipMapsStatus, idDesc, cacheable, ioType));
121 }
122 
onStealBackendTexture(GrBackendTexture * backendTexture,SkImage::BackendTextureReleaseProc * releaseProc)123 bool GrGLTexture::onStealBackendTexture(GrBackendTexture* backendTexture,
124                                         SkImage::BackendTextureReleaseProc* releaseProc) {
125     *backendTexture = this->getBackendTexture();
126     // Set the release proc to a no-op function. GL doesn't require any special cleanup.
127     *releaseProc = [](GrBackendTexture){};
128 
129     // It's important that we only abandon this texture's objects, not subclass objects such as
130     // those held by GrGLTextureRenderTarget. Those objects are not being stolen and need to be
131     // cleaned up by us.
132     this->GrGLTexture::onAbandon();
133     return true;
134 }
135 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const136 void GrGLTexture::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
137     // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
138     // which is multiply inherited from both ourselves and a texture. In these cases, one part
139     // (texture, rt) may be wrapped, while the other is owned by Skia.
140     bool refsWrappedTextureObjects =
141             this->fTextureIDOwnership == GrBackendObjectOwnership::kBorrowed;
142     if (refsWrappedTextureObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
143         return;
144     }
145 
146     // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
147     // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
148     // has no downside in the normal case.
149     SkString resourceName = this->getResourceName();
150     resourceName.append("/texture");
151 
152     // As we are only dumping our texture memory (not any additional memory tracked by classes
153     // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
154     // hitting an override.
155     this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture",
156                                    GrGLTexture::gpuMemorySize());
157 
158     SkString texture_id;
159     texture_id.appendU32(this->textureID());
160     traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
161 }
162