blob: 7b22e1cf339ecd2f0bb783e48ab449f1be050150 [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
30 #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);
103 childCanvas->readPixels(bitmap, 0, 0);
104
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,
111 const sk_gpu_test::ContextInfo& childInfo2) {
112 GrContext* mainCtx = mainInfo.grContext();
113 if (!mainCtx->caps()->fenceSyncSupport()) {
114 return;
115 }
116
117 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
118 kPremul_SkAlphaType);
119
120 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(mainCtx, SkBudgeted::kNo,
121 ii, 0, kTopLeft_GrSurfaceOrigin,
122 nullptr));
123 SkCanvas* mainCanvas = mainSurface->getCanvas();
124 mainCanvas->clear(SK_ColorBLUE);
125
126 SkAutoTArray<GrBackendSemaphore> semaphores(2);
127
128 mainSurface->flushAndSignalSemaphores(2, semaphores.get());
129
130 sk_sp<SkImage> mainImage = mainSurface->makeImageSnapshot();
131 GrBackendObject backendImage = mainImage->getTextureHandle(false);
132
133 draw_child(reporter, childInfo1, backendImage, semaphores[0]);
134
135#ifdef SK_VULKAN
136 if (kVulkan_GrBackend == mainInfo.backend()) {
137 // In Vulkan we need to make sure we are sending the correct VkImageLayout in with the
138 // backendImage. After the first child draw the layout gets changed to SHADER_READ, so
139 // we just manually set that here.
140 GrVkImageInfo* vkInfo = (GrVkImageInfo*)backendImage;
141 vkInfo->updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
142 }
143#endif
144
145 draw_child(reporter, childInfo2, backendImage, semaphores[1]);
146}
147
148DEF_GPUTEST(SurfaceSemaphores, reporter, factory) {
149#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
150 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGL_ContextType;
151#else
152 static constexpr auto kNativeGLType = sk_gpu_test::GrContextFactory::kGLES_ContextType;
153#endif
154
155 for (int typeInt = 0; typeInt < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++typeInt) {
156 sk_gpu_test::GrContextFactory::ContextType contextType =
157 (sk_gpu_test::GrContextFactory::ContextType) typeInt;
158 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
159 // desktop since tests do not account for not fixing http://skbug.com/2809
160 if (contextType == sk_gpu_test::GrContextFactory::kGL_ContextType ||
161 contextType == sk_gpu_test::GrContextFactory::kGLES_ContextType) {
162 if (contextType != kNativeGLType) {
163 continue;
164 }
165 }
166 sk_gpu_test::ContextInfo ctxInfo = factory->getContextInfo(
167 contextType, sk_gpu_test::GrContextFactory::ContextOverrides::kDisableNVPR);
168 if (!sk_gpu_test::GrContextFactory::IsRenderingContext(contextType)) {
169 continue;
170 }
171 skiatest::ReporterContext ctx(
172 reporter, SkString(sk_gpu_test::GrContextFactory::ContextTypeName(contextType)));
173 if (ctxInfo.grContext()) {
174 sk_gpu_test::ContextInfo child1 = factory->getSharedContextInfo(ctxInfo.grContext(), 0);
175 sk_gpu_test::ContextInfo child2 = factory->getSharedContextInfo(ctxInfo.grContext(), 1);
176 if (!child1.grContext() || !child2.grContext()) {
177 continue;
178 }
179
180 surface_semaphore_test(reporter, ctxInfo, child1, child2);
181 }
182 }
183}
184
Greg Danieldba7e7c2017-07-20 15:47:30 -0400185DEF_GPUTEST_FOR_RENDERING_CONTEXTS(EmptySurfaceSemaphoreTest, reporter, ctxInfo) {
186 GrContext* ctx = ctxInfo.grContext();
187 if (!ctx->caps()->fenceSyncSupport()) {
188 return;
189 }
190
191 const SkImageInfo ii = SkImageInfo::Make(MAIN_W, MAIN_H, kRGBA_8888_SkColorType,
192 kPremul_SkAlphaType);
193
194 sk_sp<SkSurface> mainSurface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
195 ii, 0, kTopLeft_GrSurfaceOrigin,
196 nullptr));
197
198 // Flush surface once without semaphores to make sure there is no peneding IO for it.
199 mainSurface->flush();
200
201 GrBackendSemaphore semaphore;
202 REPORTER_ASSERT(reporter, mainSurface->flushAndSignalSemaphores(1, &semaphore));
203
204 if (kOpenGL_GrBackend == ctxInfo.backend()) {
205 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctx->getGpu());
206 const GrGLInterface* interface = gpu->glInterface();
207 GrGLsync sync = semaphore.glSync();
208 REPORTER_ASSERT(reporter, sync);
209 bool result;
210 GR_GL_CALL_RET(interface, result, IsSync(sync));
211 REPORTER_ASSERT(reporter, result);
212 }
213
214#ifdef SK_VULKAN
215 if (kVulkan_GrBackend == ctxInfo.backend()) {
216 GrVkGpu* gpu = static_cast<GrVkGpu*>(ctx->getGpu());
217 const GrVkInterface* interface = gpu->vkInterface();
218 VkDevice device = gpu->device();
219 VkQueue queue = gpu->queue();
220 VkCommandPool cmdPool = gpu->cmdPool();
221 VkCommandBuffer cmdBuffer;
222
223 // Create Command Buffer
224 const VkCommandBufferAllocateInfo cmdInfo = {
225 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
226 nullptr, // pNext
227 cmdPool, // commandPool
228 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
229 1 // bufferCount
230 };
231
232 VkResult err = GR_VK_CALL(interface, AllocateCommandBuffers(device, &cmdInfo, &cmdBuffer));
233 if (err) {
234 return;
235 }
236
237 VkCommandBufferBeginInfo cmdBufferBeginInfo;
238 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
239 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
240 cmdBufferBeginInfo.pNext = nullptr;
241 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
242 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
243
244 GR_VK_CALL_ERRCHECK(interface, BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
245 GR_VK_CALL_ERRCHECK(interface, EndCommandBuffer(cmdBuffer));
246
247 VkFenceCreateInfo fenceInfo;
248 VkFence fence;
249
250 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
251 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
252 err = GR_VK_CALL(interface, CreateFence(device, &fenceInfo, nullptr, &fence));
253 SkASSERT(!err);
254
255 VkPipelineStageFlags waitStages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
256 VkSubmitInfo submitInfo;
257 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
258 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
259 submitInfo.pNext = nullptr;
260 submitInfo.waitSemaphoreCount = 1;
261 VkSemaphore vkSem = semaphore.vkSemaphore();
262 submitInfo.pWaitSemaphores = &vkSem;
263 submitInfo.pWaitDstStageMask = &waitStages;
264 submitInfo.commandBufferCount = 1;
265 submitInfo.pCommandBuffers = &cmdBuffer;
266 submitInfo.signalSemaphoreCount = 0;
267 submitInfo.pSignalSemaphores = nullptr;
268 GR_VK_CALL_ERRCHECK(interface, QueueSubmit(queue, 1, &submitInfo, fence));
269
270 err = GR_VK_CALL(interface, WaitForFences(device, 1, &fence, true, 3000000000));
271
272 REPORTER_ASSERT(reporter, err != VK_TIMEOUT);
273
274 GR_VK_CALL(interface, DestroyFence(device, fence, nullptr));
275 GR_VK_CALL(interface, DestroySemaphore(device, vkSem, nullptr));
276 // If the above test fails the wait semaphore will never be signaled which can cause the
277 // device to hang when tearing down (even if just tearing down GL). So we Fail here to
278 // kill things.
279 if (err == VK_TIMEOUT) {
280 SkFAIL("Waiting on semaphore indefinitely");
281 }
282 }
283#endif
284}
285
Greg Daniela5cb7812017-06-16 09:45:32 -0400286#endif