blob: 9d5a85461486aed50487d4f814a1ac31690289a3 [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 Guyf9f00162013-05-09 11:50:12 -070055 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080056 initExtensions();
Romain Guye190aa62010-11-10 19:01:29 -080057
58 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000059 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070060}
61
Romain Guy3b748a42013-04-17 18:54:38 -070062bool Caches::init() {
63 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080064
65 glGenBuffers(1, &meshBuffer);
66 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
67 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
68
69 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080070 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080071 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070072 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080073 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070074 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080075
Romain Guy15bc6432011-12-13 13:11:32 -080076 mTexCoordsArrayEnabled = false;
77
Romain Guyb1d0a4e2012-07-13 18:25:35 -070078 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070079 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080080 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
81
Romain Guya1d3c912011-12-13 14:55:06 -080082 glActiveTexture(gTextureUnits[0]);
83 mTextureUnit = 0;
84
Romain Guy8ff6b9e2011-11-09 20:10:18 -080085 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070086 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080087
88 blend = false;
89 lastSrcMode = GL_ZERO;
90 lastDstMode = GL_ZERO;
91 currentProgram = NULL;
92
Romain Guy54c1a642012-09-27 17:55:46 -070093 mFunctorsCount = 0;
94
Romain Guyc2a97212013-02-06 15:29:46 -080095 debugLayersUpdates = false;
96 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080097 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080098
Romain Guy3b748a42013-04-17 18:54:38 -070099 patchCache.init(*this);
100
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800101 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700102
103 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800104}
105
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700106void Caches::initFont() {
107 fontRenderer = GammaFontRenderer::createRenderer();
108}
109
Romain Guydfa10462012-05-12 16:18:58 -0700110void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800111 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700112 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800113
Chris Craikff785832013-03-08 13:12:16 -0800114 startMark = glPushGroupMarkerEXT;
115 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700116 } else {
117 eventMark = eventMarkNull;
118 startMark = startMarkNull;
119 endMark = endMarkNull;
120 }
121
Romain Guy0f667532013-03-01 14:31:04 -0800122 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700123 setLabel = glLabelObjectEXT;
124 getLabel = glGetObjectLabelEXT;
125 } else {
126 setLabel = setLabelNull;
127 getLabel = getLabelNull;
128 }
129}
130
131void Caches::initConstraints() {
132 GLint maxTextureUnits;
133 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
134 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
135 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
136 }
137
138 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
139}
140
Romain Guyf9f00162013-05-09 11:50:12 -0700141void Caches::initStaticProperties() {
142 gpuPixelBuffersEnabled = false;
143
144 // OpenGL ES 3.0+ specific features
145 if (mExtensions.getMajorGlVersion() >= 3) {
146 char property[PROPERTY_VALUE_MAX];
147 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
148 gpuPixelBuffersEnabled = !strcmp(property, "true");
149 }
150 }
151}
152
Romain Guy5bb3c732012-11-29 17:52:58 -0800153bool Caches::initProperties() {
154 bool prevDebugLayersUpdates = debugLayersUpdates;
155 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800156 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800157
Romain Guy4ff0cf42012-08-06 14:51:10 -0700158 char property[PROPERTY_VALUE_MAX];
159 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
160 INIT_LOGD(" Layers updates debug enabled: %s", property);
161 debugLayersUpdates = !strcmp(property, "true");
162 } else {
163 debugLayersUpdates = false;
164 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700165
166 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
167 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy78dd96d2013-05-03 14:24:16 -0700168 debugOverdraw = !strcmp(property, "show");
Romain Guy7c450aa2012-09-21 19:15:00 -0700169 } else {
170 debugOverdraw = false;
171 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800172
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800173 // See Properties.h for valid values
174 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
175 INIT_LOGD(" Stencil clip debug enabled: %s", property);
176 if (!strcmp(property, "hide")) {
177 debugStencilClip = kStencilHide;
178 } else if (!strcmp(property, "highlight")) {
179 debugStencilClip = kStencilShowHighlight;
180 } else if (!strcmp(property, "region")) {
181 debugStencilClip = kStencilShowRegion;
182 }
183 } else {
184 debugStencilClip = kStencilHide;
185 }
186
Romain Guy0f667532013-03-01 14:31:04 -0800187 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
188 drawDeferDisabled = !strcasecmp(property, "true");
189 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
190 } else {
191 INIT_LOGD(" Draw defer enabled");
192 }
193
194 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
195 drawReorderDisabled = !strcasecmp(property, "true");
196 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
197 } else {
198 INIT_LOGD(" Draw reorder enabled");
199 }
200
Romain Guy5bb3c732012-11-29 17:52:58 -0800201 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800202 (prevDebugOverdraw != debugOverdraw) ||
203 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700204}
205
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800206void Caches::terminate() {
207 if (!mInitialized) return;
208
209 glDeleteBuffers(1, &meshBuffer);
210 mCurrentBuffer = 0;
211
Romain Guy3b748a42013-04-17 18:54:38 -0700212 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700213 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700214 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800215 mRegionMesh = NULL;
216
217 fboCache.clear();
218
219 programCache.clear();
220 currentProgram = NULL;
221
Romain Guy3b748a42013-04-17 18:54:38 -0700222 assetAtlas.terminate();
223
224 patchCache.clear();
225
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800226 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700227}
228
229///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800230// Debug
231///////////////////////////////////////////////////////////////////////////////
232
233void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 String8 stringLog;
235 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000236 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700237}
238
239void Caches::dumpMemoryUsage(String8 &log) {
240 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
241 log.appendFormat(" TextureCache %8d / %8d\n",
242 textureCache.getSize(), textureCache.getMaxSize());
243 log.appendFormat(" LayerCache %8d / %8d\n",
244 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800245 log.appendFormat(" RenderBufferCache %8d / %8d\n",
246 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700247 log.appendFormat(" GradientCache %8d / %8d\n",
248 gradientCache.getSize(), gradientCache.getMaxSize());
249 log.appendFormat(" PathCache %8d / %8d\n",
250 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700251 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800252 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700253 log.appendFormat(" PatchCache %8d / %8d\n",
254 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700255 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
256 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700257 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800258 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700259 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700260 log.appendFormat(" FboCache %8d / %8d\n",
261 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800262
263 uint32_t total = 0;
264 total += textureCache.getSize();
265 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800266 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800267 total += gradientCache.getSize();
268 total += pathCache.getSize();
269 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700270 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700271 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
272 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800273 }
274
Chet Haase9c1e23b2011-03-24 10:51:31 -0700275 log.appendFormat("Total memory usage:\n");
276 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800277}
278
279///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800280// Memory management
281///////////////////////////////////////////////////////////////////////////////
282
283void Caches::clearGarbage() {
284 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800285 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800286
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700287 Vector<DisplayList*> displayLists;
288 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800289
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700290 { // scope for the lock
291 Mutex::Autolock _l(mGarbageLock);
292 displayLists = mDisplayListGarbage;
293 layers = mLayerGarbage;
294 mDisplayListGarbage.clear();
295 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800296 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800297
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700298 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800299 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700300 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800301 delete displayList;
302 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700303
304 count = layers.size();
305 for (size_t i = 0; i < count; i++) {
306 Layer* layer = layers.itemAt(i);
307 delete layer;
308 }
309 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800310}
311
Romain Guyada830f2011-01-13 12:13:20 -0800312void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800313 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800314 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800315}
316
Romain Guybb0acdf2012-03-05 13:44:35 -0800317void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
318 Mutex::Autolock _l(mGarbageLock);
319 mDisplayListGarbage.push(displayList);
320}
321
Romain Guybdf76092011-07-18 15:00:43 -0700322void Caches::flush(FlushMode mode) {
323 FLUSH_LOGD("Flushing caches (mode %d)", mode);
324
Romain Guybdf76092011-07-18 15:00:43 -0700325 switch (mode) {
326 case kFlushMode_Full:
327 textureCache.clear();
328 patchCache.clear();
329 dropShadowCache.clear();
330 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700331 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700332 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700333 // fall through
334 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700335 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700336 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700337 pathCache.clear();
Romain Guyc5cbee72013-03-20 19:15:02 -0700338 tasks.stop();
Romain Guy6d7475d2011-07-27 16:28:21 -0700339 // fall through
340 case kFlushMode_Layers:
341 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800342 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700343 break;
344 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700345
346 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700347}
348
Romain Guyfe48f652010-11-11 15:36:56 -0800349///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700350// VBO
351///////////////////////////////////////////////////////////////////////////////
352
Romain Guyf3a910b42011-12-12 20:35:21 -0800353bool Caches::bindMeshBuffer() {
354 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700355}
356
Romain Guyf3a910b42011-12-12 20:35:21 -0800357bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700358 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700359 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700360 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800361 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700362 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800363 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700364}
365
Romain Guyf3a910b42011-12-12 20:35:21 -0800366bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700367 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700368 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700369 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800370 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700371 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800372 return false;
373}
374
Romain Guy15bc6432011-12-13 13:11:32 -0800375bool Caches::bindIndicesBuffer(const GLuint buffer) {
376 if (mCurrentIndicesBuffer != buffer) {
377 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
378 mCurrentIndicesBuffer = buffer;
379 return true;
380 }
381 return false;
382}
383
Romain Guy3b748a42013-04-17 18:54:38 -0700384bool Caches::bindIndicesBuffer() {
385 if (!mMeshIndices) {
386 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
387 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
388 uint16_t quad = i * 4;
389 int index = i * 6;
390 regionIndices[index ] = quad; // top-left
391 regionIndices[index + 1] = quad + 1; // top-right
392 regionIndices[index + 2] = quad + 2; // bottom-left
393 regionIndices[index + 3] = quad + 2; // bottom-left
394 regionIndices[index + 4] = quad + 1; // top-right
395 regionIndices[index + 5] = quad + 3; // bottom-right
396 }
397
398 glGenBuffers(1, &mMeshIndices);
399 bool force = bindIndicesBuffer(mMeshIndices);
400 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
401 regionIndices, GL_STATIC_DRAW);
402
403 delete[] regionIndices;
404 return force;
405 }
406
407 return bindIndicesBuffer(mMeshIndices);
408}
409
Romain Guy15bc6432011-12-13 13:11:32 -0800410bool Caches::unbindIndicesBuffer() {
411 if (mCurrentIndicesBuffer) {
412 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
413 mCurrentIndicesBuffer = 0;
414 return true;
415 }
416 return false;
417}
418
Romain Guy85ef80d2012-09-13 20:26:50 -0700419///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700420// PBO
421///////////////////////////////////////////////////////////////////////////////
422
423bool Caches::bindPixelBuffer(const GLuint buffer) {
424 if (mCurrentPixelBuffer != buffer) {
425 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
426 mCurrentPixelBuffer = buffer;
427 return true;
428 }
429 return false;
430}
431
432bool Caches::unbindPixelBuffer() {
433 if (mCurrentPixelBuffer) {
434 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
435 mCurrentPixelBuffer = 0;
436 return true;
437 }
438 return false;
439}
440
441///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700442// Meshes and textures
443///////////////////////////////////////////////////////////////////////////////
444
Chris Craikcb4d6002012-09-25 12:00:29 -0700445void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
446 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
447 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800448 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
449 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700450 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800451 }
452}
453
Romain Guyff316ec2013-02-13 18:39:43 -0800454void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
455 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700456 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800457 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800458 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800459 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800460 }
461}
462
463void Caches::resetVertexPointers() {
464 mCurrentPositionPointer = this;
465 mCurrentTexCoordsPointer = this;
466}
467
468void Caches::resetTexCoordsVertexPointer() {
469 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700470}
471
Romain Guy15bc6432011-12-13 13:11:32 -0800472void Caches::enableTexCoordsVertexArray() {
473 if (!mTexCoordsArrayEnabled) {
474 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800475 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800476 mTexCoordsArrayEnabled = true;
477 }
478}
479
Romain Guyff316ec2013-02-13 18:39:43 -0800480void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800481 if (mTexCoordsArrayEnabled) {
482 glDisableVertexAttribArray(Program::kBindingTexCoords);
483 mTexCoordsArrayEnabled = false;
484 }
485}
486
Romain Guya1d3c912011-12-13 14:55:06 -0800487void Caches::activeTexture(GLuint textureUnit) {
488 if (mTextureUnit != textureUnit) {
489 glActiveTexture(gTextureUnits[textureUnit]);
490 mTextureUnit = textureUnit;
491 }
492}
493
Romain Guy85ef80d2012-09-13 20:26:50 -0700494///////////////////////////////////////////////////////////////////////////////
495// Scissor
496///////////////////////////////////////////////////////////////////////////////
497
Romain Guy8a4ac612012-07-17 17:32:48 -0700498bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700499 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
500 width != mScissorWidth || height != mScissorHeight)) {
501
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700502 if (x < 0) {
503 width += x;
504 x = 0;
505 }
506 if (y < 0) {
507 height += y;
508 y = 0;
509 }
510 if (width < 0) {
511 width = 0;
512 }
513 if (height < 0) {
514 height = 0;
515 }
Romain Guy8f85e802011-12-14 19:23:32 -0800516 glScissor(x, y, width, height);
517
518 mScissorX = x;
519 mScissorY = y;
520 mScissorWidth = width;
521 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700522
523 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800524 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700525 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800526}
527
Romain Guy8a4ac612012-07-17 17:32:48 -0700528bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700529 if (!scissorEnabled) {
530 glEnable(GL_SCISSOR_TEST);
531 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700532 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700533 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700534 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700535 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700536}
537
Romain Guy8a4ac612012-07-17 17:32:48 -0700538bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700539 if (scissorEnabled) {
540 glDisable(GL_SCISSOR_TEST);
541 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700542 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700543 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700544 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700545}
546
547void Caches::setScissorEnabled(bool enabled) {
548 if (scissorEnabled != enabled) {
549 if (enabled) glEnable(GL_SCISSOR_TEST);
550 else glDisable(GL_SCISSOR_TEST);
551 scissorEnabled = enabled;
552 }
553}
554
Romain Guy82bc7a72012-01-03 14:13:39 -0800555void Caches::resetScissor() {
556 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
557}
558
Romain Guy85ef80d2012-09-13 20:26:50 -0700559///////////////////////////////////////////////////////////////////////////////
560// Tiling
561///////////////////////////////////////////////////////////////////////////////
562
Romain Guyf735c8e2013-01-31 17:45:55 -0800563void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800564 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800565 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700566 }
567}
568
569void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800570 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700571 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700572 }
573}
574
Romain Guy54c1a642012-09-27 17:55:46 -0700575bool Caches::hasRegisteredFunctors() {
576 return mFunctorsCount > 0;
577}
578
579void Caches::registerFunctors(uint32_t functorCount) {
580 mFunctorsCount += functorCount;
581}
582
583void Caches::unregisterFunctors(uint32_t functorCount) {
584 if (functorCount > mFunctorsCount) {
585 mFunctorsCount = 0;
586 } else {
587 mFunctorsCount -= functorCount;
588 }
589}
590
Romain Guy85ef80d2012-09-13 20:26:50 -0700591///////////////////////////////////////////////////////////////////////////////
592// Regions
593///////////////////////////////////////////////////////////////////////////////
594
Romain Guy5b3b3522010-10-27 18:57:51 -0700595TextureVertex* Caches::getRegionMesh() {
596 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
597 if (!mRegionMesh) {
598 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700599 }
600
601 return mRegionMesh;
602}
603
Chet Haasedd78cca2010-10-22 18:59:26 -0700604}; // namespace uirenderer
605}; // namespace android