blob: f676b06c8f28708fdfaba32740641f42d582dad2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkSurface.h"
11#include "include/gpu/GrContext.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrGpu.h"
Greg Daniela3aa75a2019-04-12 14:24:55 -040014
15using namespace sk_gpu_test;
16
17static void testing_finished_proc(void* ctx) {
18 int* count = (int*)ctx;
19 *count += 1;
20}
21
22DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FlushFinishedProcTest, reporter, ctxInfo) {
23 GrContext* ctx = ctxInfo.grContext();
24
25 SkImageInfo info =
26 SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
27 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
28 SkCanvas* canvas = surface->getCanvas();
29
30 // We flush the surface first just to get rid of any discards/clears that got recorded from
31 // making the surface.
32 surface->flush();
Greg Daniele6bfb7d2019-04-17 15:26:11 -040033 GrFlushInfo flushInfoSyncCpu;
34 flushInfoSyncCpu.fFlags = kSyncCpu_GrFlushFlag;
35 ctx->flush(flushInfoSyncCpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -040036
37 int count = 0;
38
Greg Daniele6bfb7d2019-04-17 15:26:11 -040039 GrFlushInfo flushInfoFinishedProc;
40 flushInfoFinishedProc.fFinishedProc = testing_finished_proc;
41 flushInfoFinishedProc.fFinishedContext = (void*)&count;
Greg Daniela3aa75a2019-04-12 14:24:55 -040042 // There is no work on the surface so flushing should immediately call the finished proc.
Greg Daniele6bfb7d2019-04-17 15:26:11 -040043 surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040044
45 REPORTER_ASSERT(reporter, count == 1);
46
47 canvas->clear(SK_ColorRED);
48
Greg Daniele6bfb7d2019-04-17 15:26:11 -040049 surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040050
51 bool isVulkan = ctx->backend() == GrBackendApi::kVulkan;
52 if (isVulkan) {
53 // On Vulkan the command buffer we just submitted may or may not have finished immediately
54 // so the finish proc may not have been called.
55 REPORTER_ASSERT(reporter, count == 1 || count == 2);
56 } else {
57 REPORTER_ASSERT(reporter, count == 2);
58 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040059 ctx->flush(flushInfoSyncCpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -040060 REPORTER_ASSERT(reporter, count == 2);
61
62 // Test flushing via the GrContext
63 canvas->clear(SK_ColorBLUE);
Greg Daniele6bfb7d2019-04-17 15:26:11 -040064 ctx->flush(flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040065 if (isVulkan) {
66 // On Vulkan the command buffer we just submitted may or may not have finished immediately
67 // so the finish proc may not have been called.
68 REPORTER_ASSERT(reporter, count == 2 || count == 3);
69 } else {
70 REPORTER_ASSERT(reporter, count == 3);
71 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -040072 ctx->flush(flushInfoSyncCpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -040073 REPORTER_ASSERT(reporter, count == 3);
74
75 // There is no work on the surface so flushing should immediately call the finished proc.
Greg Daniele6bfb7d2019-04-17 15:26:11 -040076 ctx->flush(flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040077 REPORTER_ASSERT(reporter, count == 4);
78
79 count = 0;
80 int count2 = 0;
81 canvas->clear(SK_ColorGREEN);
Greg Daniele6bfb7d2019-04-17 15:26:11 -040082 surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040083 // There is no work to be flushed here so this will return immediately, but make sure the
84 // finished call from this proc isn't called till the previous surface flush also is finished.
Greg Daniele6bfb7d2019-04-17 15:26:11 -040085 flushInfoFinishedProc.fFinishedContext = (void*)&count2;
86 ctx->flush(flushInfoFinishedProc);
Greg Daniela3aa75a2019-04-12 14:24:55 -040087
88 REPORTER_ASSERT(reporter, count == count2);
89
Greg Daniele6bfb7d2019-04-17 15:26:11 -040090 ctx->flush(flushInfoSyncCpu);
Greg Daniela3aa75a2019-04-12 14:24:55 -040091
92 REPORTER_ASSERT(reporter, count == 1);
93 REPORTER_ASSERT(reporter, count == count2);
94}
95