blob: a1cc2e8df653c90073d850bb001d8a1bfa3ac39f [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 if ((drawDeferDisabled || drawReorderDisabled)) {
107 startMark = glPushGroupMarkerEXT;
108 endMark = glPopGroupMarkerEXT;
109 } else {
110 startMark = startMarkNull;
111 endMark = endMarkNull;
112 }
113
Romain Guydfa10462012-05-12 16:18:58 -0700114 } else {
115 eventMark = eventMarkNull;
116 startMark = startMarkNull;
117 endMark = endMarkNull;
118 }
119
Romain Guy0f667532013-03-01 14:31:04 -0800120 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700121 setLabel = glLabelObjectEXT;
122 getLabel = glGetObjectLabelEXT;
123 } else {
124 setLabel = setLabelNull;
125 getLabel = getLabelNull;
126 }
127}
128
129void Caches::initConstraints() {
130 GLint maxTextureUnits;
131 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
132 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
133 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
134 }
135
136 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
137}
138
Romain Guy5bb3c732012-11-29 17:52:58 -0800139bool Caches::initProperties() {
140 bool prevDebugLayersUpdates = debugLayersUpdates;
141 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800142 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800143
Romain Guy4ff0cf42012-08-06 14:51:10 -0700144 char property[PROPERTY_VALUE_MAX];
145 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
146 INIT_LOGD(" Layers updates debug enabled: %s", property);
147 debugLayersUpdates = !strcmp(property, "true");
148 } else {
149 debugLayersUpdates = false;
150 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700151
152 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
153 INIT_LOGD(" Overdraw debug enabled: %s", property);
154 debugOverdraw = !strcmp(property, "true");
155 } else {
156 debugOverdraw = false;
157 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800158
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800159 // See Properties.h for valid values
160 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
161 INIT_LOGD(" Stencil clip debug enabled: %s", property);
162 if (!strcmp(property, "hide")) {
163 debugStencilClip = kStencilHide;
164 } else if (!strcmp(property, "highlight")) {
165 debugStencilClip = kStencilShowHighlight;
166 } else if (!strcmp(property, "region")) {
167 debugStencilClip = kStencilShowRegion;
168 }
169 } else {
170 debugStencilClip = kStencilHide;
171 }
172
Romain Guy0f667532013-03-01 14:31:04 -0800173 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
174 drawDeferDisabled = !strcasecmp(property, "true");
175 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
176 } else {
177 INIT_LOGD(" Draw defer enabled");
178 }
179
180 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
181 drawReorderDisabled = !strcasecmp(property, "true");
182 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
183 } else {
184 INIT_LOGD(" Draw reorder enabled");
185 }
186
Romain Guy5bb3c732012-11-29 17:52:58 -0800187 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800188 (prevDebugOverdraw != debugOverdraw) ||
189 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700190}
191
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800192void Caches::terminate() {
193 if (!mInitialized) return;
194
195 glDeleteBuffers(1, &meshBuffer);
196 mCurrentBuffer = 0;
197
198 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700199 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800200 mRegionMesh = NULL;
201
202 fboCache.clear();
203
204 programCache.clear();
205 currentProgram = NULL;
206
207 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700208}
209
210///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800211// Debug
212///////////////////////////////////////////////////////////////////////////////
213
214void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700215 String8 stringLog;
216 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000217 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700218}
219
220void Caches::dumpMemoryUsage(String8 &log) {
221 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
222 log.appendFormat(" TextureCache %8d / %8d\n",
223 textureCache.getSize(), textureCache.getMaxSize());
224 log.appendFormat(" LayerCache %8d / %8d\n",
225 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800226 log.appendFormat(" RenderBufferCache %8d / %8d\n",
227 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700228 log.appendFormat(" GradientCache %8d / %8d\n",
229 gradientCache.getSize(), gradientCache.getMaxSize());
230 log.appendFormat(" PathCache %8d / %8d\n",
231 pathCache.getSize(), pathCache.getMaxSize());
232 log.appendFormat(" CircleShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800233 circleShapeCache.getSize(), circleShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 log.appendFormat(" OvalShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800235 ovalShapeCache.getSize(), ovalShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700236 log.appendFormat(" RoundRectShapeCache %8d / %8d\n",
Romain Guy01d58e42011-01-19 21:54:02 -0800237 roundRectShapeCache.getSize(), roundRectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700238 log.appendFormat(" RectShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800239 rectShapeCache.getSize(), rectShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700240 log.appendFormat(" ArcShapeCache %8d / %8d\n",
Romain Guy2fc941e2011-02-03 15:06:05 -0800241 arcShapeCache.getSize(), arcShapeCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700242 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800243 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700244 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
245 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700246 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800247 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700248 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700249 log.appendFormat(" FboCache %8d / %8d\n",
250 fboCache.getSize(), fboCache.getMaxSize());
251 log.appendFormat(" PatchCache %8d / %8d\n",
252 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800253
254 uint32_t total = 0;
255 total += textureCache.getSize();
256 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800257 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800258 total += gradientCache.getSize();
259 total += pathCache.getSize();
260 total += dropShadowCache.getSize();
Romain Guy2fc941e2011-02-03 15:06:05 -0800261 total += roundRectShapeCache.getSize();
262 total += circleShapeCache.getSize();
263 total += ovalShapeCache.getSize();
264 total += rectShapeCache.getSize();
265 total += arcShapeCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700266 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
267 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800268 }
269
Chet Haase9c1e23b2011-03-24 10:51:31 -0700270 log.appendFormat("Total memory usage:\n");
271 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800272}
273
274///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800275// Memory management
276///////////////////////////////////////////////////////////////////////////////
277
278void Caches::clearGarbage() {
279 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800280 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800281
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700282 Vector<DisplayList*> displayLists;
283 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800284
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700285 { // scope for the lock
286 Mutex::Autolock _l(mGarbageLock);
287 displayLists = mDisplayListGarbage;
288 layers = mLayerGarbage;
289 mDisplayListGarbage.clear();
290 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800291 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800292
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700293 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800294 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700295 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800296 delete displayList;
297 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700298
299 count = layers.size();
300 for (size_t i = 0; i < count; i++) {
301 Layer* layer = layers.itemAt(i);
302 delete layer;
303 }
304 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800305}
306
Romain Guyada830f2011-01-13 12:13:20 -0800307void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800308 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800309 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800310}
311
Romain Guybb0acdf2012-03-05 13:44:35 -0800312void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
313 Mutex::Autolock _l(mGarbageLock);
314 mDisplayListGarbage.push(displayList);
315}
316
Romain Guybdf76092011-07-18 15:00:43 -0700317void Caches::flush(FlushMode mode) {
318 FLUSH_LOGD("Flushing caches (mode %d)", mode);
319
Romain Guybdf76092011-07-18 15:00:43 -0700320 switch (mode) {
321 case kFlushMode_Full:
322 textureCache.clear();
323 patchCache.clear();
324 dropShadowCache.clear();
325 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700326 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700327 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700328 // fall through
329 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700330 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700331 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700332 pathCache.clear();
333 roundRectShapeCache.clear();
334 circleShapeCache.clear();
335 ovalShapeCache.clear();
336 rectShapeCache.clear();
337 arcShapeCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700338 // fall through
339 case kFlushMode_Layers:
340 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800341 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700342 break;
343 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700344
345 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700346}
347
Romain Guyfe48f652010-11-11 15:36:56 -0800348///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700349// VBO
350///////////////////////////////////////////////////////////////////////////////
351
Romain Guyf3a910b42011-12-12 20:35:21 -0800352bool Caches::bindMeshBuffer() {
353 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700354}
355
Romain Guyf3a910b42011-12-12 20:35:21 -0800356bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700357 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700358 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700359 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800360 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700361 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800362 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700363}
364
Romain Guyf3a910b42011-12-12 20:35:21 -0800365bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700366 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700367 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700368 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800369 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700370 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800371 return false;
372}
373
Romain Guy15bc6432011-12-13 13:11:32 -0800374bool Caches::bindIndicesBuffer(const GLuint buffer) {
375 if (mCurrentIndicesBuffer != buffer) {
376 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
377 mCurrentIndicesBuffer = buffer;
378 return true;
379 }
380 return false;
381}
382
383bool Caches::unbindIndicesBuffer() {
384 if (mCurrentIndicesBuffer) {
385 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
386 mCurrentIndicesBuffer = 0;
387 return true;
388 }
389 return false;
390}
391
Romain Guy85ef80d2012-09-13 20:26:50 -0700392///////////////////////////////////////////////////////////////////////////////
393// Meshes and textures
394///////////////////////////////////////////////////////////////////////////////
395
Chris Craikcb4d6002012-09-25 12:00:29 -0700396void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
397 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
398 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800399 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
400 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700401 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800402 }
403}
404
Romain Guyff316ec2013-02-13 18:39:43 -0800405void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
406 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700407 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800408 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800409 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800410 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800411 }
412}
413
414void Caches::resetVertexPointers() {
415 mCurrentPositionPointer = this;
416 mCurrentTexCoordsPointer = this;
417}
418
419void Caches::resetTexCoordsVertexPointer() {
420 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700421}
422
Romain Guy15bc6432011-12-13 13:11:32 -0800423void Caches::enableTexCoordsVertexArray() {
424 if (!mTexCoordsArrayEnabled) {
425 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800426 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800427 mTexCoordsArrayEnabled = true;
428 }
429}
430
Romain Guyff316ec2013-02-13 18:39:43 -0800431void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800432 if (mTexCoordsArrayEnabled) {
433 glDisableVertexAttribArray(Program::kBindingTexCoords);
434 mTexCoordsArrayEnabled = false;
435 }
436}
437
Romain Guya1d3c912011-12-13 14:55:06 -0800438void Caches::activeTexture(GLuint textureUnit) {
439 if (mTextureUnit != textureUnit) {
440 glActiveTexture(gTextureUnits[textureUnit]);
441 mTextureUnit = textureUnit;
442 }
443}
444
Romain Guy85ef80d2012-09-13 20:26:50 -0700445///////////////////////////////////////////////////////////////////////////////
446// Scissor
447///////////////////////////////////////////////////////////////////////////////
448
Romain Guy8a4ac612012-07-17 17:32:48 -0700449bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700450 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
451 width != mScissorWidth || height != mScissorHeight)) {
452
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700453 if (x < 0) {
454 width += x;
455 x = 0;
456 }
457 if (y < 0) {
458 height += y;
459 y = 0;
460 }
461 if (width < 0) {
462 width = 0;
463 }
464 if (height < 0) {
465 height = 0;
466 }
Romain Guy8f85e802011-12-14 19:23:32 -0800467 glScissor(x, y, width, height);
468
469 mScissorX = x;
470 mScissorY = y;
471 mScissorWidth = width;
472 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700473
474 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800475 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700476 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800477}
478
Romain Guy8a4ac612012-07-17 17:32:48 -0700479bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700480 if (!scissorEnabled) {
481 glEnable(GL_SCISSOR_TEST);
482 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700483 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700484 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700485 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700486 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700487}
488
Romain Guy8a4ac612012-07-17 17:32:48 -0700489bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700490 if (scissorEnabled) {
491 glDisable(GL_SCISSOR_TEST);
492 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700493 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700494 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700495 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700496}
497
498void Caches::setScissorEnabled(bool enabled) {
499 if (scissorEnabled != enabled) {
500 if (enabled) glEnable(GL_SCISSOR_TEST);
501 else glDisable(GL_SCISSOR_TEST);
502 scissorEnabled = enabled;
503 }
504}
505
Romain Guy82bc7a72012-01-03 14:13:39 -0800506void Caches::resetScissor() {
507 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
508}
509
Romain Guy85ef80d2012-09-13 20:26:50 -0700510///////////////////////////////////////////////////////////////////////////////
511// Tiling
512///////////////////////////////////////////////////////////////////////////////
513
Romain Guyf735c8e2013-01-31 17:45:55 -0800514void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800515 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800516 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700517 }
518}
519
520void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800521 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700522 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700523 }
524}
525
Romain Guy54c1a642012-09-27 17:55:46 -0700526bool Caches::hasRegisteredFunctors() {
527 return mFunctorsCount > 0;
528}
529
530void Caches::registerFunctors(uint32_t functorCount) {
531 mFunctorsCount += functorCount;
532}
533
534void Caches::unregisterFunctors(uint32_t functorCount) {
535 if (functorCount > mFunctorsCount) {
536 mFunctorsCount = 0;
537 } else {
538 mFunctorsCount -= functorCount;
539 }
540}
541
Romain Guy85ef80d2012-09-13 20:26:50 -0700542///////////////////////////////////////////////////////////////////////////////
543// Regions
544///////////////////////////////////////////////////////////////////////////////
545
Romain Guy5b3b3522010-10-27 18:57:51 -0700546TextureVertex* Caches::getRegionMesh() {
547 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
548 if (!mRegionMesh) {
549 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
550
551 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
552 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
553 uint16_t quad = i * 4;
554 int index = i * 6;
555 regionIndices[index ] = quad; // top-left
556 regionIndices[index + 1] = quad + 1; // top-right
557 regionIndices[index + 2] = quad + 2; // bottom-left
558 regionIndices[index + 3] = quad + 2; // bottom-left
559 regionIndices[index + 4] = quad + 1; // top-right
560 regionIndices[index + 5] = quad + 3; // bottom-right
561 }
562
563 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800564 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700565 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
566 regionIndices, GL_STATIC_DRAW);
567
568 delete[] regionIndices;
569 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800570 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700571 }
572
573 return mRegionMesh;
574}
575
Chet Haasedd78cca2010-10-22 18:59:26 -0700576}; // namespace uirenderer
577}; // namespace android