blob: 9855f718930dd763ff24dc90113dcf5433c68e17 [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"
John Reck17035b02014-09-03 07:39:53 -070027#include "RenderState.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070028
29namespace android {
30
31#ifdef USE_OPENGL_RENDERER
32using namespace uirenderer;
33ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
34#endif
35
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070039// Macros
40///////////////////////////////////////////////////////////////////////////////
41
42#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000043 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070044#else
45 #define FLUSH_LOGD(...)
46#endif
47
48///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070049// Constructors/destructor
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy8aa195d2013-06-04 18:00:09 -070052Caches::Caches(): Singleton<Caches>(),
John Reck17035b02014-09-03 07:39:53 -070053 mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(NULL) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080054 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070055 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070056 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070057 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070058 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080059 initExtensions();
Chris Craikba9b6132013-12-15 17:10:19 -080060 initTempProperties();
Romain Guye190aa62010-11-10 19:01:29 -080061
62 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000063 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070064}
65
Romain Guy3b748a42013-04-17 18:54:38 -070066bool Caches::init() {
67 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068
69 glGenBuffers(1, &meshBuffer);
70 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
71 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
72
73 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080074 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080075 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070076 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070078 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080079
Romain Guy15bc6432011-12-13 13:11:32 -080080 mTexCoordsArrayEnabled = false;
81
Romain Guyb1d0a4e2012-07-13 18:25:35 -070082 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070083 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080084 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
85
Romain Guya1d3c912011-12-13 14:55:06 -080086 glActiveTexture(gTextureUnits[0]);
87 mTextureUnit = 0;
88
Romain Guy8ff6b9e2011-11-09 20:10:18 -080089 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070090 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080091 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080092 blend = false;
93 lastSrcMode = GL_ZERO;
94 lastDstMode = GL_ZERO;
95 currentProgram = NULL;
96
Romain Guy54c1a642012-09-27 17:55:46 -070097 mFunctorsCount = 0;
98
Romain Guyc2a97212013-02-06 15:29:46 -080099 debugLayersUpdates = false;
100 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800101 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800102
Romain Guy3b748a42013-04-17 18:54:38 -0700103 patchCache.init(*this);
104
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800105 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700106
Romain Guy8aa195d2013-06-04 18:00:09 -0700107 resetBoundTextures();
108
Romain Guy3b748a42013-04-17 18:54:38 -0700109 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800110}
111
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700112void Caches::initFont() {
113 fontRenderer = GammaFontRenderer::createRenderer();
114}
115
Romain Guydfa10462012-05-12 16:18:58 -0700116void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800117 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700118 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -0800119
Chris Craikff785832013-03-08 13:12:16 -0800120 startMark = glPushGroupMarkerEXT;
121 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700122 } else {
123 eventMark = eventMarkNull;
124 startMark = startMarkNull;
125 endMark = endMarkNull;
126 }
127
Romain Guy0f6675332013-03-01 14:31:04 -0800128 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700129 setLabel = glLabelObjectEXT;
130 getLabel = glGetObjectLabelEXT;
131 } else {
132 setLabel = setLabelNull;
133 getLabel = getLabelNull;
134 }
135}
136
137void Caches::initConstraints() {
138 GLint maxTextureUnits;
139 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
140 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
141 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
142 }
143
144 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
145}
146
Romain Guyf9f00162013-05-09 11:50:12 -0700147void Caches::initStaticProperties() {
148 gpuPixelBuffersEnabled = false;
149
150 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700151 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700152 char property[PROPERTY_VALUE_MAX];
153 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
154 gpuPixelBuffersEnabled = !strcmp(property, "true");
155 }
156 }
157}
158
Romain Guy5bb3c732012-11-29 17:52:58 -0800159bool Caches::initProperties() {
160 bool prevDebugLayersUpdates = debugLayersUpdates;
161 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800162 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800163
Romain Guy4ff0cf42012-08-06 14:51:10 -0700164 char property[PROPERTY_VALUE_MAX];
165 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
166 INIT_LOGD(" Layers updates debug enabled: %s", property);
167 debugLayersUpdates = !strcmp(property, "true");
168 } else {
169 debugLayersUpdates = false;
170 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700171
Romain Guy627c6fd2013-08-21 11:53:18 -0700172 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700173 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
174 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700175 if (!strcmp(property, "show")) {
176 debugOverdraw = true;
177 mOverdrawDebugColorSet = kColorSet_Default;
178 } else if (!strcmp(property, "show_deuteranomaly")) {
179 debugOverdraw = true;
180 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
181 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700182 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800183
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800184 // See Properties.h for valid values
185 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
186 INIT_LOGD(" Stencil clip debug enabled: %s", property);
187 if (!strcmp(property, "hide")) {
188 debugStencilClip = kStencilHide;
189 } else if (!strcmp(property, "highlight")) {
190 debugStencilClip = kStencilShowHighlight;
191 } else if (!strcmp(property, "region")) {
192 debugStencilClip = kStencilShowRegion;
193 }
194 } else {
195 debugStencilClip = kStencilHide;
196 }
197
Romain Guy0f6675332013-03-01 14:31:04 -0800198 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
199 drawDeferDisabled = !strcasecmp(property, "true");
200 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
201 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700202 drawDeferDisabled = false;
Romain Guy0f6675332013-03-01 14:31:04 -0800203 INIT_LOGD(" Draw defer enabled");
204 }
205
206 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
207 drawReorderDisabled = !strcasecmp(property, "true");
208 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
209 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700210 drawReorderDisabled = false;
Romain Guy0f6675332013-03-01 14:31:04 -0800211 INIT_LOGD(" Draw reorder enabled");
212 }
213
Romain Guy5bb3c732012-11-29 17:52:58 -0800214 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800215 (prevDebugOverdraw != debugOverdraw) ||
216 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700217}
218
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800219void Caches::terminate() {
220 if (!mInitialized) return;
221
222 glDeleteBuffers(1, &meshBuffer);
223 mCurrentBuffer = 0;
224
Romain Guy3b748a42013-04-17 18:54:38 -0700225 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700226 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700227 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800228 mRegionMesh = NULL;
229
ztenghui63d41ab2014-02-14 13:13:41 -0800230 glDeleteBuffers(1, &mShadowStripsIndices);
231 mShadowStripsIndices = 0;
232
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800233 fboCache.clear();
234
235 programCache.clear();
236 currentProgram = NULL;
237
Romain Guy3b748a42013-04-17 18:54:38 -0700238 assetAtlas.terminate();
239
240 patchCache.clear();
241
Romain Guy46bfc482013-08-16 18:38:29 -0700242 clearGarbage();
243
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800244 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700245}
246
247///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800248// Debug
249///////////////////////////////////////////////////////////////////////////////
250
Romain Guy627c6fd2013-08-21 11:53:18 -0700251uint32_t Caches::getOverdrawColor(uint32_t amount) const {
252 static uint32_t sOverdrawColors[2][4] = {
253 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
254 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
255 };
256 if (amount < 1) amount = 1;
257 if (amount > 4) amount = 4;
258 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
259}
260
Romain Guyc15008e2010-11-10 11:59:15 -0800261void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700262 String8 stringLog;
263 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000264 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700265}
266
267void Caches::dumpMemoryUsage(String8 &log) {
268 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
269 log.appendFormat(" TextureCache %8d / %8d\n",
270 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700271 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
272 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
273 log.appendFormat(" Garbage layers %8zu\n", mLayerGarbage.size());
274 log.appendFormat(" Active layers %8zu\n",
275 mRenderState ? mRenderState->mActiveLayers.size() : 0);
Romain Guy8d4aeb72013-02-12 16:08:55 -0800276 log.appendFormat(" RenderBufferCache %8d / %8d\n",
277 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700278 log.appendFormat(" GradientCache %8d / %8d\n",
279 gradientCache.getSize(), gradientCache.getMaxSize());
280 log.appendFormat(" PathCache %8d / %8d\n",
281 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700282 log.appendFormat(" TessellationCache %8d / %8d\n",
283 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700284 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800285 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700286 log.appendFormat(" PatchCache %8d / %8d\n",
287 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700288 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700289 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
290 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
291 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
292 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
293 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
294 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800295 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700296 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700297 log.appendFormat(" FboCache %8d / %8d\n",
298 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800299
300 uint32_t total = 0;
301 total += textureCache.getSize();
302 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800303 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800304 total += gradientCache.getSize();
305 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700306 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800307 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700308 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700309 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700310 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
311 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800312 }
313
Chet Haase9c1e23b2011-03-24 10:51:31 -0700314 log.appendFormat("Total memory usage:\n");
315 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800316}
317
318///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800319// Memory management
320///////////////////////////////////////////////////////////////////////////////
321
322void Caches::clearGarbage() {
323 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800324 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700325 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800326
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700327 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800328
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700329 { // scope for the lock
330 Mutex::Autolock _l(mGarbageLock);
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700331 layers = mLayerGarbage;
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700332 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800333 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800334
John Reck087bc0c2014-04-04 16:20:08 -0700335 size_t count = layers.size();
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700336 for (size_t i = 0; i < count; i++) {
337 Layer* layer = layers.itemAt(i);
338 delete layer;
339 }
340 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800341}
342
Romain Guyada830f2011-01-13 12:13:20 -0800343void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800344 Mutex::Autolock _l(mGarbageLock);
Romain Guyada830f2011-01-13 12:13:20 -0800345 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800346}
347
Romain Guybdf76092011-07-18 15:00:43 -0700348void Caches::flush(FlushMode mode) {
349 FLUSH_LOGD("Flushing caches (mode %d)", mode);
350
Romain Guyb0a41ed2013-08-16 14:44:38 -0700351 // We must stop tasks before clearing caches
352 if (mode > kFlushMode_Layers) {
353 tasks.stop();
354 }
355
Romain Guybdf76092011-07-18 15:00:43 -0700356 switch (mode) {
357 case kFlushMode_Full:
358 textureCache.clear();
359 patchCache.clear();
360 dropShadowCache.clear();
361 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700362 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700363 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700364 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700365 // fall through
366 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700367 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700368 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700369 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700370 tessellationCache.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
ztenghui63d41ab2014-02-14 13:13:41 -0800407bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800408 if (mCurrentIndicesBuffer != buffer) {
409 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
410 mCurrentIndicesBuffer = buffer;
411 return true;
412 }
413 return false;
414}
415
ztenghui63d41ab2014-02-14 13:13:41 -0800416bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700417 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);
ztenghui63d41ab2014-02-14 13:13:41 -0800431 bool force = bindIndicesBufferInternal(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
ztenghui63d41ab2014-02-14 13:13:41 -0800439 return bindIndicesBufferInternal(mMeshIndices);
440}
441
442bool Caches::bindShadowIndicesBuffer() {
443 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700444 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800445 ShadowTessellator::generateShadowIndices(shadowIndices);
446 glGenBuffers(1, &mShadowStripsIndices);
447 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700448 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800449 shadowIndices, GL_STATIC_DRAW);
450
451 delete[] shadowIndices;
452 return force;
453 }
454
455 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700456}
457
Romain Guy15bc6432011-12-13 13:11:32 -0800458bool Caches::unbindIndicesBuffer() {
459 if (mCurrentIndicesBuffer) {
460 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
461 mCurrentIndicesBuffer = 0;
462 return true;
463 }
464 return false;
465}
466
Romain Guy85ef80d2012-09-13 20:26:50 -0700467///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700468// PBO
469///////////////////////////////////////////////////////////////////////////////
470
471bool Caches::bindPixelBuffer(const GLuint buffer) {
472 if (mCurrentPixelBuffer != buffer) {
473 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
474 mCurrentPixelBuffer = buffer;
475 return true;
476 }
477 return false;
478}
479
480bool Caches::unbindPixelBuffer() {
481 if (mCurrentPixelBuffer) {
482 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
483 mCurrentPixelBuffer = 0;
484 return true;
485 }
486 return false;
487}
488
489///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700490// Meshes and textures
491///////////////////////////////////////////////////////////////////////////////
492
ztenghui55bfb4e2013-12-03 10:38:55 -0800493void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700494 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
495 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800496 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
497 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700498 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800499 }
500}
501
ztenghui55bfb4e2013-12-03 10:38:55 -0800502void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800503 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700504 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800505 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800506 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800507 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800508 }
509}
510
511void Caches::resetVertexPointers() {
512 mCurrentPositionPointer = this;
513 mCurrentTexCoordsPointer = this;
514}
515
516void Caches::resetTexCoordsVertexPointer() {
517 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700518}
519
Romain Guy15bc6432011-12-13 13:11:32 -0800520void Caches::enableTexCoordsVertexArray() {
521 if (!mTexCoordsArrayEnabled) {
522 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800523 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800524 mTexCoordsArrayEnabled = true;
525 }
526}
527
Romain Guyff316ec2013-02-13 18:39:43 -0800528void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800529 if (mTexCoordsArrayEnabled) {
530 glDisableVertexAttribArray(Program::kBindingTexCoords);
531 mTexCoordsArrayEnabled = false;
532 }
533}
534
Romain Guya1d3c912011-12-13 14:55:06 -0800535void Caches::activeTexture(GLuint textureUnit) {
536 if (mTextureUnit != textureUnit) {
537 glActiveTexture(gTextureUnits[textureUnit]);
538 mTextureUnit = textureUnit;
539 }
540}
541
Chris Craik9ab2d182013-07-22 16:16:06 -0700542void Caches::resetActiveTexture() {
543 mTextureUnit = -1;
544}
545
Romain Guy8aa195d2013-06-04 18:00:09 -0700546void Caches::bindTexture(GLuint texture) {
547 if (mBoundTextures[mTextureUnit] != texture) {
548 glBindTexture(GL_TEXTURE_2D, texture);
549 mBoundTextures[mTextureUnit] = texture;
550 }
551}
552
553void Caches::bindTexture(GLenum target, GLuint texture) {
554 if (mBoundTextures[mTextureUnit] != texture) {
555 glBindTexture(target, texture);
556 mBoundTextures[mTextureUnit] = texture;
557 }
558}
559
Romain Guybe1b1272013-06-06 14:02:54 -0700560void Caches::deleteTexture(GLuint texture) {
561 // When glDeleteTextures() is called on a currently bound texture,
562 // OpenGL ES specifies that the texture is then considered unbound
563 // Consider the following series of calls:
564 //
565 // glGenTextures -> creates texture name 2
566 // glBindTexture(2)
567 // glDeleteTextures(2) -> 2 is now unbound
568 // glGenTextures -> can return 2 again
569 //
570 // If we don't call glBindTexture(2) after the second glGenTextures
571 // call, any texture operation will be performed on the default
572 // texture (name=0)
573
jiayuanr4a473c7d2014-06-10 17:41:49 +0800574 unbindTexture(texture);
575
Romain Guybe1b1272013-06-06 14:02:54 -0700576 glDeleteTextures(1, &texture);
577}
578
Romain Guy8aa195d2013-06-04 18:00:09 -0700579void Caches::resetBoundTextures() {
580 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
581}
582
jiayuanr4a473c7d2014-06-10 17:41:49 +0800583void Caches::unbindTexture(GLuint texture) {
584 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
585 if (mBoundTextures[i] == texture) {
586 mBoundTextures[i] = 0;
587 }
588 }
589}
590
Romain Guy85ef80d2012-09-13 20:26:50 -0700591///////////////////////////////////////////////////////////////////////////////
592// Scissor
593///////////////////////////////////////////////////////////////////////////////
594
Romain Guy8a4ac612012-07-17 17:32:48 -0700595bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700596 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
597 width != mScissorWidth || height != mScissorHeight)) {
598
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700599 if (x < 0) {
600 width += x;
601 x = 0;
602 }
603 if (y < 0) {
604 height += y;
605 y = 0;
606 }
607 if (width < 0) {
608 width = 0;
609 }
610 if (height < 0) {
611 height = 0;
612 }
Romain Guy8f85e802011-12-14 19:23:32 -0800613 glScissor(x, y, width, height);
614
615 mScissorX = x;
616 mScissorY = y;
617 mScissorWidth = width;
618 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700619
620 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800621 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700622 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800623}
624
Romain Guy8a4ac612012-07-17 17:32:48 -0700625bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700626 if (!scissorEnabled) {
627 glEnable(GL_SCISSOR_TEST);
628 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700629 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700630 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700631 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700632 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700633}
634
Romain Guy8a4ac612012-07-17 17:32:48 -0700635bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700636 if (scissorEnabled) {
637 glDisable(GL_SCISSOR_TEST);
638 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700639 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700640 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700641 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700642}
643
644void Caches::setScissorEnabled(bool enabled) {
645 if (scissorEnabled != enabled) {
646 if (enabled) glEnable(GL_SCISSOR_TEST);
647 else glDisable(GL_SCISSOR_TEST);
648 scissorEnabled = enabled;
649 }
650}
651
Romain Guy82bc7a72012-01-03 14:13:39 -0800652void Caches::resetScissor() {
653 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
654}
655
Romain Guy85ef80d2012-09-13 20:26:50 -0700656///////////////////////////////////////////////////////////////////////////////
657// Tiling
658///////////////////////////////////////////////////////////////////////////////
659
Romain Guyf735c8e2013-01-31 17:45:55 -0800660void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800661 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800662 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700663 }
664}
665
666void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800667 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700668 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700669 }
670}
671
Romain Guy54c1a642012-09-27 17:55:46 -0700672bool Caches::hasRegisteredFunctors() {
673 return mFunctorsCount > 0;
674}
675
676void Caches::registerFunctors(uint32_t functorCount) {
677 mFunctorsCount += functorCount;
678}
679
680void Caches::unregisterFunctors(uint32_t functorCount) {
681 if (functorCount > mFunctorsCount) {
682 mFunctorsCount = 0;
683 } else {
684 mFunctorsCount -= functorCount;
685 }
686}
687
Romain Guy85ef80d2012-09-13 20:26:50 -0700688///////////////////////////////////////////////////////////////////////////////
689// Regions
690///////////////////////////////////////////////////////////////////////////////
691
Romain Guy5b3b3522010-10-27 18:57:51 -0700692TextureVertex* Caches::getRegionMesh() {
693 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
694 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700695 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700696 }
697
698 return mRegionMesh;
699}
700
Chris Craikba9b6132013-12-15 17:10:19 -0800701///////////////////////////////////////////////////////////////////////////////
702// Temporary Properties
703///////////////////////////////////////////////////////////////////////////////
704
705void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700706 propertyLightDiameter = -1.0f;
707 propertyLightPosY = -1.0f;
708 propertyLightPosZ = -1.0f;
709 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700710 propertyAmbientShadowStrength = -1;
711 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800712}
713
714void Caches::setTempProperty(const char* name, const char* value) {
715 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700716 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700717 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
718 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800719 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700720 } else if (!strcmp(name, "lightDiameter")) {
721 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
722 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800723 return;
Chris Craik59744b72014-07-01 17:56:52 -0700724 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700725 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
726 ALOGD("lightPos Y = %.2f", propertyLightPosY);
727 return;
Chris Craik59744b72014-07-01 17:56:52 -0700728 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700729 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
730 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800731 return;
ztenghui14a4e352014-08-13 10:44:39 -0700732 } else if (!strcmp(name, "ambientShadowStrength")) {
733 propertyAmbientShadowStrength = atoi(value);
734 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
735 return;
736 } else if (!strcmp(name, "spotShadowStrength")) {
737 propertySpotShadowStrength = atoi(value);
738 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
739 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800740 }
741 ALOGD(" failed");
742}
743
Chet Haasedd78cca2010-10-22 18:59:26 -0700744}; // namespace uirenderer
745}; // namespace android