blob: 09d3c359001441eb5084abbfb917d703efa17f2a [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,
70 backendImage);
71
72 childInfo.testContext()->makeCurrent();
73
74 const SkImageInfo childII = SkImageInfo::Make(CHILD_W, CHILD_H, kRGBA_8888_SkColorType,
75 kPremul_SkAlphaType);
76
77 GrContext* childCtx = childInfo.grContext();
78 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(childCtx, SkBudgeted::kNo,
79 childII, 0, kTopLeft_GrSurfaceOrigin,
80 nullptr));
81
82 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(childCtx,
83 backendTexture,
84 kTopLeft_GrSurfaceOrigin,
85 kPremul_SkAlphaType,
86 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
131 GrVkGpu* gpu = static_cast<GrVkGpu*>(mainCtx->getGpu());
132 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();
154 GrBackendObject backendImage = mainImage->getTextureHandle(false);
155
156 draw_child(reporter, childInfo1, backendImage, semaphores[0]);
157
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.
163 GrVkImageInfo* vkInfo = (GrVkImageInfo*)backendImage;
164 vkInfo->updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
165 }
166#endif
167
168 draw_child(reporter, childInfo2, backendImage, semaphores[1]);
169}
170
171DEF_GPUTEST(SurfaceSemaphores, reporter, factory) {
172#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
173 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
174#else
175 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
176#endif
177
178 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
Greg Daniel51316782017-08-02 15:10:09 +0000179 for (auto flushContext : { false, true }) {
180 sk_gpu_test::GrContextFactory::ContextType contextType =
181 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
182 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
183 // desktop since tests do not account for not fixing http://skbug.com/2809
184 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
185 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
186 if (contextType != kNativeGLType) {
187 continue;
188 }
189 }
190 sk_gpu_test::ContextInfo ctxInfo = factory->getContextInfo(
191 contextType, sk_gpu_test::GrContextFactory::ContextOverrides::kDisableNVPR);
192 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
Greg Daniela5cb7812017-06-16 09:45:32 -0400193 continue;
194 }
Greg Daniel51316782017-08-02 15:10:09 +0000195 skiatest::ReporterContext ctx(
196 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
197 if (ctxInfo.grContext()) {
198 sk_gpu_test::ContextInfo child1 = factory->getSharedContextInfo(ctxInfo.grContext(),
199 0);
200 sk_gpu_test::ContextInfo child2 = factory->getSharedContextInfo(ctxInfo.grContext(),
201 1);
202 if (!child1.grContext() || !child2.grContext()) {
203 continue;
204 }
Yuqian Licc8eb602017-08-01 17:43:30 +0000205
Greg Daniel51316782017-08-02 15:10:09 +0000206 surface_semaphore_test(reporter, ctxInfo, child1, child2, flushContext);
207 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400208 }
209 }
210}
211
Greg Danieldba7e7c2017-07-20 15:47:30 -0400212DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
213 GrContext* ctx = ctxInfo.grContext();
214 if (!ctx->caps()->fenceSyncSupport()) {
215 return;
216 }
217
218 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
219 kPremul_SkAlphaType);
220
221 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
222 ii, 0, kTopLeft_GrSurfaceOrigin,
223 nullptr));
224
225 // Flush surface once without semaphores to make sure there is no peneding IO for it.
226 mainSurface->flush();
227
228 GrBackendSemaphore semaphore;
Greg Daniel51316782017-08-02 15:10:09 +0000229 GrSemaphoresSubmitted submitted = mainSurface->flushAndSignalSemaphores(1, &semaphore);
230 REPORTER_ASSERT(reporter, GrSemaphoresSubmitted::kYes == submitted);
Greg Danieldba7e7c2017-07-20 15:47:30 -0400231
232 if (kOpenGL_GrBackend == ctxInfo.backend()) {
233 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->getGpu());
234 const GrGLInterface* interface = gpu->glInterface();
235 GrGLsync sync = semaphore.glSync();
236 REPORTER_ASSERT(reporter, sync);
237 bool result;
238 GR_GL_CALL_RET(interface, result, IsSync(sync));
239 REPORTER_ASSERT(reporter, result);
240 }
241
242#ifdef SK_VULKAN
243 if (kVulkan_GrBackend == ctxInfo.backend()) {
244 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->getGpu());
245 const GrVkInterface* interface = gpu->vkInterface();
246 VkDevice device = gpu->device();
247 VkQueue queue = gpu->queue();
248 VkCommandPool cmdPool = gpu->cmdPool();
249 VkCommandBuffer cmdBuffer;
250
251 // Create Command Buffer
252 const VkCommandBufferAllocateInfo cmdInfo = {
253 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
254 nullptr, // pNext
255 cmdPool, // commandPool
256 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
257 1 // bufferCount
258 };
259
260 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
261 if (err) {
262 return;
263 }
264
265 VkCommandBufferBeginInfo cmdBufferBeginInfo;
266 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
267 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
268 cmdBufferBeginInfo.pNext = nullptr;
269 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
270 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
271
272 GR_VK_CALL_ERRCHECK(interface, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
273 GR_VK_CALL_ERRCHECK(interface, EndCommandBuffer(cmdBuffer));
274
275 VkFenceCreateInfo fenceInfo;
276 VkFence fence;
277
278 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
279 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
280 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
281 SkASSERT(!err);
282
283 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
284 VkSubmitInfo submitInfo;
285 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
286 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
287 submitInfo.pNext = nullptr;
288 submitInfo.waitSemaphoreCount = 1;
289 VkSemaphore vkSem = semaphore.vkSemaphore();
290 submitInfo.pWaitSemaphores = &vkSem;
291 submitInfo.pWaitDstStageMask = &waitStages;
292 submitInfo.commandBufferCount = 1;
293 submitInfo.pCommandBuffers = &cmdBuffer;
294 submitInfo.signalSemaphoreCount = 0;
295 submitInfo.pSignalSemaphores = nullptr;
296 GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence));
297
298 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
299
300 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
301
302 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
303 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
304 // If the above test fails the wait semaphore will never be signaled which can cause the
305 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
306 // kill things.
307 if (err == VK_TIMEOUT) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400308 SK_ABORT("Waiting on semaphore indefinitely");
Greg Danieldba7e7c2017-07-20 15:47:30 -0400309 }
310 }
311#endif
312}
313
Greg Daniela5cb7812017-06-16 09:45:32 -0400314#endif