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
14static constexpr int kGrGLFormatCount = static_cast<int>(GrGLFormat::kLast) + 1;
15
16class GrGLTextureParameters : public SkNVRefCnt<GrGLTextureParameters> {
17public:
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 uint32_t fSwizzleKey;
52 GrGLint fBaseMipMapLevel;
53 GrGLint fMaxMipMapLevel;
54 };
55
56 void invalidate();
57
58 ResetTimestamp resetTimestamp() const { return fResetTimestamp; }
59 const SamplerOverriddenState& samplerOverriddenState() const { return fSamplerOverriddenState; }
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
68private:
69 static constexpr ResetTimestamp kExpiredTimestamp = 0;
70
71 SamplerOverriddenState fSamplerOverriddenState;
72 NonsamplerState fNonsamplerState;
73 ResetTimestamp fResetTimestamp = kExpiredTimestamp;
74};
75
76class GrGLBackendTextureInfo {
77public:
78 GrGLBackendTextureInfo(const GrGLTextureInfo& info, GrGLTextureParameters* params)
79 : fInfo(info), fParams(params) {}
80 GrGLBackendTextureInfo(const GrGLBackendTextureInfo&) = delete;
81 GrGLBackendTextureInfo& operator=(const GrGLBackendTextureInfo&) = delete;
82 const GrGLTextureInfo& info() const { return fInfo; }
83 GrGLTextureParameters* parameters() const { return fParams; }
84 sk_sp<GrGLTextureParameters> refParameters() const { return sk_ref_sp(fParams); }
85
86 void cleanup();
87 void assign(const GrGLBackendTextureInfo&, bool thisIsValid);
88
89private:
90 GrGLTextureInfo fInfo;
91 GrGLTextureParameters* fParams;
92};
93
94#endif
95