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 "include/core/SkSize.h" 11 #include "include/gpu/GpuTypes.h" 12 #include "include/gpu/ganesh/GrBackendSurface.h" 13 #include "include/gpu/ganesh/SkImageGanesh.h" 14 #include "include/gpu/ganesh/mock/GrMockTypes.h" 15 #include "include/private/base/SkAssert.h" 16 #include "include/private/gpu/ganesh/GrTypesPriv.h" 17 #include "src/gpu/ganesh/GrAttachment.h" 18 #include "src/gpu/ganesh/GrBackendUtils.h" 19 #include "src/gpu/ganesh/GrRenderTarget.h" 20 #include "src/gpu/ganesh/GrSurface.h" 21 #include "src/gpu/ganesh/GrTexture.h" 22 #include "src/gpu/ganesh/mock/GrMockGpu.h" 23 24 #include <cstddef> 25 #include <string_view> 26 27 namespace skgpu { class ScratchKey; } 28 29 class GrMockTexture : public GrTexture { 30 public: GrMockTexture(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,std::string_view label)31 GrMockTexture(GrMockGpu* gpu, 32 skgpu::Budgeted budgeted, 33 SkISize dimensions, 34 GrMipmapStatus mipmapStatus, 35 const GrMockTextureInfo& info, 36 std::string_view label) 37 : GrMockTexture(gpu, dimensions, mipmapStatus, info, label) { 38 this->registerWithCache(budgeted); 39 } 40 GrMockTexture(GrMockGpu * gpu,SkISize dimensions,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,GrWrapCacheable cacheable,GrIOType ioType,std::string_view label)41 GrMockTexture(GrMockGpu* gpu, 42 SkISize dimensions, 43 GrMipmapStatus mipmapStatus, 44 const GrMockTextureInfo& info, 45 GrWrapCacheable cacheable, 46 GrIOType ioType, 47 std::string_view label) 48 : GrMockTexture(gpu, dimensions, mipmapStatus, info, label) { 49 if (ioType == kRead_GrIOType) { 50 this->setReadOnly(); 51 } 52 this->registerWithCacheWrapped(cacheable); 53 } 54 ~GrMockTexture()55 ~GrMockTexture() override {} 56 getBackendTexture()57 GrBackendTexture getBackendTexture() const override { 58 return GrBackendTexture(this->width(), this->height(), this->mipmapped(), fInfo); 59 } 60 backendFormat()61 GrBackendFormat backendFormat() const override { 62 return fInfo.getBackendFormat(); 63 } 64 textureParamsModified()65 void textureParamsModified() override {} 66 67 protected: 68 // constructor for subclasses GrMockTexture(GrMockGpu * gpu,const SkISize & dims,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & info,std::string_view label)69 GrMockTexture(GrMockGpu* gpu, 70 const SkISize& dims, 71 GrMipmapStatus mipmapStatus, 72 const GrMockTextureInfo& info, 73 std::string_view label) 74 : GrSurface(gpu, dims, info.getProtected(), label) 75 , GrTexture(gpu, dims, info.getProtected(), GrTextureType::k2D, mipmapStatus, label) 76 , fInfo(info) {} 77 onRelease()78 void onRelease() override { 79 INHERITED::onRelease(); 80 } 81 onAbandon()82 void onAbandon() override { 83 INHERITED::onAbandon(); 84 } 85 onStealBackendTexture(GrBackendTexture *,SkImages::BackendTextureReleaseProc *)86 bool onStealBackendTexture(GrBackendTexture*, SkImages::BackendTextureReleaseProc*) override { 87 return false; 88 } 89 90 private: onSetLabel()91 void onSetLabel() override{} 92 93 GrMockTextureInfo fInfo; 94 95 using INHERITED = GrTexture; 96 }; 97 98 class GrMockRenderTarget : public GrRenderTarget { 99 public: GrMockRenderTarget(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,int sampleCnt,const GrMockRenderTargetInfo & info,std::string_view label)100 GrMockRenderTarget(GrMockGpu* gpu, 101 skgpu::Budgeted budgeted, 102 SkISize dimensions, 103 int sampleCnt, 104 const GrMockRenderTargetInfo& info, 105 std::string_view label) 106 : GrSurface(gpu, dimensions, info.getProtected(), label) 107 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label) 108 , fInfo(info) { 109 this->registerWithCache(budgeted); 110 } 111 112 enum Wrapped { kWrapped }; GrMockRenderTarget(GrMockGpu * gpu,Wrapped,SkISize dimensions,int sampleCnt,const GrMockRenderTargetInfo & info,std::string_view label)113 GrMockRenderTarget(GrMockGpu* gpu, Wrapped, SkISize dimensions, int sampleCnt, 114 const GrMockRenderTargetInfo& info, 115 std::string_view label) 116 : GrSurface(gpu, dimensions, info.getProtected(), label) 117 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label) 118 , fInfo(info) { 119 this->registerWithCacheWrapped(GrWrapCacheable::kNo); 120 } 121 canAttemptStencilAttachment(bool useMSAASurface)122 bool canAttemptStencilAttachment(bool useMSAASurface) const override { 123 SkASSERT(useMSAASurface == (this->numSamples() > 1)); 124 return true; 125 } 126 completeStencilAttachment(GrAttachment *,bool useMSAASurface)127 bool completeStencilAttachment(GrAttachment*, bool useMSAASurface) override { 128 SkASSERT(useMSAASurface == (this->numSamples() > 1)); 129 return true; 130 } 131 onGpuMemorySize()132 size_t onGpuMemorySize() const override { 133 int numColorSamples = this->numSamples(); 134 if (numColorSamples > 1) { 135 // Add one to account for the resolve buffer. 136 ++numColorSamples; 137 } 138 return GrSurface::ComputeSize( 139 this->backendFormat(), this->dimensions(), numColorSamples, skgpu::Mipmapped::kNo); 140 } 141 getBackendRenderTarget()142 GrBackendRenderTarget getBackendRenderTarget() const override { 143 int numStencilBits = 0; 144 if (GrAttachment* stencil = this->getStencilAttachment()) { 145 numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat()); 146 } 147 return {this->width(), this->height(), this->numSamples(), numStencilBits, fInfo}; 148 } 149 backendFormat()150 GrBackendFormat backendFormat() const override { 151 return fInfo.getBackendFormat(); 152 } 153 154 protected: 155 // constructor for subclasses GrMockRenderTarget(GrMockGpu * gpu,SkISize dimensions,int sampleCnt,const GrMockRenderTargetInfo & info,std::string_view label)156 GrMockRenderTarget(GrMockGpu* gpu, 157 SkISize dimensions, 158 int sampleCnt, 159 const GrMockRenderTargetInfo& info, 160 std::string_view label) 161 : GrSurface(gpu, dimensions, info.getProtected(), label) 162 , GrRenderTarget(gpu, dimensions, sampleCnt, info.getProtected(), label) 163 , fInfo(info) {} 164 165 private: onSetLabel()166 void onSetLabel() override{} 167 168 GrMockRenderTargetInfo fInfo; 169 170 using INHERITED = GrRenderTarget; 171 }; 172 173 class GrMockTextureRenderTarget : public GrMockTexture, public GrMockRenderTarget { 174 public: 175 // Internally created. GrMockTextureRenderTarget(GrMockGpu * gpu,skgpu::Budgeted budgeted,SkISize dimensions,int sampleCnt,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo,std::string_view label)176 GrMockTextureRenderTarget(GrMockGpu* gpu, 177 skgpu::Budgeted budgeted, 178 SkISize dimensions, 179 int sampleCnt, 180 GrMipmapStatus mipmapStatus, 181 const GrMockTextureInfo& texInfo, 182 const GrMockRenderTargetInfo& rtInfo, 183 std::string_view label) 184 : GrSurface(gpu, dimensions, texInfo.getProtected(), label) 185 , GrMockTexture(gpu, dimensions, mipmapStatus, texInfo, label) 186 , GrMockRenderTarget(gpu, dimensions, sampleCnt, rtInfo, label) { 187 SkASSERT(texInfo.getProtected() == rtInfo.getProtected()); 188 this->registerWithCache(budgeted); 189 } 190 191 // Renderable wrapped backend texture. GrMockTextureRenderTarget(GrMockGpu * gpu,SkISize dimensions,int sampleCnt,GrMipmapStatus mipmapStatus,const GrMockTextureInfo & texInfo,const GrMockRenderTargetInfo & rtInfo,GrWrapCacheable cacheable,std::string_view label)192 GrMockTextureRenderTarget(GrMockGpu* gpu, 193 SkISize dimensions, 194 int sampleCnt, 195 GrMipmapStatus mipmapStatus, 196 const GrMockTextureInfo& texInfo, 197 const GrMockRenderTargetInfo& rtInfo, 198 GrWrapCacheable cacheable, 199 std::string_view label) 200 : GrSurface(gpu, dimensions, texInfo.getProtected(), label) 201 , GrMockTexture(gpu, dimensions, mipmapStatus, texInfo, label) 202 , GrMockRenderTarget(gpu, dimensions, sampleCnt, rtInfo, label) { 203 SkASSERT(texInfo.getProtected() == rtInfo.getProtected()); 204 this->registerWithCacheWrapped(cacheable); 205 } 206 asTexture()207 GrTexture* asTexture() override { return this; } asRenderTarget()208 GrRenderTarget* asRenderTarget() override { return this; } asTexture()209 const GrTexture* asTexture() const override { return this; } asRenderTarget()210 const GrRenderTarget* asRenderTarget() const override { return this; } 211 backendFormat()212 GrBackendFormat backendFormat() const override { 213 return GrMockTexture::backendFormat(); 214 } 215 216 private: onAbandon()217 void onAbandon() override { 218 GrRenderTarget::onAbandon(); 219 GrMockTexture::onAbandon(); 220 } 221 onRelease()222 void onRelease() override { 223 GrRenderTarget::onRelease(); 224 GrMockTexture::onRelease(); 225 } 226 onGpuMemorySize()227 size_t onGpuMemorySize() const override { 228 int numColorSamples = this->numSamples(); 229 if (numColorSamples > 1) { 230 // Add one to account for the resolve buffer. 231 ++numColorSamples; 232 } 233 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), 234 numColorSamples, this->mipmapped()); 235 } 236 onSetLabel()237 void onSetLabel() override{} 238 239 // This avoids an inherits via dominance warning on MSVC. computeScratchKey(skgpu::ScratchKey * key)240 void computeScratchKey(skgpu::ScratchKey* key) const override { 241 GrTexture::computeScratchKey(key); 242 } 243 }; 244 245 #endif 246