Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 8 | #include "Test.h" |
| 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "SkSurface.h" |
| 14 | |
| 15 | using namespace sk_gpu_test; |
| 16 | |
| 17 | static void testing_finished_proc(void* ctx) { |
| 18 | int* count = (int*)ctx; |
| 19 | *count += 1; |
| 20 | } |
| 21 | |
| 22 | DEF_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(); |
| 33 | ctx->flush(kSyncCpu_GrFlushFlag, 0, nullptr); |
| 34 | |
| 35 | int count = 0; |
| 36 | |
| 37 | // There is no work on the surface so flushing should immediately call the finished proc. |
| 38 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, kNone_GrFlushFlags, 0, nullptr, |
| 39 | testing_finished_proc, (void*)&count); |
| 40 | |
| 41 | REPORTER_ASSERT(reporter, count == 1); |
| 42 | |
| 43 | canvas->clear(SK_ColorRED); |
| 44 | |
| 45 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, kNone_GrFlushFlags, 0, nullptr, |
| 46 | testing_finished_proc, (void*)&count); |
| 47 | |
| 48 | bool isVulkan = ctx->backend() == GrBackendApi::kVulkan; |
| 49 | if (isVulkan) { |
| 50 | // On Vulkan the command buffer we just submitted may or may not have finished immediately |
| 51 | // so the finish proc may not have been called. |
| 52 | REPORTER_ASSERT(reporter, count == 1 || count == 2); |
| 53 | } else { |
| 54 | REPORTER_ASSERT(reporter, count == 2); |
| 55 | } |
| 56 | ctx->flush(kSyncCpu_GrFlushFlag, 0, nullptr); |
| 57 | REPORTER_ASSERT(reporter, count == 2); |
| 58 | |
| 59 | // Test flushing via the GrContext |
| 60 | canvas->clear(SK_ColorBLUE); |
| 61 | ctx->flush(kNone_GrFlushFlags, 0, nullptr, testing_finished_proc, (void*)&count); |
| 62 | if (isVulkan) { |
| 63 | // On Vulkan the command buffer we just submitted may or may not have finished immediately |
| 64 | // so the finish proc may not have been called. |
| 65 | REPORTER_ASSERT(reporter, count == 2 || count == 3); |
| 66 | } else { |
| 67 | REPORTER_ASSERT(reporter, count == 3); |
| 68 | } |
| 69 | ctx->flush(kSyncCpu_GrFlushFlag, 0, nullptr); |
| 70 | REPORTER_ASSERT(reporter, count == 3); |
| 71 | |
| 72 | // There is no work on the surface so flushing should immediately call the finished proc. |
| 73 | ctx->flush(kNone_GrFlushFlags, 0, nullptr, testing_finished_proc, (void*)&count); |
| 74 | REPORTER_ASSERT(reporter, count == 4); |
| 75 | |
| 76 | count = 0; |
| 77 | int count2 = 0; |
| 78 | canvas->clear(SK_ColorGREEN); |
| 79 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, kNone_GrFlushFlags, 0, nullptr, |
| 80 | testing_finished_proc, (void*)&count); |
| 81 | // There is no work to be flushed here so this will return immediately, but make sure the |
| 82 | // finished call from this proc isn't called till the previous surface flush also is finished. |
| 83 | ctx->flush(kNone_GrFlushFlags, 0, nullptr, testing_finished_proc, (void*)&count2); |
| 84 | |
| 85 | REPORTER_ASSERT(reporter, count == count2); |
| 86 | |
| 87 | ctx->flush(kSyncCpu_GrFlushFlag, 0, nullptr); |
| 88 | |
| 89 | REPORTER_ASSERT(reporter, count == 1); |
| 90 | REPORTER_ASSERT(reporter, count == count2); |
| 91 | } |
| 92 | |