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 8 #ifndef GrBackendSurface_DEFINED 9 #define GrBackendSurface_DEFINED 10 11 #include "GrTypes.h" 12 #include "gl/GrGLTypes.h" 13 #include "mock/GrMockTypes.h" 14 15 #ifdef SK_VULKAN 16 #include "vk/GrVkTypes.h" 17 #endif 18 19 class SK_API GrBackendFormat { 20 public: 21 // Creates an invalid backend format. 22 GrBackendFormat() : fValid(false) {} 23 24 static GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) { 25 return GrBackendFormat(format, target); 26 } 27 28 #ifdef SK_VULKAN 29 static GrBackendFormat MakeVK(VkFormat format) { 30 return GrBackendFormat(format); 31 } 32 #endif 33 34 static GrBackendFormat MakeMock(GrPixelConfig config) { 35 return GrBackendFormat(config); 36 } 37 38 GrBackend backend() const {return fBackend; } 39 40 // If the backend API is GL, these return a pointer to the format and target. Otherwise 41 // it returns nullptr. 42 const GrGLenum* getGLFormat() const; 43 const GrGLenum* getGLTarget() const; 44 45 #ifdef SK_VULKAN 46 // If the backend API is Vulkan, this returns a pointer to a VkFormat. Otherwise 47 // it returns nullptr 48 const VkFormat* getVkFormat() const; 49 #endif 50 51 // If the backend API is Mock, this returns a pointer to a GrPixelConfig. Otherwise 52 // it returns nullptr. 53 const GrPixelConfig* getMockFormat() const; 54 55 // Returns true if the backend format has been initialized. 56 bool isValid() const { return fValid; } 57 58 private: 59 GrBackendFormat(GrGLenum format, GrGLenum target); 60 61 #ifdef SK_VULKAN 62 GrBackendFormat(const VkFormat vkFormat); 63 #endif 64 65 GrBackendFormat(const GrPixelConfig config); 66 67 GrBackend fBackend; 68 bool fValid; 69 70 union { 71 struct { 72 GrGLenum fTarget; // GL_TEXTURE_2D, GL_TEXTURE_EXTERNAL or GL_TEXTURE_RECTANGLE 73 GrGLenum fFormat; // the sized, internal format of the GL resource 74 } fGL; 75 #ifdef SK_VULKAN 76 VkFormat fVkFormat; 77 #endif 78 GrPixelConfig fMockFormat; 79 }; 80 }; 81 82 class SK_API GrBackendTexture { 83 public: 84 // Creates an invalid backend texture. 85 GrBackendTexture() : fConfig(kUnknown_GrPixelConfig) {} 86 87 // GrGLTextureInfo::fFormat is ignored 88 // Deprecated: Should use version that does not take a GrPixelConfig instead 89 GrBackendTexture(int width, 90 int height, 91 GrPixelConfig config, 92 const GrGLTextureInfo& glInfo); 93 94 // GrGLTextureInfo::fFormat is ignored 95 // Deprecated: Should use version that does not take a GrPixelConfig instead 96 GrBackendTexture(int width, 97 int height, 98 GrPixelConfig config, 99 GrMipMapped, 100 const GrGLTextureInfo& glInfo); 101 102 // The GrGLTextureInfo must have a valid fFormat. 103 GrBackendTexture(int width, 104 int height, 105 GrMipMapped, 106 const GrGLTextureInfo& glInfo); 107 108 #ifdef SK_VULKAN 109 GrBackendTexture(int width, 110 int height, 111 const GrVkImageInfo& vkInfo); 112 #endif 113 114 GrBackendTexture(int width, 115 int height, 116 GrPixelConfig config, 117 const GrMockTextureInfo& mockInfo); 118 119 GrBackendTexture(int width, 120 int height, 121 GrPixelConfig config, 122 GrMipMapped, 123 const GrMockTextureInfo& mockInfo); 124 125 int width() const { return fWidth; } 126 int height() const { return fHeight; } 127 bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; } 128 GrBackend backend() const {return fBackend; } 129 130 // If the backend API is GL, this returns a pointer to the GrGLTextureInfo struct. Otherwise 131 // it returns nullptr. 132 const GrGLTextureInfo* getGLTextureInfo() const; 133 134 #ifdef SK_VULKAN 135 // If the backend API is Vulkan, this returns a pointer to the GrVkImageInfo struct. Otherwise 136 // it returns nullptr. 137 const GrVkImageInfo* getVkImageInfo() const; 138 #endif 139 140 // If the backend API is Mock, this returns a pointer to the GrMockTextureInfo struct. Otherwise 141 // it returns nullptr. 142 const GrMockTextureInfo* getMockTextureInfo() const; 143 144 // Returns true if the backend texture has been initialized. 145 bool isValid() const { return fConfig != kUnknown_GrPixelConfig; } 146 147 GrPixelConfig testingOnly_getPixelConfig() const; 148 149 private: 150 // Friending for access to the GrPixelConfig 151 friend class SkImage; 152 friend class SkSurface; 153 friend class GrBackendTextureImageGenerator; 154 friend class GrProxyProvider; 155 friend class GrGpu; 156 friend class GrGLGpu; 157 friend class GrVkGpu; 158 GrPixelConfig config() const { return fConfig; } 159 160 int fWidth; //<! width in pixels 161 int fHeight; //<! height in pixels 162 GrPixelConfig fConfig; 163 GrMipMapped fMipMapped; 164 GrBackend fBackend; 165 166 union { 167 GrGLTextureInfo fGLInfo; 168 #ifdef SK_VULKAN 169 GrVkImageInfo fVkInfo; 170 #endif 171 GrMockTextureInfo fMockInfo; 172 }; 173 }; 174 175 class SK_API GrBackendRenderTarget { 176 public: 177 // Creates an invalid backend texture. 178 GrBackendRenderTarget() : fConfig(kUnknown_GrPixelConfig) {} 179 180 // GrGLTextureInfo::fFormat is ignored 181 // Deprecated: Should use version that does not take a GrPixelConfig instead 182 GrBackendRenderTarget(int width, 183 int height, 184 int sampleCnt, 185 int stencilBits, 186 GrPixelConfig config, 187 const GrGLFramebufferInfo& glInfo); 188 189 // The GrGLTextureInfo must have a valid fFormat. 190 GrBackendRenderTarget(int width, 191 int height, 192 int sampleCnt, 193 int stencilBits, 194 const GrGLFramebufferInfo& glInfo); 195 196 #ifdef SK_VULKAN 197 GrBackendRenderTarget(int width, 198 int height, 199 int sampleCnt, 200 int stencilBits, 201 const GrVkImageInfo& vkInfo); 202 #endif 203 204 int width() const { return fWidth; } 205 int height() const { return fHeight; } 206 int sampleCnt() const { return fSampleCnt; } 207 int stencilBits() const { return fStencilBits; } 208 GrBackend backend() const {return fBackend; } 209 210 // If the backend API is GL, this returns a pointer to the GrGLFramebufferInfo struct. Otherwise 211 // it returns nullptr. 212 const GrGLFramebufferInfo* getGLFramebufferInfo() const; 213 214 #ifdef SK_VULKAN 215 // If the backend API is Vulkan, this returns a pointer to the GrVkImageInfo struct. Otherwise 216 // it returns nullptr 217 const GrVkImageInfo* getVkImageInfo() const; 218 #endif 219 220 // Returns true if the backend texture has been initialized. 221 bool isValid() const { return fConfig != kUnknown_GrPixelConfig; } 222 223 GrPixelConfig testingOnly_getPixelConfig() const; 224 225 private: 226 // Friending for access to the GrPixelConfig 227 friend class SkSurface; 228 friend class SkSurface_Gpu; 229 friend class SkImage_Gpu; 230 friend class GrGpu; 231 friend class GrGLGpu; 232 friend class GrProxyProvider; 233 friend class GrVkGpu; 234 GrPixelConfig config() const { return fConfig; } 235 236 int fWidth; //<! width in pixels 237 int fHeight; //<! height in pixels 238 239 int fSampleCnt; 240 int fStencilBits; 241 GrPixelConfig fConfig; 242 243 GrBackend fBackend; 244 245 union { 246 GrGLFramebufferInfo fGLInfo; 247 #ifdef SK_VULKAN 248 GrVkImageInfo fVkInfo; 249 #endif 250 }; 251 }; 252 253 #endif 254 255