blob: c60848c72c9ee8bc60ae0d85af0b7a2e4b939e42 [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 Guy3b748a42013-04-17 18:54:38 -070061bool Caches::init() {
62 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080063
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;
Romain Guycf51a412013-04-08 19:40:31 -070073 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080074
Romain Guy15bc6432011-12-13 13:11:32 -080075 mTexCoordsArrayEnabled = false;
76
Romain Guyb1d0a4e2012-07-13 18:25:35 -070077 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070078 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080079 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
80
Romain Guya1d3c912011-12-13 14:55:06 -080081 glActiveTexture(gTextureUnits[0]);
82 mTextureUnit = 0;
83
Romain Guy8ff6b9e2011-11-09 20:10:18 -080084 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070085 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080086
87 blend = false;
88 lastSrcMode = GL_ZERO;
89 lastDstMode = GL_ZERO;
90 currentProgram = NULL;
91
Romain Guy54c1a642012-09-27 17:55:46 -070092 mFunctorsCount = 0;
93
Romain Guyc2a97212013-02-06 15:29:46 -080094 debugLayersUpdates = false;
95 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080096 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080097
Romain Guy3b748a42013-04-17 18:54:38 -070098 patchCache.init(*this);
99
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800100 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700101
102 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800103}
104
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700105void Caches::initFont() {
106 fontRenderer = GammaFontRenderer::createRenderer();
107}
108
Romain Guydfa10462012-05-12 16:18:58 -0700109void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800110 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700111 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800112
Chris Craikff785832013-03-08 13:12:16 -0800113 startMark = glPushGroupMarkerEXT;
114 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700115 } else {
116 eventMark = eventMarkNull;
117 startMark = startMarkNull;
118 endMark = endMarkNull;
119 }
120
Romain Guy0f667532013-03-01 14:31:04 -0800121 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700122 setLabel = glLabelObjectEXT;
123 getLabel = glGetObjectLabelEXT;
124 } else {
125 setLabel = setLabelNull;
126 getLabel = getLabelNull;
127 }
128}
129
130void Caches::initConstraints() {
131 GLint maxTextureUnits;
132 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
133 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
134 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
135 }
136
137 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
138}
139
Romain Guy5bb3c732012-11-29 17:52:58 -0800140bool Caches::initProperties() {
141 bool prevDebugLayersUpdates = debugLayersUpdates;
142 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800143 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800144
Romain Guy4ff0cf42012-08-06 14:51:10 -0700145 char property[PROPERTY_VALUE_MAX];
146 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
147 INIT_LOGD(" Layers updates debug enabled: %s", property);
148 debugLayersUpdates = !strcmp(property, "true");
149 } else {
150 debugLayersUpdates = false;
151 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700152
153 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
154 INIT_LOGD(" Overdraw debug enabled: %s", property);
155 debugOverdraw = !strcmp(property, "true");
156 } else {
157 debugOverdraw = false;
158 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800159
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800160 // See Properties.h for valid values
161 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
162 INIT_LOGD(" Stencil clip debug enabled: %s", property);
163 if (!strcmp(property, "hide")) {
164 debugStencilClip = kStencilHide;
165 } else if (!strcmp(property, "highlight")) {
166 debugStencilClip = kStencilShowHighlight;
167 } else if (!strcmp(property, "region")) {
168 debugStencilClip = kStencilShowRegion;
169 }
170 } else {
171 debugStencilClip = kStencilHide;
172 }
173
Romain Guy0f667532013-03-01 14:31:04 -0800174 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
175 drawDeferDisabled = !strcasecmp(property, "true");
176 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
177 } else {
178 INIT_LOGD(" Draw defer enabled");
179 }
180
181 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
182 drawReorderDisabled = !strcasecmp(property, "true");
183 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
184 } else {
185 INIT_LOGD(" Draw reorder enabled");
186 }
187
Romain Guy5bb3c732012-11-29 17:52:58 -0800188 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800189 (prevDebugOverdraw != debugOverdraw) ||
190 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700191}
192
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800193void Caches::terminate() {
194 if (!mInitialized) return;
195
196 glDeleteBuffers(1, &meshBuffer);
197 mCurrentBuffer = 0;
198
Romain Guy3b748a42013-04-17 18:54:38 -0700199 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700200 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700201 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800202 mRegionMesh = NULL;
203
204 fboCache.clear();
205
206 programCache.clear();
207 currentProgram = NULL;
208
Romain Guy3b748a42013-04-17 18:54:38 -0700209 assetAtlas.terminate();
210
211 patchCache.clear();
212
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800213 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700214}
215
216///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800217// Debug
218///////////////////////////////////////////////////////////////////////////////
219
220void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700221 String8 stringLog;
222 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000223 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700224}
225
226void Caches::dumpMemoryUsage(String8 &log) {
227 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
228 log.appendFormat(" TextureCache %8d / %8d\n",
229 textureCache.getSize(), textureCache.getMaxSize());
230 log.appendFormat(" LayerCache %8d / %8d\n",
231 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800232 log.appendFormat(" RenderBufferCache %8d / %8d\n",
233 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 log.appendFormat(" GradientCache %8d / %8d\n",
235 gradientCache.getSize(), gradientCache.getMaxSize());
236 log.appendFormat(" PathCache %8d / %8d\n",
237 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700238 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800239 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700240 log.appendFormat(" PatchCache %8d / %8d\n",
241 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700242 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
243 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700244 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800245 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700246 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700247 log.appendFormat(" FboCache %8d / %8d\n",
248 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800249
250 uint32_t total = 0;
251 total += textureCache.getSize();
252 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800253 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800254 total += gradientCache.getSize();
255 total += pathCache.getSize();
256 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700257 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700258 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
259 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800260 }
261
Chet Haase9c1e23b2011-03-24 10:51:31 -0700262 log.appendFormat("Total memory usage:\n");
263 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800264}
265
266///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800267// Memory management
268///////////////////////////////////////////////////////////////////////////////
269
270void Caches::clearGarbage() {
271 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800272 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800273
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700274 Vector<DisplayList*> displayLists;
275 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800276
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700277 { // scope for the lock
278 Mutex::Autolock _l(mGarbageLock);
279 displayLists = mDisplayListGarbage;
280 layers = mLayerGarbage;
281 mDisplayListGarbage.clear();
282 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800283 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800284
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700285 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800286 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700287 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800288 delete displayList;
289 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700290
291 count = layers.size();
292 for (size_t i = 0; i < count; i++) {
293 Layer* layer = layers.itemAt(i);
294 delete layer;
295 }
296 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800297}
298
Romain Guyada830f2011-01-13 12:13:20 -0800299void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800300 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800301 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800302}
303
Romain Guybb0acdf2012-03-05 13:44:35 -0800304void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
305 Mutex::Autolock _l(mGarbageLock);
306 mDisplayListGarbage.push(displayList);
307}
308
Romain Guybdf76092011-07-18 15:00:43 -0700309void Caches::flush(FlushMode mode) {
310 FLUSH_LOGD("Flushing caches (mode %d)", mode);
311
Romain Guybdf76092011-07-18 15:00:43 -0700312 switch (mode) {
313 case kFlushMode_Full:
314 textureCache.clear();
315 patchCache.clear();
316 dropShadowCache.clear();
317 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700318 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700319 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700320 // fall through
321 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700322 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700323 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700324 pathCache.clear();
Romain Guyc5cbee72013-03-20 19:15:02 -0700325 tasks.stop();
Romain Guy6d7475d2011-07-27 16:28:21 -0700326 // fall through
327 case kFlushMode_Layers:
328 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800329 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700330 break;
331 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700332
333 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700334}
335
Romain Guyfe48f652010-11-11 15:36:56 -0800336///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700337// VBO
338///////////////////////////////////////////////////////////////////////////////
339
Romain Guyf3a910b42011-12-12 20:35:21 -0800340bool Caches::bindMeshBuffer() {
341 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700342}
343
Romain Guyf3a910b42011-12-12 20:35:21 -0800344bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700345 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700346 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700347 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800348 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700349 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800350 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700351}
352
Romain Guyf3a910b42011-12-12 20:35:21 -0800353bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700354 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700355 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700356 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800357 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700358 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800359 return false;
360}
361
Romain Guy15bc6432011-12-13 13:11:32 -0800362bool Caches::bindIndicesBuffer(const GLuint buffer) {
363 if (mCurrentIndicesBuffer != buffer) {
364 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
365 mCurrentIndicesBuffer = buffer;
366 return true;
367 }
368 return false;
369}
370
Romain Guy3b748a42013-04-17 18:54:38 -0700371bool Caches::bindIndicesBuffer() {
372 if (!mMeshIndices) {
373 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
374 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
375 uint16_t quad = i * 4;
376 int index = i * 6;
377 regionIndices[index ] = quad; // top-left
378 regionIndices[index + 1] = quad + 1; // top-right
379 regionIndices[index + 2] = quad + 2; // bottom-left
380 regionIndices[index + 3] = quad + 2; // bottom-left
381 regionIndices[index + 4] = quad + 1; // top-right
382 regionIndices[index + 5] = quad + 3; // bottom-right
383 }
384
385 glGenBuffers(1, &mMeshIndices);
386 bool force = bindIndicesBuffer(mMeshIndices);
387 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
388 regionIndices, GL_STATIC_DRAW);
389
390 delete[] regionIndices;
391 return force;
392 }
393
394 return bindIndicesBuffer(mMeshIndices);
395}
396
Romain Guy15bc6432011-12-13 13:11:32 -0800397bool Caches::unbindIndicesBuffer() {
398 if (mCurrentIndicesBuffer) {
399 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
400 mCurrentIndicesBuffer = 0;
401 return true;
402 }
403 return false;
404}
405
Romain Guy85ef80d2012-09-13 20:26:50 -0700406///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700407// PBO
408///////////////////////////////////////////////////////////////////////////////
409
410bool Caches::bindPixelBuffer(const GLuint buffer) {
411 if (mCurrentPixelBuffer != buffer) {
412 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
413 mCurrentPixelBuffer = buffer;
414 return true;
415 }
416 return false;
417}
418
419bool Caches::unbindPixelBuffer() {
420 if (mCurrentPixelBuffer) {
421 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
422 mCurrentPixelBuffer = 0;
423 return true;
424 }
425 return false;
426}
427
428///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700429// Meshes and textures
430///////////////////////////////////////////////////////////////////////////////
431
Chris Craikcb4d6002012-09-25 12:00:29 -0700432void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
433 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
434 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800435 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
436 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700437 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800438 }
439}
440
Romain Guyff316ec2013-02-13 18:39:43 -0800441void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
442 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700443 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800444 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800445 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800446 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800447 }
448}
449
450void Caches::resetVertexPointers() {
451 mCurrentPositionPointer = this;
452 mCurrentTexCoordsPointer = this;
453}
454
455void Caches::resetTexCoordsVertexPointer() {
456 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700457}
458
Romain Guy15bc6432011-12-13 13:11:32 -0800459void Caches::enableTexCoordsVertexArray() {
460 if (!mTexCoordsArrayEnabled) {
461 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800462 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800463 mTexCoordsArrayEnabled = true;
464 }
465}
466
Romain Guyff316ec2013-02-13 18:39:43 -0800467void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800468 if (mTexCoordsArrayEnabled) {
469 glDisableVertexAttribArray(Program::kBindingTexCoords);
470 mTexCoordsArrayEnabled = false;
471 }
472}
473
Romain Guya1d3c912011-12-13 14:55:06 -0800474void Caches::activeTexture(GLuint textureUnit) {
475 if (mTextureUnit != textureUnit) {
476 glActiveTexture(gTextureUnits[textureUnit]);
477 mTextureUnit = textureUnit;
478 }
479}
480
Romain Guy85ef80d2012-09-13 20:26:50 -0700481///////////////////////////////////////////////////////////////////////////////
482// Scissor
483///////////////////////////////////////////////////////////////////////////////
484
Romain Guy8a4ac612012-07-17 17:32:48 -0700485bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700486 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
487 width != mScissorWidth || height != mScissorHeight)) {
488
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700489 if (x < 0) {
490 width += x;
491 x = 0;
492 }
493 if (y < 0) {
494 height += y;
495 y = 0;
496 }
497 if (width < 0) {
498 width = 0;
499 }
500 if (height < 0) {
501 height = 0;
502 }
Romain Guy8f85e802011-12-14 19:23:32 -0800503 glScissor(x, y, width, height);
504
505 mScissorX = x;
506 mScissorY = y;
507 mScissorWidth = width;
508 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700509
510 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800511 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700512 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800513}
514
Romain Guy8a4ac612012-07-17 17:32:48 -0700515bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700516 if (!scissorEnabled) {
517 glEnable(GL_SCISSOR_TEST);
518 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700519 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700520 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700521 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700522 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700523}
524
Romain Guy8a4ac612012-07-17 17:32:48 -0700525bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700526 if (scissorEnabled) {
527 glDisable(GL_SCISSOR_TEST);
528 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700529 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700530 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700531 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700532}
533
534void Caches::setScissorEnabled(bool enabled) {
535 if (scissorEnabled != enabled) {
536 if (enabled) glEnable(GL_SCISSOR_TEST);
537 else glDisable(GL_SCISSOR_TEST);
538 scissorEnabled = enabled;
539 }
540}
541
Romain Guy82bc7a72012-01-03 14:13:39 -0800542void Caches::resetScissor() {
543 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
544}
545
Romain Guy85ef80d2012-09-13 20:26:50 -0700546///////////////////////////////////////////////////////////////////////////////
547// Tiling
548///////////////////////////////////////////////////////////////////////////////
549
Romain Guyf735c8e2013-01-31 17:45:55 -0800550void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800551 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800552 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700553 }
554}
555
556void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800557 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700558 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700559 }
560}
561
Romain Guy54c1a642012-09-27 17:55:46 -0700562bool Caches::hasRegisteredFunctors() {
563 return mFunctorsCount > 0;
564}
565
566void Caches::registerFunctors(uint32_t functorCount) {
567 mFunctorsCount += functorCount;
568}
569
570void Caches::unregisterFunctors(uint32_t functorCount) {
571 if (functorCount > mFunctorsCount) {
572 mFunctorsCount = 0;
573 } else {
574 mFunctorsCount -= functorCount;
575 }
576}
577
Romain Guy85ef80d2012-09-13 20:26:50 -0700578///////////////////////////////////////////////////////////////////////////////
579// Regions
580///////////////////////////////////////////////////////////////////////////////
581
Romain Guy5b3b3522010-10-27 18:57:51 -0700582TextureVertex* Caches::getRegionMesh() {
583 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
584 if (!mRegionMesh) {
585 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700586 }
587
588 return mRegionMesh;
589}
590
Chet Haasedd78cca2010-10-22 18:59:26 -0700591}; // namespace uirenderer
592}; // namespace android