Debian Codegraph @

tools/window/DisplayParams.h

raw
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 DisplayParams_DEFINED
8 #define DisplayParams_DEFINED
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkSurfaceProps.h"
13 #include "src/base/SkMathPriv.h"
14
15 #include <memory>
16
17 #if defined(SK_GANESH)
18 #include "include/gpu/ganesh/GrContextOptions.h"
19 #endif
20
21 namespace skwindow {
22
23 struct GraphiteTestOptions;
24
25 // DisplayParams should be treated as a immutable object once created.
26 class DisplayParams {
27 public:
28 DisplayParams()
29 : fColorType(kN32_SkColorType)
30 , fColorSpace(nullptr)
31 , fMSAASampleCount(1)
32 , fSurfaceProps(0, kRGB_H_SkPixelGeometry)
33 , fDisableVsync(false)
34 , fDelayDrawableAcquisition(false)
35 , fCreateProtectedNativeBackend(false)
36 {}
37
38 DisplayParams(const DisplayParams* other) {
39 SkASSERT(other);
40 fColorType = other->fColorType;
41 fColorSpace = other->fColorSpace;
42 fMSAASampleCount = other->fMSAASampleCount;
43 #if defined(SK_GANESH)
44 fGrContextOptions = other->fGrContextOptions;
45 #endif
46 fSurfaceProps = other->fSurfaceProps;
47 fDisableVsync = other->fDisableVsync;
48 fDelayDrawableAcquisition = other->fDelayDrawableAcquisition;
49 fCreateProtectedNativeBackend = other->fCreateProtectedNativeBackend;
50 }
51 DisplayParams(const DisplayParams& other) : DisplayParams(&other) {}
52
53 virtual ~DisplayParams() = default;
54
55 virtual std::unique_ptr<DisplayParams> clone() const {
56 return std::make_unique<DisplayParams>(*this);
57 }
58
59 SkColorType colorType() const { return fColorType; }
60 sk_sp<SkColorSpace> colorSpace() const { return fColorSpace; }
61 int msaaSampleCount() const { return fMSAASampleCount; }
62 #if defined(SK_GANESH)
63 const GrContextOptions& grContextOptions() const { return fGrContextOptions; }
64 #endif
65 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
66 bool disableVsync() const { return fDisableVsync; }
67 bool delayDrawableAcquisition() const { return fDelayDrawableAcquisition; }
68 bool createProtectedNativeBackend() const { return fCreateProtectedNativeBackend; }
69 virtual const GraphiteTestOptions* graphiteTestOptions() const { return nullptr; }
70
71 private:
72 friend class DisplayParamsBuilder;
73
74 SkColorType fColorType;
75 sk_sp<SkColorSpace> fColorSpace;
76 int fMSAASampleCount;
77 #if defined(SK_GANESH)
78 GrContextOptions fGrContextOptions;
79 #endif
80 SkSurfaceProps fSurfaceProps;
81 bool fDisableVsync;
82 bool fDelayDrawableAcquisition;
83 bool fCreateProtectedNativeBackend = false;
84 };
85
86 class DisplayParamsBuilder {
87 public:
88 DisplayParamsBuilder() : fDisplayParams(std::make_unique<DisplayParams>()) {}
89
90 // Call clone() in case other is a subclass of DisplayParams
91 DisplayParamsBuilder(const DisplayParams* other) : fDisplayParams(other->clone()) {}
92
93 DisplayParamsBuilder& colorType(SkColorType colorType) {
94 SkASSERT_RELEASE(fDisplayParams);
95 fDisplayParams->fColorType = colorType;
96 return *this;
97 }
98
99 DisplayParamsBuilder& colorSpace(const sk_sp<SkColorSpace>& colorSpace) {
100 SkASSERT_RELEASE(fDisplayParams);
101 fDisplayParams->fColorSpace = colorSpace;
102 return *this;
103 }
104
105 DisplayParamsBuilder& msaaSampleCount(int MSAASampleCount) {
106 SkASSERT_RELEASE(fDisplayParams);
107 fDisplayParams->fMSAASampleCount = MSAASampleCount;
108 return *this;
109 }
110
111 DisplayParamsBuilder& roundUpMSAA() {
112 SkASSERT_RELEASE(fDisplayParams);
113 // SkNextPow2 is undefined for 0, so handle that ourselves.
114 if (fDisplayParams->fMSAASampleCount <= 1) {
115 fDisplayParams->fMSAASampleCount = 1;
116 } else {
117 fDisplayParams->fMSAASampleCount = SkNextPow2(fDisplayParams->fMSAASampleCount);
118 }
119 return *this;
120 }
121
122 #if defined(SK_GANESH)
123 DisplayParamsBuilder& grContextOptions(const GrContextOptions& grContextOptions) {
124 SkASSERT_RELEASE(fDisplayParams);
125 fDisplayParams->fGrContextOptions = grContextOptions;
126 return *this;
127 }
128 #endif
129
130 DisplayParamsBuilder& surfaceProps(const SkSurfaceProps& surfaceProps) {
131 SkASSERT_RELEASE(fDisplayParams);
132 fDisplayParams->fSurfaceProps = surfaceProps;
133 return *this;
134 }
135
136 DisplayParamsBuilder& disableVsync(bool disableVsync) {
137 SkASSERT_RELEASE(fDisplayParams);
138 fDisplayParams->fDisableVsync = disableVsync;
139 return *this;
140 }
141
142 DisplayParamsBuilder& delayDrawableAcquisition(bool delayDrawableAcquisition) {
143 SkASSERT_RELEASE(fDisplayParams);
144 fDisplayParams->fDelayDrawableAcquisition = delayDrawableAcquisition;
145 return *this;
146 }
147
148 DisplayParamsBuilder& createProtectedNativeBackend(bool createProtectedNativeBackend) {
149 SkASSERT_RELEASE(fDisplayParams);
150 fDisplayParams->fCreateProtectedNativeBackend = createProtectedNativeBackend;
151 return *this;
152 }
153
154 std::unique_ptr<DisplayParams> detach() {
155 SkASSERT_RELEASE(fDisplayParams);
156 return std::move(fDisplayParams);
157 }
158
159 protected:
160 DisplayParamsBuilder(std::unique_ptr<DisplayParams> other) : fDisplayParams(std::move(other)) {}
161
162 std::unique_ptr<DisplayParams> fDisplayParams;
163 };
164
165 } // namespace skwindow
166
167 #endif