• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2016 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef GrVkTypes_DEFINED
10 #define GrVkTypes_DEFINED
11 
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/vk/GrVkVulkan.h"
14 
15 #ifndef VK_VERSION_1_1
16 #error Skia requires the use of Vulkan 1.1 headers
17 #endif
18 
19 #include <functional>
20 #include "include/gpu/GrTypes.h"
21 
22 typedef intptr_t GrVkBackendMemory;
23 class GrVkMemoryAllocator;
24 
25 /**
26  * Types for interacting with Vulkan resources created externally to Skia. GrBackendObjects for
27  * Vulkan textures are really const GrVkImageInfo*
28  */
29 struct GrVkAlloc {
30     GrVkMemoryAllocator* fAllocator = nullptr;
31 #ifdef SKIA_DFX_FOR_OHOS
32     VkDeviceSize      fBytes = 0;
33 #endif
34     // can be VK_NULL_HANDLE iff is an RT and is borrowed
35     VkDeviceMemory    fMemory = VK_NULL_HANDLE;
36     VkDeviceSize      fOffset = 0;
37     VkDeviceSize      fSize = 0;  // this can be indeterminate iff Tex uses borrow semantics
38     uint32_t          fFlags = 0;
39     GrVkBackendMemory fBackendMemory = 0; // handle to memory allocated via GrVkMemoryAllocator.
40 
41     bool              fIsExternalMemory = false; // whether imported from external memory.
42     enum Flag {
43         kNoncoherent_Flag     = 0x1,   // memory must be flushed to device after mapping
44         kMappable_Flag        = 0x2,   // memory is able to be mapped.
45         kLazilyAllocated_Flag = 0x4,   // memory was created with lazy allocation
46     };
47 
48     bool operator==(const GrVkAlloc& that) const {
49         return fMemory == that.fMemory && fOffset == that.fOffset && fSize == that.fSize &&
50                fAllocator == that.fAllocator &&
51                fFlags == that.fFlags && fUsesSystemHeap == that.fUsesSystemHeap;
52     }
53 
54 private:
55     friend class GrVkHeap; // For access to usesSystemHeap
56     bool fUsesSystemHeap = false;
57 };
58 
59 // This struct is used to pass in the necessary information to create a VkSamplerYcbcrConversion
60 // object for an VkExternalFormatANDROID.
61 struct GrVkYcbcrConversionInfo {
62     bool operator==(const GrVkYcbcrConversionInfo& that) const {
63         // Invalid objects are not required to have all other fields initialized or matching.
64         if (!this->isValid() && !that.isValid()) {
65             return true;
66         }
67         return this->fFormat == that.fFormat &&
68                this->fExternalFormat == that.fExternalFormat &&
69                this->fYcbcrModel == that.fYcbcrModel &&
70                this->fYcbcrRange == that.fYcbcrRange &&
71                this->fXChromaOffset == that.fXChromaOffset &&
72                this->fYChromaOffset == that.fYChromaOffset &&
73                this->fChromaFilter == that.fChromaFilter &&
74                this->fForceExplicitReconstruction == that.fForceExplicitReconstruction;
75     }
76     bool operator!=(const GrVkYcbcrConversionInfo& that) const { return !(*this == that); }
77 
isValidGrVkYcbcrConversionInfo78     bool isValid() const { return fYcbcrModel != VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; }
79 
80     // Format of the source image. Must be set to VK_FORMAT_UNDEFINED for external images or
81     // a valid image format otherwise.
82     VkFormat fFormat = VK_FORMAT_UNDEFINED;
83 
84     // The external format. Must be non-zero for external images, zero otherwise.
85     // Should be compatible to be used in a VkExternalFormatANDROID struct.
86     uint64_t fExternalFormat = 0;
87 
88     VkSamplerYcbcrModelConversion fYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
89     VkSamplerYcbcrRange fYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
90     VkChromaLocation fXChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
91     VkChromaLocation fYChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
92     VkFilter fChromaFilter = VK_FILTER_NEAREST;
93     VkBool32 fForceExplicitReconstruction = false;
94 
95     // For external images format features here should be those returned by a call to
96     // vkAndroidHardwareBufferFormatPropertiesANDROID
97     VkFormatFeatureFlags fFormatFeatures = 0;
98 };
99 
100 /*
101  * When wrapping a GrBackendTexture or GrBackendRendenderTarget, the fCurrentQueueFamily should
102  * either be VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_EXTERNAL, or VK_QUEUE_FAMILY_FOREIGN_EXT. If
103  * fSharingMode is VK_SHARING_MODE_EXCLUSIVE then fCurrentQueueFamily can also be the graphics
104  * queue index passed into Skia.
105  */
106 struct GrVkImageInfo {
107     VkImage                  fImage = VK_NULL_HANDLE;
108     GrVkAlloc                fAlloc;
109     VkImageTiling            fImageTiling = VK_IMAGE_TILING_OPTIMAL;
110     VkImageLayout            fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
111     VkFormat                 fFormat = VK_FORMAT_UNDEFINED;
112     VkImageUsageFlags        fImageUsageFlags = 0;
113     uint32_t                 fSampleCount = 1;
114     uint32_t                 fLevelCount = 0;
115     uint32_t                 fCurrentQueueFamily = VK_QUEUE_FAMILY_IGNORED;
116     GrProtected              fProtected = GrProtected::kNo;
117     GrVkYcbcrConversionInfo  fYcbcrConversionInfo;
118     VkSharingMode            fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
119 
120 #if GR_TEST_UTILS
121     bool operator==(const GrVkImageInfo& that) const {
122         return fImage == that.fImage && fAlloc == that.fAlloc &&
123                fImageTiling == that.fImageTiling && fImageLayout == that.fImageLayout &&
124                fFormat == that.fFormat && fImageUsageFlags == that.fImageUsageFlags &&
125                fSampleCount == that.fSampleCount && fLevelCount == that.fLevelCount &&
126                fCurrentQueueFamily == that.fCurrentQueueFamily && fProtected == that.fProtected &&
127                fYcbcrConversionInfo == that.fYcbcrConversionInfo &&
128                fSharingMode == that.fSharingMode;
129     }
130 #endif
131 };
132 
133 using GrVkGetProc = std::function<PFN_vkVoidFunction(
134         const char*, // function name
135         VkInstance,  // instance or VK_NULL_HANDLE
136         VkDevice     // device or VK_NULL_HANDLE
137         )>;
138 
139 /**
140  * This object is wrapped in a GrBackendDrawableInfo and passed in as an argument to
141  * drawBackendGpu() calls on an SkDrawable. The drawable will use this info to inject direct
142  * Vulkan calls into our stream of GPU draws.
143  *
144  * The SkDrawable is given a secondary VkCommandBuffer in which to record draws. The GPU backend
145  * will then execute that command buffer within a render pass it is using for its own draws. The
146  * drawable is also given the attachment of the color index, a compatible VkRenderPass, and the
147  * VkFormat of the color attachment so that it can make VkPipeline objects for the draws. The
148  * SkDrawable must not alter the state of the VkRenderpass or sub pass.
149  *
150  * Additionally, the SkDrawable may fill in the passed in fDrawBounds with the bounds of the draws
151  * that it submits to the command buffer. This will be used by the GPU backend for setting the
152  * bounds in vkCmdBeginRenderPass. If fDrawBounds is not updated, we will assume that the entire
153  * attachment may have been written to.
154  *
155  * The SkDrawable is always allowed to create its own command buffers and submit them to the queue
156  * to render offscreen textures which will be sampled in draws added to the passed in
157  * VkCommandBuffer. If this is done the SkDrawable is in charge of adding the required memory
158  * barriers to the queue for the sampled images since the Skia backend will not do this.
159  *
160  * The VkImage is informational only and should not be used or modified in any ways.
161  */
162 struct GrVkDrawableInfo {
163     VkCommandBuffer fSecondaryCommandBuffer;
164     uint32_t        fColorAttachmentIndex;
165     VkRenderPass    fCompatibleRenderPass;
166     VkFormat        fFormat;
167     VkRect2D*       fDrawBounds;
168     VkImage         fImage;
169 };
170 
171 struct GrVkSurfaceInfo {
172     uint32_t fSampleCount = 1;
173     uint32_t fLevelCount = 0;
174     GrProtected fProtected = GrProtected::kNo;
175 
176     VkImageTiling fImageTiling = VK_IMAGE_TILING_OPTIMAL;
177     VkFormat fFormat = VK_FORMAT_UNDEFINED;
178     VkImageUsageFlags fImageUsageFlags = 0;
179     GrVkYcbcrConversionInfo fYcbcrConversionInfo;
180     VkSharingMode fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
181 };
182 
183 #endif
184