1 /*
2  * Copyright 2017 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 #ifndef GrMockTexture_DEFINED
8 #define GrMockTexture_DEFINED
9 
10 #include "include/gpu/GrTexture.h"
11 #include "include/gpu/mock/GrMockTypes.h"
12 #include "src/gpu/GrRenderTarget.h"
13 #include "src/gpu/GrRenderTargetPriv.h"
14 #include "src/gpu/GrStencilAttachment.h"
15 #include "src/gpu/GrTexturePriv.h"
16 #include "src/gpu/mock/GrMockGpu.h"
17 
18 class GrMockTexture : public GrTexture {
19 public:
GrMockTexture(GrMockGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,GrProtected isProtected,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & info)20     GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
21                   GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
22                   const GrMockTextureInfo& info)
23             : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
24         this->registerWithCache(budgeted);
25     }
26 
GrMockTexture(GrMockGpu * gpu,const GrSurfaceDesc & desc,GrProtected isProtected,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & info,GrWrapCacheable cacheable,GrIOType ioType)27     GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
28                   GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info,
29                   GrWrapCacheable cacheable, GrIOType ioType)
30             : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
31         if (ioType == kRead_GrIOType) {
32             this->setReadOnly();
33         }
34         this->registerWithCacheWrapped(cacheable);
35     }
36 
~GrMockTexture()37     ~GrMockTexture() override {}
38 
getBackendTexture()39     GrBackendTexture getBackendTexture() const override {
40         return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(),
41                                 fInfo);
42     }
43 
backendFormat()44     GrBackendFormat backendFormat() const override {
45         return fInfo.getBackendFormat();
46     }
47 
textureParamsModified()48     void textureParamsModified() override {}
49 
50 protected:
51     // constructor for subclasses
GrMockTexture(GrMockGpu * gpu,const GrSurfaceDesc & desc,GrProtected isProtected,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & info)52     GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
53                   GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
54             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
55             , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected,
56                         GrTextureType::k2D, mipMapsStatus)
57             , fInfo(info) {}
58 
onRelease()59     void onRelease() override {
60         INHERITED::onRelease();
61     }
62 
onAbandon()63     void onAbandon() override {
64         INHERITED::onAbandon();
65     }
66 
onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)67     bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
68         return false;
69     }
70 
71 private:
72     GrMockTextureInfo fInfo;
73 
74     typedef GrTexture INHERITED;
75 };
76 
77 class GrMockRenderTarget : public GrRenderTarget {
78 public:
GrMockRenderTarget(GrMockGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info)79     GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
80                        int sampleCnt, GrProtected isProtected, const GrMockRenderTargetInfo& info)
81             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
82             , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
83             , fInfo(info) {
84         this->registerWithCache(budgeted);
85     }
86 
87     enum Wrapped { kWrapped };
GrMockRenderTarget(GrMockGpu * gpu,Wrapped,const GrSurfaceDesc & desc,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info)88     GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, int sampleCnt,
89                        GrProtected isProtected, const GrMockRenderTargetInfo& info)
90             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
91             , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
92             , fInfo(info) {
93         this->registerWithCacheWrapped(GrWrapCacheable::kNo);
94     }
95 
canAttemptStencilAttachment()96     bool canAttemptStencilAttachment() const override { return true; }
completeStencilAttachment()97     bool completeStencilAttachment() override { return true; }
98 
onGpuMemorySize()99     size_t onGpuMemorySize() const override {
100         int numColorSamples = this->numSamples();
101         if (numColorSamples > 1) {
102             // Add one to account for the resolve buffer.
103             ++numColorSamples;
104         }
105         const GrCaps& caps = *this->getGpu()->caps();
106         return GrSurface::ComputeSize(caps, this->backendFormat(), this->width(), this->height(),
107                                       numColorSamples, GrMipMapped::kNo);
108     }
109 
getBackendRenderTarget()110     GrBackendRenderTarget getBackendRenderTarget() const override {
111         int numStencilBits = 0;
112         if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
113             numStencilBits = stencil->bits();
114         }
115         return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
116     }
117 
backendFormat()118     GrBackendFormat backendFormat() const override {
119         return fInfo.getBackendFormat();
120     }
121 
122 protected:
123     // constructor for subclasses
GrMockRenderTarget(GrMockGpu * gpu,const GrSurfaceDesc & desc,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info)124     GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
125                        GrProtected isProtected, const GrMockRenderTargetInfo& info)
126             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
127             , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
128             , fInfo(info) {}
129 
130 private:
131     GrMockRenderTargetInfo fInfo;
132 
133     typedef GrRenderTarget INHERITED;
134 };
135 
136 class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
137 public:
138     // Internally created.
GrMockTextureRenderTarget(GrMockGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,int sampleCnt,GrProtected isProtected,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo)139     GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
140                               int sampleCnt, GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
141                               const GrMockTextureInfo& texInfo,
142                               const GrMockRenderTargetInfo& rtInfo)
143             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
144             , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
145             , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
146         this->registerWithCache(budgeted);
147     }
148 
149     // Renderable wrapped backend texture.
GrMockTextureRenderTarget(GrMockGpu * gpu,const GrSurfaceDesc & desc,int sampleCnt,GrProtected isProtected,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo,GrWrapCacheable cacheble)150     GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
151                               GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
152                               const GrMockTextureInfo& texInfo,
153                               const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
154             : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
155             , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
156             , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
157         this->registerWithCacheWrapped(cacheble);
158     }
159 
asTexture()160     GrTexture* asTexture() override { return this; }
asRenderTarget()161     GrRenderTarget* asRenderTarget() override { return this; }
asTexture()162     const GrTexture* asTexture() const override { return this; }
asRenderTarget()163     const GrRenderTarget* asRenderTarget() const override { return this; }
164 
backendFormat()165     GrBackendFormat backendFormat() const override {
166         return GrMockTexture::backendFormat();
167     }
168 
169 protected:
170     // This avoids an inherits via dominance warning on MSVC.
willRemoveLastRef()171     void willRemoveLastRef() override { GrTexture::willRemoveLastRef(); }
172 
173 private:
onAbandon()174     void onAbandon() override {
175         GrRenderTarget::onAbandon();
176         GrMockTexture::onAbandon();
177     }
178 
onRelease()179     void onRelease() override {
180         GrRenderTarget::onRelease();
181         GrMockTexture::onRelease();
182     }
183 
onGpuMemorySize()184     size_t onGpuMemorySize() const override {
185         int numColorSamples = this->numSamples();
186         if (numColorSamples > 1) {
187             // Add one to account for the resolve buffer.
188             ++numColorSamples;
189         }
190         const GrCaps& caps = *this->getGpu()->caps();
191         return GrSurface::ComputeSize(caps, this->backendFormat(), this->width(), this->height(),
192                                       numColorSamples, this->texturePriv().mipMapped());
193     }
194 
195     // This avoids an inherits via dominance warning on MSVC.
computeScratchKey(GrScratchKey * key)196     void computeScratchKey(GrScratchKey* key) const override { GrTexture::computeScratchKey(key); }
197 };
198 
199 #endif
200