blob: 5e7fc05b8781f924e120392569362c91f3b8ba0d [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/gpu/vk/GrVkVulkan.h"
Greg Daniela5cb7812017-06-16 09:45:32 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrContextPriv.h"
11#include "tests/Test.h"
12#include "tools/gpu/GrContextFactory.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkCanvas.h"
15#include "include/core/SkSurface.h"
16#include "include/gpu/GrBackendSemaphore.h"
17#include "include/gpu/GrBackendSurface.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040018
John Rosascoa9b348f2019-11-08 13:18:15 -080019#ifdef SK_GL
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/gl/GrGLGpu.h"
21#include "src/gpu/gl/GrGLUtil.h"
John Rosascoa9b348f2019-11-08 13:18:15 -080022#endif
Greg Danieldba7e7c2017-07-20 15:47:30 -040023
Greg Daniela5cb7812017-06-16 09:45:32 -040024#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/gpu/vk/GrVkTypes.h"
26#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
75 GrContext* childCtx = childInfo.grContext();
76 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) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400116 GrContext* mainCtx = mainInfo.grContext();
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 Daniela5cb7812017-06-16 09:45:32 -0400167
168 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
Robert Phillipsba375a82018-04-11 10:08:06 -0400169 GrBackendTexture backendTexture = mainImage->getBackendTexture(false);
Greg Daniela5cb7812017-06-16 09:45:32 -0400170
Robert Phillipsba375a82018-04-11 10:08:06 -0400171 draw_child(reporter, childInfo1, backendTexture, semaphores[0]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400172
173#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400174 if (GrBackendApi::kVulkan == mainInfo.backend()) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400175 // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the
176 // backendImage. After the first child draw the layout gets changed to SHADER_READ, so
177 // we just manually set that here.
Robert Phillipsba375a82018-04-11 10:08:06 -0400178 GrVkImageInfo vkInfo;
179 SkAssertResult(backendTexture.getVkImageInfo(&vkInfo));
180 vkInfo.updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Greg Daniela5cb7812017-06-16 09:45:32 -0400181 }
182#endif
183
Robert Phillipsba375a82018-04-11 10:08:06 -0400184 draw_child(reporter, childInfo2, backendTexture, semaphores[1]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400185}
186
John Rosascoa9b348f2019-11-08 13:18:15 -0800187#ifdef SK_GL
Brian Salomondcfca432017-11-15 15:48:03 -0500188DEF_GPUTEST(SurfaceSemaphores, reporter, options) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400189#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
190 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
191#else
192 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
193#endif
194
195 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400196 for (auto flushType : {FlushType::kSurface, FlushType::kImage, FlushType::kContext}) {
Greg Daniel51316782017-08-02 15:10:09 +0000197 sk_gpu_test::GrContextFactory::ContextType contextType =
198 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
199 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
200 // desktop since tests do not account for not fixing http://skbug.com/2809
201 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
202 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
203 if (contextType != kNativeGLType) {
204 continue;
205 }
206 }
Brian Salomondcfca432017-11-15 15:48:03 -0500207 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600208 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel51316782017-08-02 15:10:09 +0000209 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400210 continue;
211 }
Greg Daniel51316782017-08-02 15:10:09 +0000212 skiatest::ReporterContext ctx(
213 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
214 if (ctxInfo.grContext()) {
Brian Salomondcfca432017-11-15 15:48:03 -0500215 sk_gpu_test::ContextInfo child1 =
216 factory.getSharedContextInfo(ctxInfo.grContext(), 0);
217 sk_gpu_test::ContextInfo child2 =
218 factory.getSharedContextInfo(ctxInfo.grContext(), 1);
Greg Daniel51316782017-08-02 15:10:09 +0000219 if (!child1.grContext() || !child2.grContext()) {
220 continue;
221 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000222
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400223 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushType);
Greg Daniel51316782017-08-02 15:10:09 +0000224 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400225 }
226 }
227}
John Rosascoa9b348f2019-11-08 13:18:15 -0800228#endif
Greg Daniela5cb7812017-06-16 09:45:32 -0400229
Greg Danieldba7e7c2017-07-20 15:47:30 -0400230DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
231 GrContext* ctx = ctxInfo.grContext();
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400232 if (!ctx->priv().caps()->semaphoreSupport()) {
Greg Danieldba7e7c2017-07-20 15:47:30 -0400233 return;
234 }
235
236 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
237 kPremul_SkAlphaType);
238
239 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
240 ii, 0, kTopLeft_GrSurfaceOrigin,
241 nullptr));
242
243 // Flush surface once without semaphores to make sure there is no peneding IO for it.
244 mainSurface->flush();
245
246 GrBackendSemaphore semaphore;
Greg Daniel51316782017-08-02 15:10:09 +0000247 GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore);
248 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Danieldba7e7c2017-07-20 15:47:30 -0400249
John Rosascoa9b348f2019-11-08 13:18:15 -0800250#ifdef SK_GL
Greg Danielbdf12ad2018-10-12 09:31:11 -0400251 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500252 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400253 const GrGLInterface* interface = gpu->glInterface();
254 GrGLsync sync = semaphore.glSync();
255 REPORTER_ASSERT(reporter, sync);
256 bool result;
257 GR_GL_CALL_RET(interface, result, IsSync(sync));
258 REPORTER_ASSERT(reporter, result);
259 }
John Rosascoa9b348f2019-11-08 13:18:15 -0800260#endif
Greg Danieldba7e7c2017-07-20 15:47:30 -0400261
262#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400263 if (GrBackendApi::kVulkan == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500264 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400265 const GrVkInterface* interface = gpu->vkInterface();
266 VkDevice device = gpu->device();
267 VkQueue queue = gpu->queue();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500268 GrVkCommandPool* cmdPool = gpu->cmdPool();
Greg Danieldba7e7c2017-07-20 15:47:30 -0400269 VkCommandBuffer cmdBuffer;
270
271 // Create Command Buffer
272 const VkCommandBufferAllocateInfo cmdInfo = {
273 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
274 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500275 cmdPool->vkCommandPool(), // commandPool
Greg Danieldba7e7c2017-07-20 15:47:30 -0400276 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
277 1 // bufferCount
278 };
279
280 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
281 if (err) {
282 return;
283 }
284
285 VkCommandBufferBeginInfo cmdBufferBeginInfo;
286 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
287 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
288 cmdBufferBeginInfo.pNext = nullptr;
289 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
290 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
291
Greg Daniele643da62019-11-05 12:36:42 -0500292 GR_VK_CALL_ERRCHECK(gpu, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
293 GR_VK_CALL_ERRCHECK(gpu, EndCommandBuffer(cmdBuffer));
Greg Danieldba7e7c2017-07-20 15:47:30 -0400294
295 VkFenceCreateInfo fenceInfo;
296 VkFence fence;
297
298 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
299 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
300 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
301 SkASSERT(!err);
302
303 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
304 VkSubmitInfo submitInfo;
305 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
306 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
307 submitInfo.pNext = nullptr;
308 submitInfo.waitSemaphoreCount = 1;
309 VkSemaphore vkSem = semaphore.vkSemaphore();
310 submitInfo.pWaitSemaphores = &vkSem;
311 submitInfo.pWaitDstStageMask = &waitStages;
312 submitInfo.commandBufferCount = 1;
313 submitInfo.pCommandBuffers = &cmdBuffer;
314 submitInfo.signalSemaphoreCount = 0;
315 submitInfo.pSignalSemaphores = nullptr;
Greg Daniele643da62019-11-05 12:36:42 -0500316 GR_VK_CALL_ERRCHECK(gpu, QueueSubmit(queue, 1, &submitInfo, fence));
Greg Danieldba7e7c2017-07-20 15:47:30 -0400317
318 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
319
320 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
321
322 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
323 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
324 // If the above test fails the wait semaphore will never be signaled which can cause the
325 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
326 // kill things.
327 if (err == VK_TIMEOUT) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400328 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400329 }
330 }
331#endif
332}