GrGLTypesPriv.h 3.9 KB

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