1 /*
2 * Copyright 2019 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 "tests/Test.h"
9
10 #include <chrono>
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkSurface.h"
13 #include "include/gpu/GrDirectContext.h"
14 #include "src/gpu/GrDirectContextPriv.h"
15 #include "src/gpu/GrGpu.h"
16
17 using namespace sk_gpu_test;
18
testing_finished_proc(void * ctx)19 static void testing_finished_proc(void* ctx) {
20 int* count = (int*)ctx;
21 *count += 1;
22 }
23
busy_wait_for_callback(int * count,int expectedValue,GrDirectContext * dContext,skiatest::Reporter * reporter)24 static void busy_wait_for_callback(int* count, int expectedValue, GrDirectContext* dContext,
25 skiatest::Reporter* reporter) {
26 // Busy waiting should detect that the work is done.
27 auto begin = std::chrono::steady_clock::now();
28 auto end = begin;
29 do {
30 dContext->checkAsyncWorkCompletion();
31 end = std::chrono::steady_clock::now();
32 } while (*count != expectedValue && (end - begin) < std::chrono::seconds(1));
33 if (*count != expectedValue) {
34 ERRORF(reporter, "Expected count failed to reach %d within 1 second of busy waiting.",
35 expectedValue);
36 }
37 }
38
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FlushFinishedProcTest,reporter,ctxInfo)39 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FlushFinishedProcTest, reporter, ctxInfo) {
40 auto dContext = ctxInfo.directContext();
41
42 SkImageInfo info =
43 SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
44 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
45 SkCanvas* canvas = surface->getCanvas();
46
47 canvas->clear(SK_ColorGREEN);
48 auto image = surface->makeImageSnapshot();
49
50 dContext->flush();
51 dContext->submit(true);
52
53 int count = 0;
54
55 GrFlushInfo flushInfoFinishedProc;
56 flushInfoFinishedProc.fFinishedProc = testing_finished_proc;
57 flushInfoFinishedProc.fFinishedContext = (void*)&count;
58 // There is no work on the surface so flushing may immediately call the finished proc.
59 surface->flush(flushInfoFinishedProc);
60 dContext->submit();
61 REPORTER_ASSERT(reporter, count == 0 || count == 1);
62 // Busy waiting should detect that the work is done.
63 busy_wait_for_callback(&count, 1, dContext, reporter);
64
65 canvas->clear(SK_ColorRED);
66
67 surface->flush(flushInfoFinishedProc);
68 dContext->submit();
69
70 bool fenceSupport = dContext->priv().caps()->fenceSyncSupport();
71 bool expectAsyncCallback =
72 dContext->backend() == GrBackendApi::kVulkan ||
73 ((dContext->backend() == GrBackendApi::kOpenGL) && fenceSupport) ||
74 ((dContext->backend() == GrBackendApi::kMetal) && fenceSupport) ||
75 dContext->backend() == GrBackendApi::kDawn ||
76 dContext->backend() == GrBackendApi::kDirect3D;
77 if (expectAsyncCallback) {
78 // On Vulkan the command buffer we just submitted may or may not have finished immediately
79 // so the finish proc may not have been called.
80 REPORTER_ASSERT(reporter, count == 1 || count == 2);
81 } else {
82 REPORTER_ASSERT(reporter, count == 2);
83 }
84 dContext->flush();
85 dContext->submit(true);
86 REPORTER_ASSERT(reporter, count == 2);
87
88 // Test flushing via the SkImage
89 canvas->drawImage(image, 0, 0);
90 image->flush(dContext, flushInfoFinishedProc);
91 dContext->submit();
92 if (expectAsyncCallback) {
93 // On Vulkan the command buffer we just submitted may or may not have finished immediately
94 // so the finish proc may not have been called.
95 REPORTER_ASSERT(reporter, count == 2 || count == 3);
96 } else {
97 REPORTER_ASSERT(reporter, count == 3);
98 }
99 dContext->flush();
100 dContext->submit(true);
101 REPORTER_ASSERT(reporter, count == 3);
102
103 // Test flushing via the GrDirectContext
104 canvas->clear(SK_ColorBLUE);
105 dContext->flush(flushInfoFinishedProc);
106 dContext->submit();
107 if (expectAsyncCallback) {
108 // On Vulkan the command buffer we just submitted may or may not have finished immediately
109 // so the finish proc may not have been called.
110 REPORTER_ASSERT(reporter, count == 3 || count == 4);
111 } else {
112 REPORTER_ASSERT(reporter, count == 4);
113 }
114 dContext->flush();
115 dContext->submit(true);
116 REPORTER_ASSERT(reporter, count == 4);
117
118 // There is no work on the surface so flushing may immediately call the finished proc.
119 dContext->flush(flushInfoFinishedProc);
120 dContext->submit();
121 REPORTER_ASSERT(reporter, count == 4 || count == 5);
122 busy_wait_for_callback(&count, 5, dContext, reporter);
123
124 count = 0;
125 int count2 = 0;
126 canvas->clear(SK_ColorGREEN);
127 surface->flush(flushInfoFinishedProc);
128 dContext->submit();
129 // There is no work to be flushed here so this will return immediately, but make sure the
130 // finished call from this proc isn't called till the previous surface flush also is finished.
131 flushInfoFinishedProc.fFinishedContext = (void*)&count2;
132 dContext->flush(flushInfoFinishedProc);
133 dContext->submit();
134 REPORTER_ASSERT(reporter, count <= 1 && count2 <= count);
135
136 dContext->flush();
137 dContext->submit(true);
138
139 REPORTER_ASSERT(reporter, count == 1);
140 REPORTER_ASSERT(reporter, count == count2);
141 }
142
143