| 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 "GrRenderTarget.h" |
| 10 #include "SkSurface.h" | 11 #include "SkSurface.h" |
| 11 #include "VulkanWindowContext.h" | 12 #include "VulkanWindowContext.h" |
| 12 | 13 |
| 13 #include "vk/GrVkInterface.h" | 14 #include "vk/GrVkInterface.h" |
| 14 #include "vk/GrVkUtil.h" | 15 #include "vk/GrVkUtil.h" |
| 15 #include "vk/GrVkTypes.h" | 16 #include "vk/GrVkTypes.h" |
| 16 | 17 |
| 17 #ifdef VK_USE_PLATFORM_WIN32_KHR | 18 #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 18 // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW | 19 // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
| 19 #undef CreateSemaphore | 20 #undef CreateSemaphore |
| 20 #endif | 21 #endif |
| 21 | 22 |
| 22 #define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk"
#F) | 23 #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) | 24 #define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk"
#F) |
| 24 | 25 |
| 25 namespace sk_app { | 26 namespace sk_app { |
| 26 | 27 |
| 27 VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams
& params) | 28 VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams
& params) |
| 28 : fSurface(VK_NULL_HANDLE) | 29 : WindowContext() |
| 30 , fSurface(VK_NULL_HANDLE) |
| 29 , fSwapchain(VK_NULL_HANDLE) | 31 , fSwapchain(VK_NULL_HANDLE) |
| 32 , fImages(nullptr) |
| 33 , fImageLayouts(nullptr) |
| 34 , fSurfaces(nullptr) |
| 30 , fCommandPool(VK_NULL_HANDLE) | 35 , fCommandPool(VK_NULL_HANDLE) |
| 31 , fBackbuffers(nullptr) { | 36 , fBackbuffers(nullptr) { |
| 32 | 37 |
| 33 // any config code here (particularly for msaa)? | 38 // any config code here (particularly for msaa)? |
| 34 | 39 |
| 35 this->initializeContext(platformData, params); | 40 this->initializeContext(platformData, params); |
| 36 } | 41 } |
| 37 | 42 |
| 38 void VulkanWindowContext::initializeContext(void* platformData, const DisplayPar
ams& params) { | 43 void VulkanWindowContext::initializeContext(void* platformData, const DisplayPar
ams& params) { |
| 39 | 44 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 void VulkanWindowContext::createBuffers(VkFormat format) { | 248 void VulkanWindowContext::createBuffers(VkFormat format) { |
| 244 GrVkFormatToPixelConfig(format, &fPixelConfig); | 249 GrVkFormatToPixelConfig(format, &fPixelConfig); |
| 245 | 250 |
| 246 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, n
ullptr); | 251 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, n
ullptr); |
| 247 SkASSERT(fImageCount); | 252 SkASSERT(fImageCount); |
| 248 fImages = new VkImage[fImageCount]; | 253 fImages = new VkImage[fImageCount]; |
| 249 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, f
Images); | 254 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, f
Images); |
| 250 | 255 |
| 251 // set up initial image layouts and create surfaces | 256 // set up initial image layouts and create surfaces |
| 252 fImageLayouts = new VkImageLayout[fImageCount]; | 257 fImageLayouts = new VkImageLayout[fImageCount]; |
| 258 fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount]; |
| 253 fSurfaces = new sk_sp<SkSurface>[fImageCount]; | 259 fSurfaces = new sk_sp<SkSurface>[fImageCount]; |
| 254 for (uint32_t i = 0; i < fImageCount; ++i) { | 260 for (uint32_t i = 0; i < fImageCount; ++i) { |
| 255 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED; | 261 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED; |
| 256 | 262 |
| 257 GrBackendRenderTargetDesc desc; | 263 GrBackendRenderTargetDesc desc; |
| 258 GrVkImageInfo info; | 264 GrVkImageInfo info; |
| 259 info.fImage = fImages[i]; | 265 info.fImage = fImages[i]; |
| 260 info.fAlloc = VK_NULL_HANDLE; | 266 info.fAlloc = VK_NULL_HANDLE; |
| 261 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; | 267 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 262 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | 268 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 263 info.fFormat = format; | 269 info.fFormat = format; |
| 264 info.fLevelCount = 1; | 270 info.fLevelCount = 1; |
| 265 desc.fWidth = fWidth; | 271 desc.fWidth = fWidth; |
| 266 desc.fHeight = fHeight; | 272 desc.fHeight = fHeight; |
| 267 desc.fConfig = fPixelConfig; | 273 desc.fConfig = fPixelConfig; |
| 268 desc.fOrigin = kTopLeft_GrSurfaceOrigin; | 274 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 269 desc.fSampleCnt = 0; | 275 desc.fSampleCnt = 0; |
| 270 desc.fStencilBits = 0; | 276 desc.fStencilBits = 0; |
| 271 desc.fRenderTargetHandle = (GrBackendObject) &info; | 277 desc.fRenderTargetHandle = (GrBackendObject) &info; |
| 272 SkSurfaceProps props(GrPixelConfigIsSRGB(fPixelConfig) | 278 fRenderTargets[i].reset(fContext->textureProvider()->wrapBackendRenderTa
rget(desc)); |
| 273 ? SkSurfaceProps::kGammaCorrect_Flag : 0, | 279 |
| 274 kUnknown_SkPixelGeometry); | 280 fSurfaces[i] = this->createRenderSurface(fRenderTargets[i], 24); |
| 275 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc, &p
rops); | |
| 276 } | 281 } |
| 277 | 282 |
| 278 // create the command pool for the command buffers | 283 // create the command pool for the command buffers |
| 279 if (VK_NULL_HANDLE == fCommandPool) { | 284 if (VK_NULL_HANDLE == fCommandPool) { |
| 280 VkCommandPoolCreateInfo commandPoolInfo; | 285 VkCommandPoolCreateInfo commandPoolInfo; |
| 281 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); | 286 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
| 282 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; | 287 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 283 // this needs to be on the render queue | 288 // this needs to be on the render queue |
| 284 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex; | 289 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex; |
| 285 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; | 290 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 GR_VK_CALL(fBackendContext->fInterface, | 359 GR_VK_CALL(fBackendContext->fInterface, |
| 355 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU
sageFences[0], 0)); | 360 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU
sageFences[0], 0)); |
| 356 GR_VK_CALL(fBackendContext->fInterface, | 361 GR_VK_CALL(fBackendContext->fInterface, |
| 357 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU
sageFences[1], 0)); | 362 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fU
sageFences[1], 0)); |
| 358 } | 363 } |
| 359 } | 364 } |
| 360 | 365 |
| 361 delete[] fBackbuffers; | 366 delete[] fBackbuffers; |
| 362 fBackbuffers = nullptr; | 367 fBackbuffers = nullptr; |
| 363 | 368 |
| 369 // Does this actually free the surfaces? |
| 364 delete[] fSurfaces; | 370 delete[] fSurfaces; |
| 365 fSurfaces = nullptr; | 371 fSurfaces = nullptr; |
| 372 delete[] fRenderTargets; |
| 373 fRenderTargets = nullptr; |
| 366 delete[] fImageLayouts; | 374 delete[] fImageLayouts; |
| 367 fImageLayouts = nullptr; | 375 fImageLayouts = nullptr; |
| 368 delete[] fImages; | 376 delete[] fImages; |
| 369 fImages = nullptr; | 377 fImages = nullptr; |
| 370 } | 378 } |
| 371 | 379 |
| 372 VulkanWindowContext::~VulkanWindowContext() { | 380 VulkanWindowContext::~VulkanWindowContext() { |
| 373 this->destroyContext(); | 381 this->destroyContext(); |
| 374 } | 382 } |
| 375 | 383 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 391 if (VK_NULL_HANDLE != fSwapchain) { | 399 if (VK_NULL_HANDLE != fSwapchain) { |
| 392 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr); | 400 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr); |
| 393 fSwapchain = VK_NULL_HANDLE; | 401 fSwapchain = VK_NULL_HANDLE; |
| 394 } | 402 } |
| 395 | 403 |
| 396 if (VK_NULL_HANDLE != fSurface) { | 404 if (VK_NULL_HANDLE != fSurface) { |
| 397 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr); | 405 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr); |
| 398 fSurface = VK_NULL_HANDLE; | 406 fSurface = VK_NULL_HANDLE; |
| 399 } | 407 } |
| 400 | 408 |
| 401 delete fContext; | 409 fContext->abandonContext(); |
| 410 fContext->unref(); |
| 402 | 411 |
| 403 fBackendContext.reset(nullptr); | 412 fBackendContext.reset(nullptr); |
| 404 } | 413 } |
| 405 | 414 |
| 406 VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer
() { | 415 VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer
() { |
| 407 SkASSERT(fBackbuffers); | 416 SkASSERT(fBackbuffers); |
| 408 | 417 |
| 409 ++fCurrentBackbufferIndex; | 418 ++fCurrentBackbufferIndex; |
| 410 if (fCurrentBackbufferIndex > fImageCount) { | 419 if (fCurrentBackbufferIndex > fImageCount) { |
| 411 fCurrentBackbufferIndex = 0; | 420 fCurrentBackbufferIndex = 0; |
| 412 } | 421 } |
| 413 | 422 |
| 414 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; | 423 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; |
| 415 | 424 |
| 416 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | 425 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, |
| 417 WaitForFences(fBackendContext->fDevice, 2, backbuffer->f
UsageFences, | 426 WaitForFences(fBackendContext->fDevice, 2, backbuffer->f
UsageFences, |
| 418 true, UINT64_MAX)); | 427 true, UINT64_MAX)); |
| 419 return backbuffer; | 428 return backbuffer; |
| 420 } | 429 } |
| 421 | 430 |
| 422 SkSurface* VulkanWindowContext::getBackbufferSurface() { | 431 sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() { |
| 423 BackbufferInfo* backbuffer = this->getAvailableBackbuffer(); | 432 BackbufferInfo* backbuffer = this->getAvailableBackbuffer(); |
| 424 SkASSERT(backbuffer); | 433 SkASSERT(backbuffer); |
| 425 | 434 |
| 426 // reset the fence | 435 // reset the fence |
| 427 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | 436 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, |
| 428 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUs
ageFences)); | 437 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUs
ageFences)); |
| 429 // semaphores should be in unsignaled state | 438 // semaphores should be in unsignaled state |
| 430 | 439 |
| 431 // acquire the image | 440 // acquire the image |
| 432 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UI
NT64_MAX, | 441 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UI
NT64_MAX, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore; | 512 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore; |
| 504 submitInfo.pWaitDstStageMask = &waitDstStageFlags; | 513 submitInfo.pWaitDstStageMask = &waitDstStageFlags; |
| 505 submitInfo.commandBufferCount = 1; | 514 submitInfo.commandBufferCount = 1; |
| 506 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0]; | 515 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0]; |
| 507 submitInfo.signalSemaphoreCount = 0; | 516 submitInfo.signalSemaphoreCount = 0; |
| 508 | 517 |
| 509 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, | 518 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, |
| 510 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo, | 519 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo, |
| 511 backbuffer->fUsageFences[0])); | 520 backbuffer->fUsageFences[0])); |
| 512 | 521 |
| 513 return fSurfaces[backbuffer->fImageIndex].get(); | 522 return sk_ref_sp(fSurfaces[backbuffer->fImageIndex].get()); |
| 514 } | 523 } |
| 515 | 524 |
| 516 | |
| 517 void VulkanWindowContext::swapBuffers() { | 525 void VulkanWindowContext::swapBuffers() { |
| 518 | 526 |
| 519 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; | 527 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex; |
| 520 | 528 |
| 529 this->presentRenderSurface(fSurfaces[backbuffer->fImageIndex], |
| 530 fRenderTargets[backbuffer->fImageIndex], 24); |
| 531 |
| 521 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; | 532 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 522 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPU
T_BIT; | 533 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPU
T_BIT; |
| 523 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; | 534 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 524 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; | 535 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 525 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; | 536 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |
| 526 | 537 |
| 527 VkImageMemoryBarrier imageMemoryBarrier = { | 538 VkImageMemoryBarrier imageMemoryBarrier = { |
| 528 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType | 539 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 529 NULL, // pNext | 540 NULL, // pNext |
| 530 srcAccessMask, // outputMask | 541 srcAccessMask, // outputMask |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 &fSwapchain, // pSwapchains | 592 &fSwapchain, // pSwapchains |
| 582 &backbuffer->fImageIndex, // pImageIndices | 593 &backbuffer->fImageIndex, // pImageIndices |
| 583 NULL // pResults | 594 NULL // pResults |
| 584 }; | 595 }; |
| 585 | 596 |
| 586 fQueuePresentKHR(fPresentQueue, &presentInfo); | 597 fQueuePresentKHR(fPresentQueue, &presentInfo); |
| 587 | 598 |
| 588 } | 599 } |
| 589 | 600 |
| 590 } //namespace sk_app | 601 } //namespace sk_app |
| OLD | NEW |