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 | |
| 10 | #include "GrMockGpu.h" |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 11 | #include "GrRenderTarget.h" |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 12 | #include "GrRenderTargetPriv.h" |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 13 | #include "GrTexture.h" |
| 14 | #include "GrTexturePriv.h" |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 15 | #include "mock/GrMockTypes.h" |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 16 | |
| 17 | class GrMockTexture : public GrTexture { |
| 18 | public: |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 19 | GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 20 | GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info) |
| 21 | : GrMockTexture(gpu, desc, mipMapsStatus, info) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 22 | this->registerWithCache(budgeted); |
| 23 | } |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 24 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 25 | GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, |
| 26 | const GrMockTextureInfo& info, GrWrapCacheable cacheable, GrIOType ioType) |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 27 | : GrMockTexture(gpu, desc, mipMapsStatus, info) { |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 28 | if (ioType == kRead_GrIOType) { |
| 29 | this->setReadOnly(); |
| 30 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 31 | this->registerWithCacheWrapped(cacheable); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 34 | ~GrMockTexture() override {} |
| 35 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 36 | GrBackendTexture getBackendTexture() const override { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 37 | return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), |
| 38 | fInfo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 41 | GrBackendFormat backendFormat() const override { |
| 42 | return GrBackendFormat::MakeMock(fInfo.fConfig); |
| 43 | } |
| 44 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 45 | void textureParamsModified() override {} |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 46 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 47 | void setIdleProc(IdleProc proc, void* context) override { |
| 48 | fIdleProc = proc; |
| 49 | fIdleProcContext = context; |
| 50 | } |
Brian Salomon | 1bf0ed8 | 2019-01-16 13:51:35 -0500 | [diff] [blame] | 51 | void* idleContext() const override { return fIdleProcContext; } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 52 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 53 | protected: |
| 54 | // constructor for subclasses |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 55 | GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 56 | const GrMockTextureInfo& info) |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 57 | : GrSurface(gpu, desc) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 58 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 59 | , fInfo(info) {} |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 60 | |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 61 | void onRelease() override { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 62 | INHERITED::onRelease(); |
| 63 | } |
| 64 | |
| 65 | void onAbandon() override { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 66 | INHERITED::onAbandon(); |
| 67 | } |
| 68 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 69 | bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { |
| 70 | return false; |
| 71 | } |
| 72 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 73 | // protected so that GrMockTextureRenderTarget can call this to avoid "inheritance via |
| 74 | // dominance" warning. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 75 | void removedLastRefOrPendingIO() override { |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 76 | if (fIdleProc) { |
| 77 | fIdleProc(fIdleProcContext); |
| 78 | fIdleProc = nullptr; |
| 79 | fIdleProcContext = nullptr; |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 80 | } |
| 81 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 82 | |
| 83 | private: |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 84 | void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {} |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 85 | |
| 86 | GrMockTextureInfo fInfo; |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 87 | sk_sp<GrReleaseProcHelper> fReleaseHelper; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 88 | IdleProc* fIdleProc = nullptr; |
| 89 | void* fIdleProcContext = nullptr; |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 90 | |
| 91 | typedef GrTexture INHERITED; |
| 92 | }; |
| 93 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 94 | class GrMockRenderTarget : public GrRenderTarget { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 95 | public: |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 96 | GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 97 | const GrMockRenderTargetInfo& info) |
| 98 | : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 99 | this->registerWithCache(budgeted); |
| 100 | } |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 101 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 102 | enum Wrapped { kWrapped }; |
| 103 | GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, |
| 104 | const GrMockRenderTargetInfo& info) |
| 105 | : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 106 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 109 | ResolveType getResolveType() const override { return kCanResolve_ResolveType; } |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 110 | bool canAttemptStencilAttachment() const override { return true; } |
| 111 | bool completeStencilAttachment() override { return true; } |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 112 | |
| 113 | size_t onGpuMemorySize() const override { |
| 114 | int numColorSamples = this->numColorSamples(); |
| 115 | if (numColorSamples > 1) { |
| 116 | // Add one to account for the resolve buffer. |
| 117 | ++numColorSamples; |
| 118 | } |
| 119 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
| 120 | numColorSamples, GrMipMapped::kNo); |
| 121 | } |
| 122 | |
| 123 | GrBackendRenderTarget getBackendRenderTarget() const override { |
| 124 | int numStencilBits = 0; |
| 125 | if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) { |
| 126 | numStencilBits = stencil->bits(); |
| 127 | } |
| 128 | return {this->width(), this->height(), this->numColorSamples(), numStencilBits, fInfo}; |
| 129 | } |
| 130 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 131 | GrBackendFormat backendFormat() const override { |
| 132 | return GrBackendFormat::MakeMock(fInfo.fConfig); |
| 133 | } |
| 134 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 135 | protected: |
| 136 | // constructor for subclasses |
| 137 | GrMockRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, |
| 138 | const GrMockRenderTargetInfo& info) |
| 139 | : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) {} |
| 140 | |
| 141 | private: |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 142 | void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {} |
| 143 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 144 | GrMockRenderTargetInfo fInfo; |
| 145 | |
| 146 | typedef GrRenderTarget INHERITED; |
| 147 | }; |
| 148 | |
| 149 | class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget { |
| 150 | public: |
| 151 | // Internally created. |
| 152 | GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, |
| 153 | GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo, |
| 154 | const GrMockRenderTargetInfo& rtInfo) |
| 155 | : GrSurface(gpu, desc) |
| 156 | , GrMockTexture(gpu, desc, mipMapsStatus, texInfo) |
| 157 | , GrMockRenderTarget(gpu, desc, rtInfo) { |
| 158 | this->registerWithCache(budgeted); |
| 159 | } |
| 160 | |
| 161 | // Renderable wrapped backend texture. |
| 162 | GrMockTextureRenderTarget(GrMockGpu* gpu, const GrSurfaceDesc& desc, |
| 163 | GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 164 | const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble) |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 165 | : GrSurface(gpu, desc) |
| 166 | , GrMockTexture(gpu, desc, mipMapsStatus, texInfo) |
| 167 | , GrMockRenderTarget(gpu, desc, rtInfo) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 168 | this->registerWithCacheWrapped(cacheble); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 171 | GrTexture* asTexture() override { return this; } |
| 172 | GrRenderTarget* asRenderTarget() override { return this; } |
| 173 | const GrTexture* asTexture() const override { return this; } |
| 174 | const GrRenderTarget* asRenderTarget() const override { return this; } |
| 175 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 176 | GrBackendFormat backendFormat() const override { |
| 177 | return GrMockTexture::backendFormat(); |
| 178 | } |
| 179 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 180 | private: |
Greg Daniel | 2d35a1c | 2019-02-01 14:48:10 -0500 | [diff] [blame] | 181 | void onSetRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override {} |
| 182 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 183 | void onAbandon() override { |
| 184 | GrRenderTarget::onAbandon(); |
| 185 | GrMockTexture::onAbandon(); |
| 186 | } |
| 187 | |
| 188 | void onRelease() override { |
| 189 | GrRenderTarget::onRelease(); |
| 190 | GrMockTexture::onRelease(); |
| 191 | } |
| 192 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 193 | // We implement this to avoid the inheritance via dominance warning. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 194 | void removedLastRefOrPendingIO() override { GrMockTexture::removedLastRefOrPendingIO(); } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 195 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 196 | size_t onGpuMemorySize() const override { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 197 | int numColorSamples = this->numColorSamples(); |
| 198 | if (numColorSamples > 1) { |
| 199 | // Add one to account for the resolve buffer. |
| 200 | ++numColorSamples; |
| 201 | } |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 202 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Brian Salomon | 57b0430 | 2018-01-30 15:43:49 -0500 | [diff] [blame] | 203 | numColorSamples, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 204 | this->texturePriv().mipMapped()); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void computeScratchKey(GrScratchKey* key) const override { |
| 208 | GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 209 | true, this->numStencilSamples(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 210 | this->texturePriv().mipMapped(), key); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
| 214 | #endif |