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