blob: 8e3c44457fdfa1e8683b63222ff72486ee899cf3 [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"
ztenghui63d41ab2014-02-14 13:13:41 -080026#include "ShadowTessellator.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070027
28namespace android {
29
30#ifdef USE_OPENGL_RENDERER
31using namespace uirenderer;
32ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
33#endif
34
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070038// Macros
39///////////////////////////////////////////////////////////////////////////////
40
41#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000042 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070043#else
44 #define FLUSH_LOGD(...)
45#endif
46
47///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070048// Constructors/destructor
49///////////////////////////////////////////////////////////////////////////////
50
Romain Guy8aa195d2013-06-04 18:00:09 -070051Caches::Caches(): Singleton<Caches>(),
52 mExtensions(Extensions::getInstance()), mInitialized(false) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080053 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070054 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070055 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070056 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070057 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080058 initExtensions();
Chris Craikba9b6132013-12-15 17:10:19 -080059 initTempProperties();
Romain Guye190aa62010-11-10 19:01:29 -080060
61 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000062 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070063}
64
Romain Guy3b748a42013-04-17 18:54:38 -070065bool Caches::init() {
66 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080067
68 glGenBuffers(1, &meshBuffer);
69 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
70 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
71
72 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080073 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080074 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070075 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080076 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070077 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080078
Romain Guy15bc6432011-12-13 13:11:32 -080079 mTexCoordsArrayEnabled = false;
80
Romain Guyb1d0a4e2012-07-13 18:25:35 -070081 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070082 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080083 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
84
Romain Guya1d3c912011-12-13 14:55:06 -080085 glActiveTexture(gTextureUnits[0]);
86 mTextureUnit = 0;
87
Romain Guy8ff6b9e2011-11-09 20:10:18 -080088 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070089 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080090 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080091 blend = false;
92 lastSrcMode = GL_ZERO;
93 lastDstMode = GL_ZERO;
94 currentProgram = NULL;
95
Romain Guy54c1a642012-09-27 17:55:46 -070096 mFunctorsCount = 0;
97
Romain Guyc2a97212013-02-06 15:29:46 -080098 debugLayersUpdates = false;
99 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800100 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800101
Romain Guy3b748a42013-04-17 18:54:38 -0700102 patchCache.init(*this);
103
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800104 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700105
Romain Guy8aa195d2013-06-04 18:00:09 -0700106 resetBoundTextures();
107
Romain Guy3b748a42013-04-17 18:54:38 -0700108 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800109}
110
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700111void Caches::initFont() {
112 fontRenderer = GammaFontRenderer::createRenderer();
113}
114
Romain Guydfa10462012-05-12 16:18:58 -0700115void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800116 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700117 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800118
Chris Craikff785832013-03-08 13:12:16 -0800119 startMark = glPushGroupMarkerEXT;
120 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700121 } else {
122 eventMark = eventMarkNull;
123 startMark = startMarkNull;
124 endMark = endMarkNull;
125 }
126
Romain Guy0f667532013-03-01 14:31:04 -0800127 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700128 setLabel = glLabelObjectEXT;
129 getLabel = glGetObjectLabelEXT;
130 } else {
131 setLabel = setLabelNull;
132 getLabel = getLabelNull;
133 }
134}
135
136void Caches::initConstraints() {
137 GLint maxTextureUnits;
138 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
139 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
140 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
141 }
142
143 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
144}
145
Romain Guyf9f00162013-05-09 11:50:12 -0700146void Caches::initStaticProperties() {
147 gpuPixelBuffersEnabled = false;
148
149 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700150 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700151 char property[PROPERTY_VALUE_MAX];
152 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
153 gpuPixelBuffersEnabled = !strcmp(property, "true");
154 }
155 }
156}
157
Romain Guy5bb3c732012-11-29 17:52:58 -0800158bool Caches::initProperties() {
159 bool prevDebugLayersUpdates = debugLayersUpdates;
160 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800161 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800162
Romain Guy4ff0cf42012-08-06 14:51:10 -0700163 char property[PROPERTY_VALUE_MAX];
164 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
165 INIT_LOGD(" Layers updates debug enabled: %s", property);
166 debugLayersUpdates = !strcmp(property, "true");
167 } else {
168 debugLayersUpdates = false;
169 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700170
Romain Guy627c6fd2013-08-21 11:53:18 -0700171 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700172 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
173 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700174 if (!strcmp(property, "show")) {
175 debugOverdraw = true;
176 mOverdrawDebugColorSet = kColorSet_Default;
177 } else if (!strcmp(property, "show_deuteranomaly")) {
178 debugOverdraw = true;
179 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
180 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700181 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800182
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800183 // See Properties.h for valid values
184 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
185 INIT_LOGD(" Stencil clip debug enabled: %s", property);
186 if (!strcmp(property, "hide")) {
187 debugStencilClip = kStencilHide;
188 } else if (!strcmp(property, "highlight")) {
189 debugStencilClip = kStencilShowHighlight;
190 } else if (!strcmp(property, "region")) {
191 debugStencilClip = kStencilShowRegion;
192 }
193 } else {
194 debugStencilClip = kStencilHide;
195 }
196
Romain Guy0f667532013-03-01 14:31:04 -0800197 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
198 drawDeferDisabled = !strcasecmp(property, "true");
199 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
200 } else {
201 INIT_LOGD(" Draw defer enabled");
202 }
203
204 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
205 drawReorderDisabled = !strcasecmp(property, "true");
206 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
207 } else {
208 INIT_LOGD(" Draw reorder enabled");
209 }
210
Romain Guy5bb3c732012-11-29 17:52:58 -0800211 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800212 (prevDebugOverdraw != debugOverdraw) ||
213 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700214}
215
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800216void Caches::terminate() {
217 if (!mInitialized) return;
218
219 glDeleteBuffers(1, &meshBuffer);
220 mCurrentBuffer = 0;
221
Romain Guy3b748a42013-04-17 18:54:38 -0700222 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700223 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700224 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800225 mRegionMesh = NULL;
226
ztenghui63d41ab2014-02-14 13:13:41 -0800227 glDeleteBuffers(1, &mShadowStripsIndices);
228 mShadowStripsIndices = 0;
229
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800230 fboCache.clear();
231
232 programCache.clear();
233 currentProgram = NULL;
234
Romain Guy3b748a42013-04-17 18:54:38 -0700235 assetAtlas.terminate();
236
237 patchCache.clear();
238
Romain Guy46bfc482013-08-16 18:38:29 -0700239 clearGarbage();
240
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800241 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700242}
243
244///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800245// Debug
246///////////////////////////////////////////////////////////////////////////////
247
Romain Guy627c6fd2013-08-21 11:53:18 -0700248uint32_t Caches::getOverdrawColor(uint32_t amount) const {
249 static uint32_t sOverdrawColors[2][4] = {
250 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
251 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
252 };
253 if (amount < 1) amount = 1;
254 if (amount > 4) amount = 4;
255 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
256}
257
Romain Guyc15008e2010-11-10 11:59:15 -0800258void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700259 String8 stringLog;
260 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000261 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700262}
263
264void Caches::dumpMemoryUsage(String8 &log) {
265 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
266 log.appendFormat(" TextureCache %8d / %8d\n",
267 textureCache.getSize(), textureCache.getMaxSize());
268 log.appendFormat(" LayerCache %8d / %8d\n",
269 layerCache.getSize(), layerCache.getMaxSize());
Romain Guy8d4aeb72013-02-12 16:08:55 -0800270 log.appendFormat(" RenderBufferCache %8d / %8d\n",
271 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700272 log.appendFormat(" GradientCache %8d / %8d\n",
273 gradientCache.getSize(), gradientCache.getMaxSize());
274 log.appendFormat(" PathCache %8d / %8d\n",
275 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700276 log.appendFormat(" TessellationCache %8d / %8d\n",
277 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700278 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800279 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700280 log.appendFormat(" PatchCache %8d / %8d\n",
281 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700282 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700283 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
284 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
285 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
286 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
287 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
288 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800289 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700290 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700291 log.appendFormat(" FboCache %8d / %8d\n",
292 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800293
294 uint32_t total = 0;
295 total += textureCache.getSize();
296 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800297 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800298 total += gradientCache.getSize();
299 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700300 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800301 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700302 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700303 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700304 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
305 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800306 }
307
Chet Haase9c1e23b2011-03-24 10:51:31 -0700308 log.appendFormat("Total memory usage:\n");
309 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800310}
311
312///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800313// Memory management
314///////////////////////////////////////////////////////////////////////////////
315
316void Caches::clearGarbage() {
317 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800318 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700319 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800320
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700321 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800322
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700323 { // scope for the lock
324 Mutex::Autolock _l(mGarbageLock);
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700325 layers = mLayerGarbage;
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700326 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800327 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800328
John Reck087bc0c2014-04-04 16:20:08 -0700329 size_t count = layers.size();
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700330 for (size_t i = 0; i < count; i++) {
331 Layer* layer = layers.itemAt(i);
332 delete layer;
333 }
334 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800335}
336
Romain Guyada830f2011-01-13 12:13:20 -0800337void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800338 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800339 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800340}
341
Romain Guybdf76092011-07-18 15:00:43 -0700342void Caches::flush(FlushMode mode) {
343 FLUSH_LOGD("Flushing caches (mode %d)", mode);
344
Romain Guyb0a41ed2013-08-16 14:44:38 -0700345 // We must stop tasks before clearing caches
346 if (mode > kFlushMode_Layers) {
347 tasks.stop();
348 }
349
Romain Guybdf76092011-07-18 15:00:43 -0700350 switch (mode) {
351 case kFlushMode_Full:
352 textureCache.clear();
353 patchCache.clear();
354 dropShadowCache.clear();
355 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700356 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700357 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700358 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700359 // fall through
360 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700361 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700362 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700363 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700364 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700365 // fall through
366 case kFlushMode_Layers:
367 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800368 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700369 break;
370 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700371
372 clearGarbage();
Romain Guybdf76092011-07-18 15:00:43 -0700373}
374
Romain Guyfe48f652010-11-11 15:36:56 -0800375///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700376// VBO
377///////////////////////////////////////////////////////////////////////////////
378
Romain Guyf3a910b42011-12-12 20:35:21 -0800379bool Caches::bindMeshBuffer() {
380 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700381}
382
Romain Guyf3a910b42011-12-12 20:35:21 -0800383bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700384 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700385 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700386 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800387 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700388 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800389 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700390}
391
Romain Guyf3a910b42011-12-12 20:35:21 -0800392bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700393 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700394 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700395 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800396 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700397 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800398 return false;
399}
400
ztenghui63d41ab2014-02-14 13:13:41 -0800401bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800402 if (mCurrentIndicesBuffer != buffer) {
403 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
404 mCurrentIndicesBuffer = buffer;
405 return true;
406 }
407 return false;
408}
409
ztenghui63d41ab2014-02-14 13:13:41 -0800410bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700411 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700412 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
413 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700414 uint16_t quad = i * 4;
415 int index = i * 6;
416 regionIndices[index ] = quad; // top-left
417 regionIndices[index + 1] = quad + 1; // top-right
418 regionIndices[index + 2] = quad + 2; // bottom-left
419 regionIndices[index + 3] = quad + 2; // bottom-left
420 regionIndices[index + 4] = quad + 1; // top-right
421 regionIndices[index + 5] = quad + 3; // bottom-right
422 }
423
424 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800425 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700426 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700427 regionIndices, GL_STATIC_DRAW);
428
429 delete[] regionIndices;
430 return force;
431 }
432
ztenghui63d41ab2014-02-14 13:13:41 -0800433 return bindIndicesBufferInternal(mMeshIndices);
434}
435
436bool Caches::bindShadowIndicesBuffer() {
437 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700438 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800439 ShadowTessellator::generateShadowIndices(shadowIndices);
440 glGenBuffers(1, &mShadowStripsIndices);
441 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700442 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800443 shadowIndices, GL_STATIC_DRAW);
444
445 delete[] shadowIndices;
446 return force;
447 }
448
449 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700450}
451
Romain Guy15bc6432011-12-13 13:11:32 -0800452bool Caches::unbindIndicesBuffer() {
453 if (mCurrentIndicesBuffer) {
454 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
455 mCurrentIndicesBuffer = 0;
456 return true;
457 }
458 return false;
459}
460
Romain Guy85ef80d2012-09-13 20:26:50 -0700461///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700462// PBO
463///////////////////////////////////////////////////////////////////////////////
464
465bool Caches::bindPixelBuffer(const GLuint buffer) {
466 if (mCurrentPixelBuffer != buffer) {
467 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
468 mCurrentPixelBuffer = buffer;
469 return true;
470 }
471 return false;
472}
473
474bool Caches::unbindPixelBuffer() {
475 if (mCurrentPixelBuffer) {
476 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
477 mCurrentPixelBuffer = 0;
478 return true;
479 }
480 return false;
481}
482
483///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700484// Meshes and textures
485///////////////////////////////////////////////////////////////////////////////
486
ztenghui55bfb4e2013-12-03 10:38:55 -0800487void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700488 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
489 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800490 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
491 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700492 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800493 }
494}
495
ztenghui55bfb4e2013-12-03 10:38:55 -0800496void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800497 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700498 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800499 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800500 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800501 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800502 }
503}
504
505void Caches::resetVertexPointers() {
506 mCurrentPositionPointer = this;
507 mCurrentTexCoordsPointer = this;
508}
509
510void Caches::resetTexCoordsVertexPointer() {
511 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700512}
513
Romain Guy15bc6432011-12-13 13:11:32 -0800514void Caches::enableTexCoordsVertexArray() {
515 if (!mTexCoordsArrayEnabled) {
516 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800517 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800518 mTexCoordsArrayEnabled = true;
519 }
520}
521
Romain Guyff316ec2013-02-13 18:39:43 -0800522void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800523 if (mTexCoordsArrayEnabled) {
524 glDisableVertexAttribArray(Program::kBindingTexCoords);
525 mTexCoordsArrayEnabled = false;
526 }
527}
528
Romain Guya1d3c912011-12-13 14:55:06 -0800529void Caches::activeTexture(GLuint textureUnit) {
530 if (mTextureUnit != textureUnit) {
531 glActiveTexture(gTextureUnits[textureUnit]);
532 mTextureUnit = textureUnit;
533 }
534}
535
Chris Craik9ab2d182013-07-22 16:16:06 -0700536void Caches::resetActiveTexture() {
537 mTextureUnit = -1;
538}
539
Romain Guy8aa195d2013-06-04 18:00:09 -0700540void Caches::bindTexture(GLuint texture) {
541 if (mBoundTextures[mTextureUnit] != texture) {
542 glBindTexture(GL_TEXTURE_2D, texture);
543 mBoundTextures[mTextureUnit] = texture;
544 }
545}
546
547void Caches::bindTexture(GLenum target, GLuint texture) {
548 if (mBoundTextures[mTextureUnit] != texture) {
549 glBindTexture(target, texture);
550 mBoundTextures[mTextureUnit] = texture;
551 }
552}
553
Romain Guybe1b1272013-06-06 14:02:54 -0700554void Caches::deleteTexture(GLuint texture) {
555 // When glDeleteTextures() is called on a currently bound texture,
556 // OpenGL ES specifies that the texture is then considered unbound
557 // Consider the following series of calls:
558 //
559 // glGenTextures -> creates texture name 2
560 // glBindTexture(2)
561 // glDeleteTextures(2) -> 2 is now unbound
562 // glGenTextures -> can return 2 again
563 //
564 // If we don't call glBindTexture(2) after the second glGenTextures
565 // call, any texture operation will be performed on the default
566 // texture (name=0)
567
jiayuanr4a473c7d2014-06-10 17:41:49 +0800568 unbindTexture(texture);
569
Romain Guybe1b1272013-06-06 14:02:54 -0700570 glDeleteTextures(1, &texture);
571}
572
Romain Guy8aa195d2013-06-04 18:00:09 -0700573void Caches::resetBoundTextures() {
574 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
575}
576
jiayuanr4a473c7d2014-06-10 17:41:49 +0800577void Caches::unbindTexture(GLuint texture) {
578 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
579 if (mBoundTextures[i] == texture) {
580 mBoundTextures[i] = 0;
581 }
582 }
583}
584
Romain Guy85ef80d2012-09-13 20:26:50 -0700585///////////////////////////////////////////////////////////////////////////////
586// Scissor
587///////////////////////////////////////////////////////////////////////////////
588
Romain Guy8a4ac612012-07-17 17:32:48 -0700589bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700590 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
591 width != mScissorWidth || height != mScissorHeight)) {
592
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700593 if (x < 0) {
594 width += x;
595 x = 0;
596 }
597 if (y < 0) {
598 height += y;
599 y = 0;
600 }
601 if (width < 0) {
602 width = 0;
603 }
604 if (height < 0) {
605 height = 0;
606 }
Romain Guy8f85e802011-12-14 19:23:32 -0800607 glScissor(x, y, width, height);
608
609 mScissorX = x;
610 mScissorY = y;
611 mScissorWidth = width;
612 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700613
614 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800615 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700616 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800617}
618
Romain Guy8a4ac612012-07-17 17:32:48 -0700619bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700620 if (!scissorEnabled) {
621 glEnable(GL_SCISSOR_TEST);
622 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700623 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700624 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700625 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700626 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700627}
628
Romain Guy8a4ac612012-07-17 17:32:48 -0700629bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700630 if (scissorEnabled) {
631 glDisable(GL_SCISSOR_TEST);
632 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700633 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700634 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700635 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700636}
637
638void Caches::setScissorEnabled(bool enabled) {
639 if (scissorEnabled != enabled) {
640 if (enabled) glEnable(GL_SCISSOR_TEST);
641 else glDisable(GL_SCISSOR_TEST);
642 scissorEnabled = enabled;
643 }
644}
645
Romain Guy82bc7a72012-01-03 14:13:39 -0800646void Caches::resetScissor() {
647 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
648}
649
Romain Guy85ef80d2012-09-13 20:26:50 -0700650///////////////////////////////////////////////////////////////////////////////
651// Tiling
652///////////////////////////////////////////////////////////////////////////////
653
Romain Guyf735c8e2013-01-31 17:45:55 -0800654void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800655 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800656 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700657 }
658}
659
660void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800661 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700662 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700663 }
664}
665
Romain Guy54c1a642012-09-27 17:55:46 -0700666bool Caches::hasRegisteredFunctors() {
667 return mFunctorsCount > 0;
668}
669
670void Caches::registerFunctors(uint32_t functorCount) {
671 mFunctorsCount += functorCount;
672}
673
674void Caches::unregisterFunctors(uint32_t functorCount) {
675 if (functorCount > mFunctorsCount) {
676 mFunctorsCount = 0;
677 } else {
678 mFunctorsCount -= functorCount;
679 }
680}
681
Romain Guy85ef80d2012-09-13 20:26:50 -0700682///////////////////////////////////////////////////////////////////////////////
683// Regions
684///////////////////////////////////////////////////////////////////////////////
685
Romain Guy5b3b3522010-10-27 18:57:51 -0700686TextureVertex* Caches::getRegionMesh() {
687 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
688 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700689 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 }
691
692 return mRegionMesh;
693}
694
Chris Craikba9b6132013-12-15 17:10:19 -0800695///////////////////////////////////////////////////////////////////////////////
696// Temporary Properties
697///////////////////////////////////////////////////////////////////////////////
698
699void Caches::initTempProperties() {
Chris Craik797b95b2014-05-20 18:10:25 -0700700 propertyAmbientShadowStrength = 12;
701 propertySpotShadowStrength = 48;
ztenghuicc3c2562014-01-17 10:34:10 -0800702
Chris Craikf5be3ca2014-04-30 18:20:03 -0700703 propertyLightDiameter = -1.0f;
704 propertyLightPosY = -1.0f;
705 propertyLightPosZ = -1.0f;
706 propertyAmbientRatio = -1.0f;
Chris Craikba9b6132013-12-15 17:10:19 -0800707}
708
709void Caches::setTempProperty(const char* name, const char* value) {
710 ALOGD("setting property %s to %s", name, value);
Chris Craike361ad72014-03-11 10:48:43 -0700711 if (!strcmp(name, "ambientShadowStrength")) {
ztenghuief94c6f2014-02-13 17:09:45 -0800712 propertyAmbientShadowStrength = atoi(value);
713 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
714 return;
715 } else if (!strcmp(name, "spotShadowStrength")) {
716 propertySpotShadowStrength = atoi(value);
717 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
Chris Craikba9b6132013-12-15 17:10:19 -0800718 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700719 } else if (!strcmp(name, "ambientRatio")) {
720 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
721 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800722 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700723 } else if (!strcmp(name, "lightDiameter")) {
724 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
725 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800726 return;
Chris Craik59744b72014-07-01 17:56:52 -0700727 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700728 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
729 ALOGD("lightPos Y = %.2f", propertyLightPosY);
730 return;
Chris Craik59744b72014-07-01 17:56:52 -0700731 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700732 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
733 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800734 return;
Chris Craik59744b72014-07-01 17:56:52 -0700735 } else if (!strcmp(name, "extraRasterBucket")) {
736 float bucket = atof(value);
737 propertyExtraRasterBuckets.push_back(bucket);
738 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800739 }
740 ALOGD(" failed");
741}
742
Chet Haasedd78cca2010-10-22 18:59:26 -0700743}; // namespace uirenderer
744}; // namespace android