blob: f1d977c9d48bbe1418b019f54480a5b7f3af42f8 [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
Brian Salomonfa2ebea2019-01-24 15:58:58 -050025 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
26 const GrMockTextureInfo& info, GrWrapCacheable cacheable, GrIOType ioType)
Greg Daniel4684f822018-03-08 15:27:36 -050027 : GrMockTexture(gpu, desc, mipMapsStatus, info) {
Brian Salomonc67c31c2018-12-06 10:00:03 -050028 if (ioType == kRead_GrIOType) {
29 this->setReadOnly();
30 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050031 this->registerWithCacheWrapped(cacheable);
Greg Daniel4684f822018-03-08 15:27:36 -050032 }
33
Greg Daniel6a0176b2018-01-30 09:28:44 -050034 ~GrMockTexture() override {}
35
Robert Phillipsb67821d2017-12-13 15:00:45 -050036 GrBackendTexture getBackendTexture() const override {
Brian Salomon0c51eea2018-03-09 17:02:09 -050037 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(),
38 fInfo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050039 }
40
Greg Daniel4065d452018-11-16 15:43:41 -050041 GrBackendFormat backendFormat() const override {
42 return GrBackendFormat::MakeMock(fInfo.fConfig);
43 }
44
Brian Salomoncfe910d2017-07-06 16:40:18 -040045 void textureParamsModified() override {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040046
Brian Salomon614c1a82018-12-19 15:42:06 -050047 void setIdleProc(IdleProc proc, void* context) override {
48 fIdleProc = proc;
49 fIdleProcContext = context;
50 }
Brian Salomon1bf0ed82019-01-16 13:51:35 -050051 void* idleContext() const override { return fIdleProcContext; }
Brian Salomon614c1a82018-12-19 15:42:06 -050052
Brian Salomoncfe910d2017-07-06 16:40:18 -040053protected:
54 // constructor for subclasses
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040055 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
Brian Salomon8fe24272017-07-07 12:56:11 -040056 const GrMockTextureInfo& info)
Brian Salomoncfe910d2017-07-06 16:40:18 -040057 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040058 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050059 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040060
Greg Daniel4684f822018-03-08 15:27:36 -050061 void onRelease() override {
Greg Daniel4684f822018-03-08 15:27:36 -050062 INHERITED::onRelease();
63 }
64
65 void onAbandon() override {
Greg Daniel4684f822018-03-08 15:27:36 -050066 INHERITED::onAbandon();
67 }
68
Eric Karl914a36b2017-10-12 12:44:50 -070069 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
70 return false;
71 }
72
Brian Salomon614c1a82018-12-19 15:42:06 -050073 // protected so that GrMockTextureRenderTarget can call this to avoid "inheritance via
74 // dominance" warning.
Brian Salomon8cabb322019-02-22 10:44:19 -050075 void willRemoveLastRefOrPendingIO() override {
Brian Salomon614c1a82018-12-19 15:42:06 -050076 if (fIdleProc) {
77 fIdleProc(fIdleProcContext);
78 fIdleProc = nullptr;
79 fIdleProcContext = nullptr;
Greg Daniel4684f822018-03-08 15:27:36 -050080 }
81 }
Brian Salomon614c1a82018-12-19 15:42:06 -050082
83private:
Greg Daniel2d35a1c2019-02-01 14:48:10 -050084 void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {}
Brian Salomon614c1a82018-12-19 15:42:06 -050085
86 GrMockTextureInfo fInfo;
Greg Daniel6a0176b2018-01-30 09:28:44 -050087 sk_sp<GrReleaseProcHelper> fReleaseHelper;
Brian Salomon614c1a82018-12-19 15:42:06 -050088 IdleProc* fIdleProc = nullptr;
89 void* fIdleProcContext = nullptr;
Brian Salomoncfe910d2017-07-06 16:40:18 -040090
91 typedef GrTexture INHERITED;
92};
93
Brian Salomon0c51eea2018-03-09 17:02:09 -050094class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -040095public:
Brian Salomon0c51eea2018-03-09 17:02:09 -050096 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
97 const GrMockRenderTargetInfo& info)
98 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040099 this->registerWithCache(budgeted);
100 }
Robert Phillipsb67821d2017-12-13 15:00:45 -0500101
Brian Salomon0c51eea2018-03-09 17:02:09 -0500102 enum Wrapped { kWrapped };
103 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
104 const GrMockRenderTargetInfo& info)
105 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500106 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
Robert Phillipsb67821d2017-12-13 15:00:45 -0500107 }
108
Brian Salomon0c51eea2018-03-09 17:02:09 -0500109 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400110 bool canAttemptStencilAttachment() const override { return true; }
111 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -0500112
113 size_t onGpuMemorySize() const override {
114 int numColorSamples = this->numColorSamples();
115 if (numColorSamples > 1) {
116 // Add one to account for the resolve buffer.
117 ++numColorSamples;
118 }
119 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
120 numColorSamples, GrMipMapped::kNo);
121 }
122
123 GrBackendRenderTarget getBackendRenderTarget() const override {
124 int numStencilBits = 0;
125 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
126 numStencilBits = stencil->bits();
127 }
128 return {this->width(), this->height(), this->numColorSamples(), numStencilBits, fInfo};
129 }
130
Greg Daniel4065d452018-11-16 15:43:41 -0500131 GrBackendFormat backendFormat() const override {
132 return GrBackendFormat::MakeMock(fInfo.fConfig);
133 }
134
Brian Salomon0c51eea2018-03-09 17:02:09 -0500135protected:
136 // constructor for subclasses
137 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
138 const GrMockRenderTargetInfo& info)
139 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {}
140
141private:
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500142 void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {}
143
Brian Salomon0c51eea2018-03-09 17:02:09 -0500144 GrMockRenderTargetInfo fInfo;
145
146 typedef GrRenderTarget INHERITED;
147};
148
149class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
150public:
151 // Internally created.
152 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
153 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
154 const GrMockRenderTargetInfo& rtInfo)
155 : GrSurface(gpu, desc)
156 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
157 , GrMockRenderTarget(gpu, desc, rtInfo) {
158 this->registerWithCache(budgeted);
159 }
160
161 // Renderable wrapped backend texture.
162 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
163 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500164 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500165 : GrSurface(gpu, desc)
166 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
167 , GrMockRenderTarget(gpu, desc, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500168 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500169 }
170
Brian Salomoncfe910d2017-07-06 16:40:18 -0400171 GrTexture* asTexture() override { return this; }
172 GrRenderTarget* asRenderTarget() override { return this; }
173 const GrTexture* asTexture() const override { return this; }
174 const GrRenderTarget* asRenderTarget() const override { return this; }
175
Greg Daniel4065d452018-11-16 15:43:41 -0500176 GrBackendFormat backendFormat() const override {
177 return GrMockTexture::backendFormat();
178 }
179
Brian Salomoncfe910d2017-07-06 16:40:18 -0400180private:
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500181 void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {}
182
Brian Salomoncfe910d2017-07-06 16:40:18 -0400183 void onAbandon() override {
184 GrRenderTarget::onAbandon();
185 GrMockTexture::onAbandon();
186 }
187
188 void onRelease() override {
189 GrRenderTarget::onRelease();
190 GrMockTexture::onRelease();
191 }
192
Brian Salomon614c1a82018-12-19 15:42:06 -0500193 // We implement this to avoid the inheritance via dominance warning.
Brian Salomon8cabb322019-02-22 10:44:19 -0500194 void willRemoveLastRefOrPendingIO() override { GrMockTexture::willRemoveLastRefOrPendingIO(); }
Brian Salomon614c1a82018-12-19 15:42:06 -0500195
Brian Salomoncfe910d2017-07-06 16:40:18 -0400196 size_t onGpuMemorySize() const override {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500197 int numColorSamples = this->numColorSamples();
198 if (numColorSamples > 1) {
199 // Add one to account for the resolve buffer.
200 ++numColorSamples;
201 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400202 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500203 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400204 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400205 }
206
207 void computeScratchKey(GrScratchKey* key) const override {
208 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400209 true, this->numStencilSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400210 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400211 }
212};
213
214#endif