| 1 |
/* |
| 2 |
* Copyright 2023 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 "tools/window/WindowContext.h" |
| 9 |
|
| 10 |
#if defined(SK_GANESH) |
| 11 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
#if defined(SK_GRAPHITE) |
| 15 |
#include "include/gpu/graphite/Context.h" |
| 16 |
#include "include/gpu/graphite/Recorder.h" |
| 17 |
#endif |
| 18 |
|
| 19 |
namespace skwindow { |
| 20 |
|
| 21 |
WindowContext::WindowContext(std::unique_ptr<const DisplayParams> params) |
| 22 |
: fDisplayParams(std::move(params)) {} |
| 23 |
|
| 24 |
WindowContext::~WindowContext() {} |
| 25 |
|
| 26 |
void WindowContext::swapBuffers() { |
| 27 |
this->onSwapBuffers(); |
| 28 |
} |
| 29 |
|
| 30 |
bool WindowContext::supportsGpuTimer() const { |
| 31 |
#if defined(SK_GRAPHITE) || defined(SK_GANESH) |
| 32 |
auto flags = skgpu::GpuStatsFlags::kNone; |
| 33 |
#if defined(SK_GRAPHITE) |
| 34 |
if (fGraphiteContext) { |
| 35 |
flags = fGraphiteContext->supportedGpuStats(); |
| 36 |
} |
| 37 |
#endif |
| 38 |
#if defined(SK_GANESH) |
| 39 |
if (fContext) { |
| 40 |
flags = fContext->supportedGpuStats(); |
| 41 |
} |
| 42 |
#endif |
| 43 |
using T = std::underlying_type_t<skgpu::GpuStatsFlags>; |
| 44 |
return static_cast<T>(flags) & static_cast<T>(skgpu::GpuStatsFlags::kElapsedTime); |
| 45 |
#else |
| 46 |
return false; |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
void WindowContext::submitToGpu(GpuTimerCallback statsCallback) { |
| 51 |
#if defined(SK_GRAPHITE) |
| 52 |
if (fGraphiteContext) { |
| 53 |
SkASSERT(fGraphiteRecorder); |
| 54 |
std::unique_ptr<skgpu::graphite::Recording> recording = fGraphiteRecorder->snap(); |
| 55 |
if (recording) { |
| 56 |
skgpu::graphite::InsertRecordingInfo info; |
| 57 |
if (statsCallback) { |
| 58 |
auto callback = std::make_unique<GpuTimerCallback>(std::move(statsCallback)); |
| 59 |
info.fFinishedContext = callback.release(); |
| 60 |
info.fFinishedWithStatsProc = [](skgpu::graphite::GpuFinishedContext context, |
| 61 |
skgpu::CallbackResult, |
| 62 |
const skgpu::GpuStats& stats) { |
| 63 |
std::unique_ptr<GpuTimerCallback> callback{ |
| 64 |
static_cast<GpuTimerCallback*>(context)}; |
| 65 |
(*callback)(stats.elapsedTime); |
| 66 |
}; |
| 67 |
info.fGpuStatsFlags = skgpu::GpuStatsFlags::kElapsedTime; |
| 68 |
} |
| 69 |
info.fRecording = recording.get(); |
| 70 |
fGraphiteContext->insertRecording(info); |
| 71 |
fGraphiteContext->submit(skgpu::graphite::SyncToCpu::kNo); |
| 72 |
} |
| 73 |
return; |
| 74 |
} |
| 75 |
#endif |
| 76 |
#if defined(SK_GANESH) |
| 77 |
if (auto dc = this->directContext()) { |
| 78 |
GrFlushInfo info; |
| 79 |
if (statsCallback) { |
| 80 |
auto callback = std::make_unique<GpuTimerCallback>(std::move(statsCallback)); |
| 81 |
info.fFinishedContext = callback.release(); |
| 82 |
info.fFinishedWithStatsProc = [](GrGpuFinishedContext context, |
| 83 |
const skgpu::GpuStats& stats) { |
| 84 |
std::unique_ptr<GpuTimerCallback> callback{static_cast<GpuTimerCallback*>(context)}; |
| 85 |
(*callback)(stats.elapsedTime); |
| 86 |
}; |
| 87 |
info.fGpuStatsFlags = skgpu::GpuStatsFlags::kElapsedTime; |
| 88 |
} |
| 89 |
dc->flush(info); |
| 90 |
dc->submit(); |
| 91 |
return; |
| 92 |
} |
| 93 |
#endif |
| 94 |
#if defined(SK_GANESH) || defined(SK_GRAPHITE) |
| 95 |
if (statsCallback) { |
| 96 |
statsCallback(0); |
| 97 |
} |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace skwindow |