blob: f954f58c9632674b2de55cbcab8dae61b97eb6ec [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/GrTexture.h"
11#include "include/gpu/mock/GrMockTypes.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#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,
Brian Salomone8a766b2019-07-19 14:24:36 -040021 GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
22 const GrMockTextureInfo& info)
23 : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040024 this->registerWithCache(budgeted);
25 }
Greg Daniel4684f822018-03-08 15:27:36 -050026
Brian Salomone8a766b2019-07-19 14:24:36 -040027 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
28 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info,
29 GrWrapCacheable cacheable, GrIOType ioType)
30 : GrMockTexture(gpu, desc, isProtected, mipMapsStatus, info) {
Brian Salomonc67c31c2018-12-06 10:00:03 -050031 if (ioType == kRead_GrIOType) {
32 this->setReadOnly();
33 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050034 this->registerWithCacheWrapped(cacheable);
Greg Daniel4684f822018-03-08 15:27:36 -050035 }
36
Greg Daniel6a0176b2018-01-30 09:28:44 -050037 ~GrMockTexture() override {}
38
Robert Phillipsb67821d2017-12-13 15:00:45 -050039 GrBackendTexture getBackendTexture() const override {
Brian Salomon0c51eea2018-03-09 17:02:09 -050040 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(),
41 fInfo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050042 }
43
Greg Daniel4065d452018-11-16 15:43:41 -050044 GrBackendFormat backendFormat() const override {
Robert Phillipsa5e78be2019-07-09 12:34:38 -040045 return fInfo.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -050046 }
47
Brian Salomoncfe910d2017-07-06 16:40:18 -040048 void textureParamsModified() override {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040049
50protected:
51 // constructor for subclasses
Brian Salomone8a766b2019-07-19 14:24:36 -040052 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrProtected isProtected,
53 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info)
Brian Salomona9c22572019-08-05 12:57:09 -040054 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
55 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected,
56 GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050057 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040058
Greg Daniel4684f822018-03-08 15:27:36 -050059 void onRelease() override {
Greg Daniel4684f822018-03-08 15:27:36 -050060 INHERITED::onRelease();
61 }
62
63 void onAbandon() override {
Greg Daniel4684f822018-03-08 15:27:36 -050064 INHERITED::onAbandon();
65 }
66
Eric Karl914a36b2017-10-12 12:44:50 -070067 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
68 return false;
69 }
70
Brian Salomon614c1a82018-12-19 15:42:06 -050071private:
Brian Salomon614c1a82018-12-19 15:42:06 -050072 GrMockTextureInfo fInfo;
Brian Salomoncfe910d2017-07-06 16:40:18 -040073
74 typedef GrTexture INHERITED;
75};
76
Brian Salomon0c51eea2018-03-09 17:02:09 -050077class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -040078public:
Brian Salomon0c51eea2018-03-09 17:02:09 -050079 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040080 int sampleCnt, GrProtected isProtected, const GrMockRenderTargetInfo& info)
Brian Salomona9c22572019-08-05 12:57:09 -040081 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
82 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040083 , fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040084 this->registerWithCache(budgeted);
85 }
Robert Phillipsb67821d2017-12-13 15:00:45 -050086
Brian Salomon0c51eea2018-03-09 17:02:09 -050087 enum Wrapped { kWrapped };
Brian Salomon27b4d8d2019-07-22 14:23:45 -040088 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, int sampleCnt,
89 GrProtected isProtected, const GrMockRenderTargetInfo& info)
Brian Salomona9c22572019-08-05 12:57:09 -040090 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
91 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -040092 , fInfo(info) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050093 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050094 }
95
Brian Salomon0c51eea2018-03-09 17:02:09 -050096 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -040097 bool canAttemptStencilAttachment() const override { return true; }
98 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -050099
100 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600101 int numColorSamples = this->numSamples();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500102 if (numColorSamples > 1) {
103 // Add one to account for the resolve buffer.
104 ++numColorSamples;
105 }
106 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
107 numColorSamples, GrMipMapped::kNo);
108 }
109
110 GrBackendRenderTarget getBackendRenderTarget() const override {
111 int numStencilBits = 0;
112 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
113 numStencilBits = stencil->bits();
114 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600115 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
Brian Salomon0c51eea2018-03-09 17:02:09 -0500116 }
117
Greg Daniel4065d452018-11-16 15:43:41 -0500118 GrBackendFormat backendFormat() const override {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400119 return fInfo.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -0500120 }
121
Brian Salomon0c51eea2018-03-09 17:02:09 -0500122protected:
123 // constructor for subclasses
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400124 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
125 GrProtected isProtected, const GrMockRenderTargetInfo& info)
Brian Salomona9c22572019-08-05 12:57:09 -0400126 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
127 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400128 , fInfo(info) {}
Brian Salomon0c51eea2018-03-09 17:02:09 -0500129
130private:
131 GrMockRenderTargetInfo fInfo;
132
133 typedef GrRenderTarget INHERITED;
134};
135
136class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
137public:
138 // Internally created.
139 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400140 int sampleCnt, GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400141 const GrMockTextureInfo& texInfo,
Brian Salomon0c51eea2018-03-09 17:02:09 -0500142 const GrMockRenderTargetInfo& rtInfo)
Brian Salomona9c22572019-08-05 12:57:09 -0400143 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -0400144 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400145 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomon0c51eea2018-03-09 17:02:09 -0500146 this->registerWithCache(budgeted);
147 }
148
149 // Renderable wrapped backend texture.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400150 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
151 GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
152 const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500153 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomona9c22572019-08-05 12:57:09 -0400154 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -0400155 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400156 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500157 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500158 }
159
Brian Salomoncfe910d2017-07-06 16:40:18 -0400160 GrTexture* asTexture() override { return this; }
161 GrRenderTarget* asRenderTarget() override { return this; }
162 const GrTexture* asTexture() const override { return this; }
163 const GrRenderTarget* asRenderTarget() const override { return this; }
164
Greg Daniel4065d452018-11-16 15:43:41 -0500165 GrBackendFormat backendFormat() const override {
166 return GrMockTexture::backendFormat();
167 }
168
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500169protected:
170 // This avoids an inherits via dominance warning on MSVC.
171 void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); }
172
Brian Salomoncfe910d2017-07-06 16:40:18 -0400173private:
174 void onAbandon() override {
175 GrRenderTarget::onAbandon();
176 GrMockTexture::onAbandon();
177 }
178
179 void onRelease() override {
180 GrRenderTarget::onRelease();
181 GrMockTexture::onRelease();
182 }
183
184 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600185 int numColorSamples = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500186 if (numColorSamples > 1) {
187 // Add one to account for the resolve buffer.
188 ++numColorSamples;
189 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400190 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500191 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400192 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400193 }
194
195 void computeScratchKey(GrScratchKey* key) const override {
196 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400197 GrRenderable::kYes, this->numSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400198 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400199 }
200};
201
202#endif