blob: 74201d1fda5866afb03941ef582582499a428c5b [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());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800189 log.appendFormat(" RenderBufferCache %8d / %8d\n",
190 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700191 log.appendFormat(" GradientCache %8d / %8d\n",
192 gradientCache.getSize(), gradientCache.getMaxSize());
193 log.appendFormat(" PathCache %8d / %8d\n",
194 pathCache.getSize(), pathCache.getMaxSize());
195 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800196 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700197 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800198 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700199 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800200 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700201 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800202 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700203 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800204 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700205 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800206 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700207 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
208 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700209 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800210 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700211 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700212 log.appendFormat(" FboCache %8d / %8d\n",
213 fboCache.getSize(), fboCache.getMaxSize());
214 log.appendFormat(" PatchCache %8d / %8d\n",
215 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800216
217 uint32_t total = 0;
218 total += textureCache.getSize();
219 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800220 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800221 total += gradientCache.getSize();
222 total += pathCache.getSize();
223 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800224 total += roundRectShapeCache.getSize();
225 total += circleShapeCache.getSize();
226 total += ovalShapeCache.getSize();
227 total += rectShapeCache.getSize();
228 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700229 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
230 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800231 }
232
Chet Haase9c1e23b2011-03-24 10:51:31 -0700233 log.appendFormat("Total memory usage:\n");
234 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800235}
236
237///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800238// Memory management
239///////////////////////////////////////////////////////////////////////////////
240
241void Caches::clearGarbage() {
242 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800243 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800244
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700245 Vector<DisplayList*> displayLists;
246 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800247
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700248 { // scope for the lock
249 Mutex::Autolock _l(mGarbageLock);
250 displayLists = mDisplayListGarbage;
251 layers = mLayerGarbage;
252 mDisplayListGarbage.clear();
253 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800254 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800255
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700256 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800257 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700258 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800259 delete displayList;
260 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700261
262 count = layers.size();
263 for (size_t i = 0; i < count; i++) {
264 Layer* layer = layers.itemAt(i);
265 delete layer;
266 }
267 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800268}
269
Romain Guyada830f2011-01-13 12:13:20 -0800270void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800271 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800272 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800273}
274
Romain Guybb0acdf2012-03-05 13:44:35 -0800275void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
276 Mutex::Autolock _l(mGarbageLock);
277 mDisplayListGarbage.push(displayList);
278}
279
Romain Guybdf76092011-07-18 15:00:43 -0700280void Caches::flush(FlushMode mode) {
281 FLUSH_LOGD("Flushing caches (mode %d)", mode);
282
Romain Guybdf76092011-07-18 15:00:43 -0700283 switch (mode) {
284 case kFlushMode_Full:
285 textureCache.clear();
286 patchCache.clear();
287 dropShadowCache.clear();
288 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700289 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700290 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700291 // fall through
292 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700293 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700294 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700295 pathCache.clear();
296 roundRectShapeCache.clear();
297 circleShapeCache.clear();
298 ovalShapeCache.clear();
299 rectShapeCache.clear();
300 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700301 // fall through
302 case kFlushMode_Layers:
303 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800304 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700305 break;
306 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700307
308 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700309}
310
Romain Guyfe48f652010-11-11 15:36:56 -0800311///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700312// VBO
313///////////////////////////////////////////////////////////////////////////////
314
Romain Guyf3a910b42011-12-12 20:35:21 -0800315bool Caches::bindMeshBuffer() {
316 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700317}
318
Romain Guyf3a910b42011-12-12 20:35:21 -0800319bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700320 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700321 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700322 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800323 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700324 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800325 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700326}
327
Romain Guyf3a910b42011-12-12 20:35:21 -0800328bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700329 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700330 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700331 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800332 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700333 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800334 return false;
335}
336
Romain Guy15bc6432011-12-13 13:11:32 -0800337bool Caches::bindIndicesBuffer(const GLuint buffer) {
338 if (mCurrentIndicesBuffer != buffer) {
339 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
340 mCurrentIndicesBuffer = buffer;
341 return true;
342 }
343 return false;
344}
345
346bool Caches::unbindIndicesBuffer() {
347 if (mCurrentIndicesBuffer) {
348 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
349 mCurrentIndicesBuffer = 0;
350 return true;
351 }
352 return false;
353}
354
Romain Guy85ef80d2012-09-13 20:26:50 -0700355///////////////////////////////////////////////////////////////////////////////
356// Meshes and textures
357///////////////////////////////////////////////////////////////////////////////
358
Chris Craikcb4d6002012-09-25 12:00:29 -0700359void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
360 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
361 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800362 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
363 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700364 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800365 }
366}
367
Chris Craikcb4d6002012-09-25 12:00:29 -0700368void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800369 if (force || vertices != mCurrentTexCoordsPointer) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700370 GLuint slot = currentProgram->texCoords;
Romain Guyf3a910b42011-12-12 20:35:21 -0800371 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
372 mCurrentTexCoordsPointer = vertices;
373 }
374}
375
376void Caches::resetVertexPointers() {
377 mCurrentPositionPointer = this;
378 mCurrentTexCoordsPointer = this;
379}
380
381void Caches::resetTexCoordsVertexPointer() {
382 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700383}
384
Romain Guy15bc6432011-12-13 13:11:32 -0800385void Caches::enableTexCoordsVertexArray() {
386 if (!mTexCoordsArrayEnabled) {
387 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800388 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800389 mTexCoordsArrayEnabled = true;
390 }
391}
392
393void Caches::disbaleTexCoordsVertexArray() {
394 if (mTexCoordsArrayEnabled) {
395 glDisableVertexAttribArray(Program::kBindingTexCoords);
396 mTexCoordsArrayEnabled = false;
397 }
398}
399
Romain Guya1d3c912011-12-13 14:55:06 -0800400void Caches::activeTexture(GLuint textureUnit) {
401 if (mTextureUnit != textureUnit) {
402 glActiveTexture(gTextureUnits[textureUnit]);
403 mTextureUnit = textureUnit;
404 }
405}
406
Romain Guy85ef80d2012-09-13 20:26:50 -0700407///////////////////////////////////////////////////////////////////////////////
408// Scissor
409///////////////////////////////////////////////////////////////////////////////
410
Romain Guy8a4ac612012-07-17 17:32:48 -0700411bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700412 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
413 width != mScissorWidth || height != mScissorHeight)) {
414
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700415 if (x < 0) {
416 width += x;
417 x = 0;
418 }
419 if (y < 0) {
420 height += y;
421 y = 0;
422 }
423 if (width < 0) {
424 width = 0;
425 }
426 if (height < 0) {
427 height = 0;
428 }
Romain Guy8f85e802011-12-14 19:23:32 -0800429 glScissor(x, y, width, height);
430
431 mScissorX = x;
432 mScissorY = y;
433 mScissorWidth = width;
434 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700435
436 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800437 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700438 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800439}
440
Romain Guy8a4ac612012-07-17 17:32:48 -0700441bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700442 if (!scissorEnabled) {
443 glEnable(GL_SCISSOR_TEST);
444 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700445 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700446 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700447 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700448 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700449}
450
Romain Guy8a4ac612012-07-17 17:32:48 -0700451bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700452 if (scissorEnabled) {
453 glDisable(GL_SCISSOR_TEST);
454 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700455 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700456 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700457 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700458}
459
460void Caches::setScissorEnabled(bool enabled) {
461 if (scissorEnabled != enabled) {
462 if (enabled) glEnable(GL_SCISSOR_TEST);
463 else glDisable(GL_SCISSOR_TEST);
464 scissorEnabled = enabled;
465 }
466}
467
Romain Guy82bc7a72012-01-03 14:13:39 -0800468void Caches::resetScissor() {
469 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
470}
471
Romain Guy85ef80d2012-09-13 20:26:50 -0700472///////////////////////////////////////////////////////////////////////////////
473// Tiling
474///////////////////////////////////////////////////////////////////////////////
475
Romain Guyf735c8e2013-01-31 17:45:55 -0800476void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800477 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800478 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700479 }
480}
481
482void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800483 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700484 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700485 }
486}
487
Romain Guy54c1a642012-09-27 17:55:46 -0700488bool Caches::hasRegisteredFunctors() {
489 return mFunctorsCount > 0;
490}
491
492void Caches::registerFunctors(uint32_t functorCount) {
493 mFunctorsCount += functorCount;
494}
495
496void Caches::unregisterFunctors(uint32_t functorCount) {
497 if (functorCount > mFunctorsCount) {
498 mFunctorsCount = 0;
499 } else {
500 mFunctorsCount -= functorCount;
501 }
502}
503
Romain Guy85ef80d2012-09-13 20:26:50 -0700504///////////////////////////////////////////////////////////////////////////////
505// Regions
506///////////////////////////////////////////////////////////////////////////////
507
Romain Guy5b3b3522010-10-27 18:57:51 -0700508TextureVertex* Caches::getRegionMesh() {
509 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
510 if (!mRegionMesh) {
511 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
512
513 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
514 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
515 uint16_t quad = i * 4;
516 int index = i * 6;
517 regionIndices[index ] = quad; // top-left
518 regionIndices[index + 1] = quad + 1; // top-right
519 regionIndices[index + 2] = quad + 2; // bottom-left
520 regionIndices[index + 3] = quad + 2; // bottom-left
521 regionIndices[index + 4] = quad + 1; // top-right
522 regionIndices[index + 5] = quad + 3; // bottom-right
523 }
524
525 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800526 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700527 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
528 regionIndices, GL_STATIC_DRAW);
529
530 delete[] regionIndices;
531 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800532 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700533 }
534
535 return mRegionMesh;
536}
537
Chet Haasedd78cca2010-10-22 18:59:26 -0700538}; // namespace uirenderer
539}; // namespace android