2019-04-12 18:24:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2019-04-12 18:24:55 +00:00
|
|
|
|
2019-05-06 20:58:22 +00:00
|
|
|
#include <chrono>
|
2020-04-06 17:57:30 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrGpu.h"
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
using namespace sk_gpu_test;
|
|
|
|
|
|
|
|
static void testing_finished_proc(void* ctx) {
|
|
|
|
int* count = (int*)ctx;
|
|
|
|
*count += 1;
|
|
|
|
}
|
|
|
|
|
2020-07-20 14:56:01 +00:00
|
|
|
static void busy_wait_for_callback(int* count, int expectedValue, GrDirectContext* dContext,
|
2019-05-06 20:58:22 +00:00
|
|
|
skiatest::Reporter* reporter) {
|
|
|
|
// Busy waiting should detect that the work is done.
|
|
|
|
auto begin = std::chrono::steady_clock::now();
|
|
|
|
auto end = begin;
|
|
|
|
do {
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->checkAsyncWorkCompletion();
|
2019-05-06 20:58:22 +00:00
|
|
|
end = std::chrono::steady_clock::now();
|
|
|
|
} while (*count != expectedValue && (end - begin) < std::chrono::seconds(1));
|
|
|
|
if (*count != expectedValue) {
|
|
|
|
ERRORF(reporter, "Expected count failed to reach %d within 1 second of busy waiting.",
|
|
|
|
expectedValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:24:55 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FlushFinishedProcTest, reporter, ctxInfo) {
|
2020-07-20 14:56:01 +00:00
|
|
|
auto dContext = ctxInfo.directContext();
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
SkImageInfo info =
|
|
|
|
SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
2020-07-20 14:56:01 +00:00
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
|
2019-04-12 18:24:55 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2019-05-09 14:30:12 +00:00
|
|
|
canvas->clear(SK_ColorGREEN);
|
|
|
|
auto image = surface->makeImageSnapshot();
|
|
|
|
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
2019-04-17 19:26:11 +00:00
|
|
|
GrFlushInfo flushInfoFinishedProc;
|
|
|
|
flushInfoFinishedProc.fFinishedProc = testing_finished_proc;
|
|
|
|
flushInfoFinishedProc.fFinishedContext = (void*)&count;
|
2019-05-06 20:58:22 +00:00
|
|
|
// There is no work on the surface so flushing may immediately call the finished proc.
|
2020-06-30 17:42:46 +00:00
|
|
|
surface->flush(flushInfoFinishedProc);
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->submit();
|
2019-05-06 20:58:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 0 || count == 1);
|
|
|
|
// Busy waiting should detect that the work is done.
|
2020-07-20 14:56:01 +00:00
|
|
|
busy_wait_for_callback(&count, 1, dContext, reporter);
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
|
2020-06-30 17:42:46 +00:00
|
|
|
surface->flush(flushInfoFinishedProc);
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->submit();
|
2019-04-12 18:24:55 +00:00
|
|
|
|
2020-07-20 14:56:01 +00:00
|
|
|
bool fenceSupport = dContext->priv().caps()->fenceSyncSupport();
|
2019-05-06 20:58:22 +00:00
|
|
|
bool expectAsyncCallback =
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->backend() == GrBackendApi::kVulkan ||
|
|
|
|
((dContext->backend() == GrBackendApi::kOpenGL) && fenceSupport) ||
|
|
|
|
((dContext->backend() == GrBackendApi::kMetal) && fenceSupport) ||
|
|
|
|
dContext->backend() == GrBackendApi::kDawn ||
|
|
|
|
dContext->backend() == GrBackendApi::kDirect3D;
|
2019-05-06 20:58:22 +00:00
|
|
|
if (expectAsyncCallback) {
|
2019-04-12 18:24:55 +00:00
|
|
|
// On Vulkan the command buffer we just submitted may or may not have finished immediately
|
|
|
|
// so the finish proc may not have been called.
|
|
|
|
REPORTER_ASSERT(reporter, count == 1 || count == 2);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, count == 2);
|
|
|
|
}
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2019-04-12 18:24:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 2);
|
|
|
|
|
2019-05-09 14:30:12 +00:00
|
|
|
// Test flushing via the SkImage
|
|
|
|
canvas->drawImage(image, 0, 0);
|
2020-07-20 14:56:01 +00:00
|
|
|
image->flush(dContext, flushInfoFinishedProc);
|
|
|
|
dContext->submit();
|
2019-05-09 13:03:57 +00:00
|
|
|
if (expectAsyncCallback) {
|
|
|
|
// On Vulkan the command buffer we just submitted may or may not have finished immediately
|
|
|
|
// so the finish proc may not have been called.
|
2019-05-09 13:48:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 2 || count == 3);
|
2019-05-09 13:03:57 +00:00
|
|
|
} else {
|
2019-05-09 13:48:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 3);
|
2019-05-09 13:03:57 +00:00
|
|
|
}
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2019-05-09 13:48:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 3);
|
2019-05-09 13:03:57 +00:00
|
|
|
|
2020-07-20 14:56:01 +00:00
|
|
|
// Test flushing via the GrDirectContext
|
2019-05-09 14:30:12 +00:00
|
|
|
canvas->clear(SK_ColorBLUE);
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush(flushInfoFinishedProc);
|
|
|
|
dContext->submit();
|
2019-05-09 14:30:12 +00:00
|
|
|
if (expectAsyncCallback) {
|
|
|
|
// On Vulkan the command buffer we just submitted may or may not have finished immediately
|
|
|
|
// so the finish proc may not have been called.
|
|
|
|
REPORTER_ASSERT(reporter, count == 3 || count == 4);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, count == 4);
|
|
|
|
}
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2019-05-09 14:30:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 4);
|
|
|
|
|
2019-05-06 20:58:22 +00:00
|
|
|
// There is no work on the surface so flushing may immediately call the finished proc.
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush(flushInfoFinishedProc);
|
|
|
|
dContext->submit();
|
2019-05-09 14:30:12 +00:00
|
|
|
REPORTER_ASSERT(reporter, count == 4 || count == 5);
|
2020-07-20 14:56:01 +00:00
|
|
|
busy_wait_for_callback(&count, 5, dContext, reporter);
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
int count2 = 0;
|
|
|
|
canvas->clear(SK_ColorGREEN);
|
2020-06-30 17:42:46 +00:00
|
|
|
surface->flush(flushInfoFinishedProc);
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->submit();
|
2019-04-12 18:24:55 +00:00
|
|
|
// There is no work to be flushed here so this will return immediately, but make sure the
|
|
|
|
// finished call from this proc isn't called till the previous surface flush also is finished.
|
2019-04-17 19:26:11 +00:00
|
|
|
flushInfoFinishedProc.fFinishedContext = (void*)&count2;
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush(flushInfoFinishedProc);
|
|
|
|
dContext->submit();
|
2019-05-06 20:58:22 +00:00
|
|
|
REPORTER_ASSERT(reporter, count <= 1 && count2 <= count);
|
2019-04-12 18:24:55 +00:00
|
|
|
|
2020-07-20 14:56:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2019-04-12 18:24:55 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, count == 1);
|
|
|
|
REPORTER_ASSERT(reporter, count == count2);
|
|
|
|
}
|
|
|
|
|