blob: 980478bcef52a3ea03a63ed0fed8c7fd1ed711b4 [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
8#include "SkTypes.h"
9
10#if SK_SUPPORT_GPU
Robert Phillipsf35fd8d2018-01-22 10:48:15 -050011#include "GrContextPriv.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#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 Danieldba7e7c2017-07-20 15:47:30 -040021#include "gl/GrGLGpu.h"
22#include "gl/GrGLUtil.h"
23
Greg Daniela5cb7812017-06-16 09:45:32 -040024#ifdef SK_VULKAN
Greg Danieldba7e7c2017-07-20 15:47:30 -040025#include "vk/GrVkGpu.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040026#include "vk/GrVkTypes.h"
Greg Danieldba7e7c2017-07-20 15:47:30 -040027#include "vk/GrVkUtil.h"
28
29#ifdef VK_USE_PLATFORM_WIN32_KHR
30// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
Greg Daniel8761e0c2017-07-20 16:36:01 -040031#undef CreateSemaphore
32#endif
Greg Daniela5cb7812017-06-16 09:45:32 -040033#endif
34
35static const int MAIN_W = 8, MAIN_H = 16;
36static const int CHILD_W = 16, CHILD_H = 16;
37
38void 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
64void draw_child(skiatest::Reporter* reporter,
65 const sk_gpu_test::ContextInfo& childInfo,
Robert Phillipsba375a82018-04-11 10:08:06 -040066 const GrBackendTexture& backendTexture,
Greg Daniela5cb7812017-06-16 09:45:32 -040067 const GrBackendSemaphore& semaphore) {
Greg Daniela5cb7812017-06-16 09:45:32 -040068
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 Danielf5d87582017-12-18 14:48:15 -050082 kRGBA_8888_SkColorType,
Greg Daniela5cb7812017-06-16 09:45:32 -040083 kPremul_SkAlphaType,
Greg Danielf5d87582017-12-18 14:48:15 -050084 nullptr,
85 nullptr,
Greg Daniela5cb7812017-06-16 09:45:32 -040086 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 Reedf1942192017-07-21 14:24:29 -0400103 childSurface->readPixels(bitmap, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400104
105 check_pixels(reporter, bitmap);
106}
107
108void surface_semaphore_test(skiatest::Reporter* reporter,
109 const sk_gpu_test::ContextInfo& mainInfo,
110 const sk_gpu_test::ContextInfo& childInfo1,
Greg Daniel51316782017-08-02 15:10:09 +0000111 const sk_gpu_test::ContextInfo& childInfo2,
112 bool flushContext) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400113 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 Daniel8761e0c2017-07-20 16:36:01 -0400128#ifdef SK_VULKAN
129 if (kVulkan_GrBackend == mainInfo.backend()) {
130 // Initialize the secondary semaphore instead of having Ganesh create one internally
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500131 GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->contextPriv().getGpu());
Greg Daniel8761e0c2017-07-20 16:36:01 -0400132 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 Daniela5cb7812017-06-16 09:45:32 -0400146
Greg Daniel51316782017-08-02 15:10:09 +0000147 if (flushContext) {
148 mainCtx->flushAndSignalSemaphores(2, semaphores.get());
149 } else {
150 mainSurface->flushAndSignalSemaphores(2, semaphores.get());
151 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400152
153 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
Robert Phillipsba375a82018-04-11 10:08:06 -0400154 GrBackendTexture backendTexture = mainImage->getBackendTexture(false);
Greg Daniela5cb7812017-06-16 09:45:32 -0400155
Robert Phillipsba375a82018-04-11 10:08:06 -0400156 draw_child(reporter, childInfo1, backendTexture, semaphores[0]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400157
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 Phillipsba375a82018-04-11 10:08:06 -0400163 GrVkImageInfo vkInfo;
164 SkAssertResult(backendTexture.getVkImageInfo(&vkInfo));
165 vkInfo.updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
Greg Daniela5cb7812017-06-16 09:45:32 -0400166 }
167#endif
168
Robert Phillipsba375a82018-04-11 10:08:06 -0400169 draw_child(reporter, childInfo2, backendTexture, semaphores[1]);
Greg Daniela5cb7812017-06-16 09:45:32 -0400170}
171
Brian Salomondcfca432017-11-15 15:48:03 -0500172DEF_GPUTEST(SurfaceSemaphores, reporter, options) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400173#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 Daniel51316782017-08-02 15:10:09 +0000180 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 Salomondcfca432017-11-15 15:48:03 -0500191 sk_gpu_test::GrContextFactory factory(options);
192 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(
Greg Daniel51316782017-08-02 15:10:09 +0000193 contextType, sk_gpu_test::GrContextFactory::ContextOverrides::kDisableNVPR);
194 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400195 continue;
196 }
Greg Daniel51316782017-08-02 15:10:09 +0000197 skiatest::ReporterContext ctx(
198 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
199 if (ctxInfo.grContext()) {
Brian Salomondcfca432017-11-15 15:48:03 -0500200 sk_gpu_test::ContextInfo child1 =
201 factory.getSharedContextInfo(ctxInfo.grContext(), 0);
202 sk_gpu_test::ContextInfo child2 =
203 factory.getSharedContextInfo(ctxInfo.grContext(), 1);
Greg Daniel51316782017-08-02 15:10:09 +0000204 if (!child1.grContext() || !child2.grContext()) {
205 continue;
206 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000207
Greg Daniel51316782017-08-02 15:10:09 +0000208 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushContext);
209 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400210 }
211 }
212}
213
Greg Danieldba7e7c2017-07-20 15:47:30 -0400214DEF_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 Daniel51316782017-08-02 15:10:09 +0000231 GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore);
232 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Danieldba7e7c2017-07-20 15:47:30 -0400233
234 if (kOpenGL_GrBackend == ctxInfo.backend()) {
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500235 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->contextPriv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400236 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 Phillipsf35fd8d2018-01-22 10:48:15 -0500246 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->contextPriv().getGpu());
Greg Danieldba7e7c2017-07-20 15:47:30 -0400247 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 Wagnerb4aab9a2017-08-16 10:53:04 -0400310 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400311 }
312 }
313#endif
314}
315
Greg Daniela5cb7812017-06-16 09:45:32 -0400316#endif