blob: 2e7990edb78e357f16dad1a99fa402e12b61afa2 [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
Romain Guyc15008e2010-11-10 11:59:15 -080019#include <utils/Log.h>
Chet Haase9c1e23b2011-03-24 10:51:31 -070020#include <utils/String8.h>
Romain Guyc15008e2010-11-10 11:59:15 -080021
Chet Haasedd78cca2010-10-22 18:59:26 -070022#include "Caches.h"
Romain Guybb0acdf2012-03-05 13:44:35 -080023#include "DisplayListRenderer.h"
Romain Guye190aa62010-11-10 19:01:29 -080024#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080025#include "LayerRenderer.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070026
27namespace android {
28
29#ifdef USE_OPENGL_RENDERER
30using namespace uirenderer;
31ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
32#endif
33
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070037// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070042#else
43 #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070047// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy8aa195d2013-06-04 18:00:09 -070050Caches::Caches(): Singleton<Caches>(),
51 mExtensions(Extensions::getInstance()), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080052 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070053 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070054 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070055 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070056 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080057 initExtensions();
Chris Craikba9b6132013-12-15 17:10:19 -080058 initTempProperties();
Romain Guye190aa62010-11-10 19:01:29 -080059
60 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000061 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070062}
63
Romain Guy3b748a42013-04-17 18:54:38 -070064bool Caches::init() {
65 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080066
67 glGenBuffers(1, &meshBuffer);
68 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
69 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
70
71 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080072 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080073 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070074 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080075 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070076 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077
Romain Guy15bc6432011-12-13 13:11:32 -080078 mTexCoordsArrayEnabled = false;
79
Romain Guyb1d0a4e2012-07-13 18:25:35 -070080 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070081 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080082 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
83
Romain Guya1d3c912011-12-13 14:55:06 -080084 glActiveTexture(gTextureUnits[0]);
85 mTextureUnit = 0;
86
Romain Guy8ff6b9e2011-11-09 20:10:18 -080087 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070088 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080089
90 blend = false;
91 lastSrcMode = GL_ZERO;
92 lastDstMode = GL_ZERO;
93 currentProgram = NULL;
94
Romain Guy54c1a642012-09-27 17:55:46 -070095 mFunctorsCount = 0;
96
Romain Guyc2a97212013-02-06 15:29:46 -080097 debugLayersUpdates = false;
98 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080099 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800100
Romain Guy3b748a42013-04-17 18:54:38 -0700101 patchCache.init(*this);
102
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800103 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700104
Romain Guy8aa195d2013-06-04 18:00:09 -0700105 resetBoundTextures();
106
Romain Guy3b748a42013-04-17 18:54:38 -0700107 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108}
109
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700110void Caches::initFont() {
111 fontRenderer = GammaFontRenderer::createRenderer();
112}
113
Romain Guydfa10462012-05-12 16:18:58 -0700114void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800115 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700116 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800117
Chris Craikff785832013-03-08 13:12:16 -0800118 startMark = glPushGroupMarkerEXT;
119 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700120 } else {
121 eventMark = eventMarkNull;
122 startMark = startMarkNull;
123 endMark = endMarkNull;
124 }
125
Romain Guy0f667532013-03-01 14:31:04 -0800126 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700127 setLabel = glLabelObjectEXT;
128 getLabel = glGetObjectLabelEXT;
129 } else {
130 setLabel = setLabelNull;
131 getLabel = getLabelNull;
132 }
133}
134
135void Caches::initConstraints() {
136 GLint maxTextureUnits;
137 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
138 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
139 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
140 }
141
142 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
143}
144
Romain Guyf9f00162013-05-09 11:50:12 -0700145void Caches::initStaticProperties() {
146 gpuPixelBuffersEnabled = false;
147
148 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700149 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700150 char property[PROPERTY_VALUE_MAX];
151 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
152 gpuPixelBuffersEnabled = !strcmp(property, "true");
153 }
154 }
155}
156
Romain Guy5bb3c732012-11-29 17:52:58 -0800157bool Caches::initProperties() {
158 bool prevDebugLayersUpdates = debugLayersUpdates;
159 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800160 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800161
Romain Guy4ff0cf42012-08-06 14:51:10 -0700162 char property[PROPERTY_VALUE_MAX];
163 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
164 INIT_LOGD(" Layers updates debug enabled: %s", property);
165 debugLayersUpdates = !strcmp(property, "true");
166 } else {
167 debugLayersUpdates = false;
168 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700169
Romain Guy627c6fd2013-08-21 11:53:18 -0700170 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700171 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
172 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700173 if (!strcmp(property, "show")) {
174 debugOverdraw = true;
175 mOverdrawDebugColorSet = kColorSet_Default;
176 } else if (!strcmp(property, "show_deuteranomaly")) {
177 debugOverdraw = true;
178 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
179 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700180 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800181
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800182 // See Properties.h for valid values
183 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
184 INIT_LOGD(" Stencil clip debug enabled: %s", property);
185 if (!strcmp(property, "hide")) {
186 debugStencilClip = kStencilHide;
187 } else if (!strcmp(property, "highlight")) {
188 debugStencilClip = kStencilShowHighlight;
189 } else if (!strcmp(property, "region")) {
190 debugStencilClip = kStencilShowRegion;
191 }
192 } else {
193 debugStencilClip = kStencilHide;
194 }
195
Romain Guy0f667532013-03-01 14:31:04 -0800196 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
197 drawDeferDisabled = !strcasecmp(property, "true");
198 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
199 } else {
200 INIT_LOGD(" Draw defer enabled");
201 }
202
203 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
204 drawReorderDisabled = !strcasecmp(property, "true");
205 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
206 } else {
207 INIT_LOGD(" Draw reorder enabled");
208 }
209
Romain Guy5bb3c732012-11-29 17:52:58 -0800210 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800211 (prevDebugOverdraw != debugOverdraw) ||
212 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700213}
214
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800215void Caches::terminate() {
216 if (!mInitialized) return;
217
218 glDeleteBuffers(1, &meshBuffer);
219 mCurrentBuffer = 0;
220
Romain Guy3b748a42013-04-17 18:54:38 -0700221 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700222 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700223 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800224 mRegionMesh = NULL;
225
226 fboCache.clear();
227
228 programCache.clear();
229 currentProgram = NULL;
230
Romain Guy3b748a42013-04-17 18:54:38 -0700231 assetAtlas.terminate();
232
233 patchCache.clear();
234
Romain Guy46bfc482013-08-16 18:38:29 -0700235 clearGarbage();
236
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800237 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700238}
239
240///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800241// Debug
242///////////////////////////////////////////////////////////////////////////////
243
Romain Guy627c6fd2013-08-21 11:53:18 -0700244uint32_t Caches::getOverdrawColor(uint32_t amount) const {
245 static uint32_t sOverdrawColors[2][4] = {
246 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
247 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
248 };
249 if (amount < 1) amount = 1;
250 if (amount > 4) amount = 4;
251 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
252}
253
Romain Guyc15008e2010-11-10 11:59:15 -0800254void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700255 String8 stringLog;
256 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000257 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700258}
259
260void Caches::dumpMemoryUsage(String8 &log) {
261 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
262 log.appendFormat(" TextureCache %8d / %8d\n",
263 textureCache.getSize(), textureCache.getMaxSize());
264 log.appendFormat(" LayerCache %8d / %8d\n",
265 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800266 log.appendFormat(" RenderBufferCache %8d / %8d\n",
267 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700268 log.appendFormat(" GradientCache %8d / %8d\n",
269 gradientCache.getSize(), gradientCache.getMaxSize());
270 log.appendFormat(" PathCache %8d / %8d\n",
271 pathCache.getSize(), pathCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700272 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800273 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700274 log.appendFormat(" PatchCache %8d / %8d\n",
275 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700276 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700277 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
278 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
279 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
280 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
281 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
282 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800283 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700284 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700285 log.appendFormat(" FboCache %8d / %8d\n",
286 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800287
288 uint32_t total = 0;
289 total += textureCache.getSize();
290 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800291 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800292 total += gradientCache.getSize();
293 total += pathCache.getSize();
294 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700295 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700296 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700297 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
298 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800299 }
300
Chet Haase9c1e23b2011-03-24 10:51:31 -0700301 log.appendFormat("Total memory usage:\n");
302 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800303}
304
305///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800306// Memory management
307///////////////////////////////////////////////////////////////////////////////
308
309void Caches::clearGarbage() {
310 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800311 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700312 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800313
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700314 Vector<DisplayList*> displayLists;
315 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800316
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700317 { // scope for the lock
318 Mutex::Autolock _l(mGarbageLock);
319 displayLists = mDisplayListGarbage;
320 layers = mLayerGarbage;
321 mDisplayListGarbage.clear();
322 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800323 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800324
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700325 size_t count = displayLists.size();
Romain Guybb0acdf2012-03-05 13:44:35 -0800326 for (size_t i = 0; i < count; i++) {
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700327 DisplayList* displayList = displayLists.itemAt(i);
Romain Guybb0acdf2012-03-05 13:44:35 -0800328 delete displayList;
329 }
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700330
331 count = layers.size();
332 for (size_t i = 0; i < count; i++) {
333 Layer* layer = layers.itemAt(i);
334 delete layer;
335 }
336 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800337}
338
Romain Guyada830f2011-01-13 12:13:20 -0800339void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800340 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800341 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800342}
343
Romain Guybb0acdf2012-03-05 13:44:35 -0800344void Caches::deleteDisplayListDeferred(DisplayList* displayList) {
345 Mutex::Autolock _l(mGarbageLock);
346 mDisplayListGarbage.push(displayList);
347}
348
Romain Guybdf76092011-07-18 15:00:43 -0700349void Caches::flush(FlushMode mode) {
350 FLUSH_LOGD("Flushing caches (mode %d)", mode);
351
Romain Guyb0a41ed2013-08-16 14:44:38 -0700352 // We must stop tasks before clearing caches
353 if (mode > kFlushMode_Layers) {
354 tasks.stop();
355 }
356
Romain Guybdf76092011-07-18 15:00:43 -0700357 switch (mode) {
358 case kFlushMode_Full:
359 textureCache.clear();
360 patchCache.clear();
361 dropShadowCache.clear();
362 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700363 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700364 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700365 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700366 // fall through
367 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700368 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700369 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700370 pathCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700371 // fall through
372 case kFlushMode_Layers:
373 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800374 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700375 break;
376 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700377
378 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700379}
380
Romain Guyfe48f652010-11-11 15:36:56 -0800381///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700382// VBO
383///////////////////////////////////////////////////////////////////////////////
384
Romain Guyf3a910b42011-12-12 20:35:21 -0800385bool Caches::bindMeshBuffer() {
386 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700387}
388
Romain Guyf3a910b42011-12-12 20:35:21 -0800389bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700390 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700391 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700392 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800393 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700394 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800395 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700396}
397
Romain Guyf3a910b42011-12-12 20:35:21 -0800398bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700399 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700400 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700401 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800402 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700403 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800404 return false;
405}
406
Romain Guy15bc6432011-12-13 13:11:32 -0800407bool Caches::bindIndicesBuffer(const GLuint buffer) {
408 if (mCurrentIndicesBuffer != buffer) {
409 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
410 mCurrentIndicesBuffer = buffer;
411 return true;
412 }
413 return false;
414}
415
Romain Guy3b748a42013-04-17 18:54:38 -0700416bool Caches::bindIndicesBuffer() {
417 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700418 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
419 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700420 uint16_t quad = i * 4;
421 int index = i * 6;
422 regionIndices[index ] = quad; // top-left
423 regionIndices[index + 1] = quad + 1; // top-right
424 regionIndices[index + 2] = quad + 2; // bottom-left
425 regionIndices[index + 3] = quad + 2; // bottom-left
426 regionIndices[index + 4] = quad + 1; // top-right
427 regionIndices[index + 5] = quad + 3; // bottom-right
428 }
429
430 glGenBuffers(1, &mMeshIndices);
431 bool force = bindIndicesBuffer(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700432 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700433 regionIndices, GL_STATIC_DRAW);
434
435 delete[] regionIndices;
436 return force;
437 }
438
439 return bindIndicesBuffer(mMeshIndices);
440}
441
Romain Guy15bc6432011-12-13 13:11:32 -0800442bool Caches::unbindIndicesBuffer() {
443 if (mCurrentIndicesBuffer) {
444 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
445 mCurrentIndicesBuffer = 0;
446 return true;
447 }
448 return false;
449}
450
Romain Guy85ef80d2012-09-13 20:26:50 -0700451///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700452// PBO
453///////////////////////////////////////////////////////////////////////////////
454
455bool Caches::bindPixelBuffer(const GLuint buffer) {
456 if (mCurrentPixelBuffer != buffer) {
457 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
458 mCurrentPixelBuffer = buffer;
459 return true;
460 }
461 return false;
462}
463
464bool Caches::unbindPixelBuffer() {
465 if (mCurrentPixelBuffer) {
466 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
467 mCurrentPixelBuffer = 0;
468 return true;
469 }
470 return false;
471}
472
473///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700474// Meshes and textures
475///////////////////////////////////////////////////////////////////////////////
476
ztenghui55bfb4e2013-12-03 10:38:55 -0800477void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700478 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
479 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800480 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
481 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700482 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800483 }
484}
485
ztenghui55bfb4e2013-12-03 10:38:55 -0800486void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800487 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700488 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800489 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800490 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800491 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800492 }
493}
494
495void Caches::resetVertexPointers() {
496 mCurrentPositionPointer = this;
497 mCurrentTexCoordsPointer = this;
498}
499
500void Caches::resetTexCoordsVertexPointer() {
501 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700502}
503
Romain Guy15bc6432011-12-13 13:11:32 -0800504void Caches::enableTexCoordsVertexArray() {
505 if (!mTexCoordsArrayEnabled) {
506 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800507 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800508 mTexCoordsArrayEnabled = true;
509 }
510}
511
Romain Guyff316ec2013-02-13 18:39:43 -0800512void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800513 if (mTexCoordsArrayEnabled) {
514 glDisableVertexAttribArray(Program::kBindingTexCoords);
515 mTexCoordsArrayEnabled = false;
516 }
517}
518
Romain Guya1d3c912011-12-13 14:55:06 -0800519void Caches::activeTexture(GLuint textureUnit) {
520 if (mTextureUnit != textureUnit) {
521 glActiveTexture(gTextureUnits[textureUnit]);
522 mTextureUnit = textureUnit;
523 }
524}
525
Chris Craik9ab2d182013-07-22 16:16:06 -0700526void Caches::resetActiveTexture() {
527 mTextureUnit = -1;
528}
529
Romain Guy8aa195d2013-06-04 18:00:09 -0700530void Caches::bindTexture(GLuint texture) {
531 if (mBoundTextures[mTextureUnit] != texture) {
532 glBindTexture(GL_TEXTURE_2D, texture);
533 mBoundTextures[mTextureUnit] = texture;
534 }
535}
536
537void Caches::bindTexture(GLenum target, GLuint texture) {
538 if (mBoundTextures[mTextureUnit] != texture) {
539 glBindTexture(target, texture);
540 mBoundTextures[mTextureUnit] = texture;
541 }
542}
543
Romain Guybe1b1272013-06-06 14:02:54 -0700544void Caches::deleteTexture(GLuint texture) {
545 // When glDeleteTextures() is called on a currently bound texture,
546 // OpenGL ES specifies that the texture is then considered unbound
547 // Consider the following series of calls:
548 //
549 // glGenTextures -> creates texture name 2
550 // glBindTexture(2)
551 // glDeleteTextures(2) -> 2 is now unbound
552 // glGenTextures -> can return 2 again
553 //
554 // If we don't call glBindTexture(2) after the second glGenTextures
555 // call, any texture operation will be performed on the default
556 // texture (name=0)
557
558 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
559 if (mBoundTextures[i] == texture) {
560 mBoundTextures[i] = 0;
561 }
562 }
563 glDeleteTextures(1, &texture);
564}
565
Romain Guy8aa195d2013-06-04 18:00:09 -0700566void Caches::resetBoundTextures() {
567 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
568}
569
Romain Guy85ef80d2012-09-13 20:26:50 -0700570///////////////////////////////////////////////////////////////////////////////
571// Scissor
572///////////////////////////////////////////////////////////////////////////////
573
Romain Guy8a4ac612012-07-17 17:32:48 -0700574bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700575 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
576 width != mScissorWidth || height != mScissorHeight)) {
577
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700578 if (x < 0) {
579 width += x;
580 x = 0;
581 }
582 if (y < 0) {
583 height += y;
584 y = 0;
585 }
586 if (width < 0) {
587 width = 0;
588 }
589 if (height < 0) {
590 height = 0;
591 }
Romain Guy8f85e802011-12-14 19:23:32 -0800592 glScissor(x, y, width, height);
593
594 mScissorX = x;
595 mScissorY = y;
596 mScissorWidth = width;
597 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700598
599 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800600 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700601 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800602}
603
Romain Guy8a4ac612012-07-17 17:32:48 -0700604bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700605 if (!scissorEnabled) {
606 glEnable(GL_SCISSOR_TEST);
607 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700608 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700609 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700610 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700611 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700612}
613
Romain Guy8a4ac612012-07-17 17:32:48 -0700614bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700615 if (scissorEnabled) {
616 glDisable(GL_SCISSOR_TEST);
617 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700618 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700619 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700620 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700621}
622
623void Caches::setScissorEnabled(bool enabled) {
624 if (scissorEnabled != enabled) {
625 if (enabled) glEnable(GL_SCISSOR_TEST);
626 else glDisable(GL_SCISSOR_TEST);
627 scissorEnabled = enabled;
628 }
629}
630
Romain Guy82bc7a72012-01-03 14:13:39 -0800631void Caches::resetScissor() {
632 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
633}
634
Romain Guy85ef80d2012-09-13 20:26:50 -0700635///////////////////////////////////////////////////////////////////////////////
636// Tiling
637///////////////////////////////////////////////////////////////////////////////
638
Romain Guyf735c8e2013-01-31 17:45:55 -0800639void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800640 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800641 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700642 }
643}
644
645void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800646 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700647 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700648 }
649}
650
Romain Guy54c1a642012-09-27 17:55:46 -0700651bool Caches::hasRegisteredFunctors() {
652 return mFunctorsCount > 0;
653}
654
655void Caches::registerFunctors(uint32_t functorCount) {
656 mFunctorsCount += functorCount;
657}
658
659void Caches::unregisterFunctors(uint32_t functorCount) {
660 if (functorCount > mFunctorsCount) {
661 mFunctorsCount = 0;
662 } else {
663 mFunctorsCount -= functorCount;
664 }
665}
666
Romain Guy85ef80d2012-09-13 20:26:50 -0700667///////////////////////////////////////////////////////////////////////////////
668// Regions
669///////////////////////////////////////////////////////////////////////////////
670
Romain Guy5b3b3522010-10-27 18:57:51 -0700671TextureVertex* Caches::getRegionMesh() {
672 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
673 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700674 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700675 }
676
677 return mRegionMesh;
678}
679
Chris Craikba9b6132013-12-15 17:10:19 -0800680///////////////////////////////////////////////////////////////////////////////
681// Temporary Properties
682///////////////////////////////////////////////////////////////////////////////
683
684void Caches::initTempProperties() {
685 propertyDirtyViewport = false;
686 propertyEnable3d = false;
687 propertyCameraDistance = 1.0f;
688 propertyShadowStrength = 0x3f;
689}
690
691void Caches::setTempProperty(const char* name, const char* value) {
692 ALOGD("setting property %s to %s", name, value);
693 if (!strcmp(name, "enable3d")) {
694 propertyEnable3d = !strcmp(value, "true");
695 propertyDirtyViewport = true;
696 ALOGD("enable3d = %d", propertyEnable3d);
697 return;
698 } else if (!strcmp(name, "cameraDistance")) {
699 propertyCameraDistance = fmin(fmax(atof(value), 0.001), 10);
700 propertyDirtyViewport = true;
701 ALOGD("camera dist multiplier = %.2f", propertyCameraDistance);
702 return;
703 } else if (!strcmp(name, "shadowStrength")) {
704 propertyShadowStrength = atoi(value);
705 ALOGD("shadow strength = 0x%x out of 0xff", propertyShadowStrength);
706 return;
707 }
708 ALOGD(" failed");
709}
710
Chet Haasedd78cca2010-10-22 18:59:26 -0700711}; // namespace uirenderer
712}; // namespace android