blob: 492bb7d171f543bce368896359bf9d2343859a40 [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 Guy3bbacf22013-02-06 16:51:04 -080050Caches::Caches(): Singleton<Caches>(), mExtensions(Extensions::getInstance()), 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 Guyc2a97212013-02-06 15:29:46 -080092 debugLayersUpdates = false;
93 debugOverdraw = false;
94
Romain Guy8ff6b9e2011-11-09 20:10:18 -080095 mInitialized = true;
96}
97
Romain Guyb1d0a4e2012-07-13 18:25:35 -070098void Caches::initFont() {
99 fontRenderer = GammaFontRenderer::createRenderer();
100}
101
Romain Guydfa10462012-05-12 16:18:58 -0700102void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800103 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700104 eventMark = glInsertEventMarkerEXT;
105 startMark = glPushGroupMarkerEXT;
106 endMark = glPopGroupMarkerEXT;
107 } else {
108 eventMark = eventMarkNull;
109 startMark = startMarkNull;
110 endMark = endMarkNull;
111 }
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113 if (mExtensions.hasDebugLabel()) {
Romain Guydfa10462012-05-12 16:18:58 -0700114 setLabel = glLabelObjectEXT;
115 getLabel = glGetObjectLabelEXT;
116 } else {
117 setLabel = setLabelNull;
118 getLabel = getLabelNull;
119 }
120}
121
122void Caches::initConstraints() {
123 GLint maxTextureUnits;
124 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
125 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
126 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
127 }
128
129 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
130}
131
Romain Guy5bb3c732012-11-29 17:52:58 -0800132bool Caches::initProperties() {
133 bool prevDebugLayersUpdates = debugLayersUpdates;
134 bool prevDebugOverdraw = debugOverdraw;
135
Romain Guy4ff0cf42012-08-06 14:51:10 -0700136 char property[PROPERTY_VALUE_MAX];
137 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
138 INIT_LOGD(" Layers updates debug enabled: %s", property);
139 debugLayersUpdates = !strcmp(property, "true");
140 } else {
141 debugLayersUpdates = false;
142 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700143
144 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
145 INIT_LOGD(" Overdraw debug enabled: %s", property);
146 debugOverdraw = !strcmp(property, "true");
147 } else {
148 debugOverdraw = false;
149 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800150
151 return (prevDebugLayersUpdates != debugLayersUpdates) ||
152 (prevDebugOverdraw != debugOverdraw);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700153}
154
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800155void Caches::terminate() {
156 if (!mInitialized) return;
157
158 glDeleteBuffers(1, &meshBuffer);
159 mCurrentBuffer = 0;
160
161 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700162 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800163 mRegionMesh = NULL;
164
165 fboCache.clear();
166
167 programCache.clear();
168 currentProgram = NULL;
169
170 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700171}
172
173///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800174// Debug
175///////////////////////////////////////////////////////////////////////////////
176
177void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700178 String8 stringLog;
179 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000180 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700181}
182
183void Caches::dumpMemoryUsage(String8 &log) {
184 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
185 log.appendFormat(" TextureCache %8d / %8d\n",
186 textureCache.getSize(), textureCache.getMaxSize());
187 log.appendFormat(" LayerCache %8d / %8d\n",
188 layerCache.getSize(), layerCache.getMaxSize());
189 log.appendFormat(" GradientCache %8d / %8d\n",
190 gradientCache.getSize(), gradientCache.getMaxSize());
191 log.appendFormat(" PathCache %8d / %8d\n",
192 pathCache.getSize(), pathCache.getMaxSize());
193 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800194 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700195 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800196 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700197 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800198 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700199 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800200 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700201 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800202 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700203 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800204 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700205 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
206 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700207 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800208 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700209 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700210 log.appendFormat(" FboCache %8d / %8d\n",
211 fboCache.getSize(), fboCache.getMaxSize());
212 log.appendFormat(" PatchCache %8d / %8d\n",
213 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800214
215 uint32_t total = 0;
216 total += textureCache.getSize();
217 total += layerCache.getSize();
218 total += gradientCache.getSize();
219 total += pathCache.getSize();
220 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800221 total += roundRectShapeCache.getSize();
222 total += circleShapeCache.getSize();
223 total += ovalShapeCache.getSize();
224 total += rectShapeCache.getSize();
225 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700226 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
227 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800228 }
229
Chet Haase9c1e23b2011-03-24 10:51:31 -0700230 log.appendFormat("Total memory usage:\n");
231 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800232}
233
234///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800235// Memory management
236///////////////////////////////////////////////////////////////////////////////
237
238void Caches::clearGarbage() {
239 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800240 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800241
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700242 Vector<DisplayList*> displayLists;
243 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800244
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700245 { // scope for the lock
246 Mutex::Autolock _l(mGarbageLock);
247 displayLists = mDisplayListGarbage;
248 layers = mLayerGarbage;
249 mDisplayListGarbage.clear();
250 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800251 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800252
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700253 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800254 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700255 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800256 delete displayList;
257 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700258
259 count = layers.size();
260 for (size_t i = 0; i < count; i++) {
261 Layer* layer = layers.itemAt(i);
262 delete layer;
263 }
264 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800265}
266
Romain Guyada830f2011-01-13 12:13:20 -0800267void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800268 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800269 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800270}
271
Romain Guybb0acdf2012-03-05 13:44:35 -0800272void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
273 Mutex::Autolock _l(mGarbageLock);
274 mDisplayListGarbage.push(displayList);
275}
276
Romain Guybdf76092011-07-18 15:00:43 -0700277void Caches::flush(FlushMode mode) {
278 FLUSH_LOGD("Flushing caches (mode %d)", mode);
279
Romain Guybdf76092011-07-18 15:00:43 -0700280 switch (mode) {
281 case kFlushMode_Full:
282 textureCache.clear();
283 patchCache.clear();
284 dropShadowCache.clear();
285 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700286 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700287 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700288 // fall through
289 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700290 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700291 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700292 pathCache.clear();
293 roundRectShapeCache.clear();
294 circleShapeCache.clear();
295 ovalShapeCache.clear();
296 rectShapeCache.clear();
297 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700298 // fall through
299 case kFlushMode_Layers:
300 layerCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700301 break;
302 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700303
304 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700305}
306
Romain Guyfe48f652010-11-11 15:36:56 -0800307///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700308// VBO
309///////////////////////////////////////////////////////////////////////////////
310
Romain Guyf3a910b42011-12-12 20:35:21 -0800311bool Caches::bindMeshBuffer() {
312 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700313}
314
Romain Guyf3a910b42011-12-12 20:35:21 -0800315bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700316 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700317 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700318 mCurrentBuffer = buffer;
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;
Chet Haasedd78cca2010-10-22 18:59:26 -0700322}
323
Romain Guyf3a910b42011-12-12 20:35:21 -0800324bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700325 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700326 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700327 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800328 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700329 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800330 return false;
331}
332
Romain Guy15bc6432011-12-13 13:11:32 -0800333bool Caches::bindIndicesBuffer(const GLuint buffer) {
334 if (mCurrentIndicesBuffer != buffer) {
335 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
336 mCurrentIndicesBuffer = buffer;
337 return true;
338 }
339 return false;
340}
341
342bool Caches::unbindIndicesBuffer() {
343 if (mCurrentIndicesBuffer) {
344 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
345 mCurrentIndicesBuffer = 0;
346 return true;
347 }
348 return false;
349}
350
Romain Guy85ef80d2012-09-13 20:26:50 -0700351///////////////////////////////////////////////////////////////////////////////
352// Meshes and textures
353///////////////////////////////////////////////////////////////////////////////
354
Chris Craikcb4d6002012-09-25 12:00:29 -0700355void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
356 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
357 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800358 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
359 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700360 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800361 }
362}
363
Chris Craikcb4d6002012-09-25 12:00:29 -0700364void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800365 if (force || vertices != mCurrentTexCoordsPointer) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700366 GLuint slot = currentProgram->texCoords;
Romain Guyf3a910b42011-12-12 20:35:21 -0800367 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
368 mCurrentTexCoordsPointer = vertices;
369 }
370}
371
372void Caches::resetVertexPointers() {
373 mCurrentPositionPointer = this;
374 mCurrentTexCoordsPointer = this;
375}
376
377void Caches::resetTexCoordsVertexPointer() {
378 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700379}
380
Romain Guy15bc6432011-12-13 13:11:32 -0800381void Caches::enableTexCoordsVertexArray() {
382 if (!mTexCoordsArrayEnabled) {
383 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800384 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800385 mTexCoordsArrayEnabled = true;
386 }
387}
388
389void Caches::disbaleTexCoordsVertexArray() {
390 if (mTexCoordsArrayEnabled) {
391 glDisableVertexAttribArray(Program::kBindingTexCoords);
392 mTexCoordsArrayEnabled = false;
393 }
394}
395
Romain Guya1d3c912011-12-13 14:55:06 -0800396void Caches::activeTexture(GLuint textureUnit) {
397 if (mTextureUnit != textureUnit) {
398 glActiveTexture(gTextureUnits[textureUnit]);
399 mTextureUnit = textureUnit;
400 }
401}
402
Romain Guy85ef80d2012-09-13 20:26:50 -0700403///////////////////////////////////////////////////////////////////////////////
404// Scissor
405///////////////////////////////////////////////////////////////////////////////
406
Romain Guy8a4ac612012-07-17 17:32:48 -0700407bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700408 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
409 width != mScissorWidth || height != mScissorHeight)) {
410
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700411 if (x < 0) {
412 width += x;
413 x = 0;
414 }
415 if (y < 0) {
416 height += y;
417 y = 0;
418 }
419 if (width < 0) {
420 width = 0;
421 }
422 if (height < 0) {
423 height = 0;
424 }
Romain Guy8f85e802011-12-14 19:23:32 -0800425 glScissor(x, y, width, height);
426
427 mScissorX = x;
428 mScissorY = y;
429 mScissorWidth = width;
430 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700431
432 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800433 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700434 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800435}
436
Romain Guy8a4ac612012-07-17 17:32:48 -0700437bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700438 if (!scissorEnabled) {
439 glEnable(GL_SCISSOR_TEST);
440 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700441 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700442 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700443 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700444 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700445}
446
Romain Guy8a4ac612012-07-17 17:32:48 -0700447bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700448 if (scissorEnabled) {
449 glDisable(GL_SCISSOR_TEST);
450 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700451 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700452 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700453 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700454}
455
456void Caches::setScissorEnabled(bool enabled) {
457 if (scissorEnabled != enabled) {
458 if (enabled) glEnable(GL_SCISSOR_TEST);
459 else glDisable(GL_SCISSOR_TEST);
460 scissorEnabled = enabled;
461 }
462}
463
Romain Guy82bc7a72012-01-03 14:13:39 -0800464void Caches::resetScissor() {
465 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
466}
467
Romain Guy85ef80d2012-09-13 20:26:50 -0700468///////////////////////////////////////////////////////////////////////////////
469// Tiling
470///////////////////////////////////////////////////////////////////////////////
471
Romain Guyf735c8e2013-01-31 17:45:55 -0800472void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800473 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800474 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700475 }
476}
477
478void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800479 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700480 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700481 }
482}
483
Romain Guy54c1a642012-09-27 17:55:46 -0700484bool Caches::hasRegisteredFunctors() {
485 return mFunctorsCount > 0;
486}
487
488void Caches::registerFunctors(uint32_t functorCount) {
489 mFunctorsCount += functorCount;
490}
491
492void Caches::unregisterFunctors(uint32_t functorCount) {
493 if (functorCount > mFunctorsCount) {
494 mFunctorsCount = 0;
495 } else {
496 mFunctorsCount -= functorCount;
497 }
498}
499
Romain Guy85ef80d2012-09-13 20:26:50 -0700500///////////////////////////////////////////////////////////////////////////////
501// Regions
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guy5b3b3522010-10-27 18:57:51 -0700504TextureVertex* Caches::getRegionMesh() {
505 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
506 if (!mRegionMesh) {
507 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
508
509 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
510 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
511 uint16_t quad = i * 4;
512 int index = i * 6;
513 regionIndices[index ] = quad; // top-left
514 regionIndices[index + 1] = quad + 1; // top-right
515 regionIndices[index + 2] = quad + 2; // bottom-left
516 regionIndices[index + 3] = quad + 2; // bottom-left
517 regionIndices[index + 4] = quad + 1; // top-right
518 regionIndices[index + 5] = quad + 3; // bottom-right
519 }
520
521 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800522 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700523 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
524 regionIndices, GL_STATIC_DRAW);
525
526 delete[] regionIndices;
527 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800528 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700529 }
530
531 return mRegionMesh;
532}
533
Chet Haasedd78cca2010-10-22 18:59:26 -0700534}; // namespace uirenderer
535}; // namespace android