blob: 7853ae4168b98244df848f25b35052415b2d1ac5 [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -07001/*
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
Romain Guyc15008e2010-11-10 11:59:15 -080019#include <utils/Log.h>
Chet Haase9c1e23b2011-03-24 10:51:31 -070020#include <utils/String8.h>
Romain Guyc15008e2010-11-10 11:59:15 -080021
Chet Haasedd78cca2010-10-22 18:59:26 -070022#include "Caches.h"
Romain Guybb0acdf2012-03-05 13:44:35 -080023#include "DisplayListRenderer.h"
Romain Guye190aa62010-11-10 19:01:29 -080024#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080025#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070026
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070037// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070042#else
43 #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070047// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy8ff6b9e2011-11-09 20:10:18 -080050Caches::Caches(): Singleton<Caches>(), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080051 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070052 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070053 initExtensions();
54 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070055 initProperties();
Romain Guye190aa62010-11-10 19:01:29 -080056
57 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000058 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070059}
60
Romain Guy8ff6b9e2011-11-09 20:10:18 -080061void 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;
Romain Guy15bc6432011-12-13 13:11:32 -080069 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080070 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070071 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080072 mCurrentTexCoordsPointer = this;
73
Romain Guy15bc6432011-12-13 13:11:32 -080074 mTexCoordsArrayEnabled = false;
75
Romain Guyb1d0a4e2012-07-13 18:25:35 -070076 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070077 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080078 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
79
Romain Guya1d3c912011-12-13 14:55:06 -080080 glActiveTexture(gTextureUnits[0]);
81 mTextureUnit = 0;
82
Romain Guy8ff6b9e2011-11-09 20:10:18 -080083 mRegionMesh = NULL;
84
85 blend = false;
86 lastSrcMode = GL_ZERO;
87 lastDstMode = GL_ZERO;
88 currentProgram = NULL;
89
Romain Guy54c1a642012-09-27 17:55:46 -070090 mFunctorsCount = 0;
91
Romain Guy8ff6b9e2011-11-09 20:10:18 -080092 mInitialized = true;
93}
94
Romain Guyb1d0a4e2012-07-13 18:25:35 -070095void Caches::initFont() {
96 fontRenderer = GammaFontRenderer::createRenderer();
97}
98
Romain Guydfa10462012-05-12 16:18:58 -070099void Caches::initExtensions() {
100 if (extensions.hasDebugMarker()) {
101 eventMark = glInsertEventMarkerEXT;
102 startMark = glPushGroupMarkerEXT;
103 endMark = glPopGroupMarkerEXT;
104 } else {
105 eventMark = eventMarkNull;
106 startMark = startMarkNull;
107 endMark = endMarkNull;
108 }
109
110 if (extensions.hasDebugLabel()) {
111 setLabel = glLabelObjectEXT;
112 getLabel = glGetObjectLabelEXT;
113 } else {
114 setLabel = setLabelNull;
115 getLabel = getLabelNull;
116 }
117}
118
119void Caches::initConstraints() {
120 GLint maxTextureUnits;
121 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
122 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
123 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
124 }
125
126 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
127}
128
Romain Guy4ff0cf42012-08-06 14:51:10 -0700129void Caches::initProperties() {
130 char property[PROPERTY_VALUE_MAX];
131 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
132 INIT_LOGD(" Layers updates debug enabled: %s", property);
133 debugLayersUpdates = !strcmp(property, "true");
134 } else {
135 debugLayersUpdates = false;
136 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700137
138 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
139 INIT_LOGD(" Overdraw debug enabled: %s", property);
140 debugOverdraw = !strcmp(property, "true");
141 } else {
142 debugOverdraw = false;
143 }
Romain Guy4ff0cf42012-08-06 14:51:10 -0700144}
145
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800146void Caches::terminate() {
147 if (!mInitialized) return;
148
149 glDeleteBuffers(1, &meshBuffer);
150 mCurrentBuffer = 0;
151
152 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700153 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800154 mRegionMesh = NULL;
155
156 fboCache.clear();
157
158 programCache.clear();
159 currentProgram = NULL;
160
161 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700162}
163
164///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800165// Debug
166///////////////////////////////////////////////////////////////////////////////
167
168void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700169 String8 stringLog;
170 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000171 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700172}
173
174void Caches::dumpMemoryUsage(String8 &log) {
175 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
176 log.appendFormat(" TextureCache %8d / %8d\n",
177 textureCache.getSize(), textureCache.getMaxSize());
178 log.appendFormat(" LayerCache %8d / %8d\n",
179 layerCache.getSize(), layerCache.getMaxSize());
180 log.appendFormat(" GradientCache %8d / %8d\n",
181 gradientCache.getSize(), gradientCache.getMaxSize());
182 log.appendFormat(" PathCache %8d / %8d\n",
183 pathCache.getSize(), pathCache.getMaxSize());
184 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800185 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700186 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800187 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700188 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800189 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700190 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800191 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700192 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800193 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700194 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800195 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700196 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
197 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700198 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800199 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700200 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700201 log.appendFormat(" FboCache %8d / %8d\n",
202 fboCache.getSize(), fboCache.getMaxSize());
203 log.appendFormat(" PatchCache %8d / %8d\n",
204 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800205
206 uint32_t total = 0;
207 total += textureCache.getSize();
208 total += layerCache.getSize();
209 total += gradientCache.getSize();
210 total += pathCache.getSize();
211 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800212 total += roundRectShapeCache.getSize();
213 total += circleShapeCache.getSize();
214 total += ovalShapeCache.getSize();
215 total += rectShapeCache.getSize();
216 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700217 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
218 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800219 }
220
Chet Haase9c1e23b2011-03-24 10:51:31 -0700221 log.appendFormat("Total memory usage:\n");
222 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800223}
224
225///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800226// Memory management
227///////////////////////////////////////////////////////////////////////////////
228
229void Caches::clearGarbage() {
230 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800231 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800232
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700233 Vector<DisplayList*> displayLists;
234 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800235
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700236 { // scope for the lock
237 Mutex::Autolock _l(mGarbageLock);
238 displayLists = mDisplayListGarbage;
239 layers = mLayerGarbage;
240 mDisplayListGarbage.clear();
241 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800242 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800243
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700244 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800245 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700246 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800247 delete displayList;
248 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700249
250 count = layers.size();
251 for (size_t i = 0; i < count; i++) {
252 Layer* layer = layers.itemAt(i);
253 delete layer;
254 }
255 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800256}
257
Romain Guyada830f2011-01-13 12:13:20 -0800258void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800259 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800260 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800261}
262
Romain Guybb0acdf2012-03-05 13:44:35 -0800263void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
264 Mutex::Autolock _l(mGarbageLock);
265 mDisplayListGarbage.push(displayList);
266}
267
Romain Guybdf76092011-07-18 15:00:43 -0700268void Caches::flush(FlushMode mode) {
269 FLUSH_LOGD("Flushing caches (mode %d)", mode);
270
Romain Guybdf76092011-07-18 15:00:43 -0700271 switch (mode) {
272 case kFlushMode_Full:
273 textureCache.clear();
274 patchCache.clear();
275 dropShadowCache.clear();
276 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700277 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700278 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700279 // fall through
280 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700281 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700282 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700283 pathCache.clear();
284 roundRectShapeCache.clear();
285 circleShapeCache.clear();
286 ovalShapeCache.clear();
287 rectShapeCache.clear();
288 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700289 // fall through
290 case kFlushMode_Layers:
291 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700292 break;
293 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700294
295 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700296}
297
Romain Guyfe48f652010-11-11 15:36:56 -0800298///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700299// VBO
300///////////////////////////////////////////////////////////////////////////////
301
Romain Guyf3a910b42011-12-12 20:35:21 -0800302bool Caches::bindMeshBuffer() {
303 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700304}
305
Romain Guyf3a910b42011-12-12 20:35:21 -0800306bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700307 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700308 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700309 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800310 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700311 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800312 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700313}
314
Romain Guyf3a910b42011-12-12 20:35:21 -0800315bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700316 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700317 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700318 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800319 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700320 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800321 return false;
322}
323
Romain Guy15bc6432011-12-13 13:11:32 -0800324bool Caches::bindIndicesBuffer(const GLuint buffer) {
325 if (mCurrentIndicesBuffer != buffer) {
326 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
327 mCurrentIndicesBuffer = buffer;
328 return true;
329 }
330 return false;
331}
332
333bool Caches::unbindIndicesBuffer() {
334 if (mCurrentIndicesBuffer) {
335 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
336 mCurrentIndicesBuffer = 0;
337 return true;
338 }
339 return false;
340}
341
Romain Guy85ef80d2012-09-13 20:26:50 -0700342///////////////////////////////////////////////////////////////////////////////
343// Meshes and textures
344///////////////////////////////////////////////////////////////////////////////
345
Chris Craikcb4d6002012-09-25 12:00:29 -0700346void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
347 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
348 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800349 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
350 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700351 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800352 }
353}
354
Chris Craikcb4d6002012-09-25 12:00:29 -0700355void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800356 if (force || vertices != mCurrentTexCoordsPointer) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700357 GLuint slot = currentProgram->texCoords;
Romain Guyf3a910b42011-12-12 20:35:21 -0800358 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
359 mCurrentTexCoordsPointer = vertices;
360 }
361}
362
363void Caches::resetVertexPointers() {
364 mCurrentPositionPointer = this;
365 mCurrentTexCoordsPointer = this;
366}
367
368void Caches::resetTexCoordsVertexPointer() {
369 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700370}
371
Romain Guy15bc6432011-12-13 13:11:32 -0800372void Caches::enableTexCoordsVertexArray() {
373 if (!mTexCoordsArrayEnabled) {
374 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800375 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800376 mTexCoordsArrayEnabled = true;
377 }
378}
379
380void Caches::disbaleTexCoordsVertexArray() {
381 if (mTexCoordsArrayEnabled) {
382 glDisableVertexAttribArray(Program::kBindingTexCoords);
383 mTexCoordsArrayEnabled = false;
384 }
385}
386
Romain Guya1d3c912011-12-13 14:55:06 -0800387void Caches::activeTexture(GLuint textureUnit) {
388 if (mTextureUnit != textureUnit) {
389 glActiveTexture(gTextureUnits[textureUnit]);
390 mTextureUnit = textureUnit;
391 }
392}
393
Romain Guy85ef80d2012-09-13 20:26:50 -0700394///////////////////////////////////////////////////////////////////////////////
395// Scissor
396///////////////////////////////////////////////////////////////////////////////
397
Romain Guy8a4ac612012-07-17 17:32:48 -0700398bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700399 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
400 width != mScissorWidth || height != mScissorHeight)) {
401
Romain Guy35643dd2012-09-18 15:40:58 -0700402 if (x < 0) x = 0;
403 if (y < 0) y = 0;
404
Romain Guy8f85e802011-12-14 19:23:32 -0800405 glScissor(x, y, width, height);
406
407 mScissorX = x;
408 mScissorY = y;
409 mScissorWidth = width;
410 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700411
412 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800413 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700414 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800415}
416
Romain Guy8a4ac612012-07-17 17:32:48 -0700417bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700418 if (!scissorEnabled) {
419 glEnable(GL_SCISSOR_TEST);
420 scissorEnabled = true;
Romain Guy8a4ac612012-07-17 17:32:48 -0700421 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700422 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700423 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700424}
425
Romain Guy8a4ac612012-07-17 17:32:48 -0700426bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700427 if (scissorEnabled) {
428 glDisable(GL_SCISSOR_TEST);
429 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700430 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700431 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700432 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700433}
434
435void Caches::setScissorEnabled(bool enabled) {
436 if (scissorEnabled != enabled) {
437 if (enabled) glEnable(GL_SCISSOR_TEST);
438 else glDisable(GL_SCISSOR_TEST);
439 scissorEnabled = enabled;
440 }
441}
442
Romain Guy82bc7a72012-01-03 14:13:39 -0800443void Caches::resetScissor() {
444 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
445}
446
Romain Guy85ef80d2012-09-13 20:26:50 -0700447///////////////////////////////////////////////////////////////////////////////
448// Tiling
449///////////////////////////////////////////////////////////////////////////////
450
451void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool opaque) {
Romain Guy4285de32012-09-23 14:46:35 -0700452 if (extensions.hasTiledRendering() && !debugOverdraw) {
453 glStartTilingQCOM(x, y, width, height, (opaque ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700454 }
455}
456
457void Caches::endTiling() {
Romain Guy4285de32012-09-23 14:46:35 -0700458 if (extensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700459 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700460 }
461}
462
Romain Guy54c1a642012-09-27 17:55:46 -0700463bool Caches::hasRegisteredFunctors() {
464 return mFunctorsCount > 0;
465}
466
467void Caches::registerFunctors(uint32_t functorCount) {
468 mFunctorsCount += functorCount;
469}
470
471void Caches::unregisterFunctors(uint32_t functorCount) {
472 if (functorCount > mFunctorsCount) {
473 mFunctorsCount = 0;
474 } else {
475 mFunctorsCount -= functorCount;
476 }
477}
478
Romain Guy85ef80d2012-09-13 20:26:50 -0700479///////////////////////////////////////////////////////////////////////////////
480// Regions
481///////////////////////////////////////////////////////////////////////////////
482
Romain Guy5b3b3522010-10-27 18:57:51 -0700483TextureVertex* Caches::getRegionMesh() {
484 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
485 if (!mRegionMesh) {
486 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
487
488 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
489 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
490 uint16_t quad = i * 4;
491 int index = i * 6;
492 regionIndices[index ] = quad; // top-left
493 regionIndices[index + 1] = quad + 1; // top-right
494 regionIndices[index + 2] = quad + 2; // bottom-left
495 regionIndices[index + 3] = quad + 2; // bottom-left
496 regionIndices[index + 4] = quad + 1; // top-right
497 regionIndices[index + 5] = quad + 3; // bottom-right
498 }
499
500 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800501 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700502 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
503 regionIndices, GL_STATIC_DRAW);
504
505 delete[] regionIndices;
506 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800507 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700508 }
509
510 return mRegionMesh;
511}
512
Chet Haasedd78cca2010-10-22 18:59:26 -0700513}; // namespace uirenderer
514}; // namespace android