blob: 57d1a4f27a552af9e5d4859350b085487c9c5695 [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 Guy0f6675332013-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;
73
Romain Guy15bc6432011-12-13 13:11:32 -080074 mTexCoordsArrayEnabled = false;
75
Romain Guyb1d0a4e2012-07-13 18:25:35 -070076 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070077 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080078 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
79
Romain Guya1d3c912011-12-13 14:55:06 -080080 glActiveTexture(gTextureUnits[0]);
81 mTextureUnit = 0;
82
Romain Guy8ff6b9e2011-11-09 20:10:18 -080083 mRegionMesh = NULL;
84
85 blend = false;
86 lastSrcMode = GL_ZERO;
87 lastDstMode = GL_ZERO;
88 currentProgram = NULL;
89
Romain Guy54c1a642012-09-27 17:55:46 -070090 mFunctorsCount = 0;
91
Romain Guyc2a97212013-02-06 15:29:46 -080092 debugLayersUpdates = false;
93 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080094 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -080095
Romain Guy8ff6b9e2011-11-09 20:10:18 -080096 mInitialized = true;
97}
98
Romain Guyb1d0a4e2012-07-13 18:25:35 -070099void Caches::initFont() {
100 fontRenderer = GammaFontRenderer::createRenderer();
101}
102
Romain Guydfa10462012-05-12 16:18:58 -0700103void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800104 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700105 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -0800106
Chris Craikff785832013-03-08 13:12:16 -0800107 startMark = glPushGroupMarkerEXT;
108 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700109 } else {
110 eventMark = eventMarkNull;
111 startMark = startMarkNull;
112 endMark = endMarkNull;
113 }
114
Romain Guy0f6675332013-03-01 14:31:04 -0800115 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700116 setLabel = glLabelObjectEXT;
117 getLabel = glGetObjectLabelEXT;
118 } else {
119 setLabel = setLabelNull;
120 getLabel = getLabelNull;
121 }
122}
123
124void Caches::initConstraints() {
125 GLint maxTextureUnits;
126 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
127 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
128 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
129 }
130
131 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
132}
133
Romain Guy5bb3c732012-11-29 17:52:58 -0800134bool Caches::initProperties() {
135 bool prevDebugLayersUpdates = debugLayersUpdates;
136 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800137 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800138
Romain Guy4ff0cf42012-08-06 14:51:10 -0700139 char property[PROPERTY_VALUE_MAX];
140 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
141 INIT_LOGD(" Layers updates debug enabled: %s", property);
142 debugLayersUpdates = !strcmp(property, "true");
143 } else {
144 debugLayersUpdates = false;
145 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700146
147 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
148 INIT_LOGD(" Overdraw debug enabled: %s", property);
149 debugOverdraw = !strcmp(property, "true");
150 } else {
151 debugOverdraw = false;
152 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800153
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800154 // See Properties.h for valid values
155 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
156 INIT_LOGD(" Stencil clip debug enabled: %s", property);
157 if (!strcmp(property, "hide")) {
158 debugStencilClip = kStencilHide;
159 } else if (!strcmp(property, "highlight")) {
160 debugStencilClip = kStencilShowHighlight;
161 } else if (!strcmp(property, "region")) {
162 debugStencilClip = kStencilShowRegion;
163 }
164 } else {
165 debugStencilClip = kStencilHide;
166 }
167
Romain Guy0f6675332013-03-01 14:31:04 -0800168 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
169 drawDeferDisabled = !strcasecmp(property, "true");
170 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
171 } else {
172 INIT_LOGD(" Draw defer enabled");
173 }
174
175 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
176 drawReorderDisabled = !strcasecmp(property, "true");
177 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
178 } else {
179 INIT_LOGD(" Draw reorder enabled");
180 }
181
Romain Guy5bb3c732012-11-29 17:52:58 -0800182 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800183 (prevDebugOverdraw != debugOverdraw) ||
184 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700185}
186
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800187void Caches::terminate() {
188 if (!mInitialized) return;
189
190 glDeleteBuffers(1, &meshBuffer);
191 mCurrentBuffer = 0;
192
193 glDeleteBuffers(1, &mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700194 delete[] mRegionMesh;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800195 mRegionMesh = NULL;
196
197 fboCache.clear();
198
199 programCache.clear();
200 currentProgram = NULL;
201
202 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700203}
204
205///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800206// Debug
207///////////////////////////////////////////////////////////////////////////////
208
209void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700210 String8 stringLog;
211 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000212 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700213}
214
215void Caches::dumpMemoryUsage(String8 &log) {
216 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
217 log.appendFormat(" TextureCache %8d / %8d\n",
218 textureCache.getSize(), textureCache.getMaxSize());
219 log.appendFormat(" LayerCache %8d / %8d\n",
220 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800221 log.appendFormat(" RenderBufferCache %8d / %8d\n",
222 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700223 log.appendFormat(" GradientCache %8d / %8d\n",
224 gradientCache.getSize(), gradientCache.getMaxSize());
225 log.appendFormat(" PathCache %8d / %8d\n",
226 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700227 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800228 dropShadowCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700229 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
230 const uint32_t size = fontRenderer->getFontRendererSize(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700231 log.appendFormat(" FontRenderer %d %8d / %8d\n", i, size, size);
Romain Guyc15008e2010-11-10 11:59:15 -0800232 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700233 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 log.appendFormat(" FboCache %8d / %8d\n",
235 fboCache.getSize(), fboCache.getMaxSize());
236 log.appendFormat(" PatchCache %8d / %8d\n",
237 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800238
239 uint32_t total = 0;
240 total += textureCache.getSize();
241 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800242 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800243 total += gradientCache.getSize();
244 total += pathCache.getSize();
245 total += dropShadowCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700246 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
247 total += fontRenderer->getFontRendererSize(i);
Romain Guyc15008e2010-11-10 11:59:15 -0800248 }
249
Chet Haase9c1e23b2011-03-24 10:51:31 -0700250 log.appendFormat("Total memory usage:\n");
251 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800252}
253
254///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800255// Memory management
256///////////////////////////////////////////////////////////////////////////////
257
258void Caches::clearGarbage() {
259 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800260 pathCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800261
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700262 Vector<DisplayList*> displayLists;
263 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800264
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700265 { // scope for the lock
266 Mutex::Autolock _l(mGarbageLock);
267 displayLists = mDisplayListGarbage;
268 layers = mLayerGarbage;
269 mDisplayListGarbage.clear();
270 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800271 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800272
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700273 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800274 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700275 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800276 delete displayList;
277 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700278
279 count = layers.size();
280 for (size_t i = 0; i < count; i++) {
281 Layer* layer = layers.itemAt(i);
282 delete layer;
283 }
284 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800285}
286
Romain Guyada830f2011-01-13 12:13:20 -0800287void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800288 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800289 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800290}
291
Romain Guybb0acdf2012-03-05 13:44:35 -0800292void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
293 Mutex::Autolock _l(mGarbageLock);
294 mDisplayListGarbage.push(displayList);
295}
296
Romain Guybdf76092011-07-18 15:00:43 -0700297void Caches::flush(FlushMode mode) {
298 FLUSH_LOGD("Flushing caches (mode %d)", mode);
299
Romain Guybdf76092011-07-18 15:00:43 -0700300 switch (mode) {
301 case kFlushMode_Full:
302 textureCache.clear();
303 patchCache.clear();
304 dropShadowCache.clear();
305 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700306 fontRenderer->clear();
Romain Guy211efea2012-07-31 21:16:07 -0700307 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700308 // fall through
309 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700310 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700311 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700312 pathCache.clear();
Romain Guyc5cbee72013-03-20 19:15:02 -0700313 tasks.stop();
Romain Guy6d7475d2011-07-27 16:28:21 -0700314 // fall through
315 case kFlushMode_Layers:
316 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800317 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700318 break;
319 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700320
321 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700322}
323
Romain Guyfe48f652010-11-11 15:36:56 -0800324///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700325// VBO
326///////////////////////////////////////////////////////////////////////////////
327
Romain Guyf3a910b42011-12-12 20:35:21 -0800328bool Caches::bindMeshBuffer() {
329 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700330}
331
Romain Guyf3a910b42011-12-12 20:35:21 -0800332bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700333 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700334 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700335 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800336 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700337 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800338 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700339}
340
Romain Guyf3a910b42011-12-12 20:35:21 -0800341bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700342 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700343 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700344 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800345 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700346 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800347 return false;
348}
349
Romain Guy15bc6432011-12-13 13:11:32 -0800350bool Caches::bindIndicesBuffer(const GLuint buffer) {
351 if (mCurrentIndicesBuffer != buffer) {
352 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
353 mCurrentIndicesBuffer = buffer;
354 return true;
355 }
356 return false;
357}
358
359bool Caches::unbindIndicesBuffer() {
360 if (mCurrentIndicesBuffer) {
361 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
362 mCurrentIndicesBuffer = 0;
363 return true;
364 }
365 return false;
366}
367
Romain Guy85ef80d2012-09-13 20:26:50 -0700368///////////////////////////////////////////////////////////////////////////////
369// Meshes and textures
370///////////////////////////////////////////////////////////////////////////////
371
Chris Craikcb4d6002012-09-25 12:00:29 -0700372void Caches::bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
373 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
374 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800375 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
376 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700377 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800378 }
379}
380
Romain Guyff316ec2013-02-13 18:39:43 -0800381void Caches::bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride) {
382 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700383 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800384 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800385 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800386 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800387 }
388}
389
390void Caches::resetVertexPointers() {
391 mCurrentPositionPointer = this;
392 mCurrentTexCoordsPointer = this;
393}
394
395void Caches::resetTexCoordsVertexPointer() {
396 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700397}
398
Romain Guy15bc6432011-12-13 13:11:32 -0800399void Caches::enableTexCoordsVertexArray() {
400 if (!mTexCoordsArrayEnabled) {
401 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800402 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800403 mTexCoordsArrayEnabled = true;
404 }
405}
406
Romain Guyff316ec2013-02-13 18:39:43 -0800407void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800408 if (mTexCoordsArrayEnabled) {
409 glDisableVertexAttribArray(Program::kBindingTexCoords);
410 mTexCoordsArrayEnabled = false;
411 }
412}
413
Romain Guya1d3c912011-12-13 14:55:06 -0800414void Caches::activeTexture(GLuint textureUnit) {
415 if (mTextureUnit != textureUnit) {
416 glActiveTexture(gTextureUnits[textureUnit]);
417 mTextureUnit = textureUnit;
418 }
419}
420
Romain Guy85ef80d2012-09-13 20:26:50 -0700421///////////////////////////////////////////////////////////////////////////////
422// Scissor
423///////////////////////////////////////////////////////////////////////////////
424
Romain Guy8a4ac612012-07-17 17:32:48 -0700425bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700426 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
427 width != mScissorWidth || height != mScissorHeight)) {
428
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700429 if (x < 0) {
430 width += x;
431 x = 0;
432 }
433 if (y < 0) {
434 height += y;
435 y = 0;
436 }
437 if (width < 0) {
438 width = 0;
439 }
440 if (height < 0) {
441 height = 0;
442 }
Romain Guy8f85e802011-12-14 19:23:32 -0800443 glScissor(x, y, width, height);
444
445 mScissorX = x;
446 mScissorY = y;
447 mScissorWidth = width;
448 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700449
450 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800451 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700452 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800453}
454
Romain Guy8a4ac612012-07-17 17:32:48 -0700455bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700456 if (!scissorEnabled) {
457 glEnable(GL_SCISSOR_TEST);
458 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700459 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700460 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700461 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700462 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700463}
464
Romain Guy8a4ac612012-07-17 17:32:48 -0700465bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700466 if (scissorEnabled) {
467 glDisable(GL_SCISSOR_TEST);
468 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700469 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700470 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700471 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700472}
473
474void Caches::setScissorEnabled(bool enabled) {
475 if (scissorEnabled != enabled) {
476 if (enabled) glEnable(GL_SCISSOR_TEST);
477 else glDisable(GL_SCISSOR_TEST);
478 scissorEnabled = enabled;
479 }
480}
481
Romain Guy82bc7a72012-01-03 14:13:39 -0800482void Caches::resetScissor() {
483 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
484}
485
Romain Guy85ef80d2012-09-13 20:26:50 -0700486///////////////////////////////////////////////////////////////////////////////
487// Tiling
488///////////////////////////////////////////////////////////////////////////////
489
Romain Guyf735c8e2013-01-31 17:45:55 -0800490void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800491 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800492 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700493 }
494}
495
496void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800497 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700498 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700499 }
500}
501
Romain Guy54c1a642012-09-27 17:55:46 -0700502bool Caches::hasRegisteredFunctors() {
503 return mFunctorsCount > 0;
504}
505
506void Caches::registerFunctors(uint32_t functorCount) {
507 mFunctorsCount += functorCount;
508}
509
510void Caches::unregisterFunctors(uint32_t functorCount) {
511 if (functorCount > mFunctorsCount) {
512 mFunctorsCount = 0;
513 } else {
514 mFunctorsCount -= functorCount;
515 }
516}
517
Romain Guy85ef80d2012-09-13 20:26:50 -0700518///////////////////////////////////////////////////////////////////////////////
519// Regions
520///////////////////////////////////////////////////////////////////////////////
521
Romain Guy5b3b3522010-10-27 18:57:51 -0700522TextureVertex* Caches::getRegionMesh() {
523 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
524 if (!mRegionMesh) {
525 mRegionMesh = new TextureVertex[REGION_MESH_QUAD_COUNT * 4];
526
527 uint16_t* regionIndices = new uint16_t[REGION_MESH_QUAD_COUNT * 6];
528 for (int i = 0; i < REGION_MESH_QUAD_COUNT; i++) {
529 uint16_t quad = i * 4;
530 int index = i * 6;
531 regionIndices[index ] = quad; // top-left
532 regionIndices[index + 1] = quad + 1; // top-right
533 regionIndices[index + 2] = quad + 2; // bottom-left
534 regionIndices[index + 3] = quad + 2; // bottom-left
535 regionIndices[index + 4] = quad + 1; // top-right
536 regionIndices[index + 5] = quad + 3; // bottom-right
537 }
538
539 glGenBuffers(1, &mRegionMeshIndices);
Romain Guy15bc6432011-12-13 13:11:32 -0800540 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700541 glBufferData(GL_ELEMENT_ARRAY_BUFFER, REGION_MESH_QUAD_COUNT * 6 * sizeof(uint16_t),
542 regionIndices, GL_STATIC_DRAW);
543
544 delete[] regionIndices;
545 } else {
Romain Guy15bc6432011-12-13 13:11:32 -0800546 bindIndicesBuffer(mRegionMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700547 }
548
549 return mRegionMesh;
550}
551
Chet Haasedd78cca2010-10-22 18:59:26 -0700552}; // namespace uirenderer
553}; // namespace android