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