blob: bb4ac83ea864d719a8dc81937266690345bc8c7d [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"
Tom Hudson2dc236b2014-10-15 15:46:42 -040024#include "GammaFontRenderer.h"
Romain Guye190aa62010-11-10 19:01:29 -080025#include "Properties.h"
Romain Guy09b7c912011-02-02 20:28:09 -080026#include "LayerRenderer.h"
ztenghui63d41ab2014-02-14 13:13:41 -080027#include "ShadowTessellator.h"
John Reck17035b02014-09-03 07:39:53 -070028#include "RenderState.h"
Chet Haasedd78cca2010-10-22 18:59:26 -070029
30namespace android {
31
Chet Haasedd78cca2010-10-22 18:59:26 -070032using namespace uirenderer;
33ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
Chet Haasedd78cca2010-10-22 18:59:26 -070034
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>(),
John Reck17035b02014-09-03 07:39:53 -070052 mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(NULL) {
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
John Reckfbc8df02014-11-14 16:18:41 -080068 ATRACE_NAME("Caches::init");
69
Romain Guy8ff6b9e2011-11-09 20:10:18 -080070 glGenBuffers(1, &meshBuffer);
71 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
72 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
73
74 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080075 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080076 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070077 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080078 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070079 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080080
Romain Guy15bc6432011-12-13 13:11:32 -080081 mTexCoordsArrayEnabled = false;
82
Romain Guyb1d0a4e2012-07-13 18:25:35 -070083 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070084 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080085 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
86
Romain Guya1d3c912011-12-13 14:55:06 -080087 glActiveTexture(gTextureUnits[0]);
88 mTextureUnit = 0;
89
Romain Guy8ff6b9e2011-11-09 20:10:18 -080090 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070091 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080092 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080093 blend = false;
94 lastSrcMode = GL_ZERO;
95 lastDstMode = GL_ZERO;
96 currentProgram = NULL;
97
Romain Guy54c1a642012-09-27 17:55:46 -070098 mFunctorsCount = 0;
99
Romain Guyc2a97212013-02-06 15:29:46 -0800100 debugLayersUpdates = false;
101 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800102 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800103
Romain Guy3b748a42013-04-17 18:54:38 -0700104 patchCache.init(*this);
105
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800106 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700107
Romain Guy8aa195d2013-06-04 18:00:09 -0700108 resetBoundTextures();
109
Romain Guy3b748a42013-04-17 18:54:38 -0700110 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800111}
112
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700113void Caches::initFont() {
114 fontRenderer = GammaFontRenderer::createRenderer();
115}
116
Romain Guydfa10462012-05-12 16:18:58 -0700117void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800118 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700119 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800120
Chris Craikff785832013-03-08 13:12:16 -0800121 startMark = glPushGroupMarkerEXT;
122 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700123 } else {
124 eventMark = eventMarkNull;
125 startMark = startMarkNull;
126 endMark = endMarkNull;
127 }
128
Romain Guy0f667532013-03-01 14:31:04 -0800129 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700130 setLabel = glLabelObjectEXT;
131 getLabel = glGetObjectLabelEXT;
132 } else {
133 setLabel = setLabelNull;
134 getLabel = getLabelNull;
135 }
136}
137
138void Caches::initConstraints() {
139 GLint maxTextureUnits;
140 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
141 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
142 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
143 }
144
145 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
146}
147
Romain Guyf9f00162013-05-09 11:50:12 -0700148void Caches::initStaticProperties() {
149 gpuPixelBuffersEnabled = false;
150
151 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700152 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700153 char property[PROPERTY_VALUE_MAX];
154 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
155 gpuPixelBuffersEnabled = !strcmp(property, "true");
156 }
157 }
158}
159
Romain Guy5bb3c732012-11-29 17:52:58 -0800160bool Caches::initProperties() {
161 bool prevDebugLayersUpdates = debugLayersUpdates;
162 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800163 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800164
Romain Guy4ff0cf42012-08-06 14:51:10 -0700165 char property[PROPERTY_VALUE_MAX];
166 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
167 INIT_LOGD(" Layers updates debug enabled: %s", property);
168 debugLayersUpdates = !strcmp(property, "true");
169 } else {
170 debugLayersUpdates = false;
171 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700172
Romain Guy627c6fd2013-08-21 11:53:18 -0700173 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700174 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
175 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700176 if (!strcmp(property, "show")) {
177 debugOverdraw = true;
178 mOverdrawDebugColorSet = kColorSet_Default;
179 } else if (!strcmp(property, "show_deuteranomaly")) {
180 debugOverdraw = true;
181 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
182 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700183 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800184
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800185 // See Properties.h for valid values
186 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
187 INIT_LOGD(" Stencil clip debug enabled: %s", property);
188 if (!strcmp(property, "hide")) {
189 debugStencilClip = kStencilHide;
190 } else if (!strcmp(property, "highlight")) {
191 debugStencilClip = kStencilShowHighlight;
192 } else if (!strcmp(property, "region")) {
193 debugStencilClip = kStencilShowRegion;
194 }
195 } else {
196 debugStencilClip = kStencilHide;
197 }
198
Romain Guy0f667532013-03-01 14:31:04 -0800199 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
200 drawDeferDisabled = !strcasecmp(property, "true");
201 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
202 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700203 drawDeferDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800204 INIT_LOGD(" Draw defer enabled");
205 }
206
207 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
208 drawReorderDisabled = !strcasecmp(property, "true");
209 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
210 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700211 drawReorderDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800212 INIT_LOGD(" Draw reorder enabled");
213 }
214
Romain Guy5bb3c732012-11-29 17:52:58 -0800215 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800216 (prevDebugOverdraw != debugOverdraw) ||
217 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700218}
219
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800220void Caches::terminate() {
221 if (!mInitialized) return;
222
223 glDeleteBuffers(1, &meshBuffer);
224 mCurrentBuffer = 0;
225
Romain Guy3b748a42013-04-17 18:54:38 -0700226 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700227 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700228 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800229 mRegionMesh = NULL;
230
ztenghui63d41ab2014-02-14 13:13:41 -0800231 glDeleteBuffers(1, &mShadowStripsIndices);
232 mShadowStripsIndices = 0;
233
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800234 fboCache.clear();
235
236 programCache.clear();
237 currentProgram = NULL;
238
Romain Guy3b748a42013-04-17 18:54:38 -0700239 patchCache.clear();
240
Romain Guy46bfc482013-08-16 18:38:29 -0700241 clearGarbage();
242
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800243 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700244}
245
246///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800247// Debug
248///////////////////////////////////////////////////////////////////////////////
249
Romain Guy627c6fd2013-08-21 11:53:18 -0700250uint32_t Caches::getOverdrawColor(uint32_t amount) const {
251 static uint32_t sOverdrawColors[2][4] = {
252 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
253 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
254 };
255 if (amount < 1) amount = 1;
256 if (amount > 4) amount = 4;
257 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
258}
259
Romain Guyc15008e2010-11-10 11:59:15 -0800260void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700261 String8 stringLog;
262 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000263 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700264}
265
266void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700267 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700268 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());
John Reck0e89e2b2014-10-31 14:49:06 -0700273 if (mRenderState) {
274 int memused = 0;
275 for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin();
276 it != mRenderState->mActiveLayers.end(); it++) {
277 const Layer* layer = *it;
278 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
279 layer->getWidth(), layer->getHeight(),
280 layer->isTextureLayer(), layer->getTexture(),
281 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800282 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700283 }
284 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
285 memused, mRenderState->mActiveLayers.size());
286 total += memused;
287 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800288 log.appendFormat(" RenderBufferCache %8d / %8d\n",
289 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700290 log.appendFormat(" GradientCache %8d / %8d\n",
291 gradientCache.getSize(), gradientCache.getMaxSize());
292 log.appendFormat(" PathCache %8d / %8d\n",
293 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700294 log.appendFormat(" TessellationCache %8d / %8d\n",
295 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700296 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800297 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700298 log.appendFormat(" PatchCache %8d / %8d\n",
299 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700300 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700301 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
302 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
303 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
304 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
305 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
306 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800307 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700308 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700309 log.appendFormat(" FboCache %8d / %8d\n",
310 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800311
Romain Guyc15008e2010-11-10 11:59:15 -0800312 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800313 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800314 total += gradientCache.getSize();
315 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700316 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800317 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700318 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700319 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700320 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
321 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800322 }
323
Chet Haase9c1e23b2011-03-24 10:51:31 -0700324 log.appendFormat("Total memory usage:\n");
325 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800326}
327
328///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800329// Memory management
330///////////////////////////////////////////////////////////////////////////////
331
332void Caches::clearGarbage() {
333 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800334 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700335 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800336}
337
Romain Guybdf76092011-07-18 15:00:43 -0700338void Caches::flush(FlushMode mode) {
339 FLUSH_LOGD("Flushing caches (mode %d)", mode);
340
Romain Guyb0a41ed2013-08-16 14:44:38 -0700341 // We must stop tasks before clearing caches
342 if (mode > kFlushMode_Layers) {
343 tasks.stop();
344 }
345
Romain Guybdf76092011-07-18 15:00:43 -0700346 switch (mode) {
347 case kFlushMode_Full:
348 textureCache.clear();
349 patchCache.clear();
350 dropShadowCache.clear();
351 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700352 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700353 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700354 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700355 // fall through
356 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700357 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700358 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700359 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700360 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700361 // fall through
362 case kFlushMode_Layers:
363 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800364 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700365 break;
366 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700367
368 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700369 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700370}
371
Romain Guyfe48f652010-11-11 15:36:56 -0800372///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700373// VBO
374///////////////////////////////////////////////////////////////////////////////
375
Romain Guyf3a910b42011-12-12 20:35:21 -0800376bool Caches::bindMeshBuffer() {
377 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700378}
379
Romain Guyf3a910b42011-12-12 20:35:21 -0800380bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700381 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700382 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700383 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800384 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700385 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800386 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700387}
388
Romain Guyf3a910b42011-12-12 20:35:21 -0800389bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700390 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700391 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700392 mCurrentBuffer = 0;
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;
396}
397
ztenghui63d41ab2014-02-14 13:13:41 -0800398bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800399 if (mCurrentIndicesBuffer != buffer) {
400 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
401 mCurrentIndicesBuffer = buffer;
402 return true;
403 }
404 return false;
405}
406
ztenghui63d41ab2014-02-14 13:13:41 -0800407bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700408 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700409 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
410 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700411 uint16_t quad = i * 4;
412 int index = i * 6;
413 regionIndices[index ] = quad; // top-left
414 regionIndices[index + 1] = quad + 1; // top-right
415 regionIndices[index + 2] = quad + 2; // bottom-left
416 regionIndices[index + 3] = quad + 2; // bottom-left
417 regionIndices[index + 4] = quad + 1; // top-right
418 regionIndices[index + 5] = quad + 3; // bottom-right
419 }
420
421 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800422 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700423 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700424 regionIndices, GL_STATIC_DRAW);
425
426 delete[] regionIndices;
427 return force;
428 }
429
ztenghui63d41ab2014-02-14 13:13:41 -0800430 return bindIndicesBufferInternal(mMeshIndices);
431}
432
433bool Caches::bindShadowIndicesBuffer() {
434 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700435 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800436 ShadowTessellator::generateShadowIndices(shadowIndices);
437 glGenBuffers(1, &mShadowStripsIndices);
438 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700439 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800440 shadowIndices, GL_STATIC_DRAW);
441
442 delete[] shadowIndices;
443 return force;
444 }
445
446 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700447}
448
Romain Guy15bc6432011-12-13 13:11:32 -0800449bool Caches::unbindIndicesBuffer() {
450 if (mCurrentIndicesBuffer) {
451 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
452 mCurrentIndicesBuffer = 0;
453 return true;
454 }
455 return false;
456}
457
Romain Guy85ef80d2012-09-13 20:26:50 -0700458///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700459// PBO
460///////////////////////////////////////////////////////////////////////////////
461
462bool Caches::bindPixelBuffer(const GLuint buffer) {
463 if (mCurrentPixelBuffer != buffer) {
464 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
465 mCurrentPixelBuffer = buffer;
466 return true;
467 }
468 return false;
469}
470
471bool Caches::unbindPixelBuffer() {
472 if (mCurrentPixelBuffer) {
473 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
474 mCurrentPixelBuffer = 0;
475 return true;
476 }
477 return false;
478}
479
480///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700481// Meshes and textures
482///////////////////////////////////////////////////////////////////////////////
483
ztenghui55bfb4e2013-12-03 10:38:55 -0800484void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700485 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
486 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800487 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
488 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700489 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800490 }
491}
492
ztenghui55bfb4e2013-12-03 10:38:55 -0800493void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800494 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700495 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800496 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800497 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800498 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800499 }
500}
501
502void Caches::resetVertexPointers() {
503 mCurrentPositionPointer = this;
504 mCurrentTexCoordsPointer = this;
505}
506
507void Caches::resetTexCoordsVertexPointer() {
508 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700509}
510
Romain Guy15bc6432011-12-13 13:11:32 -0800511void Caches::enableTexCoordsVertexArray() {
512 if (!mTexCoordsArrayEnabled) {
513 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800514 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800515 mTexCoordsArrayEnabled = true;
516 }
517}
518
Romain Guyff316ec2013-02-13 18:39:43 -0800519void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800520 if (mTexCoordsArrayEnabled) {
521 glDisableVertexAttribArray(Program::kBindingTexCoords);
522 mTexCoordsArrayEnabled = false;
523 }
524}
525
Romain Guya1d3c912011-12-13 14:55:06 -0800526void Caches::activeTexture(GLuint textureUnit) {
527 if (mTextureUnit != textureUnit) {
528 glActiveTexture(gTextureUnits[textureUnit]);
529 mTextureUnit = textureUnit;
530 }
531}
532
Chris Craik9ab2d182013-07-22 16:16:06 -0700533void Caches::resetActiveTexture() {
534 mTextureUnit = -1;
535}
536
Romain Guy8aa195d2013-06-04 18:00:09 -0700537void Caches::bindTexture(GLuint texture) {
538 if (mBoundTextures[mTextureUnit] != texture) {
539 glBindTexture(GL_TEXTURE_2D, texture);
540 mBoundTextures[mTextureUnit] = texture;
541 }
542}
543
544void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500545 if (target == GL_TEXTURE_2D) {
546 bindTexture(texture);
547 } else {
548 // GLConsumer directly calls glBindTexture() with
549 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
550 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700551 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700552 }
553}
554
Romain Guybe1b1272013-06-06 14:02:54 -0700555void Caches::deleteTexture(GLuint texture) {
556 // When glDeleteTextures() is called on a currently bound texture,
557 // OpenGL ES specifies that the texture is then considered unbound
558 // Consider the following series of calls:
559 //
560 // glGenTextures -> creates texture name 2
561 // glBindTexture(2)
562 // glDeleteTextures(2) -> 2 is now unbound
563 // glGenTextures -> can return 2 again
564 //
565 // If we don't call glBindTexture(2) after the second glGenTextures
566 // call, any texture operation will be performed on the default
567 // texture (name=0)
568
jiayuanr4a473c7d2014-06-10 17:41:49 +0800569 unbindTexture(texture);
570
Romain Guybe1b1272013-06-06 14:02:54 -0700571 glDeleteTextures(1, &texture);
572}
573
Romain Guy8aa195d2013-06-04 18:00:09 -0700574void Caches::resetBoundTextures() {
575 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
576}
577
jiayuanr4a473c7d2014-06-10 17:41:49 +0800578void Caches::unbindTexture(GLuint texture) {
579 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
580 if (mBoundTextures[i] == texture) {
581 mBoundTextures[i] = 0;
582 }
583 }
584}
585
Romain Guy85ef80d2012-09-13 20:26:50 -0700586///////////////////////////////////////////////////////////////////////////////
587// Scissor
588///////////////////////////////////////////////////////////////////////////////
589
Romain Guy8a4ac612012-07-17 17:32:48 -0700590bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700591 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
592 width != mScissorWidth || height != mScissorHeight)) {
593
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700594 if (x < 0) {
595 width += x;
596 x = 0;
597 }
598 if (y < 0) {
599 height += y;
600 y = 0;
601 }
602 if (width < 0) {
603 width = 0;
604 }
605 if (height < 0) {
606 height = 0;
607 }
Romain Guy8f85e802011-12-14 19:23:32 -0800608 glScissor(x, y, width, height);
609
610 mScissorX = x;
611 mScissorY = y;
612 mScissorWidth = width;
613 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700614
615 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800616 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700617 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800618}
619
Romain Guy8a4ac612012-07-17 17:32:48 -0700620bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700621 if (!scissorEnabled) {
622 glEnable(GL_SCISSOR_TEST);
623 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700624 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700625 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700626 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700627 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700628}
629
Romain Guy8a4ac612012-07-17 17:32:48 -0700630bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700631 if (scissorEnabled) {
632 glDisable(GL_SCISSOR_TEST);
633 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700634 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700635 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700636 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700637}
638
639void Caches::setScissorEnabled(bool enabled) {
640 if (scissorEnabled != enabled) {
641 if (enabled) glEnable(GL_SCISSOR_TEST);
642 else glDisable(GL_SCISSOR_TEST);
643 scissorEnabled = enabled;
644 }
645}
646
Romain Guy82bc7a72012-01-03 14:13:39 -0800647void Caches::resetScissor() {
648 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
649}
650
Romain Guy85ef80d2012-09-13 20:26:50 -0700651///////////////////////////////////////////////////////////////////////////////
652// Tiling
653///////////////////////////////////////////////////////////////////////////////
654
Romain Guyf735c8e2013-01-31 17:45:55 -0800655void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800656 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800657 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700658 }
659}
660
661void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800662 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700663 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700664 }
665}
666
Romain Guy54c1a642012-09-27 17:55:46 -0700667bool Caches::hasRegisteredFunctors() {
668 return mFunctorsCount > 0;
669}
670
671void Caches::registerFunctors(uint32_t functorCount) {
672 mFunctorsCount += functorCount;
673}
674
675void Caches::unregisterFunctors(uint32_t functorCount) {
676 if (functorCount > mFunctorsCount) {
677 mFunctorsCount = 0;
678 } else {
679 mFunctorsCount -= functorCount;
680 }
681}
682
Romain Guy85ef80d2012-09-13 20:26:50 -0700683///////////////////////////////////////////////////////////////////////////////
684// Regions
685///////////////////////////////////////////////////////////////////////////////
686
Romain Guy5b3b3522010-10-27 18:57:51 -0700687TextureVertex* Caches::getRegionMesh() {
688 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
689 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700690 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700691 }
692
693 return mRegionMesh;
694}
695
Chris Craikba9b6132013-12-15 17:10:19 -0800696///////////////////////////////////////////////////////////////////////////////
697// Temporary Properties
698///////////////////////////////////////////////////////////////////////////////
699
700void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700701 propertyLightDiameter = -1.0f;
702 propertyLightPosY = -1.0f;
703 propertyLightPosZ = -1.0f;
704 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700705 propertyAmbientShadowStrength = -1;
706 propertySpotShadowStrength = -1;
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 Craika736cd92014-08-04 13:18:38 -0700711 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700712 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
713 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800714 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700715 } else if (!strcmp(name, "lightDiameter")) {
716 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
717 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800718 return;
Chris Craik59744b72014-07-01 17:56:52 -0700719 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700720 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
721 ALOGD("lightPos Y = %.2f", propertyLightPosY);
722 return;
Chris Craik59744b72014-07-01 17:56:52 -0700723 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700724 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
725 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800726 return;
ztenghui14a4e352014-08-13 10:44:39 -0700727 } else if (!strcmp(name, "ambientShadowStrength")) {
728 propertyAmbientShadowStrength = atoi(value);
729 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
730 return;
731 } else if (!strcmp(name, "spotShadowStrength")) {
732 propertySpotShadowStrength = atoi(value);
733 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
734 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800735 }
736 ALOGD(" failed");
737}
738
Chet Haasedd78cca2010-10-22 18:59:26 -0700739}; // namespace uirenderer
740}; // namespace android