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