1 /*
2 * Copyright 2020 Google LLC
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 "include/core/SkAlphaType.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSurface.h"
15 #include "include/core/SkTypes.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/ganesh/GrDirectContext.h"
18 #include "include/gpu/ganesh/GrTypes.h"
19 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
20 #include "tests/CtsEnforcement.h"
21 #include "tests/Test.h"
22 #include "tools/gpu/FenceSync.h"
23
24 struct GrContextOptions;
25
26 using namespace sk_gpu_test;
27
28 namespace {
29 struct SubmittedInfo {
30 int* fCount;
31 bool* fSuccess;
32 };
33 } // namespace
34
testing_submitted_proc(void * ctx,bool success)35 static void testing_submitted_proc(void* ctx, bool success) {
36 SubmittedInfo* info = (SubmittedInfo*)ctx;
37 *info->fCount += 1;
38 *info->fSuccess = success;
39 }
40
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(FlushSubmittedProcTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)41 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(FlushSubmittedProcTest,
42 reporter,
43 ctxInfo,
44 CtsEnforcement::kApiLevel_T) {
45 auto ctx = ctxInfo.directContext();
46
47 SkImageInfo info = SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
48 sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info);
49 SkCanvas* canvas = surface->getCanvas();
50
51 canvas->clear(SK_ColorGREEN);
52
53 int submittedCount = 0;
54 bool submittedSuccess = false;
55 SubmittedInfo submittedInfo = { &submittedCount, &submittedSuccess };
56
57 GrFlushInfo flushInfo;
58 flushInfo.fSubmittedProc = testing_submitted_proc;
59 flushInfo.fSubmittedContext = &submittedInfo;
60
61 ctx->flush(flushInfo);
62 REPORTER_ASSERT(reporter, submittedCount == 0);
63
64 ctx->submit();
65 REPORTER_ASSERT(reporter, submittedCount == 1);
66 REPORTER_ASSERT(reporter, submittedSuccess);
67
68 // There should be no work so if we flush again the submittedProc should be called immediately
69 ctx->flush(surface.get(), SkSurfaces::BackendSurfaceAccess::kNoAccess, flushInfo);
70 REPORTER_ASSERT(reporter, submittedCount == 2);
71 REPORTER_ASSERT(reporter, submittedSuccess);
72
73 // However, flushing the context we don't do any checks of work so we still require submit to be
74 // called in order for the callback to trigger.
75 ctx->flush(flushInfo);
76 REPORTER_ASSERT(reporter, submittedCount == 2);
77
78 ctx->submit();
79 REPORTER_ASSERT(reporter, submittedCount == 3);
80 REPORTER_ASSERT(reporter, submittedSuccess);
81
82 // Testing that doing multiple flushes before a submit triggers both submittedProcs to be called
83 canvas->clear(SK_ColorBLUE);
84 ctx->flush(surface.get(), SkSurfaces::BackendSurfaceAccess::kNoAccess, flushInfo);
85 REPORTER_ASSERT(reporter, submittedCount == 3);
86 canvas->clear(SK_ColorRED);
87 ctx->flush(surface.get(), SkSurfaces::BackendSurfaceAccess::kNoAccess, flushInfo);
88 REPORTER_ASSERT(reporter, submittedCount == 3);
89 ctx->submit();
90
91 REPORTER_ASSERT(reporter, submittedCount == 5);
92 REPORTER_ASSERT(reporter, submittedSuccess);
93
94 // Test an abandoned context to get a failed submit immediately when flush is called
95 canvas->clear(SK_ColorCYAN);
96 ctx->abandonContext();
97 ctx->flush(surface.get(), SkSurfaces::BackendSurfaceAccess::kNoAccess, flushInfo);
98 REPORTER_ASSERT(reporter, submittedCount == 6);
99 REPORTER_ASSERT(reporter, !submittedSuccess);
100 ctx->flush(flushInfo);
101 REPORTER_ASSERT(reporter, submittedCount == 7);
102 REPORTER_ASSERT(reporter, !submittedSuccess);
103 }
104