| 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 |
#ifndef WindowContext_DEFINED |
| 8 |
#define WindowContext_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkRefCnt.h" |
| 11 |
#include "include/core/SkSurfaceProps.h" |
| 12 |
#include "tools/window/DisplayParams.h" |
| 13 |
|
| 14 |
#if defined(SK_GANESH) |
| 15 |
#include "include/gpu/ganesh/GrTypes.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <functional> |
| 19 |
|
| 20 |
class SkSurface; |
| 21 |
|
| 22 |
#if defined(SK_GANESH) |
| 23 |
class GrDirectContext; |
| 24 |
#endif |
| 25 |
|
| 26 |
#if defined(SK_GRAPHITE) |
| 27 |
namespace skgpu::graphite { |
| 28 |
class Context; |
| 29 |
class Recorder; |
| 30 |
} |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace skwindow { |
| 34 |
|
| 35 |
class WindowContext { |
| 36 |
public: |
| 37 |
WindowContext(std::unique_ptr<const DisplayParams>); |
| 38 |
|
| 39 |
virtual ~WindowContext(); |
| 40 |
|
| 41 |
virtual sk_sp<SkSurface> getBackbufferSurface() = 0; |
| 42 |
|
| 43 |
void swapBuffers(); |
| 44 |
|
| 45 |
virtual bool isValid() = 0; |
| 46 |
|
| 47 |
virtual void resize(int w, int h) = 0; |
| 48 |
|
| 49 |
virtual void activate(bool isActive) {} |
| 50 |
|
| 51 |
const DisplayParams* getDisplayParams() { return fDisplayParams.get(); } |
| 52 |
virtual void setDisplayParams(std::unique_ptr<const DisplayParams>) = 0; |
| 53 |
|
| 54 |
#if defined(SK_GANESH) |
| 55 |
GrDirectContext* directContext() const { return fContext.get(); } |
| 56 |
#endif |
| 57 |
#if defined(SK_GRAPHITE) |
| 58 |
skgpu::graphite::Context* graphiteContext() const { return fGraphiteContext.get(); } |
| 59 |
skgpu::graphite::Recorder* graphiteRecorder() const { return fGraphiteRecorder.get(); } |
| 60 |
#endif |
| 61 |
|
| 62 |
using GpuTimerCallback = std::function<void(uint64_t ns)>; |
| 63 |
void submitToGpu(GpuTimerCallback = {}); |
| 64 |
bool supportsGpuTimer() const; |
| 65 |
|
| 66 |
int width() const { return fWidth; } |
| 67 |
int height() const { return fHeight; } |
| 68 |
SkISize dimensions() const { return {fWidth, fHeight}; } |
| 69 |
int sampleCount() const { return fSampleCount; } |
| 70 |
int stencilBits() const { return fStencilBits; } |
| 71 |
|
| 72 |
protected: |
| 73 |
virtual bool isGpuContext() { return true; } |
| 74 |
|
| 75 |
virtual void onSwapBuffers() = 0; |
| 76 |
|
| 77 |
#if defined(SK_GANESH) |
| 78 |
sk_sp<GrDirectContext> fContext; |
| 79 |
#endif |
| 80 |
#if defined(SK_GRAPHITE) |
| 81 |
std::unique_ptr<skgpu::graphite::Context> fGraphiteContext; |
| 82 |
std::unique_ptr<skgpu::graphite::Recorder> fGraphiteRecorder; |
| 83 |
#endif |
| 84 |
|
| 85 |
int fWidth; |
| 86 |
int fHeight; |
| 87 |
std::unique_ptr<const DisplayParams> fDisplayParams; |
| 88 |
|
| 89 |
// parameters obtained from the native window |
| 90 |
// Note that the platform .cpp file is responsible for |
| 91 |
// initializing fSampleCount and fStencilBits! |
| 92 |
int fSampleCount = 1; |
| 93 |
int fStencilBits = 0; |
| 94 |
}; |
| 95 |
|
| 96 |
} // namespace skwindow |
| 97 |
|
| 98 |
#endif |