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 "SkTraceMemoryDump.h"
11 
12 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
13 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
14 
sampler_type(const GrGLTexture::IDDesc & idDesc,const GrGLGpu * gpu)15 inline static GrSLType sampler_type(const GrGLTexture::IDDesc& idDesc, const GrGLGpu* gpu) {
16     if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_EXTERNAL) {
17         SkASSERT(gpu->glCaps().glslCaps()->externalTextureSupport());
18         return kTextureExternalSampler_GrSLType;
19     } else if (idDesc.fInfo.fTarget == GR_GL_TEXTURE_RECTANGLE) {
20         SkASSERT(gpu->glCaps().rectangleTextureSupport());
21         return kTexture2DRectSampler_GrSLType;
22     } else {
23         SkASSERT(idDesc.fInfo.fTarget == GR_GL_TEXTURE_2D);
24         return kTexture2DSampler_GrSLType;
25     }
26 }
27 
28 // 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)29 GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
30                          const IDDesc& idDesc)
31     : GrSurface(gpu, desc)
32     , INHERITED(gpu, desc, sampler_type(idDesc, gpu), false) {
33     this->init(desc, idDesc);
34     this->registerWithCache(budgeted);
35 }
36 
GrGLTexture(GrGLGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,const IDDesc & idDesc,bool wasMipMapDataProvided)37 GrGLTexture::GrGLTexture(GrGLGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
38                          const IDDesc& idDesc,
39                          bool wasMipMapDataProvided)
40     : GrSurface(gpu, desc)
41     , INHERITED(gpu, desc, sampler_type(idDesc, gpu), wasMipMapDataProvided) {
42     this->init(desc, idDesc);
43     this->registerWithCache(budgeted);
44 }
45 
GrGLTexture(GrGLGpu * gpu,Wrapped,const GrSurfaceDesc & desc,const IDDesc & idDesc)46 GrGLTexture::GrGLTexture(GrGLGpu* gpu, Wrapped, const GrSurfaceDesc& desc, const IDDesc& idDesc)
47     : GrSurface(gpu, desc)
48     , INHERITED(gpu, desc, sampler_type(idDesc, gpu), false) {
49     this->init(desc, idDesc);
50     this->registerWithCacheWrapped();
51 }
52 
GrGLTexture(GrGLGpu * gpu,const GrSurfaceDesc & desc,const IDDesc & idDesc)53 GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc)
54     : GrSurface(gpu, desc)
55     , INHERITED(gpu, desc, sampler_type(idDesc, gpu), false) {
56     this->init(desc, idDesc);
57 }
58 
init(const GrSurfaceDesc & desc,const IDDesc & idDesc)59 void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
60     SkASSERT(0 != idDesc.fInfo.fID);
61     fTexParams.invalidate();
62     fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
63     fInfo = idDesc.fInfo;
64     fTextureIDOwnership = idDesc.fOwnership;
65 }
66 
onRelease()67 void GrGLTexture::onRelease() {
68     if (fInfo.fID) {
69         if (GrBackendObjectOwnership::kBorrowed != fTextureIDOwnership) {
70             GL_CALL(DeleteTextures(1, &fInfo.fID));
71         }
72         fInfo.fID = 0;
73     }
74     INHERITED::onRelease();
75 }
76 
onAbandon()77 void GrGLTexture::onAbandon() {
78     fInfo.fTarget = 0;
79     fInfo.fID = 0;
80     INHERITED::onAbandon();
81 }
82 
getTextureHandle() const83 GrBackendObject GrGLTexture::getTextureHandle() const {
84 #ifdef SK_IGNORE_GL_TEXTURE_TARGET
85     return static_cast<GrBackendObject>(this->textureID());
86 #else
87     return reinterpret_cast<GrBackendObject>(&fInfo);
88 #endif
89 }
90 
setMemoryBacking(SkTraceMemoryDump * traceMemoryDump,const SkString & dumpName) const91 void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
92                                    const SkString& dumpName) const {
93     SkString texture_id;
94     texture_id.appendU32(this->textureID());
95     traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
96                                       texture_id.c_str());
97 }
98 
CreateWrapped(GrGLGpu * gpu,const GrSurfaceDesc & desc,const IDDesc & idDesc)99 GrGLTexture* GrGLTexture::CreateWrapped(GrGLGpu* gpu, const GrSurfaceDesc& desc,
100                                         const IDDesc& idDesc) {
101     return new GrGLTexture(gpu, kWrapped, desc, idDesc);
102 }
103 
104