1 /* 2 * Copyright 2020 Google LLC. 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 8 #include "src/gpu/ganesh/gl/GrGLFinishCallbacks.h" 9 10 #include "include/gpu/GpuTypes.h" 11 #include "include/private/base/SkAssert.h" 12 #include "src/gpu/ganesh/gl/GrGLGpu.h" 13 14 #include <utility> 15 GrGLFinishCallbacks(GrGLGpu * gpu)16GrGLFinishCallbacks::GrGLFinishCallbacks(GrGLGpu* gpu) : fGpu(gpu) {} 17 ~GrGLFinishCallbacks()18GrGLFinishCallbacks::~GrGLFinishCallbacks() { 19 this->callAll(true); 20 } 21 add(skgpu::AutoCallback callback,GrGLint timerQuery)22void GrGLFinishCallbacks::add(skgpu::AutoCallback callback, GrGLint timerQuery) { 23 SkASSERT(callback); 24 FinishCallback finishCallback; 25 finishCallback.fCallback = std::move(callback); 26 finishCallback.fSync = fGpu->insertSync(); 27 finishCallback.fTimerQuery = timerQuery; 28 fCallbacks.push_back(std::move(finishCallback)); 29 } 30 check()31void GrGLFinishCallbacks::check() { 32 // Bail after the first unfinished sync since we expect they signal in the order inserted. 33 while (!fCallbacks.empty() && fGpu->testSync(fCallbacks.front().fSync)) { 34 // While we are processing a proc we need to make sure to remove it from the callback list 35 // before calling it. This is because the client could trigger a call (e.g. calling 36 // flushAndSubmit(/*sync=*/true)) that has us process the finished callbacks. We also must 37 // process deleting the sync before a client may abandon the context. 38 auto& finishCallback = fCallbacks.front(); 39 if (finishCallback.fSync) { 40 fGpu->deleteSync(finishCallback.fSync); 41 } 42 skgpu::GpuStats stats; 43 if (auto timerQuery = finishCallback.fTimerQuery) { 44 stats.elapsedTime = fGpu->getTimerQueryResult(timerQuery); 45 if (finishCallback.fCallback.receivesGpuStats()) { 46 finishCallback.fCallback.setStats(stats); 47 } 48 } 49 fCallbacks.pop_front(); 50 } 51 } 52 callAll(bool doDelete)53void GrGLFinishCallbacks::callAll(bool doDelete) { 54 while (!fCallbacks.empty()) { 55 // While we are processing a proc we need to make sure to remove it from the callback list 56 // before calling it. This is because the client could trigger a call (e.g. calling 57 // flushAndSubmit(/*sync=*/true)) that has us process the finished callbacks. We also must 58 // process deleting the sync before a client may abandon the context. 59 auto& finishCallback = fCallbacks.front(); 60 skgpu::GpuStats stats; 61 if (doDelete && finishCallback.fSync) { 62 fGpu->deleteSync(finishCallback.fSync); 63 if (finishCallback.fTimerQuery) { 64 stats.elapsedTime = fGpu->getTimerQueryResult(finishCallback.fTimerQuery); 65 if (finishCallback.fCallback.receivesGpuStats()) { 66 finishCallback.fCallback.setStats(stats); 67 } 68 } 69 } 70 fCallbacks.pop_front(); 71 } 72 } 73