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/GrStencilAttachment.h"
13#include "src/gpu/GrTexture.h"
14#include "src/gpu/mock/GrMockGpu.h"
15
16class GrMockTexture : public GrTexture {
17public:
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
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
54protected:
55 // constructor for subclasses
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
74private:
75 GrMockTextureInfo fInfo;
76
77 typedef GrTexture INHERITED;
78};
79
80class GrMockRenderTarget : public GrRenderTarget {
81public:
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 };
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() const override { return true; }
104 bool completeStencilAttachment() override { return true; }
105
106 size_t onGpuMemorySize() const override {
107 int numColorSamples = this->numSamples();
108 if (numColorSamples > 1) {
109 // Add one to account for the resolve buffer.
110 ++numColorSamples;
111 }
112 const GrCaps& caps = *this->getGpu()->caps();
113 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
114 numColorSamples, GrMipmapped::kNo);
115 }
116
117 GrBackendRenderTarget getBackendRenderTarget() const override {
118 int numStencilBits = 0;
119 if (GrStencilAttachment* stencil = this->getStencilAttachment()) {
120 numStencilBits = stencil->bits();
121 }
122 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
123 }
124
125 GrBackendFormat backendFormat() const override {
126 return fInfo.getBackendFormat();
127 }
128
129protected:
130 // constructor for subclasses
131 GrMockRenderTarget(GrMockGpu* gpu,
132 SkISize dimensions,
133 int sampleCnt,
134 GrProtected isProtected,
135 const GrMockRenderTargetInfo& info)
136 : GrSurface(gpu, dimensions, isProtected)
137 , INHERITED(gpu, dimensions, sampleCnt, isProtected)
138 , fInfo(info) {}
139
140private:
141 GrMockRenderTargetInfo fInfo;
142
143 typedef GrRenderTarget INHERITED;
144};
145
146class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
147public:
148 // Internally created.
149 GrMockTextureRenderTarget(GrMockGpu* gpu,
150 SkBudgeted budgeted,
151 SkISize dimensions,
152 int sampleCnt,
153 GrProtected isProtected,
154 GrMipmapStatus mipmapStatus,
155 const GrMockTextureInfo& texInfo,
156 const GrMockRenderTargetInfo& rtInfo)
157 : GrSurface(gpu, dimensions, isProtected)
158 , GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, texInfo)
159 , GrMockRenderTarget(gpu, dimensions, sampleCnt, isProtected, rtInfo) {
160 this->registerWithCache(budgeted);
161 }
162
163 // Renderable wrapped backend texture.
164 GrMockTextureRenderTarget(GrMockGpu* gpu,
165 SkISize dimensions,
166 int sampleCnt,
167 GrProtected isProtected,
168 GrMipmapStatus mipmapStatus,
169 const GrMockTextureInfo& texInfo,
170 const GrMockRenderTargetInfo& rtInfo,
171 GrWrapCacheable cacheable)
172 : GrSurface(gpu, dimensions, isProtected)
173 , GrMockTexture(gpu, dimensions, isProtected, mipmapStatus, texInfo)
174 , GrMockRenderTarget(gpu, dimensions, sampleCnt, isProtected, rtInfo) {
175 this->registerWithCacheWrapped(cacheable);
176 }
177
178 GrTexture* asTexture() override { return this; }
179 GrRenderTarget* asRenderTarget() override { return this; }
180 const GrTexture* asTexture() const override { return this; }
181 const GrRenderTarget* asRenderTarget() const override { return this; }
182
183 GrBackendFormat backendFormat() const override {
184 return GrMockTexture::backendFormat();
185 }
186
187protected:
188 // This avoids an inherits via dominance warning on MSVC.
189 void willRemoveLastRef() override { GrTexture::willRemoveLastRef(); }
190
191private:
192 void onAbandon() override {
193 GrRenderTarget::onAbandon();
194 GrMockTexture::onAbandon();
195 }
196
197 void onRelease() override {
198 GrRenderTarget::onRelease();
199 GrMockTexture::onRelease();
200 }
201
202 size_t onGpuMemorySize() const override {
203 int numColorSamples = this->numSamples();
204 if (numColorSamples > 1) {
205 // Add one to account for the resolve buffer.
206 ++numColorSamples;
207 }
208 const GrCaps& caps = *this->getGpu()->caps();
209 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
210 numColorSamples, this->mipmapped());
211 }
212
213 // This avoids an inherits via dominance warning on MSVC.
214 void computeScratchKey(GrScratchKey* key) const override { GrTexture::computeScratchKey(key); }
215};
216
217#endif
218