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 "GrTexture.h" 13 #include "GrTexturePriv.h" 14 #include "mock/GrMockTypes.h" 15 16 class GrMockTexture : public GrTexture { 17 public: GrMockTexture(GrMockGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & info)18 GrMockTexture(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, 19 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& info) 20 : GrMockTexture(gpu, desc, mipMapsStatus, info) { 21 this->registerWithCache(budgeted); 22 } ~GrMockTexture()23 ~GrMockTexture() override {} 24 getTextureHandle()25 GrBackendObject getTextureHandle() const override { 26 return reinterpret_cast<GrBackendObject>(&fInfo); 27 } getBackendTexture()28 GrBackendTexture getBackendTexture() const override { 29 return GrBackendTexture(this->width(), this->height(), this->config(), 30 this->texturePriv().mipMapped(), fInfo); 31 } 32 textureParamsModified()33 void textureParamsModified() override {} setRelease(sk_sp<GrReleaseProcHelper> releaseHelper)34 void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) override { 35 fReleaseHelper = std::move(releaseHelper); 36 } 37 38 protected: 39 // constructor for subclasses GrMockTexture(GrMockGpu * gpu,const GrSurfaceDesc & desc,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & info)40 GrMockTexture(GrMockGpu* gpu, const GrSurfaceDesc& desc, GrMipMapsStatus mipMapsStatus, 41 const GrMockTextureInfo& info) 42 : GrSurface(gpu, desc) 43 , INHERITED(gpu, desc, kITexture2DSampler_GrSLType, GrSamplerState::Filter::kMipMap, 44 mipMapsStatus) 45 , fInfo(info) {} 46 onStealBackendTexture(GrBackendTexture *,SkImage::BackendTextureReleaseProc *)47 bool onStealBackendTexture(GrBackendTexture*, SkImage::BackendTextureReleaseProc*) override { 48 return false; 49 } 50 51 private: 52 GrMockTextureInfo fInfo; 53 sk_sp<GrReleaseProcHelper> fReleaseHelper; 54 55 typedef GrTexture INHERITED; 56 }; 57 58 class GrMockTextureRenderTarget : public GrMockTexture, public GrRenderTarget { 59 public: GrMockTextureRenderTarget(GrMockGpu * gpu,SkBudgeted budgeted,const GrSurfaceDesc & desc,GrMipMapsStatus mipMapsStatus,const GrMockTextureInfo & texInfo)60 GrMockTextureRenderTarget(GrMockGpu* gpu, SkBudgeted budgeted, const GrSurfaceDesc& desc, 61 GrMipMapsStatus mipMapsStatus, const GrMockTextureInfo& texInfo) 62 : GrSurface(gpu, desc) 63 , GrMockTexture(gpu, desc, mipMapsStatus, texInfo) 64 , GrRenderTarget(gpu, desc) { 65 this->registerWithCache(budgeted); 66 } getResolveType()67 ResolveType getResolveType() const override { return kCanResolve_ResolveType; } getRenderTargetHandle()68 GrBackendObject getRenderTargetHandle() const override { return 0; } 69 getBackendRenderTarget()70 GrBackendRenderTarget getBackendRenderTarget() const override { 71 return GrBackendRenderTarget(); // invalid 72 } 73 canAttemptStencilAttachment()74 bool canAttemptStencilAttachment() const override { return true; } completeStencilAttachment()75 bool completeStencilAttachment() override { return true; } asTexture()76 GrTexture* asTexture() override { return this; } asRenderTarget()77 GrRenderTarget* asRenderTarget() override { return this; } asTexture()78 const GrTexture* asTexture() const override { return this; } asRenderTarget()79 const GrRenderTarget* asRenderTarget() const override { return this; } 80 81 private: onAbandon()82 void onAbandon() override { 83 GrRenderTarget::onAbandon(); 84 GrMockTexture::onAbandon(); 85 } 86 onRelease()87 void onRelease() override { 88 GrRenderTarget::onRelease(); 89 GrMockTexture::onRelease(); 90 } 91 onGpuMemorySize()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, 100 this->texturePriv().mipMapped()); 101 } 102 computeScratchKey(GrScratchKey * key)103 void computeScratchKey(GrScratchKey* key) const override { 104 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), 105 true, this->numStencilSamples(), 106 this->texturePriv().mipMapped(), key); 107 } 108 }; 109 110 #endif 111