ProgramCache.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef SF_RENDER_ENGINE_PROGRAMCACHE_H
  17. #define SF_RENDER_ENGINE_PROGRAMCACHE_H
  18. #include <GLES2/gl2.h>
  19. #include <utils/Singleton.h>
  20. #include <utils/KeyedVector.h>
  21. #include <utils/TypeHelpers.h>
  22. #include "Description.h"
  23. namespace android {
  24. class Description;
  25. class Program;
  26. class String8;
  27. /*
  28. * This class generates GLSL programs suitable to handle a given
  29. * Description. It's responsible for figuring out what to
  30. * generate from a Description.
  31. * It also maintains a cache of these Programs.
  32. */
  33. class ProgramCache : public Singleton<ProgramCache> {
  34. public:
  35. /*
  36. * Key is used to retrieve a Program in the cache.
  37. * A Key is generated from a Description.
  38. */
  39. class Key {
  40. friend class ProgramCache;
  41. typedef uint32_t key_t;
  42. key_t mKey;
  43. public:
  44. enum {
  45. BLEND_PREMULT = 0x00000001,
  46. BLEND_NORMAL = 0x00000000,
  47. BLEND_MASK = 0x00000001,
  48. OPACITY_OPAQUE = 0x00000002,
  49. OPACITY_TRANSLUCENT = 0x00000000,
  50. OPACITY_MASK = 0x00000002,
  51. PLANE_ALPHA_LT_ONE = 0x00000004,
  52. PLANE_ALPHA_EQ_ONE = 0x00000000,
  53. PLANE_ALPHA_MASK = 0x00000004,
  54. TEXTURE_OFF = 0x00000000,
  55. TEXTURE_EXT = 0x00000008,
  56. TEXTURE_2D = 0x00000010,
  57. TEXTURE_MASK = 0x00000018,
  58. COLOR_MATRIX_OFF = 0x00000000,
  59. COLOR_MATRIX_ON = 0x00000020,
  60. COLOR_MATRIX_MASK = 0x00000020,
  61. TEXTURE_MASKING_OFF = 0x00000000,
  62. TEXTURE_MASKING_EXT = 0x00800000,
  63. TEXTURE_MASKING_2D = 0x01000000,
  64. TEXTURE_MASKING_MASK = 0x01800000,
  65. };
  66. inline Key() : mKey(0) { }
  67. inline Key(const Key& rhs) : mKey(rhs.mKey) { }
  68. inline Key& set(key_t mask, key_t value) {
  69. mKey = (mKey & ~mask) | value;
  70. return *this;
  71. }
  72. inline bool isTexturing() const {
  73. return (mKey & TEXTURE_MASK) != TEXTURE_OFF;
  74. }
  75. inline int getTextureTarget() const {
  76. return (mKey & TEXTURE_MASK);
  77. }
  78. inline bool isPremultiplied() const {
  79. return (mKey & BLEND_MASK) == BLEND_PREMULT;
  80. }
  81. inline bool isOpaque() const {
  82. return (mKey & OPACITY_MASK) == OPACITY_OPAQUE;
  83. }
  84. inline bool hasPlaneAlpha() const {
  85. return (mKey & PLANE_ALPHA_MASK) == PLANE_ALPHA_LT_ONE;
  86. }
  87. inline bool hasColorMatrix() const {
  88. return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON;
  89. }
  90. inline bool isTextureMasking() const {
  91. return (mKey & TEXTURE_MASKING_MASK) != TEXTURE_MASKING_OFF;
  92. }
  93. inline int getTextureMaskingTarget() const {
  94. return (mKey & TEXTURE_MASKING_MASK);
  95. }
  96. // this is the definition of a friend function -- not a method of class Needs
  97. friend inline int strictly_order_type(const Key& lhs, const Key& rhs) {
  98. return (lhs.mKey < rhs.mKey) ? 1 : 0;
  99. }
  100. };
  101. ProgramCache();
  102. ~ProgramCache();
  103. // useProgram lookup a suitable program in the cache or generates one
  104. // if none can be found.
  105. void useProgram(const Description& description);
  106. private:
  107. // Generate shaders to populate the cache
  108. void primeCache();
  109. // compute a cache Key from a Description
  110. static Key computeKey(const Description& description);
  111. // generates a program from the Key
  112. static Program* generateProgram(const Key& needs);
  113. // generates the vertex shader from the Key
  114. static String8 generateVertexShader(const Key& needs);
  115. // generates the fragment shader from the Key
  116. static String8 generateFragmentShader(const Key& needs);
  117. // Key/Value map used for caching Programs. Currently the cache
  118. // is never shrunk.
  119. DefaultKeyedVector<Key, Program*> mCache;
  120. };
  121. ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key)
  122. } /* namespace android */
  123. #endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */