Caches.cpp revision 85ef80d8902afe8d26cb75fa7837fd9e6d019620
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#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/Log.h>
20#include <utils/String8.h>
21
22#include "Caches.h"
23#include "DisplayListRenderer.h"
24#include "Properties.h"
25#include "LayerRenderer.h"
26
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
41    #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
42#else
43    #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
47// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
50Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
51    init();
52    initFont();
53    initExtensions();
54    initConstraints();
55    initProperties();
56
57    mDebugLevel = readDebugLevel();
58    ALOGD("Enabling debug mode %d", mDebugLevel);
59}
60
61void Caches::init() {
62    if (mInitialized) return;
63
64    glGenBuffers(1, &meshBuffer);
65    glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
66    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
67
68    mCurrentBuffer = meshBuffer;
69    mCurrentIndicesBuffer = 0;
70    mCurrentPositionPointer = this;
71    mCurrentTexCoordsPointer = this;
72
73    mTexCoordsArrayEnabled = false;
74
75    glDisable(GL_SCISSOR_TEST);
76    scissorEnabled = false;
77    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
78
79    glActiveTexture(gTextureUnits[0]);
80    mTextureUnit = 0;
81
82    mRegionMesh = NULL;
83
84    blend = false;
85    lastSrcMode = GL_ZERO;
86    lastDstMode = GL_ZERO;
87    currentProgram = NULL;
88
89    mInitialized = true;
90}
91
92void Caches::initFont() {
93    fontRenderer = GammaFontRenderer::createRenderer();
94}
95
96void Caches::initExtensions() {
97    if (extensions.hasDebugMarker()) {
98        eventMark = glInsertEventMarkerEXT;
99        startMark = glPushGroupMarkerEXT;
100        endMark = glPopGroupMarkerEXT;
101    } else {
102        eventMark = eventMarkNull;
103        startMark = startMarkNull;
104        endMark = endMarkNull;
105    }
106
107    if (extensions.hasDebugLabel()) {
108        setLabel = glLabelObjectEXT;
109        getLabel = glGetObjectLabelEXT;
110    } else {
111        setLabel = setLabelNull;
112        getLabel = getLabelNull;
113    }
114}
115
116void Caches::initConstraints() {
117    GLint maxTextureUnits;
118    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
119    if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
120        ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
121    }
122
123    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
124}
125
126void Caches::initProperties() {
127    char property[PROPERTY_VALUE_MAX];
128    if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
129        INIT_LOGD("  Layers updates debug enabled: %s", property);
130        debugLayersUpdates = !strcmp(property, "true");
131    } else {
132        debugLayersUpdates = false;
133    }
134}
135
136void Caches::terminate() {
137    if (!mInitialized) return;
138
139    glDeleteBuffers(1, &meshBuffer);
140    mCurrentBuffer = 0;
141
142    glDeleteBuffers(1, &mRegionMeshIndices);
143    delete[] mRegionMesh;
144    mRegionMesh = NULL;
145
146    fboCache.clear();
147
148    programCache.clear();
149    currentProgram = NULL;
150
151    mInitialized = false;
152}
153
154///////////////////////////////////////////////////////////////////////////////
155// Debug
156///////////////////////////////////////////////////////////////////////////////
157
158void Caches::dumpMemoryUsage() {
159    String8 stringLog;
160    dumpMemoryUsage(stringLog);
161    ALOGD("%s", stringLog.string());
162}
163
164void Caches::dumpMemoryUsage(String8 &log) {
165    log.appendFormat("Current memory usage / total memory usage (bytes):\n");
166    log.appendFormat("  TextureCache         %8d / %8d\n",
167            textureCache.getSize(), textureCache.getMaxSize());
168    log.appendFormat("  LayerCache           %8d / %8d\n",
169            layerCache.getSize(), layerCache.getMaxSize());
170    log.appendFormat("  GradientCache        %8d / %8d\n",
171            gradientCache.getSize(), gradientCache.getMaxSize());
172    log.appendFormat("  PathCache            %8d / %8d\n",
173            pathCache.getSize(), pathCache.getMaxSize());
174    log.appendFormat("  CircleShapeCache     %8d / %8d\n",
175            circleShapeCache.getSize(), circleShapeCache.getMaxSize());
176    log.appendFormat("  OvalShapeCache       %8d / %8d\n",
177            ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
178    log.appendFormat("  RoundRectShapeCache  %8d / %8d\n",
179            roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
180    log.appendFormat("  RectShapeCache       %8d / %8d\n",
181            rectShapeCache.getSize(), rectShapeCache.getMaxSize());
182    log.appendFormat("  ArcShapeCache        %8d / %8d\n",
183            arcShapeCache.getSize(), arcShapeCache.getMaxSize());
184    log.appendFormat("  TextDropShadowCache  %8d / %8d\n", dropShadowCache.getSize(),
185            dropShadowCache.getMaxSize());
186    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
187        const uint32_t size = fontRenderer->getFontRendererSize(i);
188        log.appendFormat("  FontRenderer %d       %8d / %8d\n", i, size, size);
189    }
190    log.appendFormat("Other:\n");
191    log.appendFormat("  FboCache             %8d / %8d\n",
192            fboCache.getSize(), fboCache.getMaxSize());
193    log.appendFormat("  PatchCache           %8d / %8d\n",
194            patchCache.getSize(), patchCache.getMaxSize());
195
196    uint32_t total = 0;
197    total += textureCache.getSize();
198    total += layerCache.getSize();
199    total += gradientCache.getSize();
200    total += pathCache.getSize();
201    total += dropShadowCache.getSize();
202    total += roundRectShapeCache.getSize();
203    total += circleShapeCache.getSize();
204    total += ovalShapeCache.getSize();
205    total += rectShapeCache.getSize();
206    total += arcShapeCache.getSize();
207    for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
208        total += fontRenderer->getFontRendererSize(i);
209    }
210
211    log.appendFormat("Total memory usage:\n");
212    log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
213}
214
215///////////////////////////////////////////////////////////////////////////////
216// Memory management
217///////////////////////////////////////////////////////////////////////////////
218
219void Caches::clearGarbage() {
220    textureCache.clearGarbage();
221    pathCache.clearGarbage();
222
223    Mutex::Autolock _l(mGarbageLock);
224
225    size_t count = mLayerGarbage.size();
226    for (size_t i = 0; i < count; i++) {
227        Layer* layer = mLayerGarbage.itemAt(i);
228        LayerRenderer::destroyLayer(layer);
229    }
230    mLayerGarbage.clear();
231
232    count = mDisplayListGarbage.size();
233    for (size_t i = 0; i < count; i++) {
234        DisplayList* displayList = mDisplayListGarbage.itemAt(i);
235        delete displayList;
236    }
237    mDisplayListGarbage.clear();
238}
239
240void Caches::deleteLayerDeferred(Layer* layer) {
241    Mutex::Autolock _l(mGarbageLock);
242    mLayerGarbage.push(layer);
243}
244
245void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
246    Mutex::Autolock _l(mGarbageLock);
247    mDisplayListGarbage.push(displayList);
248}
249
250void Caches::flush(FlushMode mode) {
251    FLUSH_LOGD("Flushing caches (mode %d)", mode);
252
253    clearGarbage();
254
255    switch (mode) {
256        case kFlushMode_Full:
257            textureCache.clear();
258            patchCache.clear();
259            dropShadowCache.clear();
260            gradientCache.clear();
261            fontRenderer->clear();
262            dither.clear();
263            // fall through
264        case kFlushMode_Moderate:
265            fontRenderer->flush();
266            textureCache.flush();
267            pathCache.clear();
268            roundRectShapeCache.clear();
269            circleShapeCache.clear();
270            ovalShapeCache.clear();
271            rectShapeCache.clear();
272            arcShapeCache.clear();
273            // fall through
274        case kFlushMode_Layers:
275            layerCache.clear();
276            break;
277    }
278}
279
280///////////////////////////////////////////////////////////////////////////////
281// VBO
282///////////////////////////////////////////////////////////////////////////////
283
284bool Caches::bindMeshBuffer() {
285    return bindMeshBuffer(meshBuffer);
286}
287
288bool Caches::bindMeshBuffer(const GLuint buffer) {
289    if (mCurrentBuffer != buffer) {
290        glBindBuffer(GL_ARRAY_BUFFER, buffer);
291        mCurrentBuffer = buffer;
292        return true;
293    }
294    return false;
295}
296
297bool Caches::unbindMeshBuffer() {
298    if (mCurrentBuffer) {
299        glBindBuffer(GL_ARRAY_BUFFER, 0);
300        mCurrentBuffer = 0;
301        return true;
302    }
303    return false;
304}
305
306bool Caches::bindIndicesBuffer(const GLuint buffer) {
307    if (mCurrentIndicesBuffer != buffer) {
308        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
309        mCurrentIndicesBuffer = buffer;
310        return true;
311    }
312    return false;
313}
314
315bool Caches::unbindIndicesBuffer() {
316    if (mCurrentIndicesBuffer) {
317        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
318        mCurrentIndicesBuffer = 0;
319        return true;
320    }
321    return false;
322}
323
324///////////////////////////////////////////////////////////////////////////////
325// Meshes and textures
326///////////////////////////////////////////////////////////////////////////////
327
328void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
329    if (force || vertices != mCurrentPositionPointer) {
330        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
331        mCurrentPositionPointer = vertices;
332    }
333}
334
335void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
336    if (force || vertices != mCurrentTexCoordsPointer) {
337        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
338        mCurrentTexCoordsPointer = vertices;
339    }
340}
341
342void Caches::resetVertexPointers() {
343    mCurrentPositionPointer = this;
344    mCurrentTexCoordsPointer = this;
345}
346
347void Caches::resetTexCoordsVertexPointer() {
348    mCurrentTexCoordsPointer = this;
349}
350
351void Caches::enableTexCoordsVertexArray() {
352    if (!mTexCoordsArrayEnabled) {
353        glEnableVertexAttribArray(Program::kBindingTexCoords);
354        mCurrentTexCoordsPointer = this;
355        mTexCoordsArrayEnabled = true;
356    }
357}
358
359void Caches::disbaleTexCoordsVertexArray() {
360    if (mTexCoordsArrayEnabled) {
361        glDisableVertexAttribArray(Program::kBindingTexCoords);
362        mTexCoordsArrayEnabled = false;
363    }
364}
365
366void Caches::activeTexture(GLuint textureUnit) {
367    if (mTextureUnit != textureUnit) {
368        glActiveTexture(gTextureUnits[textureUnit]);
369        mTextureUnit = textureUnit;
370    }
371}
372
373///////////////////////////////////////////////////////////////////////////////
374// Scissor
375///////////////////////////////////////////////////////////////////////////////
376
377bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
378    if (scissorEnabled && (x != mScissorX || y != mScissorY ||
379            width != mScissorWidth || height != mScissorHeight)) {
380
381        glScissor(x, y, width, height);
382
383        mScissorX = x;
384        mScissorY = y;
385        mScissorWidth = width;
386        mScissorHeight = height;
387
388        return true;
389    }
390    return false;
391}
392
393bool Caches::enableScissor() {
394    if (!scissorEnabled) {
395        glEnable(GL_SCISSOR_TEST);
396        scissorEnabled = true;
397        return true;
398    }
399    return false;
400}
401
402bool Caches::disableScissor() {
403    if (scissorEnabled) {
404        glDisable(GL_SCISSOR_TEST);
405        scissorEnabled = false;
406        return true;
407    }
408    return false;
409}
410
411void Caches::setScissorEnabled(bool enabled) {
412    if (scissorEnabled != enabled) {
413        if (enabled) glEnable(GL_SCISSOR_TEST);
414        else glDisable(GL_SCISSOR_TEST);
415        scissorEnabled = enabled;
416    }
417}
418
419void Caches::resetScissor() {
420    mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
421}
422
423///////////////////////////////////////////////////////////////////////////////
424// Tiling
425///////////////////////////////////////////////////////////////////////////////
426
427void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool opaque) {
428    if (extensions.hasTiledRendering()) {
429
430    }
431}
432
433void Caches::endTiling() {
434    if (extensions.hasTiledRendering()) {
435
436    }
437}
438
439///////////////////////////////////////////////////////////////////////////////
440// Regions
441///////////////////////////////////////////////////////////////////////////////
442
443TextureVertex* Caches::getRegionMesh() {
444    // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
445    if (!mRegionMesh) {
446        mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
447
448        uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
449        for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
450            uint16_t quad = i * 4;
451            int index = i * 6;
452            regionIndices[index    ] = quad;       // top-left
453            regionIndices[index + 1] = quad + 1;   // top-right
454            regionIndices[index + 2] = quad + 2;   // bottom-left
455            regionIndices[index + 3] = quad + 2;   // bottom-left
456            regionIndices[index + 4] = quad + 1;   // top-right
457            regionIndices[index + 5] = quad + 3;   // bottom-right
458        }
459
460        glGenBuffers(1, &mRegionMeshIndices);
461        bindIndicesBuffer(mRegionMeshIndices);
462        glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
463                regionIndices, GL_STATIC_DRAW);
464
465        delete[] regionIndices;
466    } else {
467        bindIndicesBuffer(mRegionMeshIndices);
468    }
469
470    return mRegionMesh;
471}
472
473}; // namespace uirenderer
474}; // namespace android
475