blob: edb231e129d392ab1e60b658119f3bc668c2a3fd [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
Chris Dalton30eea6c2019-08-21 10:22:50 -060096 ResolveType getResolveType() const override {
97 return (this->requiresManualMSAAResolve()) ?
98 kCanResolve_ResolveType : kAutoResolves_ResolveType;
99 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400100 bool canAttemptStencilAttachment() const override { return true; }
101 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -0500102
103 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600104 int numColorSamples = this->numSamples();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500105 if (numColorSamples > 1) {
106 // Add one to account for the resolve buffer.
107 ++numColorSamples;
108 }
109 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
110 numColorSamples, GrMipMapped::kNo);
111 }
112
113 GrBackendRenderTarget getBackendRenderTarget() const override {
114 int numStencilBits = 0;
115 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
116 numStencilBits = stencil->bits();
117 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600118 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
Brian Salomon0c51eea2018-03-09 17:02:09 -0500119 }
120
Greg Daniel4065d452018-11-16 15:43:41 -0500121 GrBackendFormat backendFormat() const override {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400122 return fInfo.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -0500123 }
124
Brian Salomon0c51eea2018-03-09 17:02:09 -0500125protected:
126 // constructor for subclasses
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400127 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
128 GrProtected isProtected, const GrMockRenderTargetInfo& info)
Brian Salomona9c22572019-08-05 12:57:09 -0400129 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
130 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, isProtected)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400131 , fInfo(info) {}
Brian Salomon0c51eea2018-03-09 17:02:09 -0500132
133private:
134 GrMockRenderTargetInfo fInfo;
135
136 typedef GrRenderTarget INHERITED;
137};
138
139class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
140public:
141 // Internally created.
142 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400143 int sampleCnt, GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400144 const GrMockTextureInfo& texInfo,
Brian Salomon0c51eea2018-03-09 17:02:09 -0500145 const GrMockRenderTargetInfo& rtInfo)
Brian Salomona9c22572019-08-05 12:57:09 -0400146 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -0400147 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400148 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomon0c51eea2018-03-09 17:02:09 -0500149 this->registerWithCache(budgeted);
150 }
151
152 // Renderable wrapped backend texture.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400153 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
154 GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
155 const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500156 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomona9c22572019-08-05 12:57:09 -0400157 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -0400158 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400159 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500160 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500161 }
162
Brian Salomoncfe910d2017-07-06 16:40:18 -0400163 GrTexture* asTexture() override { return this; }
164 GrRenderTarget* asRenderTarget() override { return this; }
165 const GrTexture* asTexture() const override { return this; }
166 const GrRenderTarget* asRenderTarget() const override { return this; }
167
Greg Daniel4065d452018-11-16 15:43:41 -0500168 GrBackendFormat backendFormat() const override {
169 return GrMockTexture::backendFormat();
170 }
171
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500172protected:
173 // This avoids an inherits via dominance warning on MSVC.
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400174 void willRemoveLastRef() override { GrTexture::willRemoveLastRef(); }
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500175
Brian Salomoncfe910d2017-07-06 16:40:18 -0400176private:
177 void onAbandon() override {
178 GrRenderTarget::onAbandon();
179 GrMockTexture::onAbandon();
180 }
181
182 void onRelease() override {
183 GrRenderTarget::onRelease();
184 GrMockTexture::onRelease();
185 }
186
187 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600188 int numColorSamples = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500189 if (numColorSamples > 1) {
190 // Add one to account for the resolve buffer.
191 ++numColorSamples;
192 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400193 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500194 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400195 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400196 }
197
Brian Salomon14cb4132019-09-16 13:14:47 -0400198 // This avoids an inherits via dominance warning on MSVC.
199 void computeScratchKey(GrScratchKey* key) const override { GrTexture::computeScratchKey(key); }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400200};
201
202#endif