blob: d7aa866b1a1e6ef669c1263a043244530b36270d [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"
14#include "src/gpu/GrTexturePriv.h"
15#include "src/gpu/mock/GrMockGpu.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
47protected:
48 // constructor for subclasses
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040049 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus,
Brian Salomon8fe24272017-07-07 12:56:11 -040050 const GrMockTextureInfo& info)
Brian Salomoncfe910d2017-07-06 16:40:18 -040051 : GrSurface(gpu, desc)
Brian Salomone632dfc2018-08-01 13:01:16 -040052 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Greg Daniel6a0176b2018-01-30 09:28:44 -050053 , fInfo(info) {}
Brian Salomoncfe910d2017-07-06 16:40:18 -040054
Greg Daniel4684f822018-03-08 15:27:36 -050055 void onRelease() override {
Greg Daniel4684f822018-03-08 15:27:36 -050056 INHERITED::onRelease();
57 }
58
59 void onAbandon() override {
Greg Daniel4684f822018-03-08 15:27:36 -050060 INHERITED::onAbandon();
61 }
62
Eric Karl914a36b2017-10-12 12:44:50 -070063 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override {
64 return false;
65 }
66
Brian Salomon614c1a82018-12-19 15:42:06 -050067private:
Brian Salomon614c1a82018-12-19 15:42:06 -050068 GrMockTextureInfo fInfo;
Brian Salomoncfe910d2017-07-06 16:40:18 -040069
70 typedef GrTexture INHERITED;
71};
72
Brian Salomon0c51eea2018-03-09 17:02:09 -050073class GrMockRenderTarget : public GrRenderTarget {
Brian Salomoncfe910d2017-07-06 16:40:18 -040074public:
Brian Salomon0c51eea2018-03-09 17:02:09 -050075 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
76 const GrMockRenderTargetInfo& info)
77 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040078 this->registerWithCache(budgeted);
79 }
Robert Phillipsb67821d2017-12-13 15:00:45 -050080
Brian Salomon0c51eea2018-03-09 17:02:09 -050081 enum Wrapped { kWrapped };
82 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc,
83 const GrMockRenderTargetInfo& info)
84 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050085 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
Robert Phillipsb67821d2017-12-13 15:00:45 -050086 }
87
Brian Salomon0c51eea2018-03-09 17:02:09 -050088 ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
Brian Salomoncfe910d2017-07-06 16:40:18 -040089 bool canAttemptStencilAttachment() const override { return true; }
90 bool completeStencilAttachment() override { return true; }
Brian Salomon0c51eea2018-03-09 17:02:09 -050091
92 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -060093 int numColorSamples = this->numSamples();
Brian Salomon0c51eea2018-03-09 17:02:09 -050094 if (numColorSamples > 1) {
95 // Add one to account for the resolve buffer.
96 ++numColorSamples;
97 }
98 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
99 numColorSamples, GrMipMapped::kNo);
100 }
101
102 GrBackendRenderTarget getBackendRenderTarget() const override {
103 int numStencilBits = 0;
104 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
105 numStencilBits = stencil->bits();
106 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600107 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo};
Brian Salomon0c51eea2018-03-09 17:02:09 -0500108 }
109
Greg Daniel4065d452018-11-16 15:43:41 -0500110 GrBackendFormat backendFormat() const override {
111 return GrBackendFormat::MakeMock(fInfo.fConfig);
112 }
113
Brian Salomon0c51eea2018-03-09 17:02:09 -0500114protected:
115 // constructor for subclasses
116 GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
117 const GrMockRenderTargetInfo& info)
118 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {}
119
120private:
121 GrMockRenderTargetInfo fInfo;
122
123 typedef GrRenderTarget INHERITED;
124};
125
126class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget {
127public:
128 // Internally created.
129 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc,
130 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
131 const GrMockRenderTargetInfo& rtInfo)
132 : GrSurface(gpu, desc)
133 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
134 , GrMockRenderTarget(gpu, desc, rtInfo) {
135 this->registerWithCache(budgeted);
136 }
137
138 // Renderable wrapped backend texture.
139 GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc,
140 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500141 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble)
Brian Salomon0c51eea2018-03-09 17:02:09 -0500142 : GrSurface(gpu, desc)
143 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo)
144 , GrMockRenderTarget(gpu, desc, rtInfo) {
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500145 this->registerWithCacheWrapped(cacheble);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500146 }
147
Brian Salomoncfe910d2017-07-06 16:40:18 -0400148 GrTexture* asTexture() override { return this; }
149 GrRenderTarget* asRenderTarget() override { return this; }
150 const GrTexture* asTexture() const override { return this; }
151 const GrRenderTarget* asRenderTarget() const override { return this; }
152
Greg Daniel4065d452018-11-16 15:43:41 -0500153 GrBackendFormat backendFormat() const override {
154 return GrMockTexture::backendFormat();
155 }
156
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500157protected:
158 // This avoids an inherits via dominance warning on MSVC.
159 void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); }
160
Brian Salomoncfe910d2017-07-06 16:40:18 -0400161private:
162 void onAbandon() override {
163 GrRenderTarget::onAbandon();
164 GrMockTexture::onAbandon();
165 }
166
167 void onRelease() override {
168 GrRenderTarget::onRelease();
169 GrMockTexture::onRelease();
170 }
171
172 size_t onGpuMemorySize() const override {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600173 int numColorSamples = this->numSamples();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500174 if (numColorSamples > 1) {
175 // Add one to account for the resolve buffer.
176 ++numColorSamples;
177 }
Brian Salomoncfe910d2017-07-06 16:40:18 -0400178 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Brian Salomon57b04302018-01-30 15:43:49 -0500179 numColorSamples,
Greg Daniele252f082017-10-23 16:05:23 -0400180 this->texturePriv().mipMapped());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400181 }
182
183 void computeScratchKey(GrScratchKey* key) const override {
184 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Chris Dalton6ce447a2019-06-23 18:07:38 -0600185 true, this->numSamples(),
Greg Daniele252f082017-10-23 16:05:23 -0400186 this->texturePriv().mipMapped(), key);
Brian Salomoncfe910d2017-07-06 16:40:18 -0400187 }
188};
189
190#endif