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