xref: /aosp_15_r20/external/skia/tools/gpu/TestContext.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 
2 /*
3  * Copyright 2016 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #include "tools/gpu/TestContext.h"
10 
11 #include "include/gpu/ganesh/GrDirectContext.h"
12 #include "src/core/SkTraceEvent.h"
13 #include "tools/gpu/FlushFinishTracker.h"
14 #include "tools/gpu/GpuTimer.h"
15 
16 namespace sk_gpu_test {
TestContext()17 TestContext::TestContext() : fGpuTimer(nullptr) {}
18 
~TestContext()19 TestContext::~TestContext() {
20     // Subclass should call teardown.
21     SkASSERT(!fGpuTimer);
22 }
23 
makeContext(const GrContextOptions &)24 sk_sp<GrDirectContext> TestContext::makeContext(const GrContextOptions&) {
25     return nullptr;
26 }
27 
makeNotCurrent() const28 void TestContext::makeNotCurrent() const { this->onPlatformMakeNotCurrent(); }
makeCurrent() const29 void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
30 
makeCurrentAndAutoRestore() const31 SkScopeExit TestContext::makeCurrentAndAutoRestore() const {
32     auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore());
33     this->makeCurrent();
34     return asr;
35 }
36 
flushAndWaitOnSync(GrDirectContext * context)37 void TestContext::flushAndWaitOnSync(GrDirectContext* context) {
38     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
39     SkASSERT(context);
40 
41     if (fFinishTrackers[fCurrentFlushIdx]) {
42         fFinishTrackers[fCurrentFlushIdx]->waitTillFinished();
43     }
44 
45     fFinishTrackers[fCurrentFlushIdx].reset(new FlushFinishTracker(context));
46 
47     // We add an additional ref to the current flush tracker here. This ref is owned by the finish
48     // callback on the flush call. The finish callback will unref the tracker when called.
49     fFinishTrackers[fCurrentFlushIdx]->ref();
50 
51     GrFlushInfo flushInfo;
52     flushInfo.fFinishedProc = FlushFinishTracker::FlushFinished;
53     flushInfo.fFinishedContext = fFinishTrackers[fCurrentFlushIdx].get();
54 
55     context->flush(flushInfo);
56     context->submit();
57 
58     fCurrentFlushIdx = (fCurrentFlushIdx + 1) % std::size(fFinishTrackers);
59 }
60 
flushAndSyncCpu(GrDirectContext * context)61 void TestContext::flushAndSyncCpu(GrDirectContext* context) {
62     SkASSERT(context);
63     context->flush();
64     context->submit(GrSyncCpu::kYes);
65 }
66 
testAbandon()67 void TestContext::testAbandon() {
68 }
69 
teardown()70 void TestContext::teardown() {
71     fGpuTimer.reset();
72 }
73 
74 }  // namespace sk_gpu_test
75