blob: b4f63fec413aba09a4e984e07c5e7988ff43164c [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
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 Danieldba7e7c2017-07-20 15:47:30 -040020#include "gl/GrGLGpu.h"
21#include "gl/GrGLUtil.h"
22
Greg Daniela5cb7812017-06-16 09:45:32 -040023#ifdef SK_VULKAN
Greg Danieldba7e7c2017-07-20 15:47:30 -040024#include "vk/GrVkGpu.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040025#include "vk/GrVkTypes.h"
Greg Danieldba7e7c2017-07-20 15:47:30 -040026#include "vk/GrVkUtil.h"
27
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,
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 Daniel177e6952017-10-12 12:27:11 -040070 GrMipMapped::kNo,
Greg Daniela5cb7812017-06-16 09:45:32 -040071 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,
Greg Danielf5d87582017-12-18 14:48:15 -050086 kRGBA_8888_SkColorType,
Greg Daniela5cb7812017-06-16 09:45:32 -040087 kPremul_SkAlphaType,
Greg Danielf5d87582017-12-18 14:48:15 -050088 nullptr,
89 nullptr,
Greg Daniela5cb7812017-06-16 09:45:32 -040090 nullptr);
91
92 SkCanvas* childCanvas = childSurface->getCanvas();
93 childCanvas->clear(SK_ColorRED);
94
95 childSurface->wait(1, &semaphore);
96
97 childCanvas->drawImage(childImage, CHILD_W/2, 0);
98
99 SkPaint paint;
100 paint.setColor(SK_ColorGREEN);
101 SkIRect rect = SkIRect::MakeLTRB(0, CHILD_H/2, CHILD_W, CHILD_H);
102 childCanvas->drawIRect(rect, paint);
103
104 // read pixels
105 SkBitmap bitmap;
106 bitmap.allocPixels(childII);
Mike Reedf1942192017-07-21 14:24:29 -0400107 childSurface->readPixels(bitmap, 0, 0);
Greg Daniela5cb7812017-06-16 09:45:32 -0400108
109 check_pixels(reporter, bitmap);
110}
111
112void surface_semaphore_test(skiatest::Reporter* reporter,
113 const sk_gpu_test::ContextInfo& mainInfo,
114 const sk_gpu_test::ContextInfo& childInfo1,
Greg Daniel51316782017-08-02 15:10:09 +0000115 const sk_gpu_test::ContextInfo& childInfo2,
116 bool flushContext) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400117 GrContext* mainCtx = mainInfo.grContext();
118 if (!mainCtx->caps()->fenceSyncSupport()) {
119 return;
120 }
121
122 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
123 kPremul_SkAlphaType);
124
125 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(mainCtx, SkBudgeted::kNo,
126 ii, 0, kTopLeft_GrSurfaceOrigin,
127 nullptr));
128 SkCanvas* mainCanvas = mainSurface->getCanvas();
129 mainCanvas->clear(SK_ColorBLUE);
130
131 SkAutoTArray<GrBackendSemaphore> semaphores(2);
Greg Daniel8761e0c2017-07-20 16:36:01 -0400132#ifdef SK_VULKAN
133 if (kVulkan_GrBackend == mainInfo.backend()) {
134 // Initialize the secondary semaphore instead of having Ganesh create one internally
135 GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->getGpu());
136 const GrVkInterface* interface = gpu->vkInterface();
137 VkDevice device = gpu->device();
138
139 VkSemaphore vkSem;
140
141 VkSemaphoreCreateInfo createInfo;
142 createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
143 createInfo.pNext = nullptr;
144 createInfo.flags = 0;
145 GR_VK_CALL_ERRCHECK(interface, CreateSemaphore(device, &createInfo, nullptr, &vkSem));
146
147 semaphores[1].initVulkan(vkSem);
148 }
149#endif
Greg Daniela5cb7812017-06-16 09:45:32 -0400150
Greg Daniel51316782017-08-02 15:10:09 +0000151 if (flushContext) {
152 mainCtx->flushAndSignalSemaphores(2, semaphores.get());
153 } else {
154 mainSurface->flushAndSignalSemaphores(2, semaphores.get());
155 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400156
157 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
158 GrBackendObject backendImage = mainImage->getTextureHandle(false);
159
160 draw_child(reporter, childInfo1, backendImage, semaphores[0]);
161
162#ifdef SK_VULKAN
163 if (kVulkan_GrBackend == mainInfo.backend()) {
164 // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the
165 // backendImage. After the first child draw the layout gets changed to SHADER_READ, so
166 // we just manually set that here.
167 GrVkImageInfo* vkInfo = (GrVkImageInfo*)backendImage;
168 vkInfo->updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
169 }
170#endif
171
172 draw_child(reporter, childInfo2, backendImage, semaphores[1]);
173}
174
Brian Salomondcfca432017-11-15 15:48:03 -0500175DEF_GPUTEST(SurfaceSemaphores, reporter, options) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400176#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
177 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
178#else
179 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
180#endif
181
182 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
Greg Daniel51316782017-08-02 15:10:09 +0000183 for (auto flushContext : { false, true }) {
184 sk_gpu_test::GrContextFactory::ContextType contextType =
185 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
186 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
187 // desktop since tests do not account for not fixing http://skbug.com/2809
188 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
189 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
190 if (contextType != kNativeGLType) {
191 continue;
192 }
193 }
Brian Salomondcfca432017-11-15 15:48:03 -0500194 sk_gpu_test::GrContextFactory factory(options);
195 sk_gpu_test::ContextInfo ctxInfo = factory.getContextInfo(
Greg Daniel51316782017-08-02 15:10:09 +0000196 contextType, sk_gpu_test::GrContextFactory::ContextOverrides::kDisableNVPR);
197 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400198 continue;
199 }
Greg Daniel51316782017-08-02 15:10:09 +0000200 skiatest::ReporterContext ctx(
201 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
202 if (ctxInfo.grContext()) {
Brian Salomondcfca432017-11-15 15:48:03 -0500203 sk_gpu_test::ContextInfo child1 =
204 factory.getSharedContextInfo(ctxInfo.grContext(), 0);
205 sk_gpu_test::ContextInfo child2 =
206 factory.getSharedContextInfo(ctxInfo.grContext(), 1);
Greg Daniel51316782017-08-02 15:10:09 +0000207 if (!child1.grContext() || !child2.grContext()) {
208 continue;
209 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000210
Greg Daniel51316782017-08-02 15:10:09 +0000211 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushContext);
212 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400213 }
214 }
215}
216
Greg Danieldba7e7c2017-07-20 15:47:30 -0400217DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
218 GrContext* ctx = ctxInfo.grContext();
219 if (!ctx->caps()->fenceSyncSupport()) {
220 return;
221 }
222
223 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
224 kPremul_SkAlphaType);
225
226 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
227 ii, 0, kTopLeft_GrSurfaceOrigin,
228 nullptr));
229
230 // Flush surface once without semaphores to make sure there is no peneding IO for it.
231 mainSurface->flush();
232
233 GrBackendSemaphore semaphore;
Greg Daniel51316782017-08-02 15:10:09 +0000234 GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore);
235 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Danieldba7e7c2017-07-20 15:47:30 -0400236
237 if (kOpenGL_GrBackend == ctxInfo.backend()) {
238 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->getGpu());
239 const GrGLInterface* interface = gpu->glInterface();
240 GrGLsync sync = semaphore.glSync();
241 REPORTER_ASSERT(reporter, sync);
242 bool result;
243 GR_GL_CALL_RET(interface, result, IsSync(sync));
244 REPORTER_ASSERT(reporter, result);
245 }
246
247#ifdef SK_VULKAN
248 if (kVulkan_GrBackend == ctxInfo.backend()) {
249 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->getGpu());
250 const GrVkInterface* interface = gpu->vkInterface();
251 VkDevice device = gpu->device();
252 VkQueue queue = gpu->queue();
253 VkCommandPool cmdPool = gpu->cmdPool();
254 VkCommandBuffer cmdBuffer;
255
256 // Create Command Buffer
257 const VkCommandBufferAllocateInfo cmdInfo = {
258 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
259 nullptr, // pNext
260 cmdPool, // commandPool
261 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
262 1 // bufferCount
263 };
264
265 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
266 if (err) {
267 return;
268 }
269
270 VkCommandBufferBeginInfo cmdBufferBeginInfo;
271 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
272 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
273 cmdBufferBeginInfo.pNext = nullptr;
274 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
275 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
276
277 GR_VK_CALL_ERRCHECK(interface, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
278 GR_VK_CALL_ERRCHECK(interface, EndCommandBuffer(cmdBuffer));
279
280 VkFenceCreateInfo fenceInfo;
281 VkFence fence;
282
283 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
284 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
285 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
286 SkASSERT(!err);
287
288 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
289 VkSubmitInfo submitInfo;
290 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
291 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
292 submitInfo.pNext = nullptr;
293 submitInfo.waitSemaphoreCount = 1;
294 VkSemaphore vkSem = semaphore.vkSemaphore();
295 submitInfo.pWaitSemaphores = &vkSem;
296 submitInfo.pWaitDstStageMask = &waitStages;
297 submitInfo.commandBufferCount = 1;
298 submitInfo.pCommandBuffers = &cmdBuffer;
299 submitInfo.signalSemaphoreCount = 0;
300 submitInfo.pSignalSemaphores = nullptr;
301 GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence));
302
303 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
304
305 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
306
307 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
308 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
309 // If the above test fails the wait semaphore will never be signaled which can cause the
310 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
311 // kill things.
312 if (err == VK_TIMEOUT) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400313 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400314 }
315 }
316#endif
317}
318
Greg Daniela5cb7812017-06-16 09:45:32 -0400319#endif