• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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(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; }
43     virtual void setDisplayParams(const DisplayParams& params) = 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     void snapRecordingAndSubmit();
50 #endif
51 
width()52     int width() const { return fWidth; }
height()53     int height() const { return fHeight; }
dimensions()54     SkISize dimensions() const { return {fWidth, fHeight}; }
sampleCount()55     int sampleCount() const { return fSampleCount; }
stencilBits()56     int stencilBits() const { return fStencilBits; }
57 
58 protected:
isGpuContext()59     virtual bool isGpuContext() { return true;  }
60 
61     virtual void onSwapBuffers() = 0;
62 
63     sk_sp<GrDirectContext> fContext;
64 #if defined(SK_GRAPHITE)
65     std::unique_ptr<skgpu::graphite::Context> fGraphiteContext;
66     std::unique_ptr<skgpu::graphite::Recorder> fGraphiteRecorder;
67 #endif
68 
69     int               fWidth;
70     int               fHeight;
71     DisplayParams     fDisplayParams;
72 
73     // parameters obtained from the native window
74     // Note that the platform .cpp file is responsible for
75     // initializing fSampleCount and fStencilBits!
76     int               fSampleCount = 1;
77     int               fStencilBits = 0;
78 };
79 
80 }  // namespace skwindow
81 
82 #endif
83