blob: a381a6829c24fd5bd81994c820f3699f6ec2b87f [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
Romain Guyc15008e2010-11-10 11:59:15 -080019#include <utils/Log.h>
Chet Haase9c1e23b2011-03-24 10:51:31 -070020#include <utils/String8.h>
Romain Guyc15008e2010-11-10 11:59:15 -080021
Chet Haasedd78cca2010-10-22 18:59:26 -070022#include "Caches.h"
Romain Guybb0acdf2012-03-05 13:44:35 -080023#include "DisplayListRenderer.h"
Romain Guye190aa62010-11-10 19:01:29 -080024#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080025#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070026
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070037// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070042#else
43 #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070047// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy3bbacf22013-02-06 16:51:04 -080050Caches::Caches(): Singleton<Caches>(), mExtensions(Extensions::getInstance()), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080051 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070052 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070053 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070054 initProperties();
Romain Guy0f667532013-03-01 14:31:04 -080055 initExtensions();
Romain Guye190aa62010-11-10 19:01:29 -080056
57 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000058 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070059}
60
Romain Guy8ff6b9e2011-11-09 20:10:18 -080061void Caches::init() {
62 if (mInitialized) return;
63
64 glGenBuffers(1, &meshBuffer);
65 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
66 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
67
68 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080069 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080070 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070071 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080072 mCurrentTexCoordsPointer = this;
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;
85
86 blend = false;
87 lastSrcMode = GL_ZERO;
88 lastDstMode = GL_ZERO;
89 currentProgram = NULL;
90
Romain Guy54c1a642012-09-27 17:55:46 -070091 mFunctorsCount = 0;
92
Romain Guyc2a97212013-02-06 15:29:46 -080093 debugLayersUpdates = false;
94 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080095 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080096
Romain Guy8ff6b9e2011-11-09 20:10:18 -080097 mInitialized = true;
98}
99
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700100void Caches::initFont() {
101 fontRenderer = GammaFontRenderer::createRenderer();
102}
103
Romain Guydfa10462012-05-12 16:18:58 -0700104void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800105 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700106 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800107
Chris Craikff785832013-03-08 13:12:16 -0800108 startMark = glPushGroupMarkerEXT;
109 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700110 } else {
111 eventMark = eventMarkNull;
112 startMark = startMarkNull;
113 endMark = endMarkNull;
114 }
115
Romain Guy0f667532013-03-01 14:31:04 -0800116 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700117 setLabel = glLabelObjectEXT;
118 getLabel = glGetObjectLabelEXT;
119 } else {
120 setLabel = setLabelNull;
121 getLabel = getLabelNull;
122 }
123}
124
125void Caches::initConstraints() {
126 GLint maxTextureUnits;
127 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
128 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
129 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
130 }
131
132 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
133}
134
Romain Guy5bb3c732012-11-29 17:52:58 -0800135bool Caches::initProperties() {
136 bool prevDebugLayersUpdates = debugLayersUpdates;
137 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800138 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800139
Romain Guy4ff0cf42012-08-06 14:51:10 -0700140 char property[PROPERTY_VALUE_MAX];
141 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
142 INIT_LOGD(" Layers updates debug enabled: %s", property);
143 debugLayersUpdates = !strcmp(property, "true");
144 } else {
145 debugLayersUpdates = false;
146 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700147
148 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
149 INIT_LOGD(" Overdraw debug enabled: %s", property);
150 debugOverdraw = !strcmp(property, "true");
151 } else {
152 debugOverdraw = false;
153 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800154
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800155 // See Properties.h for valid values
156 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
157 INIT_LOGD(" Stencil clip debug enabled: %s", property);
158 if (!strcmp(property, "hide")) {
159 debugStencilClip = kStencilHide;
160 } else if (!strcmp(property, "highlight")) {
161 debugStencilClip = kStencilShowHighlight;
162 } else if (!strcmp(property, "region")) {
163 debugStencilClip = kStencilShowRegion;
164 }
165 } else {
166 debugStencilClip = kStencilHide;
167 }
168
Romain Guy0f667532013-03-01 14:31:04 -0800169 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
170 drawDeferDisabled = !strcasecmp(property, "true");
171 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
172 } else {
173 INIT_LOGD(" Draw defer enabled");
174 }
175
176 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
177 drawReorderDisabled = !strcasecmp(property, "true");
178 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
179 } else {
180 INIT_LOGD(" Draw reorder enabled");
181 }
182
Romain Guy5bb3c732012-11-29 17:52:58 -0800183 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800184 (prevDebugOverdraw != debugOverdraw) ||
185 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700186}
187
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800188void Caches::terminate() {
189 if (!mInitialized) return;
190
191 glDeleteBuffers(1, &meshBuffer);
192 mCurrentBuffer = 0;
193
194 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700195 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800196 mRegionMesh = NULL;
197
198 fboCache.clear();
199
200 programCache.clear();
201 currentProgram = NULL;
202
203 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700204}
205
206///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800207// Debug
208///////////////////////////////////////////////////////////////////////////////
209
210void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700211 String8 stringLog;
212 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000213 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700214}
215
216void Caches::dumpMemoryUsage(String8 &log) {
217 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
218 log.appendFormat(" TextureCache %8d / %8d\n",
219 textureCache.getSize(), textureCache.getMaxSize());
220 log.appendFormat(" LayerCache %8d / %8d\n",
221 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800222 log.appendFormat(" RenderBufferCache %8d / %8d\n",
223 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700224 log.appendFormat(" GradientCache %8d / %8d\n",
225 gradientCache.getSize(), gradientCache.getMaxSize());
226 log.appendFormat(" PathCache %8d / %8d\n",
227 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700228 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800229 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700230 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
231 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700232 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800233 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700234 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700235 log.appendFormat(" FboCache %8d / %8d\n",
236 fboCache.getSize(), fboCache.getMaxSize());
237 log.appendFormat(" PatchCache %8d / %8d\n",
238 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800239
240 uint32_t total = 0;
241 total += textureCache.getSize();
242 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800243 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800244 total += gradientCache.getSize();
245 total += pathCache.getSize();
246 total += dropShadowCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700247 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
248 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800249 }
250
Chet Haase9c1e23b2011-03-24 10:51:31 -0700251 log.appendFormat("Total memory usage:\n");
252 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800253}
254
255///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800256// Memory management
257///////////////////////////////////////////////////////////////////////////////
258
259void Caches::clearGarbage() {
260 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800261 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800262
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700263 Vector<DisplayList*> displayLists;
264 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800265
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700266 { // scope for the lock
267 Mutex::Autolock _l(mGarbageLock);
268 displayLists = mDisplayListGarbage;
269 layers = mLayerGarbage;
270 mDisplayListGarbage.clear();
271 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800272 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800273
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700274 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800275 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700276 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800277 delete displayList;
278 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700279
280 count = layers.size();
281 for (size_t i = 0; i < count; i++) {
282 Layer* layer = layers.itemAt(i);
283 delete layer;
284 }
285 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800286}
287
Romain Guyada830f2011-01-13 12:13:20 -0800288void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800289 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800290 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800291}
292
Romain Guybb0acdf2012-03-05 13:44:35 -0800293void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
294 Mutex::Autolock _l(mGarbageLock);
295 mDisplayListGarbage.push(displayList);
296}
297
Romain Guybdf76092011-07-18 15:00:43 -0700298void Caches::flush(FlushMode mode) {
299 FLUSH_LOGD("Flushing caches (mode %d)", mode);
300
Romain Guybdf76092011-07-18 15:00:43 -0700301 switch (mode) {
302 case kFlushMode_Full:
303 textureCache.clear();
304 patchCache.clear();
305 dropShadowCache.clear();
306 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700307 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700308 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700309 // fall through
310 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700311 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700312 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700313 pathCache.clear();
Romain Guyc5cbee72013-03-20 19:15:02 -0700314 tasks.stop();
Romain Guy6d7475d2011-07-27 16:28:21 -0700315 // fall through
316 case kFlushMode_Layers:
317 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800318 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700319 break;
320 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700321
322 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700323}
324
Romain Guyfe48f652010-11-11 15:36:56 -0800325///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700326// VBO
327///////////////////////////////////////////////////////////////////////////////
328
Romain Guyf3a910b42011-12-12 20:35:21 -0800329bool Caches::bindMeshBuffer() {
330 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700331}
332
Romain Guyf3a910b42011-12-12 20:35:21 -0800333bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700334 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700335 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700336 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800337 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700338 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800339 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700340}
341
Romain Guyf3a910b42011-12-12 20:35:21 -0800342bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700343 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700344 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700345 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800346 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700347 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800348 return false;
349}
350
Romain Guy15bc6432011-12-13 13:11:32 -0800351bool Caches::bindIndicesBuffer(const GLuint buffer) {
352 if (mCurrentIndicesBuffer != buffer) {
353 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
354 mCurrentIndicesBuffer = buffer;
355 return true;
356 }
357 return false;
358}
359
360bool Caches::unbindIndicesBuffer() {
361 if (mCurrentIndicesBuffer) {
362 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
363 mCurrentIndicesBuffer = 0;
364 return true;
365 }
366 return false;
367}
368
Romain Guy85ef80d2012-09-13 20:26:50 -0700369///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700370// PBO
371///////////////////////////////////////////////////////////////////////////////
372
373bool Caches::bindPixelBuffer(const GLuint buffer) {
374 if (mCurrentPixelBuffer != buffer) {
375 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
376 mCurrentPixelBuffer = buffer;
377 return true;
378 }
379 return false;
380}
381
382bool Caches::unbindPixelBuffer() {
383 if (mCurrentPixelBuffer) {
384 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
385 mCurrentPixelBuffer = 0;
386 return true;
387 }
388 return false;
389}
390
391///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700392// Meshes and textures
393///////////////////////////////////////////////////////////////////////////////
394
Chris Craikcb4d6002012-09-25 12:00:29 -0700395void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
396 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
397 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800398 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
399 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700400 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800401 }
402}
403
Romain Guyff316ec2013-02-13 18:39:43 -0800404void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
405 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700406 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800407 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800408 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800409 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800410 }
411}
412
413void Caches::resetVertexPointers() {
414 mCurrentPositionPointer = this;
415 mCurrentTexCoordsPointer = this;
416}
417
418void Caches::resetTexCoordsVertexPointer() {
419 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700420}
421
Romain Guy15bc6432011-12-13 13:11:32 -0800422void Caches::enableTexCoordsVertexArray() {
423 if (!mTexCoordsArrayEnabled) {
424 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800425 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800426 mTexCoordsArrayEnabled = true;
427 }
428}
429
Romain Guyff316ec2013-02-13 18:39:43 -0800430void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800431 if (mTexCoordsArrayEnabled) {
432 glDisableVertexAttribArray(Program::kBindingTexCoords);
433 mTexCoordsArrayEnabled = false;
434 }
435}
436
Romain Guya1d3c912011-12-13 14:55:06 -0800437void Caches::activeTexture(GLuint textureUnit) {
438 if (mTextureUnit != textureUnit) {
439 glActiveTexture(gTextureUnits[textureUnit]);
440 mTextureUnit = textureUnit;
441 }
442}
443
Romain Guy85ef80d2012-09-13 20:26:50 -0700444///////////////////////////////////////////////////////////////////////////////
445// Scissor
446///////////////////////////////////////////////////////////////////////////////
447
Romain Guy8a4ac612012-07-17 17:32:48 -0700448bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700449 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
450 width != mScissorWidth || height != mScissorHeight)) {
451
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700452 if (x < 0) {
453 width += x;
454 x = 0;
455 }
456 if (y < 0) {
457 height += y;
458 y = 0;
459 }
460 if (width < 0) {
461 width = 0;
462 }
463 if (height < 0) {
464 height = 0;
465 }
Romain Guy8f85e802011-12-14 19:23:32 -0800466 glScissor(x, y, width, height);
467
468 mScissorX = x;
469 mScissorY = y;
470 mScissorWidth = width;
471 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700472
473 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800474 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700475 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800476}
477
Romain Guy8a4ac612012-07-17 17:32:48 -0700478bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700479 if (!scissorEnabled) {
480 glEnable(GL_SCISSOR_TEST);
481 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700482 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700483 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700484 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700485 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700486}
487
Romain Guy8a4ac612012-07-17 17:32:48 -0700488bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700489 if (scissorEnabled) {
490 glDisable(GL_SCISSOR_TEST);
491 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700492 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700493 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700494 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700495}
496
497void Caches::setScissorEnabled(bool enabled) {
498 if (scissorEnabled != enabled) {
499 if (enabled) glEnable(GL_SCISSOR_TEST);
500 else glDisable(GL_SCISSOR_TEST);
501 scissorEnabled = enabled;
502 }
503}
504
Romain Guy82bc7a72012-01-03 14:13:39 -0800505void Caches::resetScissor() {
506 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
507}
508
Romain Guy85ef80d2012-09-13 20:26:50 -0700509///////////////////////////////////////////////////////////////////////////////
510// Tiling
511///////////////////////////////////////////////////////////////////////////////
512
Romain Guyf735c8e2013-01-31 17:45:55 -0800513void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800514 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800515 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700516 }
517}
518
519void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800520 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700521 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700522 }
523}
524
Romain Guy54c1a642012-09-27 17:55:46 -0700525bool Caches::hasRegisteredFunctors() {
526 return mFunctorsCount > 0;
527}
528
529void Caches::registerFunctors(uint32_t functorCount) {
530 mFunctorsCount += functorCount;
531}
532
533void Caches::unregisterFunctors(uint32_t functorCount) {
534 if (functorCount > mFunctorsCount) {
535 mFunctorsCount = 0;
536 } else {
537 mFunctorsCount -= functorCount;
538 }
539}
540
Romain Guy85ef80d2012-09-13 20:26:50 -0700541///////////////////////////////////////////////////////////////////////////////
542// Regions
543///////////////////////////////////////////////////////////////////////////////
544
Romain Guy5b3b3522010-10-27 18:57:51 -0700545TextureVertex* Caches::getRegionMesh() {
546 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
547 if (!mRegionMesh) {
548 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
549
550 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
551 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
552 uint16_t quad = i * 4;
553 int index = i * 6;
554 regionIndices[index ] = quad; // top-left
555 regionIndices[index + 1] = quad + 1; // top-right
556 regionIndices[index + 2] = quad + 2; // bottom-left
557 regionIndices[index + 3] = quad + 2; // bottom-left
558 regionIndices[index + 4] = quad + 1; // top-right
559 regionIndices[index + 5] = quad + 3; // bottom-right
560 }
561
562 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800563 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700564 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
565 regionIndices, GL_STATIC_DRAW);
566
567 delete[] regionIndices;
568 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800569 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700570 }
571
572 return mRegionMesh;
573}
574
Chet Haasedd78cca2010-10-22 18:59:26 -0700575}; // namespace uirenderer
576}; // namespace android