blob: c0b3bfccfab6894bdbf1056e2bb28427051c7457 [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 Guy8aa195d2013-06-04 18:00:09 -070050Caches::Caches(): Singleton<Caches>(),
51 mExtensions(Extensions::getInstance()), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080052 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070053 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070054 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070055 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070056 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080057 initExtensions();
Romain Guye190aa62010-11-10 19:01:29 -080058
59 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000060 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070061}
62
Romain Guy3b748a42013-04-17 18:54:38 -070063bool Caches::init() {
64 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080065
66 glGenBuffers(1, &meshBuffer);
67 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
68 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
69
70 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080071 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080072 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070073 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080074 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070075 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080076
Romain Guy15bc6432011-12-13 13:11:32 -080077 mTexCoordsArrayEnabled = false;
78
Romain Guyb1d0a4e2012-07-13 18:25:35 -070079 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070080 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080081 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
82
Romain Guya1d3c912011-12-13 14:55:06 -080083 glActiveTexture(gTextureUnits[0]);
84 mTextureUnit = 0;
85
Romain Guy8ff6b9e2011-11-09 20:10:18 -080086 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070087 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080088
89 blend = false;
90 lastSrcMode = GL_ZERO;
91 lastDstMode = GL_ZERO;
92 currentProgram = NULL;
93
Romain Guy54c1a642012-09-27 17:55:46 -070094 mFunctorsCount = 0;
95
Romain Guyc2a97212013-02-06 15:29:46 -080096 debugLayersUpdates = false;
97 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080098 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080099
Romain Guy3b748a42013-04-17 18:54:38 -0700100 patchCache.init(*this);
101
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800102 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700103
Romain Guy8aa195d2013-06-04 18:00:09 -0700104 resetBoundTextures();
105
Romain Guy3b748a42013-04-17 18:54:38 -0700106 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800107}
108
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700109void Caches::initFont() {
110 fontRenderer = GammaFontRenderer::createRenderer();
111}
112
Romain Guydfa10462012-05-12 16:18:58 -0700113void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800114 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700115 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800116
Chris Craikff785832013-03-08 13:12:16 -0800117 startMark = glPushGroupMarkerEXT;
118 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700119 } else {
120 eventMark = eventMarkNull;
121 startMark = startMarkNull;
122 endMark = endMarkNull;
123 }
124
Romain Guy0f667532013-03-01 14:31:04 -0800125 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700126 setLabel = glLabelObjectEXT;
127 getLabel = glGetObjectLabelEXT;
128 } else {
129 setLabel = setLabelNull;
130 getLabel = getLabelNull;
131 }
132}
133
134void Caches::initConstraints() {
135 GLint maxTextureUnits;
136 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
137 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
138 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
139 }
140
141 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
142}
143
Romain Guyf9f00162013-05-09 11:50:12 -0700144void Caches::initStaticProperties() {
145 gpuPixelBuffersEnabled = false;
146
147 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700148 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700149 char property[PROPERTY_VALUE_MAX];
150 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
151 gpuPixelBuffersEnabled = !strcmp(property, "true");
152 }
153 }
154}
155
Romain Guy5bb3c732012-11-29 17:52:58 -0800156bool Caches::initProperties() {
157 bool prevDebugLayersUpdates = debugLayersUpdates;
158 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800159 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800160
Romain Guy4ff0cf42012-08-06 14:51:10 -0700161 char property[PROPERTY_VALUE_MAX];
162 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
163 INIT_LOGD(" Layers updates debug enabled: %s", property);
164 debugLayersUpdates = !strcmp(property, "true");
165 } else {
166 debugLayersUpdates = false;
167 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700168
169 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
170 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy78dd96d2013-05-03 14:24:16 -0700171 debugOverdraw = !strcmp(property, "show");
Romain Guy7c450aa2012-09-21 19:15:00 -0700172 } else {
173 debugOverdraw = false;
174 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800175
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800176 // See Properties.h for valid values
177 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
178 INIT_LOGD(" Stencil clip debug enabled: %s", property);
179 if (!strcmp(property, "hide")) {
180 debugStencilClip = kStencilHide;
181 } else if (!strcmp(property, "highlight")) {
182 debugStencilClip = kStencilShowHighlight;
183 } else if (!strcmp(property, "region")) {
184 debugStencilClip = kStencilShowRegion;
185 }
186 } else {
187 debugStencilClip = kStencilHide;
188 }
189
Romain Guy0f667532013-03-01 14:31:04 -0800190 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
191 drawDeferDisabled = !strcasecmp(property, "true");
192 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
193 } else {
194 INIT_LOGD(" Draw defer enabled");
195 }
196
197 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
198 drawReorderDisabled = !strcasecmp(property, "true");
199 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
200 } else {
201 INIT_LOGD(" Draw reorder enabled");
202 }
203
Romain Guy5bb3c732012-11-29 17:52:58 -0800204 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800205 (prevDebugOverdraw != debugOverdraw) ||
206 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700207}
208
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800209void Caches::terminate() {
210 if (!mInitialized) return;
211
212 glDeleteBuffers(1, &meshBuffer);
213 mCurrentBuffer = 0;
214
Romain Guy3b748a42013-04-17 18:54:38 -0700215 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700216 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700217 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800218 mRegionMesh = NULL;
219
220 fboCache.clear();
221
222 programCache.clear();
223 currentProgram = NULL;
224
Romain Guy3b748a42013-04-17 18:54:38 -0700225 assetAtlas.terminate();
226
227 patchCache.clear();
228
Romain Guy46bfc482013-08-16 18:38:29 -0700229 clearGarbage();
230
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800231 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700232}
233
234///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800235// Debug
236///////////////////////////////////////////////////////////////////////////////
237
238void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700239 String8 stringLog;
240 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000241 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700242}
243
244void Caches::dumpMemoryUsage(String8 &log) {
245 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
246 log.appendFormat(" TextureCache %8d / %8d\n",
247 textureCache.getSize(), textureCache.getMaxSize());
248 log.appendFormat(" LayerCache %8d / %8d\n",
249 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800250 log.appendFormat(" RenderBufferCache %8d / %8d\n",
251 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700252 log.appendFormat(" GradientCache %8d / %8d\n",
253 gradientCache.getSize(), gradientCache.getMaxSize());
254 log.appendFormat(" PathCache %8d / %8d\n",
255 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700256 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800257 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700258 log.appendFormat(" PatchCache %8d / %8d\n",
259 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700260 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700261 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
262 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
263 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
264 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
265 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
266 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800267 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700268 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700269 log.appendFormat(" FboCache %8d / %8d\n",
270 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800271
272 uint32_t total = 0;
273 total += textureCache.getSize();
274 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800275 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800276 total += gradientCache.getSize();
277 total += pathCache.getSize();
278 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700279 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700280 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700281 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
282 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800283 }
284
Chet Haase9c1e23b2011-03-24 10:51:31 -0700285 log.appendFormat("Total memory usage:\n");
286 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800287}
288
289///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800290// Memory management
291///////////////////////////////////////////////////////////////////////////////
292
293void Caches::clearGarbage() {
294 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800295 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700296 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800297
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700298 Vector<DisplayList*> displayLists;
299 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800300
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700301 { // scope for the lock
302 Mutex::Autolock _l(mGarbageLock);
303 displayLists = mDisplayListGarbage;
304 layers = mLayerGarbage;
305 mDisplayListGarbage.clear();
306 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800307 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800308
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700309 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800310 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700311 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800312 delete displayList;
313 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700314
315 count = layers.size();
316 for (size_t i = 0; i < count; i++) {
317 Layer* layer = layers.itemAt(i);
318 delete layer;
319 }
320 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800321}
322
Romain Guyada830f2011-01-13 12:13:20 -0800323void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800324 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800325 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800326}
327
Romain Guybb0acdf2012-03-05 13:44:35 -0800328void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
329 Mutex::Autolock _l(mGarbageLock);
330 mDisplayListGarbage.push(displayList);
331}
332
Romain Guybdf76092011-07-18 15:00:43 -0700333void Caches::flush(FlushMode mode) {
334 FLUSH_LOGD("Flushing caches (mode %d)", mode);
335
Romain Guyb0a41ed2013-08-16 14:44:38 -0700336 // We must stop tasks before clearing caches
337 if (mode > kFlushMode_Layers) {
338 tasks.stop();
339 }
340
Romain Guybdf76092011-07-18 15:00:43 -0700341 switch (mode) {
342 case kFlushMode_Full:
343 textureCache.clear();
344 patchCache.clear();
345 dropShadowCache.clear();
346 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700347 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700348 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700349 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700350 // fall through
351 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700352 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700353 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700354 pathCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700355 // fall through
356 case kFlushMode_Layers:
357 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800358 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700359 break;
360 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700361
362 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700363}
364
Romain Guyfe48f652010-11-11 15:36:56 -0800365///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700366// VBO
367///////////////////////////////////////////////////////////////////////////////
368
Romain Guyf3a910b42011-12-12 20:35:21 -0800369bool Caches::bindMeshBuffer() {
370 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700371}
372
Romain Guyf3a910b42011-12-12 20:35:21 -0800373bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700374 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700375 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700376 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800377 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700378 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800379 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700380}
381
Romain Guyf3a910b42011-12-12 20:35:21 -0800382bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700383 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700384 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700385 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800386 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700387 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800388 return false;
389}
390
Romain Guy15bc6432011-12-13 13:11:32 -0800391bool Caches::bindIndicesBuffer(const GLuint buffer) {
392 if (mCurrentIndicesBuffer != buffer) {
393 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
394 mCurrentIndicesBuffer = buffer;
395 return true;
396 }
397 return false;
398}
399
Romain Guy3b748a42013-04-17 18:54:38 -0700400bool Caches::bindIndicesBuffer() {
401 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700402 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
403 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700404 uint16_t quad = i * 4;
405 int index = i * 6;
406 regionIndices[index ] = quad; // top-left
407 regionIndices[index + 1] = quad + 1; // top-right
408 regionIndices[index + 2] = quad + 2; // bottom-left
409 regionIndices[index + 3] = quad + 2; // bottom-left
410 regionIndices[index + 4] = quad + 1; // top-right
411 regionIndices[index + 5] = quad + 3; // bottom-right
412 }
413
414 glGenBuffers(1, &mMeshIndices);
415 bool force = bindIndicesBuffer(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700416 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700417 regionIndices, GL_STATIC_DRAW);
418
419 delete[] regionIndices;
420 return force;
421 }
422
423 return bindIndicesBuffer(mMeshIndices);
424}
425
Romain Guy15bc6432011-12-13 13:11:32 -0800426bool Caches::unbindIndicesBuffer() {
427 if (mCurrentIndicesBuffer) {
428 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
429 mCurrentIndicesBuffer = 0;
430 return true;
431 }
432 return false;
433}
434
Romain Guy85ef80d2012-09-13 20:26:50 -0700435///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700436// PBO
437///////////////////////////////////////////////////////////////////////////////
438
439bool Caches::bindPixelBuffer(const GLuint buffer) {
440 if (mCurrentPixelBuffer != buffer) {
441 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
442 mCurrentPixelBuffer = buffer;
443 return true;
444 }
445 return false;
446}
447
448bool Caches::unbindPixelBuffer() {
449 if (mCurrentPixelBuffer) {
450 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
451 mCurrentPixelBuffer = 0;
452 return true;
453 }
454 return false;
455}
456
457///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700458// Meshes and textures
459///////////////////////////////////////////////////////////////////////////////
460
Chris Craikcb4d6002012-09-25 12:00:29 -0700461void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
462 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
463 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800464 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
465 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700466 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800467 }
468}
469
Romain Guyff316ec2013-02-13 18:39:43 -0800470void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
471 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700472 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800473 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800474 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800475 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800476 }
477}
478
479void Caches::resetVertexPointers() {
480 mCurrentPositionPointer = this;
481 mCurrentTexCoordsPointer = this;
482}
483
484void Caches::resetTexCoordsVertexPointer() {
485 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700486}
487
Romain Guy15bc6432011-12-13 13:11:32 -0800488void Caches::enableTexCoordsVertexArray() {
489 if (!mTexCoordsArrayEnabled) {
490 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800491 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800492 mTexCoordsArrayEnabled = true;
493 }
494}
495
Romain Guyff316ec2013-02-13 18:39:43 -0800496void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800497 if (mTexCoordsArrayEnabled) {
498 glDisableVertexAttribArray(Program::kBindingTexCoords);
499 mTexCoordsArrayEnabled = false;
500 }
501}
502
Romain Guya1d3c912011-12-13 14:55:06 -0800503void Caches::activeTexture(GLuint textureUnit) {
504 if (mTextureUnit != textureUnit) {
505 glActiveTexture(gTextureUnits[textureUnit]);
506 mTextureUnit = textureUnit;
507 }
508}
509
Chris Craik9ab2d182013-07-22 16:16:06 -0700510void Caches::resetActiveTexture() {
511 mTextureUnit = -1;
512}
513
Romain Guy8aa195d2013-06-04 18:00:09 -0700514void Caches::bindTexture(GLuint texture) {
515 if (mBoundTextures[mTextureUnit] != texture) {
516 glBindTexture(GL_TEXTURE_2D, texture);
517 mBoundTextures[mTextureUnit] = texture;
518 }
519}
520
521void Caches::bindTexture(GLenum target, GLuint texture) {
522 if (mBoundTextures[mTextureUnit] != texture) {
523 glBindTexture(target, texture);
524 mBoundTextures[mTextureUnit] = texture;
525 }
526}
527
Romain Guybe1b1272013-06-06 14:02:54 -0700528void Caches::deleteTexture(GLuint texture) {
529 // When glDeleteTextures() is called on a currently bound texture,
530 // OpenGL ES specifies that the texture is then considered unbound
531 // Consider the following series of calls:
532 //
533 // glGenTextures -> creates texture name 2
534 // glBindTexture(2)
535 // glDeleteTextures(2) -> 2 is now unbound
536 // glGenTextures -> can return 2 again
537 //
538 // If we don't call glBindTexture(2) after the second glGenTextures
539 // call, any texture operation will be performed on the default
540 // texture (name=0)
541
542 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
543 if (mBoundTextures[i] == texture) {
544 mBoundTextures[i] = 0;
545 }
546 }
547 glDeleteTextures(1, &texture);
548}
549
Romain Guy8aa195d2013-06-04 18:00:09 -0700550void Caches::resetBoundTextures() {
551 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
552}
553
Romain Guy85ef80d2012-09-13 20:26:50 -0700554///////////////////////////////////////////////////////////////////////////////
555// Scissor
556///////////////////////////////////////////////////////////////////////////////
557
Romain Guy8a4ac612012-07-17 17:32:48 -0700558bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700559 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
560 width != mScissorWidth || height != mScissorHeight)) {
561
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700562 if (x < 0) {
563 width += x;
564 x = 0;
565 }
566 if (y < 0) {
567 height += y;
568 y = 0;
569 }
570 if (width < 0) {
571 width = 0;
572 }
573 if (height < 0) {
574 height = 0;
575 }
Romain Guy8f85e802011-12-14 19:23:32 -0800576 glScissor(x, y, width, height);
577
578 mScissorX = x;
579 mScissorY = y;
580 mScissorWidth = width;
581 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700582
583 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800584 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700585 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800586}
587
Romain Guy8a4ac612012-07-17 17:32:48 -0700588bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700589 if (!scissorEnabled) {
590 glEnable(GL_SCISSOR_TEST);
591 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700592 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700593 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700594 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700595 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700596}
597
Romain Guy8a4ac612012-07-17 17:32:48 -0700598bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700599 if (scissorEnabled) {
600 glDisable(GL_SCISSOR_TEST);
601 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700602 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700603 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700604 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700605}
606
607void Caches::setScissorEnabled(bool enabled) {
608 if (scissorEnabled != enabled) {
609 if (enabled) glEnable(GL_SCISSOR_TEST);
610 else glDisable(GL_SCISSOR_TEST);
611 scissorEnabled = enabled;
612 }
613}
614
Romain Guy82bc7a72012-01-03 14:13:39 -0800615void Caches::resetScissor() {
616 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
617}
618
Romain Guy85ef80d2012-09-13 20:26:50 -0700619///////////////////////////////////////////////////////////////////////////////
620// Tiling
621///////////////////////////////////////////////////////////////////////////////
622
Romain Guyf735c8e2013-01-31 17:45:55 -0800623void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800624 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800625 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700626 }
627}
628
629void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800630 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700631 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700632 }
633}
634
Romain Guy54c1a642012-09-27 17:55:46 -0700635bool Caches::hasRegisteredFunctors() {
636 return mFunctorsCount > 0;
637}
638
639void Caches::registerFunctors(uint32_t functorCount) {
640 mFunctorsCount += functorCount;
641}
642
643void Caches::unregisterFunctors(uint32_t functorCount) {
644 if (functorCount > mFunctorsCount) {
645 mFunctorsCount = 0;
646 } else {
647 mFunctorsCount -= functorCount;
648 }
649}
650
Romain Guy85ef80d2012-09-13 20:26:50 -0700651///////////////////////////////////////////////////////////////////////////////
652// Regions
653///////////////////////////////////////////////////////////////////////////////
654
Romain Guy5b3b3522010-10-27 18:57:51 -0700655TextureVertex* Caches::getRegionMesh() {
656 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
657 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700658 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700659 }
660
661 return mRegionMesh;
662}
663
Chet Haasedd78cca2010-10-22 18:59:26 -0700664}; // namespace uirenderer
665}; // namespace android