xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/GrGLTypesPriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 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 GrGLTypesPriv_DEFINED
8 #define GrGLTypesPriv_DEFINED
9 
10 #include "include/core/SkRefCnt.h"
11 #include "include/gpu/ganesh/gl/GrGLTypes.h"
12 
13 #include <cstdint>
14 #include <utility>
15 
16 namespace skgpu {
17 enum class Protected : bool;
18 }
19 
20 static constexpr int kGrGLColorFormatCount = static_cast<int>(GrGLFormat::kLastColorFormat) + 1;
21 
22 class GrGLTextureParameters : public SkNVRefCnt<GrGLTextureParameters> {
23 public:
24     // We currently consider texture parameters invalid on all textures
25     // GrContext::resetContext(). We use this type to track whether instances of
26     // GrGLTextureParameters were updated before or after the most recent resetContext(). At 10
27     // resets / frame and 60fps a 64bit timestamp will overflow in about a billion years.
28     // TODO: Require clients to use GrBackendTexture::glTextureParametersModified() to invalidate
29     // texture parameters and get rid of timestamp checking.
30     using ResetTimestamp = uint64_t;
31 
32     // This initializes the params to have an expired timestamp. They'll be considered invalid the
33     // first time the texture is used unless set() is called.
34     GrGLTextureParameters() = default;
35 
36     // This is texture parameter state that is overridden when a non-zero sampler object is bound.
37     struct SamplerOverriddenState {
38         SamplerOverriddenState();
39         void invalidate();
40 
41         GrGLenum fMinFilter;
42         GrGLenum fMagFilter;
43         GrGLenum fWrapS;
44         GrGLenum fWrapT;
45         GrGLfloat fMinLOD;
46         GrGLfloat fMaxLOD;
47         GrGLfloat fMaxAniso;
48         // We always want the border color to be transparent black, so no need to store 4 floats.
49         // Just track if it's been invalidated and no longer the default
50         bool fBorderColorInvalid;
51     };
52 
53     // Texture parameter state that is not overridden by a bound sampler object.
54     struct NonsamplerState {
55         NonsamplerState();
56         void invalidate();
57 
58         GrGLint fBaseMipMapLevel;
59         GrGLint fMaxMipmapLevel;
60         bool    fSwizzleIsRGBA;
61     };
62 
63     void invalidate();
64 
resetTimestamp()65     ResetTimestamp resetTimestamp() const { return fResetTimestamp; }
samplerOverriddenState()66     const SamplerOverriddenState& samplerOverriddenState() const { return fSamplerOverriddenState; }
nonsamplerState()67     const NonsamplerState& nonsamplerState() const { return fNonsamplerState; }
68 
69     // SamplerOverriddenState is optional because we don't track it when we're using sampler
70     // objects.
71     void set(const SamplerOverriddenState* samplerState,
72              const NonsamplerState& nonsamplerState,
73              ResetTimestamp currTimestamp);
74 
75 private:
76     static constexpr ResetTimestamp kExpiredTimestamp = 0;
77 
78     SamplerOverriddenState fSamplerOverriddenState;
79     NonsamplerState fNonsamplerState;
80     ResetTimestamp fResetTimestamp = kExpiredTimestamp;
81 };
82 
83 class GrGLBackendTextureInfo {
84 public:
GrGLBackendTextureInfo(const GrGLTextureInfo & info,sk_sp<GrGLTextureParameters> params)85     GrGLBackendTextureInfo(const GrGLTextureInfo& info, sk_sp<GrGLTextureParameters> params)
86             : fInfo(info), fParams(std::move(params)) {}
info()87     const GrGLTextureInfo& info() const { return fInfo; }
parameters()88     GrGLTextureParameters* parameters() const { return fParams.get(); }
refParameters()89     sk_sp<GrGLTextureParameters> refParameters() const { return fParams; }
90 
isProtected()91     bool isProtected() const { return fInfo.isProtected(); }
92 
93 private:
94     GrGLTextureInfo fInfo;
95     sk_sp<GrGLTextureParameters> fParams;  // not const because we might call invalidate() on it.
96 };
97 
98 struct GrGLTextureSpec {
GrGLTextureSpecGrGLTextureSpec99     GrGLTextureSpec() : fTarget(0), fFormat(0) {}
GrGLTextureSpecGrGLTextureSpec100     GrGLTextureSpec(const GrGLSurfaceInfo& info) : fTarget(info.fTarget), fFormat(info.fFormat) {}
101 
102     GrGLenum fTarget;
103     GrGLenum fFormat;
104 };
105 
106 GrGLSurfaceInfo GrGLTextureSpecToSurfaceInfo(const GrGLTextureSpec& glSpec,
107                                              uint32_t sampleCount,
108                                              uint32_t levelCount,
109                                              skgpu::Protected isProtected);
110 
111 #endif
112