blob: f8d3589168b6b61ca1ed6e706a54fa2331492316 [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
Romain Guy627c6fd2013-08-21 11:53:18 -0700169 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700170 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
171 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700172 if (!strcmp(property, "show")) {
173 debugOverdraw = true;
174 mOverdrawDebugColorSet = kColorSet_Default;
175 } else if (!strcmp(property, "show_deuteranomaly")) {
176 debugOverdraw = true;
177 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
178 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700179 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800180
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800181 // See Properties.h for valid values
182 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
183 INIT_LOGD(" Stencil clip debug enabled: %s", property);
184 if (!strcmp(property, "hide")) {
185 debugStencilClip = kStencilHide;
186 } else if (!strcmp(property, "highlight")) {
187 debugStencilClip = kStencilShowHighlight;
188 } else if (!strcmp(property, "region")) {
189 debugStencilClip = kStencilShowRegion;
190 }
191 } else {
192 debugStencilClip = kStencilHide;
193 }
194
Romain Guy0f667532013-03-01 14:31:04 -0800195 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
196 drawDeferDisabled = !strcasecmp(property, "true");
197 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
198 } else {
199 INIT_LOGD(" Draw defer enabled");
200 }
201
202 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
203 drawReorderDisabled = !strcasecmp(property, "true");
204 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
205 } else {
206 INIT_LOGD(" Draw reorder enabled");
207 }
208
Romain Guy5bb3c732012-11-29 17:52:58 -0800209 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800210 (prevDebugOverdraw != debugOverdraw) ||
211 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700212}
213
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800214void Caches::terminate() {
215 if (!mInitialized) return;
216
217 glDeleteBuffers(1, &meshBuffer);
218 mCurrentBuffer = 0;
219
Romain Guy3b748a42013-04-17 18:54:38 -0700220 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700221 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700222 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800223 mRegionMesh = NULL;
224
225 fboCache.clear();
226
227 programCache.clear();
228 currentProgram = NULL;
229
Romain Guy3b748a42013-04-17 18:54:38 -0700230 assetAtlas.terminate();
231
232 patchCache.clear();
233
Romain Guy46bfc482013-08-16 18:38:29 -0700234 clearGarbage();
235
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800236 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700237}
238
239///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800240// Debug
241///////////////////////////////////////////////////////////////////////////////
242
Romain Guy627c6fd2013-08-21 11:53:18 -0700243uint32_t Caches::getOverdrawColor(uint32_t amount) const {
244 static uint32_t sOverdrawColors[2][4] = {
245 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
246 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
247 };
248 if (amount < 1) amount = 1;
249 if (amount > 4) amount = 4;
250 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
251}
252
Romain Guyc15008e2010-11-10 11:59:15 -0800253void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700254 String8 stringLog;
255 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000256 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700257}
258
259void Caches::dumpMemoryUsage(String8 &log) {
260 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
261 log.appendFormat(" TextureCache %8d / %8d\n",
262 textureCache.getSize(), textureCache.getMaxSize());
263 log.appendFormat(" LayerCache %8d / %8d\n",
264 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800265 log.appendFormat(" RenderBufferCache %8d / %8d\n",
266 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700267 log.appendFormat(" GradientCache %8d / %8d\n",
268 gradientCache.getSize(), gradientCache.getMaxSize());
269 log.appendFormat(" PathCache %8d / %8d\n",
270 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700271 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800272 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700273 log.appendFormat(" PatchCache %8d / %8d\n",
274 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700275 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700276 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
277 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
278 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
279 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
280 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
281 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800282 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700283 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700284 log.appendFormat(" FboCache %8d / %8d\n",
285 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800286
287 uint32_t total = 0;
288 total += textureCache.getSize();
289 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800290 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800291 total += gradientCache.getSize();
292 total += pathCache.getSize();
293 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700294 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700295 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700296 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
297 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800298 }
299
Chet Haase9c1e23b2011-03-24 10:51:31 -0700300 log.appendFormat("Total memory usage:\n");
301 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800302}
303
304///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800305// Memory management
306///////////////////////////////////////////////////////////////////////////////
307
308void Caches::clearGarbage() {
309 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800310 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700311 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800312
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700313 Vector<DisplayList*> displayLists;
314 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800315
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700316 { // scope for the lock
317 Mutex::Autolock _l(mGarbageLock);
318 displayLists = mDisplayListGarbage;
319 layers = mLayerGarbage;
320 mDisplayListGarbage.clear();
321 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800322 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800323
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700324 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800325 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700326 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800327 delete displayList;
328 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700329
330 count = layers.size();
331 for (size_t i = 0; i < count; i++) {
332 Layer* layer = layers.itemAt(i);
333 delete layer;
334 }
335 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800336}
337
Romain Guyada830f2011-01-13 12:13:20 -0800338void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800339 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800340 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800341}
342
Romain Guybb0acdf2012-03-05 13:44:35 -0800343void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
344 Mutex::Autolock _l(mGarbageLock);
345 mDisplayListGarbage.push(displayList);
346}
347
Romain Guybdf76092011-07-18 15:00:43 -0700348void Caches::flush(FlushMode mode) {
349 FLUSH_LOGD("Flushing caches (mode %d)", mode);
350
Romain Guyb0a41ed2013-08-16 14:44:38 -0700351 // We must stop tasks before clearing caches
352 if (mode > kFlushMode_Layers) {
353 tasks.stop();
354 }
355
Romain Guybdf76092011-07-18 15:00:43 -0700356 switch (mode) {
357 case kFlushMode_Full:
358 textureCache.clear();
359 patchCache.clear();
360 dropShadowCache.clear();
361 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700362 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700363 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700364 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700365 // fall through
366 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700367 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700368 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700369 pathCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700370 // fall through
371 case kFlushMode_Layers:
372 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800373 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700374 break;
375 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700376
377 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700378}
379
Romain Guyfe48f652010-11-11 15:36:56 -0800380///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700381// VBO
382///////////////////////////////////////////////////////////////////////////////
383
Romain Guyf3a910b42011-12-12 20:35:21 -0800384bool Caches::bindMeshBuffer() {
385 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700386}
387
Romain Guyf3a910b42011-12-12 20:35:21 -0800388bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700389 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700390 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700391 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800392 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700393 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800394 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700395}
396
Romain Guyf3a910b42011-12-12 20:35:21 -0800397bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700398 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700399 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700400 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800401 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700402 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800403 return false;
404}
405
Romain Guy15bc6432011-12-13 13:11:32 -0800406bool Caches::bindIndicesBuffer(const GLuint buffer) {
407 if (mCurrentIndicesBuffer != buffer) {
408 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
409 mCurrentIndicesBuffer = buffer;
410 return true;
411 }
412 return false;
413}
414
Romain Guy3b748a42013-04-17 18:54:38 -0700415bool Caches::bindIndicesBuffer() {
416 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700417 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
418 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700419 uint16_t quad = i * 4;
420 int index = i * 6;
421 regionIndices[index ] = quad; // top-left
422 regionIndices[index + 1] = quad + 1; // top-right
423 regionIndices[index + 2] = quad + 2; // bottom-left
424 regionIndices[index + 3] = quad + 2; // bottom-left
425 regionIndices[index + 4] = quad + 1; // top-right
426 regionIndices[index + 5] = quad + 3; // bottom-right
427 }
428
429 glGenBuffers(1, &mMeshIndices);
430 bool force = bindIndicesBuffer(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700431 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700432 regionIndices, GL_STATIC_DRAW);
433
434 delete[] regionIndices;
435 return force;
436 }
437
438 return bindIndicesBuffer(mMeshIndices);
439}
440
Romain Guy15bc6432011-12-13 13:11:32 -0800441bool Caches::unbindIndicesBuffer() {
442 if (mCurrentIndicesBuffer) {
443 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
444 mCurrentIndicesBuffer = 0;
445 return true;
446 }
447 return false;
448}
449
Romain Guy85ef80d2012-09-13 20:26:50 -0700450///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700451// PBO
452///////////////////////////////////////////////////////////////////////////////
453
454bool Caches::bindPixelBuffer(const GLuint buffer) {
455 if (mCurrentPixelBuffer != buffer) {
456 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
457 mCurrentPixelBuffer = buffer;
458 return true;
459 }
460 return false;
461}
462
463bool Caches::unbindPixelBuffer() {
464 if (mCurrentPixelBuffer) {
465 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
466 mCurrentPixelBuffer = 0;
467 return true;
468 }
469 return false;
470}
471
472///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700473// Meshes and textures
474///////////////////////////////////////////////////////////////////////////////
475
Chris Craikcb4d6002012-09-25 12:00:29 -0700476void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
477 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
478 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800479 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
480 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700481 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800482 }
483}
484
Romain Guyff316ec2013-02-13 18:39:43 -0800485void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
486 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700487 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800488 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800489 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800490 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800491 }
492}
493
494void Caches::resetVertexPointers() {
495 mCurrentPositionPointer = this;
496 mCurrentTexCoordsPointer = this;
497}
498
499void Caches::resetTexCoordsVertexPointer() {
500 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700501}
502
Romain Guy15bc6432011-12-13 13:11:32 -0800503void Caches::enableTexCoordsVertexArray() {
504 if (!mTexCoordsArrayEnabled) {
505 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800506 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800507 mTexCoordsArrayEnabled = true;
508 }
509}
510
Romain Guyff316ec2013-02-13 18:39:43 -0800511void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800512 if (mTexCoordsArrayEnabled) {
513 glDisableVertexAttribArray(Program::kBindingTexCoords);
514 mTexCoordsArrayEnabled = false;
515 }
516}
517
Romain Guya1d3c912011-12-13 14:55:06 -0800518void Caches::activeTexture(GLuint textureUnit) {
519 if (mTextureUnit != textureUnit) {
520 glActiveTexture(gTextureUnits[textureUnit]);
521 mTextureUnit = textureUnit;
522 }
523}
524
Chris Craik9ab2d182013-07-22 16:16:06 -0700525void Caches::resetActiveTexture() {
526 mTextureUnit = -1;
527}
528
Romain Guy8aa195d2013-06-04 18:00:09 -0700529void Caches::bindTexture(GLuint texture) {
530 if (mBoundTextures[mTextureUnit] != texture) {
531 glBindTexture(GL_TEXTURE_2D, texture);
532 mBoundTextures[mTextureUnit] = texture;
533 }
534}
535
536void Caches::bindTexture(GLenum target, GLuint texture) {
537 if (mBoundTextures[mTextureUnit] != texture) {
538 glBindTexture(target, texture);
539 mBoundTextures[mTextureUnit] = texture;
540 }
541}
542
Romain Guybe1b1272013-06-06 14:02:54 -0700543void Caches::deleteTexture(GLuint texture) {
544 // When glDeleteTextures() is called on a currently bound texture,
545 // OpenGL ES specifies that the texture is then considered unbound
546 // Consider the following series of calls:
547 //
548 // glGenTextures -> creates texture name 2
549 // glBindTexture(2)
550 // glDeleteTextures(2) -> 2 is now unbound
551 // glGenTextures -> can return 2 again
552 //
553 // If we don't call glBindTexture(2) after the second glGenTextures
554 // call, any texture operation will be performed on the default
555 // texture (name=0)
556
557 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
558 if (mBoundTextures[i] == texture) {
559 mBoundTextures[i] = 0;
560 }
561 }
562 glDeleteTextures(1, &texture);
563}
564
Romain Guy8aa195d2013-06-04 18:00:09 -0700565void Caches::resetBoundTextures() {
566 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
567}
568
Romain Guy85ef80d2012-09-13 20:26:50 -0700569///////////////////////////////////////////////////////////////////////////////
570// Scissor
571///////////////////////////////////////////////////////////////////////////////
572
Romain Guy8a4ac612012-07-17 17:32:48 -0700573bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700574 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
575 width != mScissorWidth || height != mScissorHeight)) {
576
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700577 if (x < 0) {
578 width += x;
579 x = 0;
580 }
581 if (y < 0) {
582 height += y;
583 y = 0;
584 }
585 if (width < 0) {
586 width = 0;
587 }
588 if (height < 0) {
589 height = 0;
590 }
Romain Guy8f85e802011-12-14 19:23:32 -0800591 glScissor(x, y, width, height);
592
593 mScissorX = x;
594 mScissorY = y;
595 mScissorWidth = width;
596 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700597
598 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800599 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700600 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800601}
602
Romain Guy8a4ac612012-07-17 17:32:48 -0700603bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700604 if (!scissorEnabled) {
605 glEnable(GL_SCISSOR_TEST);
606 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700607 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700608 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700609 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700610 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700611}
612
Romain Guy8a4ac612012-07-17 17:32:48 -0700613bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700614 if (scissorEnabled) {
615 glDisable(GL_SCISSOR_TEST);
616 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700617 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700618 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700619 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700620}
621
622void Caches::setScissorEnabled(bool enabled) {
623 if (scissorEnabled != enabled) {
624 if (enabled) glEnable(GL_SCISSOR_TEST);
625 else glDisable(GL_SCISSOR_TEST);
626 scissorEnabled = enabled;
627 }
628}
629
Romain Guy82bc7a72012-01-03 14:13:39 -0800630void Caches::resetScissor() {
631 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
632}
633
Romain Guy85ef80d2012-09-13 20:26:50 -0700634///////////////////////////////////////////////////////////////////////////////
635// Tiling
636///////////////////////////////////////////////////////////////////////////////
637
Romain Guyf735c8e2013-01-31 17:45:55 -0800638void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800639 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800640 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700641 }
642}
643
644void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800645 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700646 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700647 }
648}
649
Romain Guy54c1a642012-09-27 17:55:46 -0700650bool Caches::hasRegisteredFunctors() {
651 return mFunctorsCount > 0;
652}
653
654void Caches::registerFunctors(uint32_t functorCount) {
655 mFunctorsCount += functorCount;
656}
657
658void Caches::unregisterFunctors(uint32_t functorCount) {
659 if (functorCount > mFunctorsCount) {
660 mFunctorsCount = 0;
661 } else {
662 mFunctorsCount -= functorCount;
663 }
664}
665
Romain Guy85ef80d2012-09-13 20:26:50 -0700666///////////////////////////////////////////////////////////////////////////////
667// Regions
668///////////////////////////////////////////////////////////////////////////////
669
Romain Guy5b3b3522010-10-27 18:57:51 -0700670TextureVertex* Caches::getRegionMesh() {
671 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
672 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700673 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700674 }
675
676 return mRegionMesh;
677}
678
Chet Haasedd78cca2010-10-22 18:59:26 -0700679}; // namespace uirenderer
680}; // namespace android