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
19class SK_API GrBackendTexture {
20public:
21    // Creates an invalid backend texture.
22    GrBackendTexture() : fConfig(kUnknown_GrPixelConfig) {}
23
24    // GrGLTextureInfo::fFormat is ignored
25    // Deprecated: Should use version that does not take a GrPixelConfig instead
26    GrBackendTexture(int width,
27                     int height,
28                     GrPixelConfig config,
29                     const GrGLTextureInfo& glInfo);
30
31    // GrGLTextureInfo::fFormat is ignored
32    // Deprecated: Should use version that does not take a GrPixelConfig instead
33    GrBackendTexture(int width,
34                     int height,
35                     GrPixelConfig config,
36                     GrMipMapped,
37                     const GrGLTextureInfo& glInfo);
38
39    // The GrGLTextureInfo must have a valid fFormat.
40    GrBackendTexture(int width,
41                     int height,
42                     GrMipMapped,
43                     const GrGLTextureInfo& glInfo);
44
45#ifdef SK_VULKAN
46    GrBackendTexture(int width,
47                     int height,
48                     const GrVkImageInfo& vkInfo);
49#endif
50
51    GrBackendTexture(int width,
52                     int height,
53                     GrPixelConfig config,
54                     const GrMockTextureInfo& mockInfo);
55
56    GrBackendTexture(int width,
57                     int height,
58                     GrPixelConfig config,
59                     GrMipMapped,
60                     const GrMockTextureInfo& mockInfo);
61
62    int width() const { return fWidth; }
63    int height() const { return fHeight; }
64    bool hasMipMaps() const { return GrMipMapped::kYes == fMipMapped; }
65    GrBackend backend() const {return fBackend; }
66
67    // If the backend API is GL, this returns a pointer to the GrGLTextureInfo struct. Otherwise
68    // it returns nullptr.
69    const GrGLTextureInfo* getGLTextureInfo() const;
70
71#ifdef SK_VULKAN
72    // If the backend API is Vulkan, this returns a pointer to the GrVkImageInfo struct. Otherwise
73    // it returns nullptr.
74    const GrVkImageInfo* getVkImageInfo() const;
75#endif
76
77    // If the backend API is Mock, this returns a pointer to the GrMockTextureInfo struct. Otherwise
78    // it returns nullptr.
79    const GrMockTextureInfo* getMockTextureInfo() const;
80
81    // Returns true if the backend texture has been initialized.
82    bool isValid() const { return fConfig != kUnknown_GrPixelConfig; }
83
84private:
85    // Friending for access to the GrPixelConfig
86    friend class SkImage;
87    friend class SkSurface;
88    friend class GrBackendTextureImageGenerator;
89    friend class GrProxyProvider;
90    friend class GrGpu;
91    friend class GrGLGpu;
92    friend class GrVkGpu;
93    GrPixelConfig config() const { return fConfig; }
94
95    int fWidth;         //<! width in pixels
96    int fHeight;        //<! height in pixels
97    GrPixelConfig fConfig;
98    GrMipMapped fMipMapped;
99    GrBackend fBackend;
100
101    union {
102        GrGLTextureInfo fGLInfo;
103#ifdef SK_VULKAN
104        GrVkImageInfo   fVkInfo;
105#endif
106        GrMockTextureInfo fMockInfo;
107    };
108};
109
110class SK_API GrBackendRenderTarget {
111public:
112    // Creates an invalid backend texture.
113    GrBackendRenderTarget() : fConfig(kUnknown_GrPixelConfig) {}
114
115    // GrGLTextureInfo::fFormat is ignored
116    // Deprecated: Should use version that does not take a GrPixelConfig instead
117    GrBackendRenderTarget(int width,
118                          int height,
119                          int sampleCnt,
120                          int stencilBits,
121                          GrPixelConfig config,
122                          const GrGLFramebufferInfo& glInfo);
123
124    // The GrGLTextureInfo must have a valid fFormat.
125    GrBackendRenderTarget(int width,
126                          int height,
127                          int sampleCnt,
128                          int stencilBits,
129                          const GrGLFramebufferInfo& glInfo);
130
131#ifdef SK_VULKAN
132    GrBackendRenderTarget(int width,
133                          int height,
134                          int sampleCnt,
135                          int stencilBits,
136                          const GrVkImageInfo& vkInfo);
137#endif
138
139    int width() const { return fWidth; }
140    int height() const { return fHeight; }
141    int sampleCnt() const { return fSampleCnt; }
142    int stencilBits() const { return fStencilBits; }
143    GrBackend backend() const {return fBackend; }
144
145    // If the backend API is GL, this returns a pointer to the GrGLFramebufferInfo struct. Otherwise
146    // it returns nullptr.
147    const GrGLFramebufferInfo* getGLFramebufferInfo() const;
148
149#ifdef SK_VULKAN
150    // If the backend API is Vulkan, this returns a pointer to the GrVkImageInfo struct. Otherwise
151    // it returns nullptr
152    const GrVkImageInfo* getVkImageInfo() const;
153#endif
154
155    // Returns true if the backend texture has been initialized.
156    bool isValid() const { return fConfig != kUnknown_GrPixelConfig; }
157
158private:
159    // Friending for access to the GrPixelConfig
160    friend class SkSurface;
161    friend class SkSurface_Gpu;
162    friend class SkImage_Gpu;
163    friend class GrGpu;
164    friend class GrGLGpu;
165    friend class GrVkGpu;
166    GrPixelConfig config() const { return fConfig; }
167
168    int fWidth;         //<! width in pixels
169    int fHeight;        //<! height in pixels
170
171    int fSampleCnt;
172    int fStencilBits;
173    GrPixelConfig fConfig;
174
175    GrBackend fBackend;
176
177    union {
178        GrGLFramebufferInfo fGLInfo;
179#ifdef SK_VULKAN
180        GrVkImageInfo   fVkInfo;
181#endif
182    };
183};
184
185#endif
186
187