13f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian/* 23f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Copyright 2013 The Android Open Source Project 33f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 43f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 53f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * you may not use this file except in compliance with the License. 63f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * You may obtain a copy of the License at 73f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 83f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 93f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * 103f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Unless required by applicable law or agreed to in writing, software 113f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 123f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * See the License for the specific language governing permissions and 143f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * limitations under the License. 153f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian */ 163f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 173f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#ifndef SF_RENDER_ENGINE_PROGRAMCACHE_H 183f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#define SF_RENDER_ENGINE_PROGRAMCACHE_H 193f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 203f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <GLES2/gl2.h> 213f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 223f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <utils/Singleton.h> 233f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <utils/KeyedVector.h> 243f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include <utils/TypeHelpers.h> 253f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 263f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#include "Description.h" 273f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 283f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopiannamespace android { 293f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 303f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianclass Description; 313f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianclass Program; 323f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianclass String8; 333f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 343f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian/* 353f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * This class generates GLSL programs suitable to handle a given 363f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Description. It's responsible for figuring out what to 373f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * generate from a Description. 383f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * It also maintains a cache of these Programs. 393f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian */ 403f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianclass ProgramCache : public Singleton<ProgramCache> { 413f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianpublic: 423f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian /* 433f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * Key is used to retrieve a Program in the cache. 443f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian * A Key is generated from a Description. 453f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian */ 463f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian class Key { 473f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian friend class ProgramCache; 483f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian typedef uint32_t key_t; 493f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian key_t mKey; 503f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian public: 513f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian enum { 523f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian BLEND_PREMULT = 0x00000001, 533f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian BLEND_NORMAL = 0x00000000, 543f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian BLEND_MASK = 0x00000001, 553f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 563f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian OPACITY_OPAQUE = 0x00000002, 573f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian OPACITY_TRANSLUCENT = 0x00000000, 583f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian OPACITY_MASK = 0x00000002, 593f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 603f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian PLANE_ALPHA_LT_ONE = 0x00000004, 613f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian PLANE_ALPHA_EQ_ONE = 0x00000000, 623f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian PLANE_ALPHA_MASK = 0x00000004, 633f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 643f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian TEXTURE_OFF = 0x00000000, 653f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian TEXTURE_EXT = 0x00000008, 663f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian TEXTURE_2D = 0x00000010, 673f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian TEXTURE_MASK = 0x00000018, 68ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian 69ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian COLOR_MATRIX_OFF = 0x00000000, 70ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian COLOR_MATRIX_ON = 0x00000020, 71ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian COLOR_MATRIX_MASK = 0x00000020, 723f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian }; 733f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 743f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline Key() : mKey(0) { } 753f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline Key(const Key& rhs) : mKey(rhs.mKey) { } 763f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 773f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline Key& set(key_t mask, key_t value) { 783f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian mKey = (mKey & ~mask) | value; 793f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return *this; 803f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 813f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 823f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline bool isTexturing() const { 833f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (mKey & TEXTURE_MASK) != TEXTURE_OFF; 843f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 853f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline int getTextureTarget() const { 863f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (mKey & TEXTURE_MASK); 873f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 883f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline bool isPremultiplied() const { 893f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (mKey & BLEND_MASK) == BLEND_PREMULT; 903f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 913f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline bool isOpaque() const { 923f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (mKey & OPACITY_MASK) == OPACITY_OPAQUE; 933f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 943f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian inline bool hasPlaneAlpha() const { 953f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (mKey & PLANE_ALPHA_MASK) == PLANE_ALPHA_LT_ONE; 963f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 97ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian inline bool hasColorMatrix() const { 98ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian return (mKey & COLOR_MATRIX_MASK) == COLOR_MATRIX_ON; 99ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian } 1003f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1013f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // this is the definition of a friend function -- not a method of class Needs 1023f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian friend inline int strictly_order_type(const Key& lhs, const Key& rhs) { 1033f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian return (lhs.mKey < rhs.mKey) ? 1 : 0; 1043f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian } 1053f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian }; 1063f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1073f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian ProgramCache(); 1083f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian ~ProgramCache(); 1093f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1103f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // useProgram lookup a suitable program in the cache or generates one 1113f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // if none can be found. 1123f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian void useProgram(const Description& description); 1133f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1143f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopianprivate: 115a51fafc4d83ab1d1582c1c870c31be44b057aa95Riley Andrews // Generate shaders to populate the cache 116a51fafc4d83ab1d1582c1c870c31be44b057aa95Riley Andrews void primeCache(); 1173f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // compute a cache Key from a Description 1183f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian static Key computeKey(const Description& description); 1193f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // generates a program from the Key 1203f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian static Program* generateProgram(const Key& needs); 1213f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // generates the vertex shader from the Key 1223f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian static String8 generateVertexShader(const Key& needs); 1233f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // generates the fragment shader from the Key 1243f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian static String8 generateFragmentShader(const Key& needs); 1253f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1263f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // Key/Value map used for caching Programs. Currently the cache 1273f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian // is never shrunk. 1283f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian DefaultKeyedVector<Key, Program*> mCache; 1293f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian}; 1303f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1313f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1323f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias AgopianANDROID_BASIC_TYPES_TRAITS(ProgramCache::Key) 1333f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1343f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian} /* namespace android */ 1353f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian 1363f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#endif /* SF_RENDER_ENGINE_PROGRAMCACHE_H */ 137