• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mock/GrMockTypes.h"
11 #include "src/gpu/ganesh/GrAttachment.h"
12 #include "src/gpu/ganesh/GrRenderTarget.h"
13 #include "src/gpu/ganesh/GrTexture.h"
14 #include "src/gpu/ganesh/mock/GrMockGpu.h"
15 
16 class GrMockTexture : public GrTexture {
17 public:
GrMockTexture(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,GrProtected isProtected,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,std::string_view label)18     GrMockTexture(GrMockGpu* gpu,
19                   skgpu::Budgeted budgeted,
20                   SkISize dimensions,
21                   GrProtected isProtected,
22                   GrMipmapStatus mipmapStatus,
23                   const GrMockTextureInfo& info,
24                   std::string_view label)
25             : GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, info, label) {
26         this->registerWithCache(budgeted);
27     }
28 
GrMockTexture(GrMockGpu * gpu,SkISize dimensions,GrProtected isProtected,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,GrWrapCacheable cacheable,GrIOType ioType,std::string_view label)29     GrMockTexture(GrMockGpu* gpu,
30                   SkISize dimensions,
31                   GrProtected isProtected,
32                   GrMipmapStatus mipmapStatus,
33                   const GrMockTextureInfo& info,
34                   GrWrapCacheable cacheable,
35                   GrIOType ioType,
36                   std::string_view label)
37             : GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, info, label) {
38         if (ioType == kRead_GrIOType) {
39             this->setReadOnly();
40         }
41         this->registerWithCacheWrapped(cacheable);
42     }
43 
~GrMockTexture()44     ~GrMockTexture() override {}
45 
getBackendTexture()46     GrBackendTexture getBackendTexture() const override {
47         return GrBackendTexture(this->width(), this->height(), this->mipmapped(), fInfo);
48     }
49 
backendFormat()50     GrBackendFormat backendFormat() const override {
51         return fInfo.getBackendFormat();
52     }
53 
textureParamsModified()54     void textureParamsModified() override {}
55 
56 protected:
57     // constructor for subclasses
GrMockTexture(GrMockGpu * gpu,const SkISize & dimensions,GrProtected isProtected,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,std::string_view label)58     GrMockTexture(GrMockGpu* gpu, const SkISize& dimensions, GrProtected isProtected,
59                   GrMipmapStatus mipmapStatus,
60                   const GrMockTextureInfo& info,
61                   std::string_view label)
62             : GrSurface(gpu, dimensions, isProtected, label)
63             , INHERITED(gpu, dimensions, isProtected, GrTextureType::k2D, mipmapStatus, label)
64             , fInfo(info) {}
65 
onRelease()66     void onRelease() override {
67         INHERITED::onRelease();
68     }
69 
onAbandon()70     void onAbandon() override {
71         INHERITED::onAbandon();
72     }
73 
onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)74     bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
75         return false;
76     }
77 
78 private:
onSetLabel()79     void onSetLabel() override{}
80 
81     GrMockTextureInfo fInfo;
82 
83     using INHERITED = GrTexture;
84 };
85 
86 class GrMockRenderTarget : public GrRenderTarget {
87 public:
GrMockRenderTarget(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info,std::string_view label)88     GrMockRenderTarget(GrMockGpu* gpu,
89                        skgpu::Budgeted budgeted,
90                        SkISize dimensions,
91                        int sampleCnt,
92                        GrProtected isProtected,
93                        const GrMockRenderTargetInfo& info,
94                        std::string_view label)
95             : GrSurface(gpu, dimensions, isProtected, label)
96             , INHERITED(gpu, dimensions, sampleCnt, isProtected, label)
97             , fInfo(info) {
98         this->registerWithCache(budgeted);
99     }
100 
101     enum Wrapped { kWrapped };
GrMockRenderTarget(GrMockGpu * gpu,Wrapped,SkISize dimensions,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info,std::string_view label)102     GrMockRenderTarget(GrMockGpu* gpu, Wrapped, SkISize dimensions, int sampleCnt,
103                        GrProtected isProtected,
104                        const GrMockRenderTargetInfo& info,
105                        std::string_view label)
106             : GrSurface(gpu, dimensions, isProtected, label)
107             , INHERITED(gpu, dimensions, sampleCnt, isProtected, label)
108             , fInfo(info) {
109         this->registerWithCacheWrapped(GrWrapCacheable::kNo);
110     }
111 
canAttemptStencilAttachment(bool useMSAASurface)112     bool canAttemptStencilAttachment(bool useMSAASurface) const override {
113         SkASSERT(useMSAASurface == (this->numSamples() > 1));
114         return true;
115     }
116 
completeStencilAttachment(GrAttachment *,bool useMSAASurface)117     bool completeStencilAttachment(GrAttachment*, bool useMSAASurface) override {
118         SkASSERT(useMSAASurface == (this->numSamples() > 1));
119         return true;
120     }
121 
onGpuMemorySize()122     size_t onGpuMemorySize() const override {
123         int numColorSamples = this->numSamples();
124         if (numColorSamples > 1) {
125             // Add one to account for the resolve buffer.
126             ++numColorSamples;
127         }
128         return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
129                                       numColorSamples, GrMipmapped::kNo);
130     }
131 
getBackendRenderTarget()132     GrBackendRenderTarget getBackendRenderTarget() const override {
133         int numStencilBits = 0;
134         if (GrAttachment* stencil = this->getStencilAttachment()) {
135             numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat());
136         }
137         return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
138     }
139 
backendFormat()140     GrBackendFormat backendFormat() const override {
141         return fInfo.getBackendFormat();
142     }
143 
144 protected:
145     // constructor for subclasses
GrMockRenderTarget(GrMockGpu * gpu,SkISize dimensions,int sampleCnt,GrProtected isProtected,const GrMockRenderTargetInfo & info,std::string_view label)146     GrMockRenderTarget(GrMockGpu* gpu,
147                        SkISize dimensions,
148                        int sampleCnt,
149                        GrProtected isProtected,
150                        const GrMockRenderTargetInfo& info,
151                        std::string_view label)
152             : GrSurface(gpu, dimensions, isProtected, label)
153             , INHERITED(gpu, dimensions, sampleCnt, isProtected, label)
154             , fInfo(info) {}
155 
156 private:
onSetLabel()157     void onSetLabel() override{}
158 
159     GrMockRenderTargetInfo fInfo;
160 
161     using INHERITED = GrRenderTarget;
162 };
163 
164 class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
165 public:
166     // Internally created.
GrMockTextureRenderTarget(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,int sampleCnt,GrProtected isProtected,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo,std::string_view label)167     GrMockTextureRenderTarget(GrMockGpu* gpu,
168                               skgpu::Budgeted budgeted,
169                               SkISize dimensions,
170                               int sampleCnt,
171                               GrProtected isProtected,
172                               GrMipmapStatus mipmapStatus,
173                               const GrMockTextureInfo& texInfo,
174                               const GrMockRenderTargetInfo& rtInfo,
175                               std::string_view label)
176             : GrSurface(gpu, dimensions, isProtected, label)
177             , GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, texInfo, label)
178             , GrMockRenderTarget(gpu, dimensions, sampleCnt, isProtected, rtInfo, label) {
179         this->registerWithCache(budgeted);
180     }
181 
182     // Renderable wrapped backend texture.
GrMockTextureRenderTarget(GrMockGpu * gpu,SkISize dimensions,int sampleCnt,GrProtected isProtected,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo,GrWrapCacheable cacheable,std::string_view label)183     GrMockTextureRenderTarget(GrMockGpu* gpu,
184                               SkISize dimensions,
185                               int sampleCnt,
186                               GrProtected isProtected,
187                               GrMipmapStatus mipmapStatus,
188                               const GrMockTextureInfo& texInfo,
189                               const GrMockRenderTargetInfo& rtInfo,
190                               GrWrapCacheable cacheable,
191                               std::string_view label)
192             : GrSurface(gpu, dimensions, isProtected, label)
193             , GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, texInfo, label)
194             , GrMockRenderTarget(gpu, dimensions, sampleCnt, isProtected, rtInfo, label) {
195         this->registerWithCacheWrapped(cacheable);
196     }
197 
asTexture()198     GrTexture* asTexture() override { return this; }
asRenderTarget()199     GrRenderTarget* asRenderTarget() override { return this; }
asTexture()200     const GrTexture* asTexture() const override { return this; }
asRenderTarget()201     const GrRenderTarget* asRenderTarget() const override { return this; }
202 
backendFormat()203     GrBackendFormat backendFormat() const override {
204         return GrMockTexture::backendFormat();
205     }
206 
207 private:
onAbandon()208     void onAbandon() override {
209         GrRenderTarget::onAbandon();
210         GrMockTexture::onAbandon();
211     }
212 
onRelease()213     void onRelease() override {
214         GrRenderTarget::onRelease();
215         GrMockTexture::onRelease();
216     }
217 
onGpuMemorySize()218     size_t onGpuMemorySize() const override {
219         int numColorSamples = this->numSamples();
220         if (numColorSamples > 1) {
221             // Add one to account for the resolve buffer.
222             ++numColorSamples;
223         }
224         return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
225                                       numColorSamples, this->mipmapped());
226     }
227 
onSetLabel()228     void onSetLabel() override{}
229 
230     // This avoids an inherits via dominance warning on MSVC.
computeScratchKey(skgpu::ScratchKey * key)231     void computeScratchKey(skgpu::ScratchKey* key) const override {
232         GrTexture::computeScratchKey(key);
233     }
234 };
235 
236 #endif
237