blob: 990e0783fb6d38c82fb602403901c827b2f86292 [file] [log] [blame]
Greg Daniela5cb7812017-06-16 09:45:32 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrBackendSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040013#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrContextPriv.h"
15#include "tests/Test.h"
16#include "tools/gpu/GrContextFactory.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040017
John Rosascoa9b348f2019-11-08 13:18:15 -080018#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/gl/GrGLGpu.h"
20#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080021#endif
Greg Danieldba7e7c2017-07-20 15:47:30 -040022
Greg Daniela5cb7812017-06-16 09:45:32 -040023#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/gpu/vk/GrVkTypes.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040025#include "include/gpu/vk/GrVkVulkan.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/vk/GrVkCommandPool.h"
27#include "src/gpu/vk/GrVkGpu.h"
28#include "src/gpu/vk/GrVkUtil.h"
Greg Danieldba7e7c2017-07-20 15:47:30 -040029
30#ifdef VK_USE_PLATFORM_WIN32_KHR
31// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
Greg Daniel8761e0c2017-07-20 16:36:01 -040032#undef CreateSemaphore
33#endif
Greg Daniela5cb7812017-06-16 09:45:32 -040034#endif
35
36static const int MAIN_W = 8, MAIN_H = 16;
37static const int CHILD_W = 16, CHILD_H = 16;
38
39void check_pixels(skiatest::Reporter* reporter, const SkBitmap& bitmap) {
40 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
41
42 bool failureFound = false;
43 SkPMColor expectedPixel;
44 for (int cy = 0; cy < CHILD_H && !failureFound; ++cy) {
45 for (int cx = 0; cx < CHILD_W && !failureFound; ++cx) {
46 SkPMColor canvasPixel = canvasPixels[cy * CHILD_W + cx];
47 if (cy < CHILD_H / 2) {
48 if (cx < CHILD_W / 2) {
49 expectedPixel = 0xFF0000FF; // Red
50 } else {
51 expectedPixel = 0xFFFF0000; // Blue
52 }
53 } else {
54 expectedPixel = 0xFF00FF00; // Green
55 }
56 if (expectedPixel != canvasPixel) {
57 failureFound = true;
58 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x",
59 cx, cy, canvasPixel, expectedPixel);
60 }
61 }
62 }
63}
64
65void draw_child(skiatest::Reporter* reporter,
66 const sk_gpu_test::ContextInfo& childInfo,
Robert Phillipsba375a82018-04-11 10:08:06 -040067 const GrBackendTexture& backendTexture,
Greg Daniela5cb7812017-06-16 09:45:32 -040068 const GrBackendSemaphore& semaphore) {
Greg Daniela5cb7812017-06-16 09:45:32 -040069
70 childInfo.testContext()->makeCurrent();
71
72 const SkImageInfo childII = SkImageInfo::Make(CHILD_W, CHILD_H, kRGBA_8888_SkColorType,
73 kPremul_SkAlphaType);
74
Robert Phillips6d344c32020-07-06 10:56:46 -040075 auto childCtx = childInfo.directContext();
Greg Daniela5cb7812017-06-16 09:45:32 -040076 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(childCtx, SkBudgeted::kNo,
77 childII, 0, kTopLeft_GrSurfaceOrigin,
78 nullptr));
79
80 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(childCtx,
81 backendTexture,
82 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -050083 kRGBA_8888_SkColorType,
Greg Daniela5cb7812017-06-16 09:45:32 -040084 kPremul_SkAlphaType,
Greg Danielf5d87582017-12-18 14:48:15 -050085 nullptr,
86 nullptr,
Greg Daniela5cb7812017-06-16 09:45:32 -040087 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 Reedf1942192017-07-21 14:24:29 -0400104 childSurface->readPixels(bitmap, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400105
106 check_pixels(reporter, bitmap);
107}
108
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400109enum class FlushType { kSurface, kImage, kContext };
110
Greg Daniela5cb7812017-06-16 09:45:32 -0400111void surface_semaphore_test(skiatest::Reporter* reporter,
112 const sk_gpu_test::ContextInfo& mainInfo,
113 const sk_gpu_test::ContextInfo& childInfo1,
Greg Daniel51316782017-08-02 15:10:09 +0000114 const sk_gpu_test::ContextInfo& childInfo2,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400115 FlushType flushType) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400116 auto mainCtx = mainInfo.directContext();
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400117 if (!mainCtx->priv().caps()->semaphoreSupport()) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400118 return;
119 }
120
121 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
122 kPremul_SkAlphaType);
123
124 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(mainCtx, SkBudgeted::kNo,
125 ii, 0, kTopLeft_GrSurfaceOrigin,
126 nullptr));
127 SkCanvas* mainCanvas = mainSurface->getCanvas();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400128 auto blueSurface = mainSurface->makeSurface(ii);
129 blueSurface->getCanvas()->clear(SK_ColorBLUE);
130 auto blueImage = blueSurface->makeImageSnapshot();
131 blueSurface.reset();
132 mainCanvas->drawImage(blueImage, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400133
134 SkAutoTArray<GrBackendSemaphore> semaphores(2);
Greg Daniel8761e0c2017-07-20 16:36:01 -0400135#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400136 if (GrBackendApi::kVulkan == mainInfo.backend()) {
Greg Daniel8761e0c2017-07-20 16:36:01 -0400137 // Initialize the secondary semaphore instead of having Ganesh create one internally
Robert Phillips9da87e02019-02-04 13:26:26 -0500138 GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->priv().getGpu());
Greg Daniel8761e0c2017-07-20 16:36:01 -0400139 VkDevice device = gpu->device();
140
141 VkSemaphore vkSem;
142
143 VkSemaphoreCreateInfo createInfo;
144 createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
145 createInfo.pNext = nullptr;
146 createInfo.flags = 0;
Greg Daniele643da62019-11-05 12:36:42 -0500147 GR_VK_CALL_ERRCHECK(gpu, CreateSemaphore(device, &createInfo, nullptr, &vkSem));
Greg Daniel8761e0c2017-07-20 16:36:01 -0400148
149 semaphores[1].initVulkan(vkSem);
150 }
151#endif
Greg Daniela5cb7812017-06-16 09:45:32 -0400152
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400153 GrFlushInfo info;
154 info.fNumSemaphores = 2;
155 info.fSignalSemaphores = semaphores.get();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400156 switch (flushType) {
157 case FlushType::kSurface:
158 mainSurface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, info);
159 break;
160 case FlushType::kImage:
161 blueImage->flush(mainCtx, info);
162 break;
163 case FlushType::kContext:
164 mainCtx->flush(info);
165 break;
Greg Daniel51316782017-08-02 15:10:09 +0000166 }
Greg Daniel0a2464f2020-05-14 15:45:44 -0400167 mainCtx->submit();
Greg Daniela5cb7812017-06-16 09:45:32 -0400168
169 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
Robert Phillipsba375a82018-04-11 10:08:06 -0400170 GrBackendTexture backendTexture = mainImage->getBackendTexture(false);
Greg Daniela5cb7812017-06-16 09:45:32 -0400171
Robert Phillipsba375a82018-04-11 10:08:06 -0400172 draw_child(reporter, childInfo1, backendTexture, semaphores[0]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400173
174#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400175 if (GrBackendApi::kVulkan == mainInfo.backend()) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400176 // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the
177 // backendImage. After the first child draw the layout gets changed to SHADER_READ, so
178 // we just manually set that here.
Jim Van Verth6ec56882020-03-26 11:47:26 -0400179 backendTexture.setVkImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Greg Daniela5cb7812017-06-16 09:45:32 -0400180 }
181#endif
182
Robert Phillipsba375a82018-04-11 10:08:06 -0400183 draw_child(reporter, childInfo2, backendTexture, semaphores[1]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400184}
185
John Rosascoa9b348f2019-11-08 13:18:15 -0800186#ifdef SK_GL
Brian Salomondcfca432017-11-15 15:48:03 -0500187DEF_GPUTEST(SurfaceSemaphores, reporter, options) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400188#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
189 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
190#else
191 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
192#endif
193
194 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400195 for (auto flushType : {FlushType::kSurface, FlushType::kImage, FlushType::kContext}) {
Greg Daniel51316782017-08-02 15:10:09 +0000196 sk_gpu_test::GrContextFactory::ContextType contextType =
197 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
198 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
199 // desktop since tests do not account for not fixing http://skbug.com/2809
200 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
201 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
202 if (contextType != kNativeGLType) {
203 continue;
204 }
205 }
Brian Salomondcfca432017-11-15 15:48:03 -0500206 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600207 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel51316782017-08-02 15:10:09 +0000208 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400209 continue;
210 }
Greg Daniel51316782017-08-02 15:10:09 +0000211 skiatest::ReporterContext ctx(
212 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
Robert Phillips6d344c32020-07-06 10:56:46 -0400213 if (ctxInfo.directContext()) {
Brian Salomondcfca432017-11-15 15:48:03 -0500214 sk_gpu_test::ContextInfo child1 =
Robert Phillips00f78de2020-07-01 16:09:43 -0400215 factory.getSharedContextInfo(ctxInfo.directContext(), 0);
Brian Salomondcfca432017-11-15 15:48:03 -0500216 sk_gpu_test::ContextInfo child2 =
Robert Phillips00f78de2020-07-01 16:09:43 -0400217 factory.getSharedContextInfo(ctxInfo.directContext(), 1);
Robert Phillips6d344c32020-07-06 10:56:46 -0400218 if (!child1.directContext() || !child2.directContext()) {
Greg Daniel51316782017-08-02 15:10:09 +0000219 continue;
220 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000221
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400222 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushType);
Greg Daniel51316782017-08-02 15:10:09 +0000223 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400224 }
225 }
226}
John Rosascoa9b348f2019-11-08 13:18:15 -0800227#endif
Greg Daniela5cb7812017-06-16 09:45:32 -0400228
Greg Danieldba7e7c2017-07-20 15:47:30 -0400229DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400230 auto ctx = ctxInfo.directContext();
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400231 if (!ctx->priv().caps()->semaphoreSupport()) {
Greg Danieldba7e7c2017-07-20 15:47:30 -0400232 return;
233 }
234
235 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
236 kPremul_SkAlphaType);
237
238 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
239 ii, 0, kTopLeft_GrSurfaceOrigin,
240 nullptr));
241
242 // Flush surface once without semaphores to make sure there is no peneding IO for it.
Greg Daniel0a2464f2020-05-14 15:45:44 -0400243 mainSurface->flushAndSubmit();
Greg Danieldba7e7c2017-07-20 15:47:30 -0400244
245 GrBackendSemaphore semaphore;
Greg Daniel49de1032020-05-19 18:06:26 -0400246 GrFlushInfo flushInfo;
247 flushInfo.fNumSemaphores = 1;
248 flushInfo.fSignalSemaphores = &semaphore;
Greg Daniel04283f32020-05-20 13:16:00 -0400249 GrSemaphoresSubmitted submitted =
250 mainSurface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Greg Daniel51316782017-08-02 15:10:09 +0000251 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Daniel04283f32020-05-20 13:16:00 -0400252 ctx->submit();
Greg Danieldba7e7c2017-07-20 15:47:30 -0400253
John Rosascoa9b348f2019-11-08 13:18:15 -0800254#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400255 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500256 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400257 const GrGLInterface* interface = gpu->glInterface();
258 GrGLsync sync = semaphore.glSync();
259 REPORTER_ASSERT(reporter, sync);
260 bool result;
261 GR_GL_CALL_RET(interface, result, IsSync(sync));
262 REPORTER_ASSERT(reporter, result);
263 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800264#endif
Greg Danieldba7e7c2017-07-20 15:47:30 -0400265
266#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400267 if (GrBackendApi::kVulkan == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500268 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400269 const GrVkInterface* interface = gpu->vkInterface();
270 VkDevice device = gpu->device();
271 VkQueue queue = gpu->queue();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500272 GrVkCommandPool* cmdPool = gpu->cmdPool();
Greg Danieldba7e7c2017-07-20 15:47:30 -0400273 VkCommandBuffer cmdBuffer;
274
275 // Create Command Buffer
276 const VkCommandBufferAllocateInfo cmdInfo = {
277 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
278 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500279 cmdPool->vkCommandPool(), // commandPool
Greg Danieldba7e7c2017-07-20 15:47:30 -0400280 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
281 1 // bufferCount
282 };
283
284 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
285 if (err) {
286 return;
287 }
288
289 VkCommandBufferBeginInfo cmdBufferBeginInfo;
290 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
291 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
292 cmdBufferBeginInfo.pNext = nullptr;
293 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
294 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
295
Greg Daniele643da62019-11-05 12:36:42 -0500296 GR_VK_CALL_ERRCHECK(gpu, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
297 GR_VK_CALL_ERRCHECK(gpu, EndCommandBuffer(cmdBuffer));
Greg Danieldba7e7c2017-07-20 15:47:30 -0400298
299 VkFenceCreateInfo fenceInfo;
300 VkFence fence;
301
302 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
303 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
304 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
305 SkASSERT(!err);
306
307 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
308 VkSubmitInfo submitInfo;
309 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
310 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
311 submitInfo.pNext = nullptr;
312 submitInfo.waitSemaphoreCount = 1;
313 VkSemaphore vkSem = semaphore.vkSemaphore();
314 submitInfo.pWaitSemaphores = &vkSem;
315 submitInfo.pWaitDstStageMask = &waitStages;
316 submitInfo.commandBufferCount = 1;
317 submitInfo.pCommandBuffers = &cmdBuffer;
318 submitInfo.signalSemaphoreCount = 0;
319 submitInfo.pSignalSemaphores = nullptr;
Greg Daniele643da62019-11-05 12:36:42 -0500320 GR_VK_CALL_ERRCHECK(gpu, QueueSubmit(queue, 1, &submitInfo, fence));
Greg Danieldba7e7c2017-07-20 15:47:30 -0400321
322 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
323
324 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
325
326 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
327 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
328 // If the above test fails the wait semaphore will never be signaled which can cause the
329 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
330 // kill things.
331 if (err == VK_TIMEOUT) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400332 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400333 }
334 }
335#endif
336}