Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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 Canary | 02eefbe | 2019-06-26 13:54:14 -0400 | [diff] [blame^] | 14 | #include "src/gpu/GrStencilAttachment.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrTexturePriv.h" |
| 16 | #include "src/gpu/mock/GrMockGpu.h" |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 17 | |
| 18 | class GrMockTexture : public GrTexture { |
| 19 | public: |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 20 | GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 21 | GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info) |
| 22 | : GrMockTexture(gpu, desc, mipMapsStatus, info) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 23 | this->registerWithCache(budgeted); |
| 24 | } |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 25 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 26 | GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, |
| 27 | const GrMockTextureInfo& info, GrWrapCacheable cacheable, GrIOType ioType) |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 28 | : GrMockTexture(gpu, desc, mipMapsStatus, info) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 29 | if (ioType == kRead_GrIOType) { |
| 30 | this->setReadOnly(); |
| 31 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 32 | this->registerWithCacheWrapped(cacheable); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 35 | ~GrMockTexture() override {} |
| 36 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 37 | GrBackendTexture getBackendTexture() const override { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 38 | return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), |
| 39 | fInfo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 42 | GrBackendFormat backendFormat() const override { |
| 43 | return GrBackendFormat::MakeMock(fInfo.fConfig); |
| 44 | } |
| 45 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 46 | void textureParamsModified() override {} |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 47 | |
| 48 | protected: |
| 49 | // constructor for subclasses |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 50 | GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 51 | const GrMockTextureInfo& info) |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 52 | : GrSurface(gpu, desc) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 53 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 54 | , fInfo(info) {} |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 55 | |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 56 | void onRelease() override { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 57 | INHERITED::onRelease(); |
| 58 | } |
| 59 | |
| 60 | void onAbandon() override { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 61 | INHERITED::onAbandon(); |
| 62 | } |
| 63 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 64 | bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { |
| 65 | return false; |
| 66 | } |
| 67 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 68 | private: |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 69 | GrMockTextureInfo fInfo; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 70 | |
| 71 | typedef GrTexture INHERITED; |
| 72 | }; |
| 73 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 74 | class GrMockRenderTarget : public GrRenderTarget { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 75 | public: |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 76 | GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 77 | const GrMockRenderTargetInfo& info) |
| 78 | : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 79 | this->registerWithCache(budgeted); |
| 80 | } |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 81 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 82 | 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 Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 86 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 89 | ResolveType getResolveType() const override { return kCanResolve_ResolveType; } |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 90 | bool canAttemptStencilAttachment() const override { return true; } |
| 91 | bool completeStencilAttachment() override { return true; } |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 92 | |
| 93 | size_t onGpuMemorySize() const override { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 94 | int numColorSamples = this->numSamples(); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 95 | 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 Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 108 | return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo}; |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 111 | GrBackendFormat backendFormat() const override { |
| 112 | return GrBackendFormat::MakeMock(fInfo.fConfig); |
| 113 | } |
| 114 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 115 | protected: |
| 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 | |
| 121 | private: |
| 122 | GrMockRenderTargetInfo fInfo; |
| 123 | |
| 124 | typedef GrRenderTarget INHERITED; |
| 125 | }; |
| 126 | |
| 127 | class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget { |
| 128 | public: |
| 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 Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 142 | const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble) |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 143 | : GrSurface(gpu, desc) |
| 144 | , GrMockTexture(gpu, desc, mipMapsStatus, texInfo) |
| 145 | , GrMockRenderTarget(gpu, desc, rtInfo) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 146 | this->registerWithCacheWrapped(cacheble); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 149 | 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 Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 154 | GrBackendFormat backendFormat() const override { |
| 155 | return GrMockTexture::backendFormat(); |
| 156 | } |
| 157 | |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 158 | protected: |
| 159 | // This avoids an inherits via dominance warning on MSVC. |
| 160 | void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); } |
| 161 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 162 | private: |
| 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 Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 174 | int numColorSamples = this->numSamples(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 175 | if (numColorSamples > 1) { |
| 176 | // Add one to account for the resolve buffer. |
| 177 | ++numColorSamples; |
| 178 | } |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 179 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Brian Salomon | 57b0430 | 2018-01-30 15:43:49 -0500 | [diff] [blame] | 180 | numColorSamples, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 181 | this->texturePriv().mipMapped()); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void computeScratchKey(GrScratchKey* key) const override { |
| 185 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 186 | true, this->numSamples(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 187 | this->texturePriv().mipMapped(), key); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
| 191 | #endif |