blob: ddf5e52737c4f956a60dc043b6eeb1e0980b25c2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrRenderTarget.h"
11#include "include/gpu/GrTexture.h"
12#include "include/gpu/mock/GrMockTypes.h"
13#include "src/gpu/GrRenderTargetPriv.h"
Hal Canary02eefbe2019-06-26 13:54:14 -040014#include "src/gpu/GrStencilAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrTexturePriv.h"
16#include "src/gpu/mock/GrMockGpu.h"
Brian Salomoncfe910d2017-07-06 16:40:18 -040017
18class GrMockTexture : public GrTexture {
19public:
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040020 GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
21 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
22 : GrMockTexture(gpu, desc, mipMapsStatus, info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040023 this->registerWithCache(budgeted);
24 }
Greg Daniel4684f822018-03-08 15:27:36 -050025
Brian Salomonfa2ebea2019-01-24 15:58:58 -050026 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
27 const GrMockTextureInfo& info, GrWrapCacheable cacheable, GrIOType ioType)
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 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050032 this->registerWithCacheWrapped(cacheable);
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 {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040047
48protected:
49 // constructor for subclasses
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040050 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
Brian Salomon8fe24272017-07-07 12:56:11 -040051 const GrMockTextureInfo& info)
Brian Salomoncfe910d2017-07-06 16:40:18 -040052 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040053 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050054 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040055
Greg Daniel4684f822018-03-08 15:27:36 -050056 void onRelease() override {
Greg Daniel4684f822018-03-08 15:27:36 -050057 INHERITED::onRelease();
58 }
59
60 void onAbandon() override {
Greg Daniel4684f822018-03-08 15:27:36 -050061 INHERITED::onAbandon();
62 }
63
Eric Karl914a36b2017-10-12 12:44:50 -070064 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
65 return false;
66 }
67
Brian Salomon614c1a82018-12-19 15:42:06 -050068private:
Brian Salomon614c1a82018-12-19 15:42:06 -050069 GrMockTextureInfo fInfo;
Brian Salomoncfe910d2017-07-06 16:40:18 -040070
71 typedef GrTexture INHERITED;
72};
73
Brian Salomon0c51eea2018-03-09 17:02:09 -050074class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -040075public:
Brian Salomon0c51eea2018-03-09 17:02:09 -050076 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
77 const GrMockRenderTargetInfo& info)
78 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040079 this->registerWithCache(budgeted);
80 }
Robert Phillipsb67821d2017-12-13 15:00:45 -050081
Brian Salomon0c51eea2018-03-09 17:02:09 -050082 enum Wrapped { kWrapped };
83 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
84 const GrMockRenderTargetInfo& info)
85 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050086 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050087 }
88
Brian Salomon0c51eea2018-03-09 17:02:09 -050089 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -040090 bool canAttemptStencilAttachment() const override { return true; }
91 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -050092
93 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -060094 int numColorSamples = this->numSamples();
Brian Salomon0c51eea2018-03-09 17:02:09 -050095 if (numColorSamples > 1) {
96 // Add one to account for the resolve buffer.
97 ++numColorSamples;
98 }
99 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
100 numColorSamples, GrMipMapped::kNo);
101 }
102
103 GrBackendRenderTarget getBackendRenderTarget() const override {
104 int numStencilBits = 0;
105 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
106 numStencilBits = stencil->bits();
107 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600108 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
Brian Salomon0c51eea2018-03-09 17:02:09 -0500109 }
110
Greg Daniel4065d452018-11-16 15:43:41 -0500111 GrBackendFormat backendFormat() const override {
112 return GrBackendFormat::MakeMock(fInfo.fConfig);
113 }
114
Brian Salomon0c51eea2018-03-09 17:02:09 -0500115protected:
116 // constructor for subclasses
117 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
118 const GrMockRenderTargetInfo& info)
119 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {}
120
121private:
122 GrMockRenderTargetInfo fInfo;
123
124 typedef GrRenderTarget INHERITED;
125};
126
127class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
128public:
129 // Internally created.
130 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
131 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
132 const GrMockRenderTargetInfo& rtInfo)
133 : GrSurface(gpu, desc)
134 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
135 , GrMockRenderTarget(gpu, desc, rtInfo) {
136 this->registerWithCache(budgeted);
137 }
138
139 // Renderable wrapped backend texture.
140 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
141 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500142 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500143 : GrSurface(gpu, desc)
144 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
145 , GrMockRenderTarget(gpu, desc, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500146 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500147 }
148
Brian Salomoncfe910d2017-07-06 16:40:18 -0400149 GrTexture* asTexture() override { return this; }
150 GrRenderTarget* asRenderTarget() override { return this; }
151 const GrTexture* asTexture() const override { return this; }
152 const GrRenderTarget* asRenderTarget() const override { return this; }
153
Greg Daniel4065d452018-11-16 15:43:41 -0500154 GrBackendFormat backendFormat() const override {
155 return GrMockTexture::backendFormat();
156 }
157
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500158protected:
159 // This avoids an inherits via dominance warning on MSVC.
160 void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); }
161
Brian Salomoncfe910d2017-07-06 16:40:18 -0400162private:
163 void onAbandon() override {
164 GrRenderTarget::onAbandon();
165 GrMockTexture::onAbandon();
166 }
167
168 void onRelease() override {
169 GrRenderTarget::onRelease();
170 GrMockTexture::onRelease();
171 }
172
173 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600174 int numColorSamples = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500175 if (numColorSamples > 1) {
176 // Add one to account for the resolve buffer.
177 ++numColorSamples;
178 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400179 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500180 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400181 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400182 }
183
184 void computeScratchKey(GrScratchKey* key) const override {
185 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Chris Dalton6ce447a2019-06-23 18:07:38 -0600186 true, this->numSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400187 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400188 }
189};
190
191#endif