Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 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 | #include "SkTypes.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 12 | #include "GrContextFactory.h" |
| 13 | #include "GrTest.h" |
| 14 | #include "Test.h" |
| 15 | |
| 16 | #include "GrBackendSemaphore.h" |
| 17 | #include "GrBackendSurface.h" |
| 18 | #include "SkCanvas.h" |
| 19 | #include "SkSurface.h" |
| 20 | |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 21 | #include "gl/GrGLGpu.h" |
| 22 | #include "gl/GrGLUtil.h" |
| 23 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 24 | #ifdef SK_VULKAN |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 25 | #include "vk/GrVkGpu.h" |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 26 | #include "vk/GrVkTypes.h" |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 27 | #include "vk/GrVkUtil.h" |
| 28 | |
| 29 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 30 | // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
Greg Daniel | 8761e0c | 2017-07-20 16:36:01 -0400 | [diff] [blame] | 31 | #undef CreateSemaphore |
| 32 | #endif |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | static const int MAIN_W = 8, MAIN_H = 16; |
| 36 | static const int CHILD_W = 16, CHILD_H = 16; |
| 37 | |
| 38 | void check_pixels(skiatest::Reporter* reporter, const SkBitmap& bitmap) { |
| 39 | const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels()); |
| 40 | |
| 41 | bool failureFound = false; |
| 42 | SkPMColor expectedPixel; |
| 43 | for (int cy = 0; cy < CHILD_H && !failureFound; ++cy) { |
| 44 | for (int cx = 0; cx < CHILD_W && !failureFound; ++cx) { |
| 45 | SkPMColor canvasPixel = canvasPixels[cy * CHILD_W + cx]; |
| 46 | if (cy < CHILD_H / 2) { |
| 47 | if (cx < CHILD_W / 2) { |
| 48 | expectedPixel = 0xFF0000FF; // Red |
| 49 | } else { |
| 50 | expectedPixel = 0xFFFF0000; // Blue |
| 51 | } |
| 52 | } else { |
| 53 | expectedPixel = 0xFF00FF00; // Green |
| 54 | } |
| 55 | if (expectedPixel != canvasPixel) { |
| 56 | failureFound = true; |
| 57 | ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x", |
| 58 | cx, cy, canvasPixel, expectedPixel); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void draw_child(skiatest::Reporter* reporter, |
| 65 | const sk_gpu_test::ContextInfo& childInfo, |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 66 | const GrBackendTexture& backendTexture, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 67 | const GrBackendSemaphore& semaphore) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 68 | |
| 69 | childInfo.testContext()->makeCurrent(); |
| 70 | |
| 71 | const SkImageInfo childII = SkImageInfo::Make(CHILD_W, CHILD_H, kRGBA_8888_SkColorType, |
| 72 | kPremul_SkAlphaType); |
| 73 | |
| 74 | GrContext* childCtx = childInfo.grContext(); |
| 75 | sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(childCtx, SkBudgeted::kNo, |
| 76 | childII, 0, kTopLeft_GrSurfaceOrigin, |
| 77 | nullptr)); |
| 78 | |
| 79 | sk_sp<SkImage> childImage = SkImage::MakeFromTexture(childCtx, |
| 80 | backendTexture, |
| 81 | kTopLeft_GrSurfaceOrigin, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 82 | kRGBA_8888_SkColorType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 83 | kPremul_SkAlphaType, |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 84 | nullptr, |
| 85 | nullptr, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 86 | nullptr); |
| 87 | |
| 88 | SkCanvas* childCanvas = childSurface->getCanvas(); |
| 89 | childCanvas->clear(SK_ColorRED); |
| 90 | |
| 91 | childSurface->wait(1, &semaphore); |
| 92 | |
| 93 | childCanvas->drawImage(childImage, CHILD_W/2, 0); |
| 94 | |
| 95 | SkPaint paint; |
| 96 | paint.setColor(SK_ColorGREEN); |
| 97 | SkIRect rect = SkIRect::MakeLTRB(0, CHILD_H/2, CHILD_W, CHILD_H); |
| 98 | childCanvas->drawIRect(rect, paint); |
| 99 | |
| 100 | // read pixels |
| 101 | SkBitmap bitmap; |
| 102 | bitmap.allocPixels(childII); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 103 | childSurface->readPixels(bitmap, 0, 0); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 104 | |
| 105 | check_pixels(reporter, bitmap); |
| 106 | } |
| 107 | |
| 108 | void surface_semaphore_test(skiatest::Reporter* reporter, |
| 109 | const sk_gpu_test::ContextInfo& mainInfo, |
| 110 | const sk_gpu_test::ContextInfo& childInfo1, |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 111 | const sk_gpu_test::ContextInfo& childInfo2, |
| 112 | bool flushContext) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 113 | GrContext* mainCtx = mainInfo.grContext(); |
| 114 | if (!mainCtx->caps()->fenceSyncSupport()) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType, |
| 119 | kPremul_SkAlphaType); |
| 120 | |
| 121 | sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(mainCtx, SkBudgeted::kNo, |
| 122 | ii, 0, kTopLeft_GrSurfaceOrigin, |
| 123 | nullptr)); |
| 124 | SkCanvas* mainCanvas = mainSurface->getCanvas(); |
| 125 | mainCanvas->clear(SK_ColorBLUE); |
| 126 | |
| 127 | SkAutoTArray<GrBackendSemaphore> semaphores(2); |
Greg Daniel | 8761e0c | 2017-07-20 16:36:01 -0400 | [diff] [blame] | 128 | #ifdef SK_VULKAN |
| 129 | if (kVulkan_GrBackend == mainInfo.backend()) { |
| 130 | // Initialize the secondary semaphore instead of having Ganesh create one internally |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 131 | GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->contextPriv().getGpu()); |
Greg Daniel | 8761e0c | 2017-07-20 16:36:01 -0400 | [diff] [blame] | 132 | const GrVkInterface* interface = gpu->vkInterface(); |
| 133 | VkDevice device = gpu->device(); |
| 134 | |
| 135 | VkSemaphore vkSem; |
| 136 | |
| 137 | VkSemaphoreCreateInfo createInfo; |
| 138 | createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 139 | createInfo.pNext = nullptr; |
| 140 | createInfo.flags = 0; |
| 141 | GR_VK_CALL_ERRCHECK(interface, CreateSemaphore(device, &createInfo, nullptr, &vkSem)); |
| 142 | |
| 143 | semaphores[1].initVulkan(vkSem); |
| 144 | } |
| 145 | #endif |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 146 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 147 | if (flushContext) { |
| 148 | mainCtx->flushAndSignalSemaphores(2, semaphores.get()); |
| 149 | } else { |
| 150 | mainSurface->flushAndSignalSemaphores(2, semaphores.get()); |
| 151 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 152 | |
| 153 | sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot(); |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 154 | GrBackendTexture backendTexture = mainImage->getBackendTexture(false); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 155 | |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 156 | draw_child(reporter, childInfo1, backendTexture, semaphores[0]); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 157 | |
| 158 | #ifdef SK_VULKAN |
| 159 | if (kVulkan_GrBackend == mainInfo.backend()) { |
| 160 | // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the |
| 161 | // backendImage. After the first child draw the layout gets changed to SHADER_READ, so |
| 162 | // we just manually set that here. |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 163 | GrVkImageInfo vkInfo; |
| 164 | SkAssertResult(backendTexture.getVkImageInfo(&vkInfo)); |
| 165 | vkInfo.updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 169 | draw_child(reporter, childInfo2, backendTexture, semaphores[1]); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 172 | DEF_GPUTEST(SurfaceSemaphores, reporter, options) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 173 | #if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC) |
| 174 | static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType; |
| 175 | #else |
| 176 | static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType; |
| 177 | #endif |
| 178 | |
| 179 | for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) { |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 180 | for (auto flushContext : { false, true }) { |
| 181 | sk_gpu_test::GrContextFactory::ContextType contextType = |
| 182 | (sk_gpu_test::GrContextFactory::ContextType) typeInt; |
| 183 | // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on |
| 184 | // desktop since tests do not account for not fixing http://skbug.com/2809 |
| 185 | if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType || |
| 186 | contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) { |
| 187 | if (contextType != kNativeGLType) { |
| 188 | continue; |
| 189 | } |
| 190 | } |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 191 | sk_gpu_test::GrContextFactory factory(options); |
| 192 | sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo( |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 193 | contextType, sk_gpu_test::GrContextFactory::ContextOverrides::kDisableNVPR); |
| 194 | if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 195 | continue; |
| 196 | } |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 197 | skiatest::ReporterContext ctx( |
| 198 | reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType))); |
| 199 | if (ctxInfo.grContext()) { |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 200 | sk_gpu_test::ContextInfo child1 = |
| 201 | factory.getSharedContextInfo(ctxInfo.grContext(), 0); |
| 202 | sk_gpu_test::ContextInfo child2 = |
| 203 | factory.getSharedContextInfo(ctxInfo.grContext(), 1); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 204 | if (!child1.grContext() || !child2.grContext()) { |
| 205 | continue; |
| 206 | } |
Yuqian Li | cc8eb60 | 2017-08-01 17:43:30 +0000 | [diff] [blame] | 207 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 208 | surface_semaphore_test(reporter, ctxInfo, child1, child2, flushContext); |
| 209 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 214 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) { |
| 215 | GrContext* ctx = ctxInfo.grContext(); |
| 216 | if (!ctx->caps()->fenceSyncSupport()) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType, |
| 221 | kPremul_SkAlphaType); |
| 222 | |
| 223 | sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, |
| 224 | ii, 0, kTopLeft_GrSurfaceOrigin, |
| 225 | nullptr)); |
| 226 | |
| 227 | // Flush surface once without semaphores to make sure there is no peneding IO for it. |
| 228 | mainSurface->flush(); |
| 229 | |
| 230 | GrBackendSemaphore semaphore; |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 231 | GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore); |
| 232 | REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted); |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 233 | |
| 234 | if (kOpenGL_GrBackend == ctxInfo.backend()) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 235 | GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->contextPriv().getGpu()); |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 236 | const GrGLInterface* interface = gpu->glInterface(); |
| 237 | GrGLsync sync = semaphore.glSync(); |
| 238 | REPORTER_ASSERT(reporter, sync); |
| 239 | bool result; |
| 240 | GR_GL_CALL_RET(interface, result, IsSync(sync)); |
| 241 | REPORTER_ASSERT(reporter, result); |
| 242 | } |
| 243 | |
| 244 | #ifdef SK_VULKAN |
| 245 | if (kVulkan_GrBackend == ctxInfo.backend()) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 246 | GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->contextPriv().getGpu()); |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 247 | const GrVkInterface* interface = gpu->vkInterface(); |
| 248 | VkDevice device = gpu->device(); |
| 249 | VkQueue queue = gpu->queue(); |
| 250 | VkCommandPool cmdPool = gpu->cmdPool(); |
| 251 | VkCommandBuffer cmdBuffer; |
| 252 | |
| 253 | // Create Command Buffer |
| 254 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 255 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
| 256 | nullptr, // pNext |
| 257 | cmdPool, // commandPool |
| 258 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 259 | 1 // bufferCount |
| 260 | }; |
| 261 | |
| 262 | VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer)); |
| 263 | if (err) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 268 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 269 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 270 | cmdBufferBeginInfo.pNext = nullptr; |
| 271 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 272 | cmdBufferBeginInfo.pInheritanceInfo = nullptr; |
| 273 | |
| 274 | GR_VK_CALL_ERRCHECK(interface, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo)); |
| 275 | GR_VK_CALL_ERRCHECK(interface, EndCommandBuffer(cmdBuffer)); |
| 276 | |
| 277 | VkFenceCreateInfo fenceInfo; |
| 278 | VkFence fence; |
| 279 | |
| 280 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 281 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 282 | err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence)); |
| 283 | SkASSERT(!err); |
| 284 | |
| 285 | VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 286 | VkSubmitInfo submitInfo; |
| 287 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 288 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 289 | submitInfo.pNext = nullptr; |
| 290 | submitInfo.waitSemaphoreCount = 1; |
| 291 | VkSemaphore vkSem = semaphore.vkSemaphore(); |
| 292 | submitInfo.pWaitSemaphores = &vkSem; |
| 293 | submitInfo.pWaitDstStageMask = &waitStages; |
| 294 | submitInfo.commandBufferCount = 1; |
| 295 | submitInfo.pCommandBuffers = &cmdBuffer; |
| 296 | submitInfo.signalSemaphoreCount = 0; |
| 297 | submitInfo.pSignalSemaphores = nullptr; |
| 298 | GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence)); |
| 299 | |
| 300 | err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000)); |
| 301 | |
| 302 | REPORTER_ASSERT(reporter, err != VK_TIMEOUT); |
| 303 | |
| 304 | GR_VK_CALL(interface, DestroyFence(device, fence, nullptr)); |
| 305 | GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr)); |
| 306 | // If the above test fails the wait semaphore will never be signaled which can cause the |
| 307 | // device to hang when tearing down (even if just tearing down GL). So we Fail here to |
| 308 | // kill things. |
| 309 | if (err == VK_TIMEOUT) { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 310 | SK_ABORT("Waiting on semaphore indefinitely"); |
Greg Daniel | dba7e7c | 2017-07-20 15:47:30 -0400 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | #endif |
| 314 | } |
| 315 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 316 | #endif |