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