blob: 7406cedb092a5f4f7f08bfa0cfd6a8e0c03973d4 [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,
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)
54 : GrSurface(gpu, desc, isProtected)
55 , INHERITED(gpu, desc, isProtected, GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050056 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040057
Greg Daniel4684f822018-03-08 15:27:36 -050058 void onRelease() override {
Greg Daniel4684f822018-03-08 15:27:36 -050059 INHERITED::onRelease();
60 }
61
62 void onAbandon() override {
Greg Daniel4684f822018-03-08 15:27:36 -050063 INHERITED::onAbandon();
64 }
65
Eric Karl914a36b2017-10-12 12:44:50 -070066 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
67 return false;
68 }
69
Brian Salomon614c1a82018-12-19 15:42:06 -050070private:
Brian Salomon614c1a82018-12-19 15:42:06 -050071 GrMockTextureInfo fInfo;
Brian Salomoncfe910d2017-07-06 16:40:18 -040072
73 typedef GrTexture INHERITED;
74};
75
Brian Salomon0c51eea2018-03-09 17:02:09 -050076class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -040077public:
Brian Salomon0c51eea2018-03-09 17:02:09 -050078 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040079 int sampleCnt, GrProtected isProtected, const GrMockRenderTargetInfo& info)
80 : GrSurface(gpu, desc, isProtected)
81 , INHERITED(gpu, desc, sampleCnt, isProtected)
82 , fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040083 this->registerWithCache(budgeted);
84 }
Robert Phillipsb67821d2017-12-13 15:00:45 -050085
Brian Salomon0c51eea2018-03-09 17:02:09 -050086 enum Wrapped { kWrapped };
Brian Salomon27b4d8d2019-07-22 14:23:45 -040087 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, int sampleCnt,
88 GrProtected isProtected, const GrMockRenderTargetInfo& info)
89 : GrSurface(gpu, desc, isProtected)
90 , INHERITED(gpu, desc, sampleCnt, isProtected)
91 , fInfo(info) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050092 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050093 }
94
Brian Salomon0c51eea2018-03-09 17:02:09 -050095 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -040096 bool canAttemptStencilAttachment() const override { return true; }
97 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -050098
99 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600100 int numColorSamples = this->numSamples();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500101 if (numColorSamples > 1) {
102 // Add one to account for the resolve buffer.
103 ++numColorSamples;
104 }
105 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
106 numColorSamples, GrMipMapped::kNo);
107 }
108
109 GrBackendRenderTarget getBackendRenderTarget() const override {
110 int numStencilBits = 0;
111 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
112 numStencilBits = stencil->bits();
113 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600114 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
Brian Salomon0c51eea2018-03-09 17:02:09 -0500115 }
116
Greg Daniel4065d452018-11-16 15:43:41 -0500117 GrBackendFormat backendFormat() const override {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400118 return fInfo.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -0500119 }
120
Brian Salomon0c51eea2018-03-09 17:02:09 -0500121protected:
122 // constructor for subclasses
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400123 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
124 GrProtected isProtected, const GrMockRenderTargetInfo& info)
125 : GrSurface(gpu, desc, isProtected)
126 , INHERITED(gpu, desc, sampleCnt, isProtected)
127 , fInfo(info) {}
Brian Salomon0c51eea2018-03-09 17:02:09 -0500128
129private:
130 GrMockRenderTargetInfo fInfo;
131
132 typedef GrRenderTarget INHERITED;
133};
134
135class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
136public:
137 // Internally created.
138 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400139 int sampleCnt, GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
Brian Salomone8a766b2019-07-19 14:24:36 -0400140 const GrMockTextureInfo& texInfo,
Brian Salomon0c51eea2018-03-09 17:02:09 -0500141 const GrMockRenderTargetInfo& rtInfo)
Brian Salomone8a766b2019-07-19 14:24:36 -0400142 : GrSurface(gpu, desc, isProtected)
143 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400144 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomon0c51eea2018-03-09 17:02:09 -0500145 this->registerWithCache(budgeted);
146 }
147
148 // Renderable wrapped backend texture.
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400149 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, int sampleCnt,
150 GrProtected isProtected, GrMipMapsStatus mipMapsStatus,
151 const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500152 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomone8a766b2019-07-19 14:24:36 -0400153 : GrSurface(gpu, desc, isProtected)
154 , GrMockTexture(gpu, desc, isProtected, mipMapsStatus, texInfo)
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400155 , GrMockRenderTarget(gpu, desc, sampleCnt, isProtected, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500156 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500157 }
158
Brian Salomoncfe910d2017-07-06 16:40:18 -0400159 GrTexture* asTexture() override { return this; }
160 GrRenderTarget* asRenderTarget() override { return this; }
161 const GrTexture* asTexture() const override { return this; }
162 const GrRenderTarget* asRenderTarget() const override { return this; }
163
Greg Daniel4065d452018-11-16 15:43:41 -0500164 GrBackendFormat backendFormat() const override {
165 return GrMockTexture::backendFormat();
166 }
167
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500168protected:
169 // This avoids an inherits via dominance warning on MSVC.
170 void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); }
171
Brian Salomoncfe910d2017-07-06 16:40:18 -0400172private:
173 void onAbandon() override {
174 GrRenderTarget::onAbandon();
175 GrMockTexture::onAbandon();
176 }
177
178 void onRelease() override {
179 GrRenderTarget::onRelease();
180 GrMockTexture::onRelease();
181 }
182
183 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600184 int numColorSamples = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500185 if (numColorSamples > 1) {
186 // Add one to account for the resolve buffer.
187 ++numColorSamples;
188 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400189 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500190 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400191 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400192 }
193
194 void computeScratchKey(GrScratchKey* key) const override {
195 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400196 GrRenderable::kYes, this->numSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400197 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400198 }
199};
200
201#endif