blob: 9dd45154daf5ae062cd494ec83f45db8bded8805 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/gl/GrGLGpu.h"
20#include "src/gpu/gl/GrGLUtil.h"
Greg Danieldba7e7c2017-07-20 15:47:30 -040021
Greg Daniela5cb7812017-06-16 09:45:32 -040022#ifdef SK_VULKAN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/gpu/vk/GrVkTypes.h"
24#include "src/gpu/vk/GrVkCommandPool.h"
25#include "src/gpu/vk/GrVkGpu.h"
26#include "src/gpu/vk/GrVkUtil.h"
Greg Danieldba7e7c2017-07-20 15:47:30 -040027
28#ifdef VK_USE_PLATFORM_WIN32_KHR
29// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
Greg Daniel8761e0c2017-07-20 16:36:01 -040030#undef CreateSemaphore
31#endif
Greg Daniela5cb7812017-06-16 09:45:32 -040032#endif
33
34static const int MAIN_W = 8, MAIN_H = 16;
35static const int CHILD_W = 16, CHILD_H = 16;
36
37void 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
63void draw_child(skiatest::Reporter* reporter,
64 const sk_gpu_test::ContextInfo& childInfo,
Robert Phillipsba375a82018-04-11 10:08:06 -040065 const GrBackendTexture& backendTexture,
Greg Daniela5cb7812017-06-16 09:45:32 -040066 const GrBackendSemaphore& semaphore) {
Greg Daniela5cb7812017-06-16 09:45:32 -040067
68 childInfo.testContext()->makeCurrent();
69
70 const SkImageInfo childII = SkImageInfo::Make(CHILD_W, CHILD_H, kRGBA_8888_SkColorType,
71 kPremul_SkAlphaType);
72
73 GrContext* childCtx = childInfo.grContext();
74 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(childCtx, SkBudgeted::kNo,
75 childII, 0, kTopLeft_GrSurfaceOrigin,
76 nullptr));
77
78 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(childCtx,
79 backendTexture,
80 kTopLeft_GrSurfaceOrigin,
Greg Danielf5d87582017-12-18 14:48:15 -050081 kRGBA_8888_SkColorType,
Greg Daniela5cb7812017-06-16 09:45:32 -040082 kPremul_SkAlphaType,
Greg Danielf5d87582017-12-18 14:48:15 -050083 nullptr,
84 nullptr,
Greg Daniela5cb7812017-06-16 09:45:32 -040085 nullptr);
86
87 SkCanvas* childCanvas = childSurface->getCanvas();
88 childCanvas->clear(SK_ColorRED);
89
90 childSurface->wait(1, &semaphore);
91
92 childCanvas->drawImage(childImage, CHILD_W/2, 0);
93
94 SkPaint paint;
95 paint.setColor(SK_ColorGREEN);
96 SkIRect rect = SkIRect::MakeLTRB(0, CHILD_H/2, CHILD_W, CHILD_H);
97 childCanvas->drawIRect(rect, paint);
98
99 // read pixels
100 SkBitmap bitmap;
101 bitmap.allocPixels(childII);
Mike Reedf1942192017-07-21 14:24:29 -0400102 childSurface->readPixels(bitmap, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400103
104 check_pixels(reporter, bitmap);
105}
106
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400107enum class FlushType { kSurface, kImage, kContext };
108
Greg Daniela5cb7812017-06-16 09:45:32 -0400109void surface_semaphore_test(skiatest::Reporter* reporter,
110 const sk_gpu_test::ContextInfo& mainInfo,
111 const sk_gpu_test::ContextInfo& childInfo1,
Greg Daniel51316782017-08-02 15:10:09 +0000112 const sk_gpu_test::ContextInfo& childInfo2,
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400113 FlushType flushType) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400114 GrContext* mainCtx = mainInfo.grContext();
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400115 if (!mainCtx->priv().caps()->semaphoreSupport()) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400116 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();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400126 auto blueSurface = mainSurface->makeSurface(ii);
127 blueSurface->getCanvas()->clear(SK_ColorBLUE);
128 auto blueImage = blueSurface->makeImageSnapshot();
129 blueSurface.reset();
130 mainCanvas->drawImage(blueImage, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400131
132 SkAutoTArray<GrBackendSemaphore> semaphores(2);
Greg Daniel8761e0c2017-07-20 16:36:01 -0400133#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400134 if (GrBackendApi::kVulkan == mainInfo.backend()) {
Greg Daniel8761e0c2017-07-20 16:36:01 -0400135 // Initialize the secondary semaphore instead of having Ganesh create one internally
Robert Phillips9da87e02019-02-04 13:26:26 -0500136 GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->priv().getGpu());
Greg Daniel8761e0c2017-07-20 16:36:01 -0400137 const GrVkInterface* interface = gpu->vkInterface();
138 VkDevice device = gpu->device();
139
140 VkSemaphore vkSem;
141
142 VkSemaphoreCreateInfo createInfo;
143 createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
144 createInfo.pNext = nullptr;
145 createInfo.flags = 0;
146 GR_VK_CALL_ERRCHECK(interface, CreateSemaphore(device, &createInfo, nullptr, &vkSem));
147
148 semaphores[1].initVulkan(vkSem);
149 }
150#endif
Greg Daniela5cb7812017-06-16 09:45:32 -0400151
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400152 GrFlushInfo info;
153 info.fNumSemaphores = 2;
154 info.fSignalSemaphores = semaphores.get();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400155 switch (flushType) {
156 case FlushType::kSurface:
157 mainSurface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, info);
158 break;
159 case FlushType::kImage:
160 blueImage->flush(mainCtx, info);
161 break;
162 case FlushType::kContext:
163 mainCtx->flush(info);
164 break;
Greg Daniel51316782017-08-02 15:10:09 +0000165 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400166
167 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
Robert Phillipsba375a82018-04-11 10:08:06 -0400168 GrBackendTexture backendTexture = mainImage->getBackendTexture(false);
Greg Daniela5cb7812017-06-16 09:45:32 -0400169
Robert Phillipsba375a82018-04-11 10:08:06 -0400170 draw_child(reporter, childInfo1, backendTexture, semaphores[0]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400171
172#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400173 if (GrBackendApi::kVulkan == mainInfo.backend()) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400174 // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the
175 // backendImage. After the first child draw the layout gets changed to SHADER_READ, so
176 // we just manually set that here.
Robert Phillipsba375a82018-04-11 10:08:06 -0400177 GrVkImageInfo vkInfo;
178 SkAssertResult(backendTexture.getVkImageInfo(&vkInfo));
179 vkInfo.updateImageLayout(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
Brian Salomondcfca432017-11-15 15:48:03 -0500186DEF_GPUTEST(SurfaceSemaphores, reporter, options) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400187#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
188 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
189#else
190 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
191#endif
192
193 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400194 for (auto flushType : {FlushType::kSurface, FlushType::kImage, FlushType::kContext}) {
Greg Daniel51316782017-08-02 15:10:09 +0000195 sk_gpu_test::GrContextFactory::ContextType contextType =
196 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
197 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
198 // desktop since tests do not account for not fixing http://skbug.com/2809
199 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
200 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
201 if (contextType != kNativeGLType) {
202 continue;
203 }
204 }
Brian Salomondcfca432017-11-15 15:48:03 -0500205 sk_gpu_test::GrContextFactory factory(options);
Chris Daltonb3c97452019-06-25 20:07:56 -0600206 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(contextType);
Greg Daniel51316782017-08-02 15:10:09 +0000207 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400208 continue;
209 }
Greg Daniel51316782017-08-02 15:10:09 +0000210 skiatest::ReporterContext ctx(
211 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
212 if (ctxInfo.grContext()) {
Brian Salomondcfca432017-11-15 15:48:03 -0500213 sk_gpu_test::ContextInfo child1 =
214 factory.getSharedContextInfo(ctxInfo.grContext(), 0);
215 sk_gpu_test::ContextInfo child2 =
216 factory.getSharedContextInfo(ctxInfo.grContext(), 1);
Greg Daniel51316782017-08-02 15:10:09 +0000217 if (!child1.grContext() || !child2.grContext()) {
218 continue;
219 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000220
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400221 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushType);
Greg Daniel51316782017-08-02 15:10:09 +0000222 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400223 }
224 }
225}
226
Greg Danieldba7e7c2017-07-20 15:47:30 -0400227DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
228 GrContext* ctx = ctxInfo.grContext();
Brian Salomon9ff5acb2019-05-08 09:04:47 -0400229 if (!ctx->priv().caps()->semaphoreSupport()) {
Greg Danieldba7e7c2017-07-20 15:47:30 -0400230 return;
231 }
232
233 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
234 kPremul_SkAlphaType);
235
236 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
237 ii, 0, kTopLeft_GrSurfaceOrigin,
238 nullptr));
239
240 // Flush surface once without semaphores to make sure there is no peneding IO for it.
241 mainSurface->flush();
242
243 GrBackendSemaphore semaphore;
Greg Daniel51316782017-08-02 15:10:09 +0000244 GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore);
245 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Danieldba7e7c2017-07-20 15:47:30 -0400246
Greg Danielbdf12ad2018-10-12 09:31:11 -0400247 if (GrBackendApi::kOpenGL == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500248 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400249 const GrGLInterface* interface = gpu->glInterface();
250 GrGLsync sync = semaphore.glSync();
251 REPORTER_ASSERT(reporter, sync);
252 bool result;
253 GR_GL_CALL_RET(interface, result, IsSync(sync));
254 REPORTER_ASSERT(reporter, result);
255 }
256
257#ifdef SK_VULKAN
Greg Danielbdf12ad2018-10-12 09:31:11 -0400258 if (GrBackendApi::kVulkan == ctxInfo.backend()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500259 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->priv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400260 const GrVkInterface* interface = gpu->vkInterface();
261 VkDevice device = gpu->device();
262 VkQueue queue = gpu->queue();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500263 GrVkCommandPool* cmdPool = gpu->cmdPool();
Greg Danieldba7e7c2017-07-20 15:47:30 -0400264 VkCommandBuffer cmdBuffer;
265
266 // Create Command Buffer
267 const VkCommandBufferAllocateInfo cmdInfo = {
268 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
269 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500270 cmdPool->vkCommandPool(), // commandPool
Greg Danieldba7e7c2017-07-20 15:47:30 -0400271 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
272 1 // bufferCount
273 };
274
275 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
276 if (err) {
277 return;
278 }
279
280 VkCommandBufferBeginInfo cmdBufferBeginInfo;
281 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
282 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
283 cmdBufferBeginInfo.pNext = nullptr;
284 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
285 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
286
287 GR_VK_CALL_ERRCHECK(interface, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
288 GR_VK_CALL_ERRCHECK(interface, EndCommandBuffer(cmdBuffer));
289
290 VkFenceCreateInfo fenceInfo;
291 VkFence fence;
292
293 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
294 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
295 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
296 SkASSERT(!err);
297
298 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
299 VkSubmitInfo submitInfo;
300 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
301 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
302 submitInfo.pNext = nullptr;
303 submitInfo.waitSemaphoreCount = 1;
304 VkSemaphore vkSem = semaphore.vkSemaphore();
305 submitInfo.pWaitSemaphores = &vkSem;
306 submitInfo.pWaitDstStageMask = &waitStages;
307 submitInfo.commandBufferCount = 1;
308 submitInfo.pCommandBuffers = &cmdBuffer;
309 submitInfo.signalSemaphoreCount = 0;
310 submitInfo.pSignalSemaphores = nullptr;
311 GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence));
312
313 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
314
315 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
316
317 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
318 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
319 // If the above test fails the wait semaphore will never be signaled which can cause the
320 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
321 // kill things.
322 if (err == VK_TIMEOUT) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400323 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400324 }
325 }
326#endif
327}