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