blob: 7e645d298c5c1a5c8407eccb6761b8f57902428e [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 Guy5bb3c732012-11-29 17:52:58 -0800129bool Caches::initProperties() {
130 bool prevDebugLayersUpdates = debugLayersUpdates;
131 bool prevDebugOverdraw = debugOverdraw;
132
Romain Guy4ff0cf42012-08-06 14:51:10 -0700133 char property[PROPERTY_VALUE_MAX];
134 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
135 INIT_LOGD(" Layers updates debug enabled: %s", property);
136 debugLayersUpdates = !strcmp(property, "true");
137 } else {
138 debugLayersUpdates = false;
139 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700140
141 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
142 INIT_LOGD(" Overdraw debug enabled: %s", property);
143 debugOverdraw = !strcmp(property, "true");
144 } else {
145 debugOverdraw = false;
146 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800147
148 return (prevDebugLayersUpdates != debugLayersUpdates) ||
149 (prevDebugOverdraw != debugOverdraw);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700150}
151
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800152void Caches::terminate() {
153 if (!mInitialized) return;
154
155 glDeleteBuffers(1, &meshBuffer);
156 mCurrentBuffer = 0;
157
158 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700159 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800160 mRegionMesh = NULL;
161
162 fboCache.clear();
163
164 programCache.clear();
165 currentProgram = NULL;
166
167 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700168}
169
170///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800171// Debug
172///////////////////////////////////////////////////////////////////////////////
173
174void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700175 String8 stringLog;
176 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000177 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700178}
179
180void Caches::dumpMemoryUsage(String8 &log) {
181 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
182 log.appendFormat(" TextureCache %8d / %8d\n",
183 textureCache.getSize(), textureCache.getMaxSize());
184 log.appendFormat(" LayerCache %8d / %8d\n",
185 layerCache.getSize(), layerCache.getMaxSize());
186 log.appendFormat(" GradientCache %8d / %8d\n",
187 gradientCache.getSize(), gradientCache.getMaxSize());
188 log.appendFormat(" PathCache %8d / %8d\n",
189 pathCache.getSize(), pathCache.getMaxSize());
190 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800191 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700192 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800193 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700194 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800195 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700196 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800197 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700198 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800199 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700200 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800201 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700202 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
203 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700204 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800205 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700206 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700207 log.appendFormat(" FboCache %8d / %8d\n",
208 fboCache.getSize(), fboCache.getMaxSize());
209 log.appendFormat(" PatchCache %8d / %8d\n",
210 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800211
212 uint32_t total = 0;
213 total += textureCache.getSize();
214 total += layerCache.getSize();
215 total += gradientCache.getSize();
216 total += pathCache.getSize();
217 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800218 total += roundRectShapeCache.getSize();
219 total += circleShapeCache.getSize();
220 total += ovalShapeCache.getSize();
221 total += rectShapeCache.getSize();
222 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700223 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
224 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800225 }
226
Chet Haase9c1e23b2011-03-24 10:51:31 -0700227 log.appendFormat("Total memory usage:\n");
228 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800229}
230
231///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800232// Memory management
233///////////////////////////////////////////////////////////////////////////////
234
235void Caches::clearGarbage() {
236 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800237 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800238
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700239 Vector<DisplayList*> displayLists;
240 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800241
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700242 { // scope for the lock
243 Mutex::Autolock _l(mGarbageLock);
244 displayLists = mDisplayListGarbage;
245 layers = mLayerGarbage;
246 mDisplayListGarbage.clear();
247 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800248 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800249
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700250 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800251 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700252 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800253 delete displayList;
254 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700255
256 count = layers.size();
257 for (size_t i = 0; i < count; i++) {
258 Layer* layer = layers.itemAt(i);
259 delete layer;
260 }
261 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800262}
263
Romain Guyada830f2011-01-13 12:13:20 -0800264void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800265 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800266 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800267}
268
Romain Guybb0acdf2012-03-05 13:44:35 -0800269void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
270 Mutex::Autolock _l(mGarbageLock);
271 mDisplayListGarbage.push(displayList);
272}
273
Romain Guybdf76092011-07-18 15:00:43 -0700274void Caches::flush(FlushMode mode) {
275 FLUSH_LOGD("Flushing caches (mode %d)", mode);
276
Romain Guybdf76092011-07-18 15:00:43 -0700277 switch (mode) {
278 case kFlushMode_Full:
279 textureCache.clear();
280 patchCache.clear();
281 dropShadowCache.clear();
282 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700283 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700284 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700285 // fall through
286 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700287 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700288 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700289 pathCache.clear();
290 roundRectShapeCache.clear();
291 circleShapeCache.clear();
292 ovalShapeCache.clear();
293 rectShapeCache.clear();
294 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700295 // fall through
296 case kFlushMode_Layers:
297 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700298 break;
299 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700300
301 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700302}
303
Romain Guyfe48f652010-11-11 15:36:56 -0800304///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700305// VBO
306///////////////////////////////////////////////////////////////////////////////
307
Romain Guyf3a910b42011-12-12 20:35:21 -0800308bool Caches::bindMeshBuffer() {
309 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700310}
311
Romain Guyf3a910b42011-12-12 20:35:21 -0800312bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700313 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700314 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700315 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800316 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700317 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800318 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700319}
320
Romain Guyf3a910b42011-12-12 20:35:21 -0800321bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700322 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700323 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700324 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800325 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700326 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800327 return false;
328}
329
Romain Guy15bc6432011-12-13 13:11:32 -0800330bool Caches::bindIndicesBuffer(const GLuint buffer) {
331 if (mCurrentIndicesBuffer != buffer) {
332 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
333 mCurrentIndicesBuffer = buffer;
334 return true;
335 }
336 return false;
337}
338
339bool Caches::unbindIndicesBuffer() {
340 if (mCurrentIndicesBuffer) {
341 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
342 mCurrentIndicesBuffer = 0;
343 return true;
344 }
345 return false;
346}
347
Romain Guy85ef80d2012-09-13 20:26:50 -0700348///////////////////////////////////////////////////////////////////////////////
349// Meshes and textures
350///////////////////////////////////////////////////////////////////////////////
351
Chris Craikcb4d6002012-09-25 12:00:29 -0700352void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
353 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
354 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800355 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
356 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700357 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800358 }
359}
360
Chris Craikcb4d6002012-09-25 12:00:29 -0700361void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800362 if (force || vertices != mCurrentTexCoordsPointer) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700363 GLuint slot = currentProgram->texCoords;
Romain Guyf3a910b42011-12-12 20:35:21 -0800364 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
365 mCurrentTexCoordsPointer = vertices;
366 }
367}
368
369void Caches::resetVertexPointers() {
370 mCurrentPositionPointer = this;
371 mCurrentTexCoordsPointer = this;
372}
373
374void Caches::resetTexCoordsVertexPointer() {
375 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700376}
377
Romain Guy15bc6432011-12-13 13:11:32 -0800378void Caches::enableTexCoordsVertexArray() {
379 if (!mTexCoordsArrayEnabled) {
380 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800381 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800382 mTexCoordsArrayEnabled = true;
383 }
384}
385
386void Caches::disbaleTexCoordsVertexArray() {
387 if (mTexCoordsArrayEnabled) {
388 glDisableVertexAttribArray(Program::kBindingTexCoords);
389 mTexCoordsArrayEnabled = false;
390 }
391}
392
Romain Guya1d3c912011-12-13 14:55:06 -0800393void Caches::activeTexture(GLuint textureUnit) {
394 if (mTextureUnit != textureUnit) {
395 glActiveTexture(gTextureUnits[textureUnit]);
396 mTextureUnit = textureUnit;
397 }
398}
399
Romain Guy85ef80d2012-09-13 20:26:50 -0700400///////////////////////////////////////////////////////////////////////////////
401// Scissor
402///////////////////////////////////////////////////////////////////////////////
403
Romain Guy8a4ac612012-07-17 17:32:48 -0700404bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700405 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
406 width != mScissorWidth || height != mScissorHeight)) {
407
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700408 if (x < 0) {
409 width += x;
410 x = 0;
411 }
412 if (y < 0) {
413 height += y;
414 y = 0;
415 }
416 if (width < 0) {
417 width = 0;
418 }
419 if (height < 0) {
420 height = 0;
421 }
Romain Guy8f85e802011-12-14 19:23:32 -0800422 glScissor(x, y, width, height);
423
424 mScissorX = x;
425 mScissorY = y;
426 mScissorWidth = width;
427 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700428
429 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800430 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700431 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800432}
433
Romain Guy8a4ac612012-07-17 17:32:48 -0700434bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700435 if (!scissorEnabled) {
436 glEnable(GL_SCISSOR_TEST);
437 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700438 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700439 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700440 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700441 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700442}
443
Romain Guy8a4ac612012-07-17 17:32:48 -0700444bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700445 if (scissorEnabled) {
446 glDisable(GL_SCISSOR_TEST);
447 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700448 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700449 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700450 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700451}
452
453void Caches::setScissorEnabled(bool enabled) {
454 if (scissorEnabled != enabled) {
455 if (enabled) glEnable(GL_SCISSOR_TEST);
456 else glDisable(GL_SCISSOR_TEST);
457 scissorEnabled = enabled;
458 }
459}
460
Romain Guy82bc7a72012-01-03 14:13:39 -0800461void Caches::resetScissor() {
462 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
463}
464
Romain Guy85ef80d2012-09-13 20:26:50 -0700465///////////////////////////////////////////////////////////////////////////////
466// Tiling
467///////////////////////////////////////////////////////////////////////////////
468
Romain Guyf735c8e2013-01-31 17:45:55 -0800469void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy4285de32012-09-23 14:46:35 -0700470 if (extensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800471 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700472 }
473}
474
475void Caches::endTiling() {
Romain Guy4285de32012-09-23 14:46:35 -0700476 if (extensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700477 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700478 }
479}
480
Romain Guy54c1a642012-09-27 17:55:46 -0700481bool Caches::hasRegisteredFunctors() {
482 return mFunctorsCount > 0;
483}
484
485void Caches::registerFunctors(uint32_t functorCount) {
486 mFunctorsCount += functorCount;
487}
488
489void Caches::unregisterFunctors(uint32_t functorCount) {
490 if (functorCount > mFunctorsCount) {
491 mFunctorsCount = 0;
492 } else {
493 mFunctorsCount -= functorCount;
494 }
495}
496
Romain Guy85ef80d2012-09-13 20:26:50 -0700497///////////////////////////////////////////////////////////////////////////////
498// Regions
499///////////////////////////////////////////////////////////////////////////////
500
Romain Guy5b3b3522010-10-27 18:57:51 -0700501TextureVertex* Caches::getRegionMesh() {
502 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
503 if (!mRegionMesh) {
504 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
505
506 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
507 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
508 uint16_t quad = i * 4;
509 int index = i * 6;
510 regionIndices[index ] = quad; // top-left
511 regionIndices[index + 1] = quad + 1; // top-right
512 regionIndices[index + 2] = quad + 2; // bottom-left
513 regionIndices[index + 3] = quad + 2; // bottom-left
514 regionIndices[index + 4] = quad + 1; // top-right
515 regionIndices[index + 5] = quad + 3; // bottom-right
516 }
517
518 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800519 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700520 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
521 regionIndices, GL_STATIC_DRAW);
522
523 delete[] regionIndices;
524 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800525 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700526 }
527
528 return mRegionMesh;
529}
530
Chet Haasedd78cca2010-10-22 18:59:26 -0700531}; // namespace uirenderer
532}; // namespace android