blob: 4642a4f6eb3612d8f833aa81272b187bad4f15ea [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 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070054 initProperties();
Romain Guy0f667532013-03-01 14:31:04 -080055 initExtensions();
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;
Romain Guy0f667532013-03-01 14:31:04 -0800106
Chris Craikff785832013-03-08 13:12:16 -0800107 startMark = glPushGroupMarkerEXT;
108 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700109 } else {
110 eventMark = eventMarkNull;
111 startMark = startMarkNull;
112 endMark = endMarkNull;
113 }
114
Romain Guy0f667532013-03-01 14:31:04 -0800115 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700116 setLabel = glLabelObjectEXT;
117 getLabel = glGetObjectLabelEXT;
118 } else {
119 setLabel = setLabelNull;
120 getLabel = getLabelNull;
121 }
122}
123
124void Caches::initConstraints() {
125 GLint maxTextureUnits;
126 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
127 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
128 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
129 }
130
131 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
132}
133
Romain Guy5bb3c732012-11-29 17:52:58 -0800134bool Caches::initProperties() {
135 bool prevDebugLayersUpdates = debugLayersUpdates;
136 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800137 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800138
Romain Guy4ff0cf42012-08-06 14:51:10 -0700139 char property[PROPERTY_VALUE_MAX];
140 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
141 INIT_LOGD(" Layers updates debug enabled: %s", property);
142 debugLayersUpdates = !strcmp(property, "true");
143 } else {
144 debugLayersUpdates = false;
145 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700146
147 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
148 INIT_LOGD(" Overdraw debug enabled: %s", property);
149 debugOverdraw = !strcmp(property, "true");
150 } else {
151 debugOverdraw = false;
152 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800153
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800154 // See Properties.h for valid values
155 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
156 INIT_LOGD(" Stencil clip debug enabled: %s", property);
157 if (!strcmp(property, "hide")) {
158 debugStencilClip = kStencilHide;
159 } else if (!strcmp(property, "highlight")) {
160 debugStencilClip = kStencilShowHighlight;
161 } else if (!strcmp(property, "region")) {
162 debugStencilClip = kStencilShowRegion;
163 }
164 } else {
165 debugStencilClip = kStencilHide;
166 }
167
Romain Guy0f667532013-03-01 14:31:04 -0800168 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
169 drawDeferDisabled = !strcasecmp(property, "true");
170 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
171 } else {
172 INIT_LOGD(" Draw defer enabled");
173 }
174
175 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
176 drawReorderDisabled = !strcasecmp(property, "true");
177 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
178 } else {
179 INIT_LOGD(" Draw reorder enabled");
180 }
181
Romain Guy5bb3c732012-11-29 17:52:58 -0800182 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800183 (prevDebugOverdraw != debugOverdraw) ||
184 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700185}
186
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800187void Caches::terminate() {
188 if (!mInitialized) return;
189
190 glDeleteBuffers(1, &meshBuffer);
191 mCurrentBuffer = 0;
192
193 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700194 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800195 mRegionMesh = NULL;
196
197 fboCache.clear();
198
199 programCache.clear();
200 currentProgram = NULL;
201
202 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700203}
204
205///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800206// Debug
207///////////////////////////////////////////////////////////////////////////////
208
209void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700210 String8 stringLog;
211 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000212 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700213}
214
215void Caches::dumpMemoryUsage(String8 &log) {
216 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
217 log.appendFormat(" TextureCache %8d / %8d\n",
218 textureCache.getSize(), textureCache.getMaxSize());
219 log.appendFormat(" LayerCache %8d / %8d\n",
220 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800221 log.appendFormat(" RenderBufferCache %8d / %8d\n",
222 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700223 log.appendFormat(" GradientCache %8d / %8d\n",
224 gradientCache.getSize(), gradientCache.getMaxSize());
225 log.appendFormat(" PathCache %8d / %8d\n",
226 pathCache.getSize(), pathCache.getMaxSize());
227 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800228 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700229 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800230 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700231 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800232 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700233 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800234 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700235 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800236 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700237 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800238 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700239 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
240 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700241 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800242 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700243 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700244 log.appendFormat(" FboCache %8d / %8d\n",
245 fboCache.getSize(), fboCache.getMaxSize());
246 log.appendFormat(" PatchCache %8d / %8d\n",
247 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800248
249 uint32_t total = 0;
250 total += textureCache.getSize();
251 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800252 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800253 total += gradientCache.getSize();
254 total += pathCache.getSize();
255 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800256 total += roundRectShapeCache.getSize();
257 total += circleShapeCache.getSize();
258 total += ovalShapeCache.getSize();
259 total += rectShapeCache.getSize();
260 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700261 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
262 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800263 }
264
Chet Haase9c1e23b2011-03-24 10:51:31 -0700265 log.appendFormat("Total memory usage:\n");
266 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800267}
268
269///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800270// Memory management
271///////////////////////////////////////////////////////////////////////////////
272
273void Caches::clearGarbage() {
274 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800275 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800276
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700277 Vector<DisplayList*> displayLists;
278 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800279
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700280 { // scope for the lock
281 Mutex::Autolock _l(mGarbageLock);
282 displayLists = mDisplayListGarbage;
283 layers = mLayerGarbage;
284 mDisplayListGarbage.clear();
285 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800286 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800287
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700288 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800289 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700290 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800291 delete displayList;
292 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700293
294 count = layers.size();
295 for (size_t i = 0; i < count; i++) {
296 Layer* layer = layers.itemAt(i);
297 delete layer;
298 }
299 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800300}
301
Romain Guyada830f2011-01-13 12:13:20 -0800302void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800303 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800304 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800305}
306
Romain Guybb0acdf2012-03-05 13:44:35 -0800307void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
308 Mutex::Autolock _l(mGarbageLock);
309 mDisplayListGarbage.push(displayList);
310}
311
Romain Guybdf76092011-07-18 15:00:43 -0700312void Caches::flush(FlushMode mode) {
313 FLUSH_LOGD("Flushing caches (mode %d)", mode);
314
Romain Guybdf76092011-07-18 15:00:43 -0700315 switch (mode) {
316 case kFlushMode_Full:
317 textureCache.clear();
318 patchCache.clear();
319 dropShadowCache.clear();
320 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700321 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700322 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700323 // fall through
324 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700325 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700326 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700327 pathCache.clear();
328 roundRectShapeCache.clear();
329 circleShapeCache.clear();
330 ovalShapeCache.clear();
331 rectShapeCache.clear();
332 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700333 // fall through
334 case kFlushMode_Layers:
335 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800336 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700337 break;
338 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700339
340 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700341}
342
Romain Guyfe48f652010-11-11 15:36:56 -0800343///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700344// VBO
345///////////////////////////////////////////////////////////////////////////////
346
Romain Guyf3a910b42011-12-12 20:35:21 -0800347bool Caches::bindMeshBuffer() {
348 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700349}
350
Romain Guyf3a910b42011-12-12 20:35:21 -0800351bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700352 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700353 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700354 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800355 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700356 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800357 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700358}
359
Romain Guyf3a910b42011-12-12 20:35:21 -0800360bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700361 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700362 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700363 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800364 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700365 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 return false;
367}
368
Romain Guy15bc6432011-12-13 13:11:32 -0800369bool Caches::bindIndicesBuffer(const GLuint buffer) {
370 if (mCurrentIndicesBuffer != buffer) {
371 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
372 mCurrentIndicesBuffer = buffer;
373 return true;
374 }
375 return false;
376}
377
378bool Caches::unbindIndicesBuffer() {
379 if (mCurrentIndicesBuffer) {
380 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
381 mCurrentIndicesBuffer = 0;
382 return true;
383 }
384 return false;
385}
386
Romain Guy85ef80d2012-09-13 20:26:50 -0700387///////////////////////////////////////////////////////////////////////////////
388// Meshes and textures
389///////////////////////////////////////////////////////////////////////////////
390
Chris Craikcb4d6002012-09-25 12:00:29 -0700391void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
392 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
393 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800394 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
395 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700396 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800397 }
398}
399
Romain Guyff316ec2013-02-13 18:39:43 -0800400void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
401 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700402 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800403 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800404 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800405 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800406 }
407}
408
409void Caches::resetVertexPointers() {
410 mCurrentPositionPointer = this;
411 mCurrentTexCoordsPointer = this;
412}
413
414void Caches::resetTexCoordsVertexPointer() {
415 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700416}
417
Romain Guy15bc6432011-12-13 13:11:32 -0800418void Caches::enableTexCoordsVertexArray() {
419 if (!mTexCoordsArrayEnabled) {
420 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800421 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800422 mTexCoordsArrayEnabled = true;
423 }
424}
425
Romain Guyff316ec2013-02-13 18:39:43 -0800426void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800427 if (mTexCoordsArrayEnabled) {
428 glDisableVertexAttribArray(Program::kBindingTexCoords);
429 mTexCoordsArrayEnabled = false;
430 }
431}
432
Romain Guya1d3c912011-12-13 14:55:06 -0800433void Caches::activeTexture(GLuint textureUnit) {
434 if (mTextureUnit != textureUnit) {
435 glActiveTexture(gTextureUnits[textureUnit]);
436 mTextureUnit = textureUnit;
437 }
438}
439
Romain Guy85ef80d2012-09-13 20:26:50 -0700440///////////////////////////////////////////////////////////////////////////////
441// Scissor
442///////////////////////////////////////////////////////////////////////////////
443
Romain Guy8a4ac612012-07-17 17:32:48 -0700444bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700445 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
446 width != mScissorWidth || height != mScissorHeight)) {
447
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700448 if (x < 0) {
449 width += x;
450 x = 0;
451 }
452 if (y < 0) {
453 height += y;
454 y = 0;
455 }
456 if (width < 0) {
457 width = 0;
458 }
459 if (height < 0) {
460 height = 0;
461 }
Romain Guy8f85e802011-12-14 19:23:32 -0800462 glScissor(x, y, width, height);
463
464 mScissorX = x;
465 mScissorY = y;
466 mScissorWidth = width;
467 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700468
469 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800470 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700471 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800472}
473
Romain Guy8a4ac612012-07-17 17:32:48 -0700474bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700475 if (!scissorEnabled) {
476 glEnable(GL_SCISSOR_TEST);
477 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700478 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700479 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700480 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700481 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700482}
483
Romain Guy8a4ac612012-07-17 17:32:48 -0700484bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700485 if (scissorEnabled) {
486 glDisable(GL_SCISSOR_TEST);
487 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700488 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700489 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700490 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700491}
492
493void Caches::setScissorEnabled(bool enabled) {
494 if (scissorEnabled != enabled) {
495 if (enabled) glEnable(GL_SCISSOR_TEST);
496 else glDisable(GL_SCISSOR_TEST);
497 scissorEnabled = enabled;
498 }
499}
500
Romain Guy82bc7a72012-01-03 14:13:39 -0800501void Caches::resetScissor() {
502 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
503}
504
Romain Guy85ef80d2012-09-13 20:26:50 -0700505///////////////////////////////////////////////////////////////////////////////
506// Tiling
507///////////////////////////////////////////////////////////////////////////////
508
Romain Guyf735c8e2013-01-31 17:45:55 -0800509void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800510 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800511 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700512 }
513}
514
515void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800516 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700517 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700518 }
519}
520
Romain Guy54c1a642012-09-27 17:55:46 -0700521bool Caches::hasRegisteredFunctors() {
522 return mFunctorsCount > 0;
523}
524
525void Caches::registerFunctors(uint32_t functorCount) {
526 mFunctorsCount += functorCount;
527}
528
529void Caches::unregisterFunctors(uint32_t functorCount) {
530 if (functorCount > mFunctorsCount) {
531 mFunctorsCount = 0;
532 } else {
533 mFunctorsCount -= functorCount;
534 }
535}
536
Romain Guy85ef80d2012-09-13 20:26:50 -0700537///////////////////////////////////////////////////////////////////////////////
538// Regions
539///////////////////////////////////////////////////////////////////////////////
540
Romain Guy5b3b3522010-10-27 18:57:51 -0700541TextureVertex* Caches::getRegionMesh() {
542 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
543 if (!mRegionMesh) {
544 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
545
546 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
547 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
548 uint16_t quad = i * 4;
549 int index = i * 6;
550 regionIndices[index ] = quad; // top-left
551 regionIndices[index + 1] = quad + 1; // top-right
552 regionIndices[index + 2] = quad + 2; // bottom-left
553 regionIndices[index + 3] = quad + 2; // bottom-left
554 regionIndices[index + 4] = quad + 1; // top-right
555 regionIndices[index + 5] = quad + 3; // bottom-right
556 }
557
558 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800559 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700560 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
561 regionIndices, GL_STATIC_DRAW);
562
563 delete[] regionIndices;
564 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800565 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700566 }
567
568 return mRegionMesh;
569}
570
Chet Haasedd78cca2010-10-22 18:59:26 -0700571}; // namespace uirenderer
572}; // namespace android