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