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