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