• 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 "src/gpu/gl/GrGLTexture.h"
9 
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "src/core/SkTraceEvent.h"
12 #include "src/gpu/GrSemaphore.h"
13 #include "src/gpu/GrShaderCaps.h"
14 #include "src/gpu/GrTexture.h"
15 #include "src/gpu/gl/GrGLGpu.h"
16 
17 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
18 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
19 
TextureTypeFromTarget(GrGLenum target)20 GrTextureType GrGLTexture::TextureTypeFromTarget(GrGLenum target) {
21     switch (target) {
22         case GR_GL_TEXTURE_2D:
23             return GrTextureType::k2D;
24         case GR_GL_TEXTURE_RECTANGLE:
25             return GrTextureType::kRectangle;
26         case GR_GL_TEXTURE_EXTERNAL:
27             return GrTextureType::kExternal;
28     }
29     SK_ABORT("Unexpected texture target");
30 }
31 
target_from_texture_type(GrTextureType type)32 static inline GrGLenum target_from_texture_type(GrTextureType type) {
33     switch (type) {
34         case GrTextureType::k2D:
35             return GR_GL_TEXTURE_2D;
36         case GrTextureType::kRectangle:
37             return GR_GL_TEXTURE_RECTANGLE;
38         case GrTextureType::kExternal:
39             return GR_GL_TEXTURE_EXTERNAL;
40         default:
41             SK_ABORT("Unexpected texture target");
42     }
43     SK_ABORT("Unexpected texture type");
44 }
45 
46 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
GrGLTexture(GrGLGpu * gpu,SkBudgeted budgeted,const Desc & desc,GrMipmapStatus mipmapStatus)47 GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const Desc& desc,
48                          GrMipmapStatus mipmapStatus)
49         : GrSurface(gpu, desc.fSize, GrProtected::kNo)
50         , INHERITED(gpu, desc.fSize, GrProtected::kNo,
51                     TextureTypeFromTarget(desc.fTarget), mipmapStatus)
52         , fParameters(sk_make_sp<GrGLTextureParameters>()) {
53     this->init(desc);
54     this->registerWithCache(budgeted);
55     if (GrGLFormatIsCompressed(desc.fFormat)) {
56         this->setReadOnly();
57     }
58 }
59 
GrGLTexture(GrGLGpu * gpu,const Desc & desc,GrMipmapStatus mipmapStatus,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType)60 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, GrMipmapStatus mipmapStatus,
61                          sk_sp<GrGLTextureParameters> parameters, GrWrapCacheable cacheable,
62                          GrIOType ioType)
63         : GrSurface(gpu, desc.fSize, GrProtected::kNo)
64         , INHERITED(gpu, desc.fSize, GrProtected::kNo,
65                     TextureTypeFromTarget(desc.fTarget), mipmapStatus)
66         , fParameters(std::move(parameters)) {
67     SkASSERT(fParameters);
68     this->init(desc);
69     this->registerWithCacheWrapped(cacheable);
70     if (ioType == kRead_GrIOType) {
71         this->setReadOnly();
72     }
73 }
74 
GrGLTexture(GrGLGpu * gpu,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrMipmapStatus mipmapStatus)75 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const Desc& desc, sk_sp<GrGLTextureParameters> parameters,
76                          GrMipmapStatus mipmapStatus)
77         : GrSurface(gpu, desc.fSize, GrProtected::kNo)
78         , INHERITED(gpu, desc.fSize, GrProtected::kNo,
79                     TextureTypeFromTarget(desc.fTarget), mipmapStatus) {
80     SkASSERT(parameters || desc.fOwnership == GrBackendObjectOwnership::kOwned);
81     fParameters = parameters ? std::move(parameters) : sk_make_sp<GrGLTextureParameters>();
82     this->init(desc);
83 }
84 
init(const Desc & desc)85 void GrGLTexture::init(const Desc& desc) {
86     SkASSERT(0 != desc.fID);
87     SkASSERT(GrGLFormat::kUnknown != desc.fFormat);
88     fID = desc.fID;
89     fFormat = desc.fFormat;
90     fTextureIDOwnership = desc.fOwnership;
91 }
92 
target() const93 GrGLenum GrGLTexture::target() const { return target_from_texture_type(this->textureType()); }
94 
onRelease()95 void GrGLTexture::onRelease() {
96     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
97     ATRACE_ANDROID_FRAMEWORK_ALWAYS("Texture release(%u)", this->uniqueID().asUInt());
98 
99     if (fID) {
100         if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
101             GL_CALL(DeleteTextures(1, &fID));
102         }
103         fID = 0;
104     }
105     INHERITED::onRelease();
106 }
107 
onAbandon()108 void GrGLTexture::onAbandon() {
109     fID = 0;
110     INHERITED::onAbandon();
111 }
112 
getBackendTexture() const113 GrBackendTexture GrGLTexture::getBackendTexture() const {
114     GrGLTextureInfo info;
115     info.fTarget = target_from_texture_type(this->textureType());
116     info.fID = fID;
117     info.fFormat = GrGLFormatToEnum(fFormat);
118     return GrBackendTexture(this->width(), this->height(), this->mipmapped(), info, fParameters);
119 }
120 
backendFormat() const121 GrBackendFormat GrGLTexture::backendFormat() const {
122     return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat),
123                                    target_from_texture_type(this->textureType()));
124 }
125 
MakeWrapped(GrGLGpu * gpu,GrMipmapStatus mipmapStatus,const Desc & desc,sk_sp<GrGLTextureParameters> parameters,GrWrapCacheable cacheable,GrIOType ioType)126 sk_sp<GrGLTexture> GrGLTexture::MakeWrapped(GrGLGpu* gpu,
127                                             GrMipmapStatus mipmapStatus,
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, mipmapStatus, 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     size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), 1,
160                                          this->mipmapped());
161 
162     // Dump as skia/gpu_resources/resource_#/texture, to avoid conflicts in the
163     // GrGLTextureRenderTarget case, where multiple things may dump to the same resource. This
164     // has no downside in the normal case.
165     SkString resourceName = this->getResourceName();
166     resourceName.append("/texture");
167 
168     // As we are only dumping our texture memory (not any additional memory tracked by classes
169     // which may inherit from us), specifically call GrGLTexture::gpuMemorySize to avoid
170     // hitting an override.
171     this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "Texture", size);
172 
173     SkString texture_id;
174     texture_id.appendU32(this->textureID());
175     traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_texture", texture_id.c_str());
176 }
177