blob: ac5eab0576e6187b8190be408923f6e8a4eb7b77 [file] [log] [blame]
Brian Salomoncfe910d2017-07-06 16:40:18 -04001/*
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 "GrMockGpu.h"
Robert Phillips2890fbf2017-07-26 15:48:41 -040011#include "GrRenderTarget.h"
Brian Salomon0c51eea2018-03-09 17:02:09 -050012#include "GrRenderTargetPriv.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040013#include "GrTexture.h"
14#include "GrTexturePriv.h"
Brian Salomon8fe24272017-07-07 12:56:11 -040015#include "mock/GrMockTypes.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040016
17class GrMockTexture : public GrTexture {
18public:
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040019 GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
20 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
21 : GrMockTexture(gpu, desc, mipMapsStatus, info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040022 this->registerWithCache(budgeted);
23 }
Greg Daniel4684f822018-03-08 15:27:36 -050024
25 enum Wrapped { kWrapped };
Brian Salomonc67c31c2018-12-06 10:00:03 -050026 GrMockTexture(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
27 const GrMockTextureInfo& info, GrIOType ioType, bool purgeImmediately)
Greg Daniel4684f822018-03-08 15:27:36 -050028 : GrMockTexture(gpu, desc, mipMapsStatus, info) {
Brian Salomonc67c31c2018-12-06 10:00:03 -050029 if (ioType == kRead_GrIOType) {
30 this->setReadOnly();
31 }
Greg Daniel2268ad22018-11-15 09:27:38 -050032 this->registerWithCacheWrapped(purgeImmediately);
Greg Daniel4684f822018-03-08 15:27:36 -050033 }
34
Greg Daniel6a0176b2018-01-30 09:28:44 -050035 ~GrMockTexture() override {}
36
Robert Phillipsb67821d2017-12-13 15:00:45 -050037 GrBackendTexture getBackendTexture() const override {
Brian Salomon0c51eea2018-03-09 17:02:09 -050038 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(),
39 fInfo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050040 }
41
Greg Daniel4065d452018-11-16 15:43:41 -050042 GrBackendFormat backendFormat() const override {
43 return GrBackendFormat::MakeMock(fInfo.fConfig);
44 }
45
Brian Salomoncfe910d2017-07-06 16:40:18 -040046 void textureParamsModified() override {}
Greg Daniel6a0176b2018-01-30 09:28:44 -050047 void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {
48 fReleaseHelper = std::move(releaseHelper);
Brian Salomoncfe910d2017-07-06 16:40:18 -040049 }
50
Brian Salomon614c1a82018-12-19 15:42:06 -050051 void setIdleProc(IdleProc proc, void* context) override {
52 fIdleProc = proc;
53 fIdleProcContext = context;
54 }
55
Brian Salomoncfe910d2017-07-06 16:40:18 -040056protected:
57 // constructor for subclasses
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040058 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
Brian Salomon8fe24272017-07-07 12:56:11 -040059 const GrMockTextureInfo& info)
Brian Salomoncfe910d2017-07-06 16:40:18 -040060 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040061 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050062 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040063
Greg Daniel4684f822018-03-08 15:27:36 -050064 void onRelease() override {
65 this->invokeReleaseProc();
66 INHERITED::onRelease();
67 }
68
69 void onAbandon() override {
70 this->invokeReleaseProc();
71 INHERITED::onAbandon();
72 }
73
Eric Karl914a36b2017-10-12 12:44:50 -070074 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
75 return false;
76 }
77
Brian Salomon614c1a82018-12-19 15:42:06 -050078 // protected so that GrMockTextureRenderTarget can call this to avoid "inheritance via
79 // dominance" warning.
80 void becamePurgeable() override {
81 if (fIdleProc) {
82 fIdleProc(fIdleProcContext);
83 fIdleProc = nullptr;
84 fIdleProcContext = nullptr;
Greg Daniel4684f822018-03-08 15:27:36 -050085 }
86 }
Brian Salomon614c1a82018-12-19 15:42:06 -050087
88private:
89 void invokeReleaseProc() {
90 // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
91 // ReleaseProc to be called.
92 fReleaseHelper.reset();
93 }
94
95 GrMockTextureInfo fInfo;
Greg Daniel6a0176b2018-01-30 09:28:44 -050096 sk_sp<GrReleaseProcHelper> fReleaseHelper;
Brian Salomon614c1a82018-12-19 15:42:06 -050097 IdleProc* fIdleProc = nullptr;
98 void* fIdleProcContext = nullptr;
Brian Salomoncfe910d2017-07-06 16:40:18 -040099
100 typedef GrTexture INHERITED;
101};
102
Brian Salomon0c51eea2018-03-09 17:02:09 -0500103class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400104public:
Brian Salomon0c51eea2018-03-09 17:02:09 -0500105 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
106 const GrMockRenderTargetInfo& info)
107 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -0400108 this->registerWithCache(budgeted);
109 }
Robert Phillipsb67821d2017-12-13 15:00:45 -0500110
Brian Salomon0c51eea2018-03-09 17:02:09 -0500111 enum Wrapped { kWrapped };
112 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
113 const GrMockRenderTargetInfo& info)
114 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
115 this->registerWithCacheWrapped();
Robert Phillipsb67821d2017-12-13 15:00:45 -0500116 }
117
Brian Salomon0c51eea2018-03-09 17:02:09 -0500118 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400119 bool canAttemptStencilAttachment() const override { return true; }
120 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -0500121
122 size_t onGpuMemorySize() const override {
123 int numColorSamples = this->numColorSamples();
124 if (numColorSamples > 1) {
125 // Add one to account for the resolve buffer.
126 ++numColorSamples;
127 }
128 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
129 numColorSamples, GrMipMapped::kNo);
130 }
131
132 GrBackendRenderTarget getBackendRenderTarget() const override {
133 int numStencilBits = 0;
134 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
135 numStencilBits = stencil->bits();
136 }
137 return {this->width(), this->height(), this->numColorSamples(), numStencilBits, fInfo};
138 }
139
Greg Daniel4065d452018-11-16 15:43:41 -0500140 GrBackendFormat backendFormat() const override {
141 return GrBackendFormat::MakeMock(fInfo.fConfig);
142 }
143
Brian Salomon0c51eea2018-03-09 17:02:09 -0500144protected:
145 // constructor for subclasses
146 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
147 const GrMockRenderTargetInfo& info)
148 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {}
149
150private:
151 GrMockRenderTargetInfo fInfo;
152
153 typedef GrRenderTarget INHERITED;
154};
155
156class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
157public:
158 // Internally created.
159 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
160 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
161 const GrMockRenderTargetInfo& rtInfo)
162 : GrSurface(gpu, desc)
163 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
164 , GrMockRenderTarget(gpu, desc, rtInfo) {
165 this->registerWithCache(budgeted);
166 }
167
168 // Renderable wrapped backend texture.
169 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
170 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
171 const GrMockRenderTargetInfo& rtInfo)
172 : GrSurface(gpu, desc)
173 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
174 , GrMockRenderTarget(gpu, desc, rtInfo) {
175 this->registerWithCacheWrapped();
176 }
177
Brian Salomoncfe910d2017-07-06 16:40:18 -0400178 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
Greg Daniel4065d452018-11-16 15:43:41 -0500183 GrBackendFormat backendFormat() const override {
184 return GrMockTexture::backendFormat();
185 }
186
Brian Salomoncfe910d2017-07-06 16:40:18 -0400187private:
188 void onAbandon() override {
189 GrRenderTarget::onAbandon();
190 GrMockTexture::onAbandon();
191 }
192
193 void onRelease() override {
194 GrRenderTarget::onRelease();
195 GrMockTexture::onRelease();
196 }
197
Brian Salomon614c1a82018-12-19 15:42:06 -0500198 // We implement this to avoid the inheritance via dominance warning.
199 void becamePurgeable() override { GrMockTexture::becamePurgeable(); }
200
Brian Salomoncfe910d2017-07-06 16:40:18 -0400201 size_t onGpuMemorySize() const override {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500202 int numColorSamples = this->numColorSamples();
203 if (numColorSamples > 1) {
204 // Add one to account for the resolve buffer.
205 ++numColorSamples;
206 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400207 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500208 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400209 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400210 }
211
212 void computeScratchKey(GrScratchKey* key) const override {
213 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400214 true, this->numStencilSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400215 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400216 }
217};
218
219#endif