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