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 17#ifndef SF_RENDER_ENGINE_PROGRAMCACHE_H 18#define SF_RENDER_ENGINE_PROGRAMCACHE_H 19 20#include <GLES2/gl2.h> 21 22#include <utils/Singleton.h> 23#include <utils/KeyedVector.h> 24#include <utils/TypeHelpers.h> 25 26#include "Description.h" 27 28namespace android { 29 30class Description; 31class Program; 32class String8; 33 34/* 35 * This class generates GLSL programs suitable to handle a given 36 * Description. It's responsible for figuring out what to 37 * generate from a Description. 38 * It also maintains a cache of these Programs. 39 */ 40class ProgramCache : public Singleton<ProgramCache> { 41public: 42 /* 43 * Key is used to retrieve a Program in the cache. 44 * A Key is generated from a Description. 45 */ 46 class Key { 47 friend class ProgramCache; 48 typedef uint32_t key_t; 49 key_t mKey; 50 public: 51 enum { 52 BLEND_PREMULT = 0x00000001, 53 BLEND_NORMAL = 0x00000000, 54 BLEND_MASK = 0x00000001, 55 56 OPACITY_OPAQUE = 0x00000002, 57 OPACITY_TRANSLUCENT = 0x00000000, 58 OPACITY_MASK = 0x00000002, 59 60 PLANE_ALPHA_LT_ONE = 0x00000004, 61 PLANE_ALPHA_EQ_ONE = 0x00000000, 62 PLANE_ALPHA_MASK = 0x00000004, 63 64 TEXTURE_OFF = 0x00000000, 65 TEXTURE_EXT = 0x00000008, 66 TEXTURE_2D = 0x00000010, 67 TEXTURE_MASK = 0x00000018, 68 69 COLOR_MATRIX_OFF = 0x00000000, 70 COLOR_MATRIX_ON = 0x00000020, 71 COLOR_MATRIX_MASK = 0x00000020, 72 73 WIDE_GAMUT_OFF = 0x00000000, 74 WIDE_GAMUT_ON = 0x00000040, 75 WIDE_GAMUT_MASK = 0x00000040, 76 }; 77 78 inline Key() : mKey(0) { } 79 inline Key(const Key& rhs) : mKey(rhs.mKey) { } 80 81 inline Key& set(key_t mask, key_t value) { 82 mKey = (mKey & ~mask) | value; 83 return *this; 84 } 85 86 inline bool isTexturing() const { 87 return (mKey & TEXTURE_MASK) != TEXTURE_OFF; 88 } 89 inline int getTextureTarget() const { 90 return (mKey & TEXTURE_MASK); 91 } 92 inline bool isPremultiplied() const { 93 return (mKey & BLEND_MASK) == BLEND_PREMULT; 94 } 95 inline bool isOpaque() const { 96 return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; 97 } 98 inline bool hasPlaneAlpha() const { 99 return (mKey & PLANE_ALPHA_MASK) == PLANE_ALPHA_LT_ONE; 100 } 101 inline bool hasColorMatrix() const { 102 return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON; 103 } 104 inline bool isWideGamut() const { 105 return (mKey & WIDE_GAMUT_MASK) == WIDE_GAMUT_ON; 106 } 107 108 // this is the definition of a friend function -- not a method of class Needs 109 friend inline int strictly_order_type(const Key& lhs, const Key& rhs) { 110 return (lhs.mKey < rhs.mKey) ? 1 : 0; 111 } 112 }; 113 114 ProgramCache(); 115 ~ProgramCache(); 116 117 // useProgram lookup a suitable program in the cache or generates one 118 // if none can be found. 119 void useProgram(const Description& description); 120 121private: 122 // Generate shaders to populate the cache 123 void primeCache(); 124 // compute a cache Key from a Description 125 static Key computeKey(const Description& description); 126 // generates a program from the Key 127 static Program* generateProgram(const Key& needs); 128 // generates the vertex shader from the Key 129 static String8 generateVertexShader(const Key& needs); 130 // generates the fragment shader from the Key 131 static String8 generateFragmentShader(const Key& needs); 132 133 // Key/Value map used for caching Programs. Currently the cache 134 // is never shrunk. 135 DefaultKeyedVector<Key, Program*> mCache; 136}; 137 138 139ANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key) 140 141} /* namespace android */ 142 143#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */ 144