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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 8 | #include "tests/Test.h" |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 10 | #include "include/core/SkSurface.h" |
| 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrGpu.h" |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 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(); |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 33 | GrFlushInfo flushInfoSyncCpu; |
| 34 | flushInfoSyncCpu.fFlags = kSyncCpu_GrFlushFlag; |
| 35 | ctx->flush(flushInfoSyncCpu); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 36 | |
| 37 | int count = 0; |
| 38 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 39 | GrFlushInfo flushInfoFinishedProc; |
| 40 | flushInfoFinishedProc.fFinishedProc = testing_finished_proc; |
| 41 | flushInfoFinishedProc.fFinishedContext = (void*)&count; |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 42 | // There is no work on the surface so flushing should immediately call the finished proc. |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 43 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 44 | |
| 45 | REPORTER_ASSERT(reporter, count == 1); |
| 46 | |
| 47 | canvas->clear(SK_ColorRED); |
| 48 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 49 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 50 | |
| 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 Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 59 | ctx->flush(flushInfoSyncCpu); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, count == 2); |
| 61 | |
| 62 | // Test flushing via the GrContext |
| 63 | canvas->clear(SK_ColorBLUE); |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 64 | ctx->flush(flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 65 | 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 Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 72 | ctx->flush(flushInfoSyncCpu); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, count == 3); |
| 74 | |
| 75 | // There is no work on the surface so flushing should immediately call the finished proc. |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 76 | ctx->flush(flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, count == 4); |
| 78 | |
| 79 | count = 0; |
| 80 | int count2 = 0; |
| 81 | canvas->clear(SK_ColorGREEN); |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 82 | surface->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 83 | // 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 Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 85 | flushInfoFinishedProc.fFinishedContext = (void*)&count2; |
| 86 | ctx->flush(flushInfoFinishedProc); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 87 | |
| 88 | REPORTER_ASSERT(reporter, count == count2); |
| 89 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 90 | ctx->flush(flushInfoSyncCpu); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 91 | |
| 92 | REPORTER_ASSERT(reporter, count == 1); |
| 93 | REPORTER_ASSERT(reporter, count == count2); |
| 94 | } |
| 95 | |