blob: 5befb9518dd193ea9da0a4ac01c0bf1b03086efe [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;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080094 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080095
Romain Guy8ff6b9e2011-11-09 20:10:18 -080096 mInitialized = true;
97}
98
Romain Guyb1d0a4e2012-07-13 18:25:35 -070099void Caches::initFont() {
100 fontRenderer = GammaFontRenderer::createRenderer();
101}
102
Romain Guydfa10462012-05-12 16:18:58 -0700103void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800104 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700105 eventMark = glInsertEventMarkerEXT;
106 startMark = glPushGroupMarkerEXT;
107 endMark = glPopGroupMarkerEXT;
108 } else {
109 eventMark = eventMarkNull;
110 startMark = startMarkNull;
111 endMark = endMarkNull;
112 }
113
Romain Guy3bbacf22013-02-06 16:51:04 -0800114 if (mExtensions.hasDebugLabel()) {
Romain Guydfa10462012-05-12 16:18:58 -0700115 setLabel = glLabelObjectEXT;
116 getLabel = glGetObjectLabelEXT;
117 } else {
118 setLabel = setLabelNull;
119 getLabel = getLabelNull;
120 }
121}
122
123void Caches::initConstraints() {
124 GLint maxTextureUnits;
125 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
126 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
127 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
128 }
129
130 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
131}
132
Romain Guy5bb3c732012-11-29 17:52:58 -0800133bool Caches::initProperties() {
134 bool prevDebugLayersUpdates = debugLayersUpdates;
135 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800136 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800137
Romain Guy4ff0cf42012-08-06 14:51:10 -0700138 char property[PROPERTY_VALUE_MAX];
139 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
140 INIT_LOGD(" Layers updates debug enabled: %s", property);
141 debugLayersUpdates = !strcmp(property, "true");
142 } else {
143 debugLayersUpdates = false;
144 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700145
146 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
147 INIT_LOGD(" Overdraw debug enabled: %s", property);
148 debugOverdraw = !strcmp(property, "true");
149 } else {
150 debugOverdraw = false;
151 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800152
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800153 // See Properties.h for valid values
154 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
155 INIT_LOGD(" Stencil clip debug enabled: %s", property);
156 if (!strcmp(property, "hide")) {
157 debugStencilClip = kStencilHide;
158 } else if (!strcmp(property, "highlight")) {
159 debugStencilClip = kStencilShowHighlight;
160 } else if (!strcmp(property, "region")) {
161 debugStencilClip = kStencilShowRegion;
162 }
163 } else {
164 debugStencilClip = kStencilHide;
165 }
166
Romain Guy5bb3c732012-11-29 17:52:58 -0800167 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800168 (prevDebugOverdraw != debugOverdraw) ||
169 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700170}
171
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800172void Caches::terminate() {
173 if (!mInitialized) return;
174
175 glDeleteBuffers(1, &meshBuffer);
176 mCurrentBuffer = 0;
177
178 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700179 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800180 mRegionMesh = NULL;
181
182 fboCache.clear();
183
184 programCache.clear();
185 currentProgram = NULL;
186
187 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700188}
189
190///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800191// Debug
192///////////////////////////////////////////////////////////////////////////////
193
194void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700195 String8 stringLog;
196 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000197 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700198}
199
200void Caches::dumpMemoryUsage(String8 &log) {
201 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
202 log.appendFormat(" TextureCache %8d / %8d\n",
203 textureCache.getSize(), textureCache.getMaxSize());
204 log.appendFormat(" LayerCache %8d / %8d\n",
205 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800206 log.appendFormat(" RenderBufferCache %8d / %8d\n",
207 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700208 log.appendFormat(" GradientCache %8d / %8d\n",
209 gradientCache.getSize(), gradientCache.getMaxSize());
210 log.appendFormat(" PathCache %8d / %8d\n",
211 pathCache.getSize(), pathCache.getMaxSize());
212 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800213 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700214 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800215 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700216 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800217 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700218 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800219 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700220 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800221 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700222 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800223 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700224 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
225 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700226 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800227 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700228 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700229 log.appendFormat(" FboCache %8d / %8d\n",
230 fboCache.getSize(), fboCache.getMaxSize());
231 log.appendFormat(" PatchCache %8d / %8d\n",
232 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800233
234 uint32_t total = 0;
235 total += textureCache.getSize();
236 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800237 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800238 total += gradientCache.getSize();
239 total += pathCache.getSize();
240 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800241 total += roundRectShapeCache.getSize();
242 total += circleShapeCache.getSize();
243 total += ovalShapeCache.getSize();
244 total += rectShapeCache.getSize();
245 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700246 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
247 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800248 }
249
Chet Haase9c1e23b2011-03-24 10:51:31 -0700250 log.appendFormat("Total memory usage:\n");
251 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800252}
253
254///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800255// Memory management
256///////////////////////////////////////////////////////////////////////////////
257
258void Caches::clearGarbage() {
259 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800260 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800261
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700262 Vector<DisplayList*> displayLists;
263 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800264
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700265 { // scope for the lock
266 Mutex::Autolock _l(mGarbageLock);
267 displayLists = mDisplayListGarbage;
268 layers = mLayerGarbage;
269 mDisplayListGarbage.clear();
270 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800271 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800272
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700273 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800274 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700275 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800276 delete displayList;
277 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700278
279 count = layers.size();
280 for (size_t i = 0; i < count; i++) {
281 Layer* layer = layers.itemAt(i);
282 delete layer;
283 }
284 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800285}
286
Romain Guyada830f2011-01-13 12:13:20 -0800287void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800288 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800289 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800290}
291
Romain Guybb0acdf2012-03-05 13:44:35 -0800292void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
293 Mutex::Autolock _l(mGarbageLock);
294 mDisplayListGarbage.push(displayList);
295}
296
Romain Guybdf76092011-07-18 15:00:43 -0700297void Caches::flush(FlushMode mode) {
298 FLUSH_LOGD("Flushing caches (mode %d)", mode);
299
Romain Guybdf76092011-07-18 15:00:43 -0700300 switch (mode) {
301 case kFlushMode_Full:
302 textureCache.clear();
303 patchCache.clear();
304 dropShadowCache.clear();
305 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700306 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700307 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700308 // fall through
309 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700310 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700311 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700312 pathCache.clear();
313 roundRectShapeCache.clear();
314 circleShapeCache.clear();
315 ovalShapeCache.clear();
316 rectShapeCache.clear();
317 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700318 // fall through
319 case kFlushMode_Layers:
320 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800321 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700322 break;
323 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700324
325 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700326}
327
Romain Guyfe48f652010-11-11 15:36:56 -0800328///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700329// VBO
330///////////////////////////////////////////////////////////////////////////////
331
Romain Guyf3a910b42011-12-12 20:35:21 -0800332bool Caches::bindMeshBuffer() {
333 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700334}
335
Romain Guyf3a910b42011-12-12 20:35:21 -0800336bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700337 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700338 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700339 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800340 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700341 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800342 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700343}
344
Romain Guyf3a910b42011-12-12 20:35:21 -0800345bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700346 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700347 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700348 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800349 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700350 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800351 return false;
352}
353
Romain Guy15bc6432011-12-13 13:11:32 -0800354bool Caches::bindIndicesBuffer(const GLuint buffer) {
355 if (mCurrentIndicesBuffer != buffer) {
356 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
357 mCurrentIndicesBuffer = buffer;
358 return true;
359 }
360 return false;
361}
362
363bool Caches::unbindIndicesBuffer() {
364 if (mCurrentIndicesBuffer) {
365 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
366 mCurrentIndicesBuffer = 0;
367 return true;
368 }
369 return false;
370}
371
Romain Guy85ef80d2012-09-13 20:26:50 -0700372///////////////////////////////////////////////////////////////////////////////
373// Meshes and textures
374///////////////////////////////////////////////////////////////////////////////
375
Chris Craikcb4d6002012-09-25 12:00:29 -0700376void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
377 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
378 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800379 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
380 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700381 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800382 }
383}
384
Romain Guyff316ec2013-02-13 18:39:43 -0800385void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
386 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700387 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800388 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800389 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800390 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800391 }
392}
393
394void Caches::resetVertexPointers() {
395 mCurrentPositionPointer = this;
396 mCurrentTexCoordsPointer = this;
397}
398
399void Caches::resetTexCoordsVertexPointer() {
400 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700401}
402
Romain Guy15bc6432011-12-13 13:11:32 -0800403void Caches::enableTexCoordsVertexArray() {
404 if (!mTexCoordsArrayEnabled) {
405 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800406 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800407 mTexCoordsArrayEnabled = true;
408 }
409}
410
Romain Guyff316ec2013-02-13 18:39:43 -0800411void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800412 if (mTexCoordsArrayEnabled) {
413 glDisableVertexAttribArray(Program::kBindingTexCoords);
414 mTexCoordsArrayEnabled = false;
415 }
416}
417
Romain Guya1d3c912011-12-13 14:55:06 -0800418void Caches::activeTexture(GLuint textureUnit) {
419 if (mTextureUnit != textureUnit) {
420 glActiveTexture(gTextureUnits[textureUnit]);
421 mTextureUnit = textureUnit;
422 }
423}
424
Romain Guy85ef80d2012-09-13 20:26:50 -0700425///////////////////////////////////////////////////////////////////////////////
426// Scissor
427///////////////////////////////////////////////////////////////////////////////
428
Romain Guy8a4ac612012-07-17 17:32:48 -0700429bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700430 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
431 width != mScissorWidth || height != mScissorHeight)) {
432
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700433 if (x < 0) {
434 width += x;
435 x = 0;
436 }
437 if (y < 0) {
438 height += y;
439 y = 0;
440 }
441 if (width < 0) {
442 width = 0;
443 }
444 if (height < 0) {
445 height = 0;
446 }
Romain Guy8f85e802011-12-14 19:23:32 -0800447 glScissor(x, y, width, height);
448
449 mScissorX = x;
450 mScissorY = y;
451 mScissorWidth = width;
452 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700453
454 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800455 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700456 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800457}
458
Romain Guy8a4ac612012-07-17 17:32:48 -0700459bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700460 if (!scissorEnabled) {
461 glEnable(GL_SCISSOR_TEST);
462 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700463 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700464 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700465 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700466 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700467}
468
Romain Guy8a4ac612012-07-17 17:32:48 -0700469bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700470 if (scissorEnabled) {
471 glDisable(GL_SCISSOR_TEST);
472 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700473 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700474 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700475 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700476}
477
478void Caches::setScissorEnabled(bool enabled) {
479 if (scissorEnabled != enabled) {
480 if (enabled) glEnable(GL_SCISSOR_TEST);
481 else glDisable(GL_SCISSOR_TEST);
482 scissorEnabled = enabled;
483 }
484}
485
Romain Guy82bc7a72012-01-03 14:13:39 -0800486void Caches::resetScissor() {
487 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
488}
489
Romain Guy85ef80d2012-09-13 20:26:50 -0700490///////////////////////////////////////////////////////////////////////////////
491// Tiling
492///////////////////////////////////////////////////////////////////////////////
493
Romain Guyf735c8e2013-01-31 17:45:55 -0800494void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800495 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800496 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700497 }
498}
499
500void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800501 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700502 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700503 }
504}
505
Romain Guy54c1a642012-09-27 17:55:46 -0700506bool Caches::hasRegisteredFunctors() {
507 return mFunctorsCount > 0;
508}
509
510void Caches::registerFunctors(uint32_t functorCount) {
511 mFunctorsCount += functorCount;
512}
513
514void Caches::unregisterFunctors(uint32_t functorCount) {
515 if (functorCount > mFunctorsCount) {
516 mFunctorsCount = 0;
517 } else {
518 mFunctorsCount -= functorCount;
519 }
520}
521
Romain Guy85ef80d2012-09-13 20:26:50 -0700522///////////////////////////////////////////////////////////////////////////////
523// Regions
524///////////////////////////////////////////////////////////////////////////////
525
Romain Guy5b3b3522010-10-27 18:57:51 -0700526TextureVertex* Caches::getRegionMesh() {
527 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
528 if (!mRegionMesh) {
529 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
530
531 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
532 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
533 uint16_t quad = i * 4;
534 int index = i * 6;
535 regionIndices[index ] = quad; // top-left
536 regionIndices[index + 1] = quad + 1; // top-right
537 regionIndices[index + 2] = quad + 2; // bottom-left
538 regionIndices[index + 3] = quad + 2; // bottom-left
539 regionIndices[index + 4] = quad + 1; // top-right
540 regionIndices[index + 5] = quad + 3; // bottom-right
541 }
542
543 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800544 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700545 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
546 regionIndices, GL_STATIC_DRAW);
547
548 delete[] regionIndices;
549 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800550 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700551 }
552
553 return mRegionMesh;
554}
555
Chet Haasedd78cca2010-10-22 18:59:26 -0700556}; // namespace uirenderer
557}; // namespace android