1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/GrFinishCallbacks.h"
9cb93a386Sopenharmony_ci#include "src/gpu/GrGpu.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ciGrFinishCallbacks::GrFinishCallbacks(GrGpu* gpu) : fGpu(gpu) {}
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ciGrFinishCallbacks::~GrFinishCallbacks() {
14cb93a386Sopenharmony_ci    this->callAll(true);
15cb93a386Sopenharmony_ci}
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_civoid GrFinishCallbacks::add(GrGpuFinishedProc finishedProc,
18cb93a386Sopenharmony_ci                            GrGpuFinishedContext finishedContext) {
19cb93a386Sopenharmony_ci    SkASSERT(finishedProc);
20cb93a386Sopenharmony_ci    FinishCallback callback;
21cb93a386Sopenharmony_ci    callback.fCallback = finishedProc;
22cb93a386Sopenharmony_ci    callback.fContext = finishedContext;
23cb93a386Sopenharmony_ci    callback.fFence = fGpu->insertFence();
24cb93a386Sopenharmony_ci    fCallbacks.push_back(callback);
25cb93a386Sopenharmony_ci}
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_civoid GrFinishCallbacks::check() {
28cb93a386Sopenharmony_ci    // Bail after the first unfinished sync since we expect they signal in the order inserted.
29cb93a386Sopenharmony_ci    while (!fCallbacks.empty() && fGpu->waitFence(fCallbacks.front().fFence)) {
30cb93a386Sopenharmony_ci        fCallbacks.front().fCallback(fCallbacks.front().fContext);
31cb93a386Sopenharmony_ci        fGpu->deleteFence(fCallbacks.front().fFence);
32cb93a386Sopenharmony_ci        fCallbacks.pop_front();
33cb93a386Sopenharmony_ci    }
34cb93a386Sopenharmony_ci}
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_civoid GrFinishCallbacks::callAll(bool doDelete) {
37cb93a386Sopenharmony_ci    while (!fCallbacks.empty()) {
38cb93a386Sopenharmony_ci        fCallbacks.front().fCallback(fCallbacks.front().fContext);
39cb93a386Sopenharmony_ci        if (doDelete) {
40cb93a386Sopenharmony_ci            fGpu->deleteFence(fCallbacks.front().fFence);
41cb93a386Sopenharmony_ci        }
42cb93a386Sopenharmony_ci        fCallbacks.pop_front();
43cb93a386Sopenharmony_ci    }
44cb93a386Sopenharmony_ci}
45