aboutsummaryrefslogtreecommitdiff
path: root/src/gpu/ganesh/GrFinishCallbacks.cpp
blob: 031e826eaa1dc3ab95b66465c00adf64c288d34a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * Copyright 2020 Google LLC.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "src/gpu/ganesh/GrFinishCallbacks.h"
#include "src/gpu/ganesh/GrGpu.h"

GrFinishCallbacks::GrFinishCallbacks(GrGpu* gpu) : fGpu(gpu) {}

GrFinishCallbacks::~GrFinishCallbacks() {
    this->callAll(true);
}

void GrFinishCallbacks::add(GrGpuFinishedProc finishedProc,
                            GrGpuFinishedContext finishedContext) {
    SkASSERT(finishedProc);
    FinishCallback callback;
    callback.fCallback = finishedProc;
    callback.fContext = finishedContext;
    callback.fFence = fGpu->insertFence();
    fCallbacks.push_back(callback);
}

void GrFinishCallbacks::check() {
    // Bail after the first unfinished sync since we expect they signal in the order inserted.
    while (!fCallbacks.empty() && fGpu->waitFence(fCallbacks.front().fFence)) {
        // While we are processing a proc we need to make sure to remove it from the callback list
        // before calling it. This is because the client could trigger a call (e.g. calling
        // flushAndSubmit(/*sync=*/true)) that has us process the finished callbacks. We also must
        // process deleting the fence before a client may abandon the context.
        auto finishCallback = fCallbacks.front();
        fGpu->deleteFence(finishCallback.fFence);
        fCallbacks.pop_front();
        finishCallback.fCallback(finishCallback.fContext);
    }
}

void GrFinishCallbacks::callAll(bool doDelete) {
    while (!fCallbacks.empty()) {
        // While we are processing a proc we need to make sure to remove it from the callback list
        // before calling it. This is because the client could trigger a call (e.g. calling
        // flushAndSubmit(/*sync=*/true)) that has us process the finished callbacks. We also must
        // process deleting the fence before a client may abandon the context.
        auto finishCallback = fCallbacks.front();
        if (doDelete) {
            fGpu->deleteFence(finishCallback.fFence);
        }
        fCallbacks.pop_front();
        finishCallback.fCallback(finishCallback.fContext);
    }
}