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" 11 #include "GrRenderTarget.h" 12 #include "GrRenderTargetPriv.h" 13 #include "GrTexture.h" 14 #include "GrTexturePriv.h" 15 #include "mock/GrMockTypes.h" 16 17 class GrMockTexture : public GrTexture { 18 public: 19 GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, 20 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info) 21 : GrMockTexture(gpu, desc, mipMapsStatus, info) { 22 this->registerWithCache(budgeted); 23 } 24 25 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, 26 const GrMockTextureInfo& info, GrWrapCacheable cacheable, GrIOType ioType) 27 : GrMockTexture(gpu, desc, mipMapsStatus, info) { 28 if (ioType == kRead_GrIOType) { 29 this->setReadOnly(); 30 } 31 this->registerWithCacheWrapped(cacheable); 32 } 33 34 ~GrMockTexture() override {} 35 36 GrBackendTexture getBackendTexture() const override { 37 return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), 38 fInfo); 39 } 40 41 GrBackendFormat backendFormat() const override { 42 return GrBackendFormat::MakeMock(fInfo.fConfig); 43 } 44 45 void textureParamsModified() override {} 46 47 protected: 48 // constructor for subclasses 49 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, 50 const GrMockTextureInfo& info) 51 : GrSurface(gpu, desc) 52 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) 53 , fInfo(info) {} 54 55 void onRelease() override { 56 INHERITED::onRelease(); 57 } 58 59 void onAbandon() override { 60 INHERITED::onAbandon(); 61 } 62 63 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { 64 return false; 65 } 66 67 private: 68 GrMockTextureInfo fInfo; 69 70 typedef GrTexture INHERITED; 71 }; 72 73 class GrMockRenderTarget : public GrRenderTarget { 74 public: 75 GrMockRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, 76 const GrMockRenderTargetInfo& info) 77 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) { 78 this->registerWithCache(budgeted); 79 } 80 81 enum Wrapped { kWrapped }; 82 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, const GrSurfaceDesc& desc, 83 const GrMockRenderTargetInfo& info) 84 : GrSurface(gpu, desc), INHERITED(gpu, desc), fInfo(info) { 85 this->registerWithCacheWrapped(GrWrapCacheable::kNo); 86 } 87 88 ResolveType getResolveType() const override { return kCanResolve_ResolveType; } 89 bool canAttemptStencilAttachment() const override { return true; } 90 bool completeStencilAttachment() override { return true; } 91 92 size_t onGpuMemorySize() const override { 93 int numColorSamples = this->numColorSamples(); 94 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 } 107 return {this->width(), this->height(), this->numColorSamples(), numStencilBits, fInfo}; 108 } 109 110 GrBackendFormat backendFormat() const override { 111 return GrBackendFormat::MakeMock(fInfo.fConfig); 112 } 113 114 protected: 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 120 private: 121 GrMockRenderTargetInfo fInfo; 122 123 typedef GrRenderTarget INHERITED; 124 }; 125 126 class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget { 127 public: 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, 141 const GrMockRenderTargetInfo& rtInfo, GrWrapCacheable cacheble) 142 : GrSurface(gpu, desc) 143 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo) 144 , GrMockRenderTarget(gpu, desc, rtInfo) { 145 this->registerWithCacheWrapped(cacheble); 146 } 147 148 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 153 GrBackendFormat backendFormat() const override { 154 return GrMockTexture::backendFormat(); 155 } 156 157 protected: 158 // This avoids an inherits via dominance warning on MSVC. 159 void willRemoveLastRefOrPendingIO() override { GrTexture::willRemoveLastRefOrPendingIO(); } 160 161 private: 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 { 173 int numColorSamples = this->numColorSamples(); 174 if (numColorSamples > 1) { 175 // Add one to account for the resolve buffer. 176 ++numColorSamples; 177 } 178 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 179 numColorSamples, 180 this->texturePriv().mipMapped()); 181 } 182 183 void computeScratchKey(GrScratchKey* key) const override { 184 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), 185 true, this->numStencilSamples(), 186 this->texturePriv().mipMapped(), key); 187 } 188 }; 189 190 #endif 191