blob: 6453206eac3d7a4a2b00dde862e5be087836023b [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 Guy0f667532013-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
John Reckfbc8df02014-11-14 16:18:41 -080069 ATRACE_NAME("Caches::init");
70
Romain Guy8ff6b9e2011-11-09 20:10:18 -080071 glGenBuffers(1, &meshBuffer);
72 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
74
75 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080076 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070078 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080079 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070080 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080081
Romain Guy15bc6432011-12-13 13:11:32 -080082 mTexCoordsArrayEnabled = false;
83
Romain Guyb1d0a4e2012-07-13 18:25:35 -070084 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070085 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080086 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
87
Romain Guya1d3c912011-12-13 14:55:06 -080088 glActiveTexture(gTextureUnits[0]);
89 mTextureUnit = 0;
90
Romain Guy8ff6b9e2011-11-09 20:10:18 -080091 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070092 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080093 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080094 blend = false;
95 lastSrcMode = GL_ZERO;
96 lastDstMode = GL_ZERO;
97 currentProgram = NULL;
98
Romain Guy54c1a642012-09-27 17:55:46 -070099 mFunctorsCount = 0;
100
Romain Guyc2a97212013-02-06 15:29:46 -0800101 debugLayersUpdates = false;
102 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800103 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800104
Romain Guy3b748a42013-04-17 18:54:38 -0700105 patchCache.init(*this);
106
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800107 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700108
Romain Guy8aa195d2013-06-04 18:00:09 -0700109 resetBoundTextures();
110
Romain Guy3b748a42013-04-17 18:54:38 -0700111 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800112}
113
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700114void Caches::initFont() {
115 fontRenderer = GammaFontRenderer::createRenderer();
116}
117
Romain Guydfa10462012-05-12 16:18:58 -0700118void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800119 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700120 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800121
Chris Craikff785832013-03-08 13:12:16 -0800122 startMark = glPushGroupMarkerEXT;
123 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700124 } else {
125 eventMark = eventMarkNull;
126 startMark = startMarkNull;
127 endMark = endMarkNull;
128 }
129
Romain Guy0f667532013-03-01 14:31:04 -0800130 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700131 setLabel = glLabelObjectEXT;
132 getLabel = glGetObjectLabelEXT;
133 } else {
134 setLabel = setLabelNull;
135 getLabel = getLabelNull;
136 }
137}
138
139void Caches::initConstraints() {
140 GLint maxTextureUnits;
141 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
142 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
143 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
144 }
145
146 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
147}
148
Romain Guyf9f00162013-05-09 11:50:12 -0700149void Caches::initStaticProperties() {
150 gpuPixelBuffersEnabled = false;
151
152 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700153 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700154 char property[PROPERTY_VALUE_MAX];
155 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
156 gpuPixelBuffersEnabled = !strcmp(property, "true");
157 }
158 }
159}
160
Romain Guy5bb3c732012-11-29 17:52:58 -0800161bool Caches::initProperties() {
162 bool prevDebugLayersUpdates = debugLayersUpdates;
163 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800164 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800165
Romain Guy4ff0cf42012-08-06 14:51:10 -0700166 char property[PROPERTY_VALUE_MAX];
167 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
168 INIT_LOGD(" Layers updates debug enabled: %s", property);
169 debugLayersUpdates = !strcmp(property, "true");
170 } else {
171 debugLayersUpdates = false;
172 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700173
Romain Guy627c6fd2013-08-21 11:53:18 -0700174 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700175 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
176 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700177 if (!strcmp(property, "show")) {
178 debugOverdraw = true;
179 mOverdrawDebugColorSet = kColorSet_Default;
180 } else if (!strcmp(property, "show_deuteranomaly")) {
181 debugOverdraw = true;
182 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
183 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700184 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800185
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800186 // See Properties.h for valid values
187 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
188 INIT_LOGD(" Stencil clip debug enabled: %s", property);
189 if (!strcmp(property, "hide")) {
190 debugStencilClip = kStencilHide;
191 } else if (!strcmp(property, "highlight")) {
192 debugStencilClip = kStencilShowHighlight;
193 } else if (!strcmp(property, "region")) {
194 debugStencilClip = kStencilShowRegion;
195 }
196 } else {
197 debugStencilClip = kStencilHide;
198 }
199
Romain Guy0f667532013-03-01 14:31:04 -0800200 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
201 drawDeferDisabled = !strcasecmp(property, "true");
202 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
203 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700204 drawDeferDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800205 INIT_LOGD(" Draw defer enabled");
206 }
207
208 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
209 drawReorderDisabled = !strcasecmp(property, "true");
210 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
211 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700212 drawReorderDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800213 INIT_LOGD(" Draw reorder enabled");
214 }
215
Romain Guy5bb3c732012-11-29 17:52:58 -0800216 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800217 (prevDebugOverdraw != debugOverdraw) ||
218 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700219}
220
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800221void Caches::terminate() {
222 if (!mInitialized) return;
223
224 glDeleteBuffers(1, &meshBuffer);
225 mCurrentBuffer = 0;
226
Romain Guy3b748a42013-04-17 18:54:38 -0700227 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700228 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700229 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800230 mRegionMesh = NULL;
231
ztenghui63d41ab2014-02-14 13:13:41 -0800232 glDeleteBuffers(1, &mShadowStripsIndices);
233 mShadowStripsIndices = 0;
234
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800235 fboCache.clear();
236
237 programCache.clear();
238 currentProgram = NULL;
239
Romain Guy3b748a42013-04-17 18:54:38 -0700240 assetAtlas.terminate();
241
242 patchCache.clear();
243
Romain Guy46bfc482013-08-16 18:38:29 -0700244 clearGarbage();
245
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800246 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700247}
248
249///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800250// Debug
251///////////////////////////////////////////////////////////////////////////////
252
Romain Guy627c6fd2013-08-21 11:53:18 -0700253uint32_t Caches::getOverdrawColor(uint32_t amount) const {
254 static uint32_t sOverdrawColors[2][4] = {
255 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
256 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
257 };
258 if (amount < 1) amount = 1;
259 if (amount > 4) amount = 4;
260 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
261}
262
Romain Guyc15008e2010-11-10 11:59:15 -0800263void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700264 String8 stringLog;
265 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000266 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700267}
268
269void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700270 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700271 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
272 log.appendFormat(" TextureCache %8d / %8d\n",
273 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700274 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
275 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700276 if (mRenderState) {
277 int memused = 0;
278 for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin();
279 it != mRenderState->mActiveLayers.end(); it++) {
280 const Layer* layer = *it;
281 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
282 layer->getWidth(), layer->getHeight(),
283 layer->isTextureLayer(), layer->getTexture(),
284 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800285 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700286 }
287 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
288 memused, mRenderState->mActiveLayers.size());
289 total += memused;
290 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800291 log.appendFormat(" RenderBufferCache %8d / %8d\n",
292 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700293 log.appendFormat(" GradientCache %8d / %8d\n",
294 gradientCache.getSize(), gradientCache.getMaxSize());
295 log.appendFormat(" PathCache %8d / %8d\n",
296 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700297 log.appendFormat(" TessellationCache %8d / %8d\n",
298 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700299 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800300 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700301 log.appendFormat(" PatchCache %8d / %8d\n",
302 patchCache.getSize(), patchCache.getMaxSize());
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 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
305 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
306 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
307 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
308 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
309 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800310 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700311 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700312 log.appendFormat(" FboCache %8d / %8d\n",
313 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800314
Romain Guyc15008e2010-11-10 11:59:15 -0800315 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800316 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800317 total += gradientCache.getSize();
318 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700319 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800320 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700321 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700322 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700323 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
324 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800325 }
326
Chet Haase9c1e23b2011-03-24 10:51:31 -0700327 log.appendFormat("Total memory usage:\n");
328 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800329}
330
331///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800332// Memory management
333///////////////////////////////////////////////////////////////////////////////
334
335void Caches::clearGarbage() {
336 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800337 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700338 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800339}
340
Romain Guybdf76092011-07-18 15:00:43 -0700341void Caches::flush(FlushMode mode) {
342 FLUSH_LOGD("Flushing caches (mode %d)", mode);
343
Romain Guyb0a41ed2013-08-16 14:44:38 -0700344 // We must stop tasks before clearing caches
345 if (mode > kFlushMode_Layers) {
346 tasks.stop();
347 }
348
Romain Guybdf76092011-07-18 15:00:43 -0700349 switch (mode) {
350 case kFlushMode_Full:
351 textureCache.clear();
352 patchCache.clear();
353 dropShadowCache.clear();
354 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700355 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700356 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700357 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700358 // fall through
359 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700360 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700361 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700362 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700363 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700364 // fall through
365 case kFlushMode_Layers:
366 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800367 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700368 break;
369 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700370
371 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700372 glFinish();
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) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500548 if (target == GL_TEXTURE_2D) {
549 bindTexture(texture);
550 } else {
551 // GLConsumer directly calls glBindTexture() with
552 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
553 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700554 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700555 }
556}
557
Romain Guybe1b1272013-06-06 14:02:54 -0700558void Caches::deleteTexture(GLuint texture) {
559 // When glDeleteTextures() is called on a currently bound texture,
560 // OpenGL ES specifies that the texture is then considered unbound
561 // Consider the following series of calls:
562 //
563 // glGenTextures -> creates texture name 2
564 // glBindTexture(2)
565 // glDeleteTextures(2) -> 2 is now unbound
566 // glGenTextures -> can return 2 again
567 //
568 // If we don't call glBindTexture(2) after the second glGenTextures
569 // call, any texture operation will be performed on the default
570 // texture (name=0)
571
jiayuanr4a473c7d2014-06-10 17:41:49 +0800572 unbindTexture(texture);
573
Romain Guybe1b1272013-06-06 14:02:54 -0700574 glDeleteTextures(1, &texture);
575}
576
Romain Guy8aa195d2013-06-04 18:00:09 -0700577void Caches::resetBoundTextures() {
578 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
579}
580
jiayuanr4a473c7d2014-06-10 17:41:49 +0800581void Caches::unbindTexture(GLuint texture) {
582 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
583 if (mBoundTextures[i] == texture) {
584 mBoundTextures[i] = 0;
585 }
586 }
587}
588
Romain Guy85ef80d2012-09-13 20:26:50 -0700589///////////////////////////////////////////////////////////////////////////////
590// Scissor
591///////////////////////////////////////////////////////////////////////////////
592
Romain Guy8a4ac612012-07-17 17:32:48 -0700593bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700594 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
595 width != mScissorWidth || height != mScissorHeight)) {
596
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700597 if (x < 0) {
598 width += x;
599 x = 0;
600 }
601 if (y < 0) {
602 height += y;
603 y = 0;
604 }
605 if (width < 0) {
606 width = 0;
607 }
608 if (height < 0) {
609 height = 0;
610 }
Romain Guy8f85e802011-12-14 19:23:32 -0800611 glScissor(x, y, width, height);
612
613 mScissorX = x;
614 mScissorY = y;
615 mScissorWidth = width;
616 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700617
618 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800619 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700620 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800621}
622
Romain Guy8a4ac612012-07-17 17:32:48 -0700623bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700624 if (!scissorEnabled) {
625 glEnable(GL_SCISSOR_TEST);
626 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700627 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700628 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700629 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700630 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700631}
632
Romain Guy8a4ac612012-07-17 17:32:48 -0700633bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700634 if (scissorEnabled) {
635 glDisable(GL_SCISSOR_TEST);
636 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700637 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700638 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700639 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700640}
641
642void Caches::setScissorEnabled(bool enabled) {
643 if (scissorEnabled != enabled) {
644 if (enabled) glEnable(GL_SCISSOR_TEST);
645 else glDisable(GL_SCISSOR_TEST);
646 scissorEnabled = enabled;
647 }
648}
649
Romain Guy82bc7a72012-01-03 14:13:39 -0800650void Caches::resetScissor() {
651 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
652}
653
Romain Guy85ef80d2012-09-13 20:26:50 -0700654///////////////////////////////////////////////////////////////////////////////
655// Tiling
656///////////////////////////////////////////////////////////////////////////////
657
Romain Guyf735c8e2013-01-31 17:45:55 -0800658void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800659 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800660 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700661 }
662}
663
664void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800665 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700666 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700667 }
668}
669
Romain Guy54c1a642012-09-27 17:55:46 -0700670bool Caches::hasRegisteredFunctors() {
671 return mFunctorsCount > 0;
672}
673
674void Caches::registerFunctors(uint32_t functorCount) {
675 mFunctorsCount += functorCount;
676}
677
678void Caches::unregisterFunctors(uint32_t functorCount) {
679 if (functorCount > mFunctorsCount) {
680 mFunctorsCount = 0;
681 } else {
682 mFunctorsCount -= functorCount;
683 }
684}
685
Romain Guy85ef80d2012-09-13 20:26:50 -0700686///////////////////////////////////////////////////////////////////////////////
687// Regions
688///////////////////////////////////////////////////////////////////////////////
689
Romain Guy5b3b3522010-10-27 18:57:51 -0700690TextureVertex* Caches::getRegionMesh() {
691 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
692 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700693 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700694 }
695
696 return mRegionMesh;
697}
698
Chris Craikba9b6132013-12-15 17:10:19 -0800699///////////////////////////////////////////////////////////////////////////////
700// Temporary Properties
701///////////////////////////////////////////////////////////////////////////////
702
703void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700704 propertyLightDiameter = -1.0f;
705 propertyLightPosY = -1.0f;
706 propertyLightPosZ = -1.0f;
707 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700708 propertyAmbientShadowStrength = -1;
709 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800710}
711
712void Caches::setTempProperty(const char* name, const char* value) {
713 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700714 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700715 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
716 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800717 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700718 } else if (!strcmp(name, "lightDiameter")) {
719 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
720 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800721 return;
Chris Craik59744b72014-07-01 17:56:52 -0700722 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700723 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
724 ALOGD("lightPos Y = %.2f", propertyLightPosY);
725 return;
Chris Craik59744b72014-07-01 17:56:52 -0700726 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700727 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
728 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800729 return;
ztenghui14a4e352014-08-13 10:44:39 -0700730 } else if (!strcmp(name, "ambientShadowStrength")) {
731 propertyAmbientShadowStrength = atoi(value);
732 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
733 return;
734 } else if (!strcmp(name, "spotShadowStrength")) {
735 propertySpotShadowStrength = atoi(value);
736 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
737 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800738 }
739 ALOGD(" failed");
740}
741
Chet Haasedd78cca2010-10-22 18:59:26 -0700742}; // namespace uirenderer
743}; // namespace android