blob: 481c06d2bb53f6860c98ae00bdfa5ef0ea931413 [file] [log] [blame]
Greg Daniela3aa75a2019-04-12 14:24:55 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Greg Daniela3aa75a2019-04-12 14:24:55 -04009
Brian Salomonb0d8b762019-05-06 16:58:22 -040010#include <chrono>
Robert Phillipse19babf2020-04-06 13:57:30 -040011#include "include/core/SkCanvas.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040013#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrGpu.h"
Greg Daniela3aa75a2019-04-12 14:24:55 -040016
17using namespace sk_gpu_test;
18
19static void testing_finished_proc(void* ctx) {
20 int* count = (int*)ctx;
21 *count += 1;
22}
23
Robert Phillipsc8ae4942020-07-20 10:56:01 -040024static void busy_wait_for_callback(int* count, int expectedValue, GrDirectContext* dContext,
Brian Salomonb0d8b762019-05-06 16:58:22 -040025 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 {
Robert Phillipsc8ae4942020-07-20 10:56:01 -040030 dContext->checkAsyncWorkCompletion();
Brian Salomonb0d8b762019-05-06 16:58:22 -040031 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
Greg Daniela3aa75a2019-04-12 14:24:55 -040039DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FlushFinishedProcTest, reporter, ctxInfo) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -040040 auto dContext = ctxInfo.directContext();
Greg Daniela3aa75a2019-04-12 14:24:55 -040041
42 SkImageInfo info =
43 SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsc8ae4942020-07-20 10:56:01 -040044 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Greg Daniela3aa75a2019-04-12 14:24:55 -040045 SkCanvas* canvas = surface->getCanvas();
46
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040047 canvas->clear(SK_ColorGREEN);
48 auto image = surface->makeImageSnapshot();
49
Robert Phillipsc8ae4942020-07-20 10:56:01 -040050 dContext->flush();
51 dContext->submit(true);
Greg Daniela3aa75a2019-04-12 14:24:55 -040052
53 int count = 0;
54
Greg Daniele6bfb7d2019-04-17 15:26:11 -040055 GrFlushInfo flushInfoFinishedProc;
56 flushInfoFinishedProc.fFinishedProc = testing_finished_proc;
57 flushInfoFinishedProc.fFinishedContext = (void*)&count;
Brian Salomonb0d8b762019-05-06 16:58:22 -040058 // There is no work on the surface so flushing may immediately call the finished proc.
Greg Danielce9f0162020-06-30 13:42:46 -040059 surface->flush(flushInfoFinishedProc);
Robert Phillipsc8ae4942020-07-20 10:56:01 -040060 dContext->submit();
Brian Salomonb0d8b762019-05-06 16:58:22 -040061 REPORTER_ASSERT(reporter, count == 0 || count == 1);
62 // Busy waiting should detect that the work is done.
Robert Phillipsc8ae4942020-07-20 10:56:01 -040063 busy_wait_for_callback(&count, 1, dContext, reporter);
Greg Daniela3aa75a2019-04-12 14:24:55 -040064
65 canvas->clear(SK_ColorRED);
66
Greg Danielce9f0162020-06-30 13:42:46 -040067 surface->flush(flushInfoFinishedProc);
Robert Phillipsc8ae4942020-07-20 10:56:01 -040068 dContext->submit();
Greg Daniela3aa75a2019-04-12 14:24:55 -040069
Robert Phillipsc8ae4942020-07-20 10:56:01 -040070 bool fenceSupport = dContext->priv().caps()->fenceSyncSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -040071 bool expectAsyncCallback =
Robert Phillipsc8ae4942020-07-20 10:56:01 -040072 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;
Brian Salomonb0d8b762019-05-06 16:58:22 -040077 if (expectAsyncCallback) {
Greg Daniela3aa75a2019-04-12 14:24:55 -040078 // 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 }
Robert Phillipsc8ae4942020-07-20 10:56:01 -040084 dContext->flush();
85 dContext->submit(true);
Greg Daniela3aa75a2019-04-12 14:24:55 -040086 REPORTER_ASSERT(reporter, count == 2);
87
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040088 // Test flushing via the SkImage
89 canvas->drawImage(image, 0, 0);
Robert Phillipsc8ae4942020-07-20 10:56:01 -040090 image->flush(dContext, flushInfoFinishedProc);
91 dContext->submit();
Brian Salomonb0d8b762019-05-06 16:58:22 -040092 if (expectAsyncCallback) {
Greg Daniela3aa75a2019-04-12 14:24:55 -040093 // 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 }
Robert Phillipsc8ae4942020-07-20 10:56:01 -040099 dContext->flush();
100 dContext->submit(true);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400101 REPORTER_ASSERT(reporter, count == 3);
102
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400103 // Test flushing via the GrDirectContext
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400104 canvas->clear(SK_ColorBLUE);
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400105 dContext->flush(flushInfoFinishedProc);
106 dContext->submit();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400107 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 }
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400114 dContext->flush();
115 dContext->submit(true);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400116 REPORTER_ASSERT(reporter, count == 4);
117
Brian Salomonb0d8b762019-05-06 16:58:22 -0400118 // There is no work on the surface so flushing may immediately call the finished proc.
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400119 dContext->flush(flushInfoFinishedProc);
120 dContext->submit();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400121 REPORTER_ASSERT(reporter, count == 4 || count == 5);
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400122 busy_wait_for_callback(&count, 5, dContext, reporter);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400123
124 count = 0;
125 int count2 = 0;
126 canvas->clear(SK_ColorGREEN);
Greg Danielce9f0162020-06-30 13:42:46 -0400127 surface->flush(flushInfoFinishedProc);
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400128 dContext->submit();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400129 // 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.
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400131 flushInfoFinishedProc.fFinishedContext = (void*)&count2;
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400132 dContext->flush(flushInfoFinishedProc);
133 dContext->submit();
Brian Salomonb0d8b762019-05-06 16:58:22 -0400134 REPORTER_ASSERT(reporter, count <= 1 && count2 <= count);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400135
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400136 dContext->flush();
137 dContext->submit(true);
Greg Daniela3aa75a2019-04-12 14:24:55 -0400138
139 REPORTER_ASSERT(reporter, count == 1);
140 REPORTER_ASSERT(reporter, count == count2);
141}
142