OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrContext.h" | 9 #include "GrContext.h" |
10 #include "SkSurface.h" | 10 #include "SkSurface.h" |
11 #include "VulkanWindowContext.h" | 11 #include "VulkanWindowContext.h" |
12 | 12 |
13 #include "vk/GrVkInterface.h" | 13 #include "vk/GrVkInterface.h" |
14 #include "vk/GrVkUtil.h" | 14 #include "vk/GrVkUtil.h" |
15 #include "vk/GrVkTypes.h" | 15 #include "vk/GrVkTypes.h" |
16 | 16 |
17 #ifdef VK_USE_PLATFORM_WIN32_KHR | 17 #ifdef VK_USE_PLATFORM_WIN32_KHR |
18 // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW | 18 // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
19 #undef CreateSemaphore | 19 #undef CreateSemaphore |
20 #endif | 20 #endif |
21 | 21 |
22 #define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk"
#F) | 22 #define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk"
#F) |
23 #define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk"
#F) | 23 #define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk"
#F) |
24 | 24 |
25 namespace sk_app { | 25 namespace sk_app { |
26 | 26 |
27 VulkanWindowContext::VulkanWindowContext(void* platformData, int msaaSampleCount
) | 27 VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams
& params) |
28 : fSurface(VK_NULL_HANDLE) | 28 : fSurface(VK_NULL_HANDLE) |
29 , fSwapchain(VK_NULL_HANDLE) | 29 , fSwapchain(VK_NULL_HANDLE) |
30 , fCommandPool(VK_NULL_HANDLE) | 30 , fCommandPool(VK_NULL_HANDLE) |
31 , fBackbuffers(nullptr) { | 31 , fBackbuffers(nullptr) { |
32 | 32 |
33 // any config code here (particularly for msaa)? | 33 // any config code here (particularly for msaa)? |
34 | 34 |
35 this->initializeContext(platformData); | 35 this->initializeContext(platformData, params); |
36 } | 36 } |
37 | 37 |
38 void VulkanWindowContext::initializeContext(void* platformData) { | 38 void VulkanWindowContext::initializeContext(void* platformData, const DisplayPar
ams& params) { |
39 | 39 |
40 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPre
sent)); | 40 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPre
sent)); |
41 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) || | 41 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) || |
42 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) { | 42 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) { |
43 fBackendContext.reset(nullptr); | 43 fBackendContext.reset(nullptr); |
44 return; | 44 return; |
45 } | 45 } |
46 | 46 |
47 VkInstance instance = fBackendContext->fInstance; | 47 VkInstance instance = fBackendContext->fInstance; |
48 VkDevice device = fBackendContext->fDevice; | 48 VkDevice device = fBackendContext->fDevice; |
(...skipping 18 matching lines...) Expand all Loading... |
67 | 67 |
68 VkBool32 supported; | 68 VkBool32 supported; |
69 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysica
lDevice, | 69 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysica
lDevice, |
70 fPresentQueueIndex, fSurf
ace, | 70 fPresentQueueIndex, fSurf
ace, |
71 &supported); | 71 &supported); |
72 if (VK_SUCCESS != res) { | 72 if (VK_SUCCESS != res) { |
73 this->destroyContext(); | 73 this->destroyContext(); |
74 return; | 74 return; |
75 } | 75 } |
76 | 76 |
77 if (!this->createSwapchain(-1, -1)) { | 77 if (!this->createSwapchain(-1, -1, params)) { |
78 this->destroyContext(); | 78 this->destroyContext(); |
79 return; | 79 return; |
80 } | 80 } |
81 | 81 |
82 // create presentQueue | 82 // create presentQueue |
83 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQ
ueue); | 83 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQ
ueue); |
84 } | 84 } |
85 | 85 |
86 bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height) { | 86 bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height, |
| 87 const DisplayParams& params) { |
87 // check for capabilities | 88 // check for capabilities |
88 VkSurfaceCapabilitiesKHR caps; | 89 VkSurfaceCapabilitiesKHR caps; |
89 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPh
ysicalDevice, | 90 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPh
ysicalDevice, |
90 fSurface, &caps); | 91 fSurface, &caps); |
91 if (VK_SUCCESS != res) { | 92 if (VK_SUCCESS != res) { |
92 return false; | 93 return false; |
93 } | 94 } |
94 | 95 |
95 uint32_t surfaceFormatCount; | 96 uint32_t surfaceFormatCount; |
96 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice,
fSurface, | 97 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice,
fSurface, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 VK_IMAGE_USAGE_TRANSFER_DST_BIT; | 156 VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
156 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); | 157 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); |
157 SkASSERT(caps.supportedTransforms & caps.currentTransform); | 158 SkASSERT(caps.supportedTransforms & caps.currentTransform); |
158 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | | 159 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | |
159 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)
); | 160 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)
); |
160 VkCompositeAlphaFlagBitsKHR composite_alpha = | 161 VkCompositeAlphaFlagBitsKHR composite_alpha = |
161 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ? | 162 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ? |
162 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR : | 163 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR : |
163 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; | 164 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
164 | 165 |
165 // Pick our surface format -- for now, the first one | 166 // Pick our surface format. For now, just make sure it matches our sRGB requ
est: |
166 VkFormat surfaceFormat = surfaceFormats[0].format; | 167 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED; |
167 VkColorSpaceKHR colorSpace = surfaceFormats[0].colorSpace; | 168 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 169 bool wantSRGB = kSRGB_SkColorProfileType == params.fProfileType; |
| 170 for (uint32_t i = 0; i < surfaceFormatCount; ++i) { |
| 171 GrPixelConfig config; |
| 172 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) && |
| 173 GrPixelConfigIsSRGB(config) == wantSRGB) { |
| 174 surfaceFormat = surfaceFormats[i].format; |
| 175 colorSpace = surfaceFormats[i].colorSpace; |
| 176 break; |
| 177 } |
| 178 } |
| 179 fDisplayParams = params; |
| 180 |
| 181 if (VK_FORMAT_UNDEFINED == surfaceFormat) { |
| 182 return false; |
| 183 } |
168 | 184 |
169 // If mailbox mode is available, use it, as it is the lowest-latency non- | 185 // If mailbox mode is available, use it, as it is the lowest-latency non- |
170 // tearing mode. If not, fall back to FIFO which is always available. | 186 // tearing mode. If not, fall back to FIFO which is always available. |
171 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; | 187 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; |
172 for (uint32_t i = 0; i < presentModeCount; ++i) { | 188 for (uint32_t i = 0; i < presentModeCount; ++i) { |
173 // use mailbox | 189 // use mailbox |
174 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { | 190 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { |
175 mode = presentModes[i]; | 191 mode = presentModes[i]; |
176 break; | 192 break; |
177 } | 193 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; | 261 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
246 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | 262 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
247 info.fFormat = format; | 263 info.fFormat = format; |
248 desc.fWidth = fWidth; | 264 desc.fWidth = fWidth; |
249 desc.fHeight = fHeight; | 265 desc.fHeight = fHeight; |
250 desc.fConfig = fPixelConfig; | 266 desc.fConfig = fPixelConfig; |
251 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | 267 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
252 desc.fSampleCnt = 0; | 268 desc.fSampleCnt = 0; |
253 desc.fStencilBits = 0; | 269 desc.fStencilBits = 0; |
254 desc.fRenderTargetHandle = (GrBackendObject) &info; | 270 desc.fRenderTargetHandle = (GrBackendObject) &info; |
255 SkSurfaceProps props(0, kUnknown_SkPixelGeometry); | 271 SkSurfaceProps props(GrPixelConfigIsSRGB(fPixelConfig) |
| 272 ? SkSurfaceProps::kGammaCorrect_Flag : 0, |
| 273 kUnknown_SkPixelGeometry); |
256 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &p
rops); | 274 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &p
rops); |
257 } | 275 } |
258 | 276 |
259 // create the command pool for the command buffers | 277 // create the command pool for the command buffers |
260 if (VK_NULL_HANDLE == fCommandPool) { | 278 if (VK_NULL_HANDLE == fCommandPool) { |
261 VkCommandPoolCreateInfo commandPoolInfo; | 279 VkCommandPoolCreateInfo commandPoolInfo; |
262 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); | 280 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
263 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; | 281 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
264 // this needs to be on the render queue | 282 // this needs to be on the render queue |
265 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex; | 283 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UI
NT64_MAX, | 431 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UI
NT64_MAX, |
414 backbuffer->fAcquireSemaphore, VK_NULL_H
ANDLE, | 432 backbuffer->fAcquireSemaphore, VK_NULL_H
ANDLE, |
415 &backbuffer->fImageIndex); | 433 &backbuffer->fImageIndex); |
416 if (VK_ERROR_SURFACE_LOST_KHR == res) { | 434 if (VK_ERROR_SURFACE_LOST_KHR == res) { |
417 // need to figure out how to create a new vkSurface without the platform
Data* | 435 // need to figure out how to create a new vkSurface without the platform
Data* |
418 // maybe use attach somehow? but need a Window | 436 // maybe use attach somehow? but need a Window |
419 return nullptr; | 437 return nullptr; |
420 } | 438 } |
421 if (VK_ERROR_OUT_OF_DATE_KHR == res) { | 439 if (VK_ERROR_OUT_OF_DATE_KHR == res) { |
422 // tear swapchain down and try again | 440 // tear swapchain down and try again |
423 if (!this->createSwapchain(0, 0)) { | 441 if (!this->createSwapchain(0, 0, fDisplayParams)) { |
424 return nullptr; | 442 return nullptr; |
425 } | 443 } |
426 | 444 |
427 // acquire the image | 445 // acquire the image |
428 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_
MAX, | 446 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_
MAX, |
429 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE
, | 447 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE
, |
430 &backbuffer->fImageIndex); | 448 &backbuffer->fImageIndex); |
431 | 449 |
432 if (VK_SUCCESS != res) { | 450 if (VK_SUCCESS != res) { |
433 return nullptr; | 451 return nullptr; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 &fSwapchain, // pSwapchains | 580 &fSwapchain, // pSwapchains |
563 &backbuffer->fImageIndex, // pImageIndices | 581 &backbuffer->fImageIndex, // pImageIndices |
564 NULL // pResults | 582 NULL // pResults |
565 }; | 583 }; |
566 | 584 |
567 fQueuePresentKHR(fPresentQueue, &presentInfo); | 585 fQueuePresentKHR(fPresentQueue, &presentInfo); |
568 | 586 |
569 } | 587 } |
570 | 588 |
571 } //namespace sk_app | 589 } //namespace sk_app |
OLD | NEW |