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