Caches.h revision 03750a067e818ca7fbd0f590e2ff6a8fded21e6c
1/*
2 * Copyright (C) 2010 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 ANDROID_UI_CACHES_H
18#define ANDROID_UI_CACHES_H
19
20#ifndef LOG_TAG
21    #define LOG_TAG "OpenGLRenderer"
22#endif
23
24#include <utils/Singleton.h>
25
26#include "FontRenderer.h"
27#include "GammaFontRenderer.h"
28#include "TextureCache.h"
29#include "LayerCache.h"
30#include "GradientCache.h"
31#include "PatchCache.h"
32#include "ProgramCache.h"
33#include "PathCache.h"
34#include "TextDropShadowCache.h"
35#include "FboCache.h"
36#include "Line.h"
37
38namespace android {
39namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Globals
43///////////////////////////////////////////////////////////////////////////////
44
45#define REQUIRED_TEXTURE_UNITS_COUNT 3
46
47// Generates simple and textured vertices
48#define FV(x, y, u, v) { { x, y }, { u, v } }
49
50// This array is never used directly but used as a memcpy source in the
51// OpenGLRenderer constructor
52static const TextureVertex gMeshVertices[] = {
53        FV(0.0f, 0.0f, 0.0f, 0.0f),
54        FV(1.0f, 0.0f, 1.0f, 0.0f),
55        FV(0.0f, 1.0f, 0.0f, 1.0f),
56        FV(1.0f, 1.0f, 1.0f, 1.0f)
57};
58static const GLsizei gMeshStride = sizeof(TextureVertex);
59static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
60static const GLsizei gMeshCount = 4;
61
62///////////////////////////////////////////////////////////////////////////////
63// Debug
64///////////////////////////////////////////////////////////////////////////////
65
66struct CacheLogger {
67    CacheLogger() {
68        LOGD("Creating caches");
69    }
70}; // struct CacheLogger
71
72///////////////////////////////////////////////////////////////////////////////
73// Caches
74///////////////////////////////////////////////////////////////////////////////
75
76class Caches: public Singleton<Caches> {
77    Caches(): Singleton<Caches>(), blend(false), lastSrcMode(GL_ZERO),
78            lastDstMode(GL_ZERO), currentProgram(NULL) {
79        GLint maxTextureUnits;
80        glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
81        if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
82            LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
83        }
84
85        glGenBuffers(1, &meshBuffer);
86        glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
87        glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
88
89        currentBuffer = meshBuffer;
90    }
91
92    friend class Singleton<Caches>;
93
94    CacheLogger logger;
95
96public:
97    /**
98     * Binds the VBO used to render simple textured quads.
99     */
100    inline void bindMeshBuffer() {
101        bindMeshBuffer(meshBuffer);
102    }
103
104    /**
105     * Binds the specified VBO.
106     */
107    inline void bindMeshBuffer(const GLuint buffer) {
108        if (currentBuffer != buffer) {
109            glBindBuffer(GL_ARRAY_BUFFER, buffer);
110            currentBuffer = buffer;
111        }
112    }
113
114    /**
115     * Unbinds the VBO used to render simple textured quads.
116     */
117    inline void unbindMeshBuffer() {
118        if (currentBuffer) {
119            glBindBuffer(GL_ARRAY_BUFFER, 0);
120            currentBuffer = 0;
121        }
122    }
123
124    bool blend;
125    GLenum lastSrcMode;
126    GLenum lastDstMode;
127    Program* currentProgram;
128
129    GLuint meshBuffer;
130    GLuint currentBuffer;
131
132    TextureCache textureCache;
133    LayerCache layerCache;
134    GradientCache gradientCache;
135    ProgramCache programCache;
136    PathCache pathCache;
137    PatchCache patchCache;
138    TextDropShadowCache dropShadowCache;
139    FboCache fboCache;
140    GammaFontRenderer fontRenderer;
141
142    Line line;
143}; // class Caches
144
145}; // namespace uirenderer
146
147#ifdef USE_OPENGL_RENDERER
148using namespace uirenderer;
149ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
150#endif
151
152}; // namespace android
153
154#endif // ANDROID_UI_CACHES_H
155