1 /* 2 * Copyright 2023 Google LLC 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 8 #ifndef GrBackendSurfacePriv_DEFINED 9 #define GrBackendSurfacePriv_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/gpu/ganesh/GrBackendSurface.h" 13 #include "include/gpu/MutableTextureState.h" // IWYU pragma: keep 14 #include "include/private/base/SkAssert.h" 15 #include "include/private/base/SkDebug.h" 16 #include "include/private/gpu/ganesh/GrTypesPriv.h" 17 18 #include <cstddef> 19 #include <cstdint> 20 #include <string> 21 #include <string_view> 22 23 enum class GrBackendApi : unsigned int; 24 enum class SkTextureCompressionType; 25 26 namespace skgpu { 27 enum class Mipmapped : bool; 28 } 29 30 class GrBackendFormatData { 31 public: 32 virtual ~GrBackendFormatData(); 33 virtual SkTextureCompressionType compressionType() const = 0; 34 virtual size_t bytesPerBlock() const = 0; 35 virtual int stencilBits() const = 0; 36 virtual bool equal(const GrBackendFormatData* that) const = 0; 37 #if defined(SK_DEBUG) 38 virtual GrBackendApi type() const = 0; 39 #endif 40 protected: 41 GrBackendFormatData() = default; 42 GrBackendFormatData(const GrBackendFormatData&) = default; 43 44 using AnyFormatData = GrBackendFormat::AnyFormatData; 45 46 private: 47 friend class GrBackendFormat; 48 virtual uint32_t channelMask() const = 0; 49 virtual GrColorFormatDesc desc() const = 0; 50 virtual std::string toString() const = 0; 51 virtual void copyTo(AnyFormatData&) const = 0; 52 53 // Vulkan-only API: makeTexture2D()54 virtual void makeTexture2D() {} 55 }; 56 57 class GrBackendTextureData { 58 public: 59 virtual ~GrBackendTextureData(); 60 #if defined(SK_DEBUG) 61 virtual GrBackendApi type() const = 0; 62 #endif 63 protected: 64 GrBackendTextureData() = default; 65 GrBackendTextureData(const GrBackendTextureData&) = default; 66 67 using AnyTextureData = GrBackendTexture::AnyTextureData; 68 69 private: 70 friend class GrBackendTexture; 71 virtual bool isProtected() const = 0; 72 virtual bool equal(const GrBackendTextureData* that) const = 0; 73 virtual bool isSameTexture(const GrBackendTextureData*) const = 0; 74 virtual GrBackendFormat getBackendFormat() const = 0; 75 virtual void copyTo(AnyTextureData&) const = 0; 76 77 // Vulkan-only API: getMutableState()78 virtual sk_sp<skgpu::MutableTextureState> getMutableState() const { return nullptr; } setMutableState(const skgpu::MutableTextureState &)79 virtual void setMutableState(const skgpu::MutableTextureState&) {} 80 }; 81 82 class GrBackendRenderTargetData { 83 public: 84 virtual ~GrBackendRenderTargetData(); 85 #if defined(SK_DEBUG) 86 virtual GrBackendApi type() const = 0; 87 #endif 88 protected: 89 GrBackendRenderTargetData() = default; 90 GrBackendRenderTargetData(const GrBackendRenderTargetData&) = default; 91 92 using AnyRenderTargetData = GrBackendRenderTarget::AnyRenderTargetData; 93 94 private: 95 friend class GrBackendRenderTarget; 96 virtual GrBackendFormat getBackendFormat() const = 0; 97 virtual bool isProtected() const = 0; 98 virtual bool equal(const GrBackendRenderTargetData* that) const = 0; 99 virtual void copyTo(AnyRenderTargetData&) const = 0; 100 101 // Vulkan-only API: getMutableState()102 virtual sk_sp<skgpu::MutableTextureState> getMutableState() const { return nullptr; } setMutableState(const skgpu::MutableTextureState &)103 virtual void setMutableState(const skgpu::MutableTextureState&) {} 104 }; 105 106 class GrBackendSurfacePriv final { 107 public: 108 template <typename FormatData> MakeGrBackendFormat(GrTextureType textureType,GrBackendApi api,const FormatData & data)109 static GrBackendFormat MakeGrBackendFormat(GrTextureType textureType, 110 GrBackendApi api, 111 const FormatData& data) { 112 return GrBackendFormat(textureType, api, data); 113 } 114 GetBackendData(const GrBackendFormat & format)115 static const GrBackendFormatData* GetBackendData(const GrBackendFormat& format) { 116 return format.fFormatData.get(); 117 } 118 119 template <typename TextureData> MakeGrBackendTexture(int width,int height,std::string_view label,skgpu::Mipmapped mipped,GrBackendApi backend,GrTextureType texture,const TextureData & data)120 static GrBackendTexture MakeGrBackendTexture(int width, 121 int height, 122 std::string_view label, 123 skgpu::Mipmapped mipped, 124 GrBackendApi backend, 125 GrTextureType texture, 126 const TextureData& data) { 127 return GrBackendTexture(width, height, label, mipped, backend, texture, data); 128 } 129 GetBackendData(const GrBackendTexture & tex)130 static const GrBackendTextureData* GetBackendData(const GrBackendTexture& tex) { 131 return tex.fTextureData.get(); 132 } 133 GetBackendData(GrBackendTexture * tex)134 static GrBackendTextureData* GetBackendData(GrBackendTexture* tex) { 135 SkASSERT(tex); 136 return tex->fTextureData.get(); 137 } 138 139 template <typename RenderTargetData> MakeGrBackendRenderTarget(int width,int height,int sampleCnt,int stencilBits,GrBackendApi backend,bool framebufferOnly,const RenderTargetData & data)140 static GrBackendRenderTarget MakeGrBackendRenderTarget(int width, 141 int height, 142 int sampleCnt, 143 int stencilBits, 144 GrBackendApi backend, 145 bool framebufferOnly, 146 const RenderTargetData& data) { 147 return GrBackendRenderTarget( 148 width, height, sampleCnt, stencilBits, backend, framebufferOnly, data); 149 } 150 GetBackendData(const GrBackendRenderTarget & rt)151 static const GrBackendRenderTargetData* GetBackendData(const GrBackendRenderTarget& rt) { 152 return rt.fRTData.get(); 153 } 154 GetBackendData(GrBackendRenderTarget * rt)155 static GrBackendRenderTargetData* GetBackendData(GrBackendRenderTarget* rt) { 156 return rt->fRTData.get(); 157 } 158 }; 159 160 #endif 161