blob: 107eb08b2ca6f64c6e7b06b11acb6159b68a5cc3 [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>(),
Chris Craikd41c4d82015-01-05 15:51:13 -080052 mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(nullptr) {
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
Chris Craikd41c4d82015-01-05 15:51:13 -080090 mRegionMesh = nullptr;
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;
Chris Craikd41c4d82015-01-05 15:51:13 -080096 currentProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080097
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];
Chris Craikd41c4d82015-01-05 15:51:13 -0800166 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, nullptr) > 0) {
Romain Guy4ff0cf42012-08-06 14:51:10 -0700167 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;
Chris Craikd41c4d82015-01-05 15:51:13 -0800174 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, nullptr) > 0) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700175 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
Chris Craikd41c4d82015-01-05 15:51:13 -0800186 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, nullptr) > 0) {
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800187 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 Guy3b748a42013-04-17 18:54:38 -0700227 mMeshIndices = 0;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800228 mRegionMesh.release();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800229
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();
Chris Craikd41c4d82015-01-05 15:51:13 -0800236 currentProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800237
Romain Guy3b748a42013-04-17 18:54:38 -0700238 patchCache.clear();
239
Romain Guy46bfc482013-08-16 18:38:29 -0700240 clearGarbage();
241
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800242 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700243}
244
245///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800246// Debug
247///////////////////////////////////////////////////////////////////////////////
248
Romain Guy627c6fd2013-08-21 11:53:18 -0700249uint32_t Caches::getOverdrawColor(uint32_t amount) const {
250 static uint32_t sOverdrawColors[2][4] = {
251 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
252 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
253 };
254 if (amount < 1) amount = 1;
255 if (amount > 4) amount = 4;
256 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
257}
258
Romain Guyc15008e2010-11-10 11:59:15 -0800259void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700260 String8 stringLog;
261 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000262 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700263}
264
265void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700266 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700267 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
268 log.appendFormat(" TextureCache %8d / %8d\n",
269 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700270 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
271 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700272 if (mRenderState) {
273 int memused = 0;
274 for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin();
275 it != mRenderState->mActiveLayers.end(); it++) {
276 const Layer* layer = *it;
277 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
278 layer->getWidth(), layer->getHeight(),
279 layer->isTextureLayer(), layer->getTexture(),
280 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800281 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700282 }
283 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
284 memused, mRenderState->mActiveLayers.size());
285 total += memused;
286 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800287 log.appendFormat(" RenderBufferCache %8d / %8d\n",
288 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700289 log.appendFormat(" GradientCache %8d / %8d\n",
290 gradientCache.getSize(), gradientCache.getMaxSize());
291 log.appendFormat(" PathCache %8d / %8d\n",
292 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700293 log.appendFormat(" TessellationCache %8d / %8d\n",
294 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700295 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800296 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700297 log.appendFormat(" PatchCache %8d / %8d\n",
298 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700299 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700300 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
301 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
302 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
303 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
304 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
305 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800306 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700307 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700308 log.appendFormat(" FboCache %8d / %8d\n",
309 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800310
Romain Guyc15008e2010-11-10 11:59:15 -0800311 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800312 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800313 total += gradientCache.getSize();
314 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700315 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800316 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700317 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700318 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700319 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
320 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800321 }
322
Chet Haase9c1e23b2011-03-24 10:51:31 -0700323 log.appendFormat("Total memory usage:\n");
324 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800325}
326
327///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800328// Memory management
329///////////////////////////////////////////////////////////////////////////////
330
331void Caches::clearGarbage() {
332 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800333 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700334 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800335}
336
Romain Guybdf76092011-07-18 15:00:43 -0700337void Caches::flush(FlushMode mode) {
338 FLUSH_LOGD("Flushing caches (mode %d)", mode);
339
Romain Guyb0a41ed2013-08-16 14:44:38 -0700340 // We must stop tasks before clearing caches
341 if (mode > kFlushMode_Layers) {
342 tasks.stop();
343 }
344
Romain Guybdf76092011-07-18 15:00:43 -0700345 switch (mode) {
346 case kFlushMode_Full:
347 textureCache.clear();
348 patchCache.clear();
349 dropShadowCache.clear();
350 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700351 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700352 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700353 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700354 // fall through
355 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700356 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700357 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700358 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700359 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700360 // fall through
361 case kFlushMode_Layers:
362 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800363 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700364 break;
365 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700366
367 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700368 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700369}
370
Romain Guyfe48f652010-11-11 15:36:56 -0800371///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700372// VBO
373///////////////////////////////////////////////////////////////////////////////
374
Romain Guyf3a910b42011-12-12 20:35:21 -0800375bool Caches::bindMeshBuffer() {
376 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700377}
378
Romain Guyf3a910b42011-12-12 20:35:21 -0800379bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700380 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700381 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700382 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800383 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700384 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800385 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700386}
387
Romain Guyf3a910b42011-12-12 20:35:21 -0800388bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700389 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700390 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700391 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800392 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700393 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800394 return false;
395}
396
ztenghui63d41ab2014-02-14 13:13:41 -0800397bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800398 if (mCurrentIndicesBuffer != buffer) {
399 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
400 mCurrentIndicesBuffer = buffer;
401 return true;
402 }
403 return false;
404}
405
ztenghui63d41ab2014-02-14 13:13:41 -0800406bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700407 if (!mMeshIndices) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800408 std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[gMaxNumberOfQuads * 6]);
Romain Guy31e08e92013-06-18 15:53:53 -0700409 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700410 uint16_t quad = i * 4;
411 int index = i * 6;
412 regionIndices[index ] = quad; // top-left
413 regionIndices[index + 1] = quad + 1; // top-right
414 regionIndices[index + 2] = quad + 2; // bottom-left
415 regionIndices[index + 3] = quad + 2; // bottom-left
416 regionIndices[index + 4] = quad + 1; // top-right
417 regionIndices[index + 5] = quad + 3; // bottom-right
418 }
419
420 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800421 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700422 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Chris Craik51d6a3d2014-12-22 17:16:56 -0800423 regionIndices.get(), GL_STATIC_DRAW);
Romain Guy3b748a42013-04-17 18:54:38 -0700424 return force;
425 }
426
ztenghui63d41ab2014-02-14 13:13:41 -0800427 return bindIndicesBufferInternal(mMeshIndices);
428}
429
430bool Caches::bindShadowIndicesBuffer() {
431 if (!mShadowStripsIndices) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800432 std::unique_ptr<uint16_t[]> shadowIndices(new uint16_t[MAX_SHADOW_INDEX_COUNT]);
433 ShadowTessellator::generateShadowIndices(shadowIndices.get());
ztenghui63d41ab2014-02-14 13:13:41 -0800434 glGenBuffers(1, &mShadowStripsIndices);
435 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700436 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
Chris Craik51d6a3d2014-12-22 17:16:56 -0800437 shadowIndices.get(), GL_STATIC_DRAW);
ztenghui63d41ab2014-02-14 13:13:41 -0800438 return force;
439 }
440
441 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700442}
443
Romain Guy15bc6432011-12-13 13:11:32 -0800444bool Caches::unbindIndicesBuffer() {
445 if (mCurrentIndicesBuffer) {
446 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
447 mCurrentIndicesBuffer = 0;
448 return true;
449 }
450 return false;
451}
452
Romain Guy85ef80d2012-09-13 20:26:50 -0700453///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700454// PBO
455///////////////////////////////////////////////////////////////////////////////
456
457bool Caches::bindPixelBuffer(const GLuint buffer) {
458 if (mCurrentPixelBuffer != buffer) {
459 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
460 mCurrentPixelBuffer = buffer;
461 return true;
462 }
463 return false;
464}
465
466bool Caches::unbindPixelBuffer() {
467 if (mCurrentPixelBuffer) {
468 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
469 mCurrentPixelBuffer = 0;
470 return true;
471 }
472 return false;
473}
474
475///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700476// Meshes and textures
477///////////////////////////////////////////////////////////////////////////////
478
ztenghui55bfb4e2013-12-03 10:38:55 -0800479void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700480 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
481 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800482 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
483 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700484 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800485 }
486}
487
ztenghui55bfb4e2013-12-03 10:38:55 -0800488void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800489 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700490 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800491 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800492 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800493 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800494 }
495}
496
497void Caches::resetVertexPointers() {
498 mCurrentPositionPointer = this;
499 mCurrentTexCoordsPointer = this;
500}
501
502void Caches::resetTexCoordsVertexPointer() {
503 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700504}
505
Romain Guy15bc6432011-12-13 13:11:32 -0800506void Caches::enableTexCoordsVertexArray() {
507 if (!mTexCoordsArrayEnabled) {
508 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800509 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800510 mTexCoordsArrayEnabled = true;
511 }
512}
513
Romain Guyff316ec2013-02-13 18:39:43 -0800514void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800515 if (mTexCoordsArrayEnabled) {
516 glDisableVertexAttribArray(Program::kBindingTexCoords);
517 mTexCoordsArrayEnabled = false;
518 }
519}
520
Romain Guya1d3c912011-12-13 14:55:06 -0800521void Caches::activeTexture(GLuint textureUnit) {
522 if (mTextureUnit != textureUnit) {
523 glActiveTexture(gTextureUnits[textureUnit]);
524 mTextureUnit = textureUnit;
525 }
526}
527
Chris Craik9ab2d182013-07-22 16:16:06 -0700528void Caches::resetActiveTexture() {
529 mTextureUnit = -1;
530}
531
Romain Guy8aa195d2013-06-04 18:00:09 -0700532void Caches::bindTexture(GLuint texture) {
533 if (mBoundTextures[mTextureUnit] != texture) {
534 glBindTexture(GL_TEXTURE_2D, texture);
535 mBoundTextures[mTextureUnit] = texture;
536 }
537}
538
539void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500540 if (target == GL_TEXTURE_2D) {
541 bindTexture(texture);
542 } else {
543 // GLConsumer directly calls glBindTexture() with
544 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
545 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700546 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700547 }
548}
549
Romain Guybe1b1272013-06-06 14:02:54 -0700550void Caches::deleteTexture(GLuint texture) {
551 // When glDeleteTextures() is called on a currently bound texture,
552 // OpenGL ES specifies that the texture is then considered unbound
553 // Consider the following series of calls:
554 //
555 // glGenTextures -> creates texture name 2
556 // glBindTexture(2)
557 // glDeleteTextures(2) -> 2 is now unbound
558 // glGenTextures -> can return 2 again
559 //
560 // If we don't call glBindTexture(2) after the second glGenTextures
561 // call, any texture operation will be performed on the default
562 // texture (name=0)
563
jiayuanr4a473c7d2014-06-10 17:41:49 +0800564 unbindTexture(texture);
565
Romain Guybe1b1272013-06-06 14:02:54 -0700566 glDeleteTextures(1, &texture);
567}
568
Romain Guy8aa195d2013-06-04 18:00:09 -0700569void Caches::resetBoundTextures() {
570 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
571}
572
jiayuanr4a473c7d2014-06-10 17:41:49 +0800573void Caches::unbindTexture(GLuint texture) {
574 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
575 if (mBoundTextures[i] == texture) {
576 mBoundTextures[i] = 0;
577 }
578 }
579}
580
Romain Guy85ef80d2012-09-13 20:26:50 -0700581///////////////////////////////////////////////////////////////////////////////
582// Scissor
583///////////////////////////////////////////////////////////////////////////////
584
Romain Guy8a4ac612012-07-17 17:32:48 -0700585bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700586 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
587 width != mScissorWidth || height != mScissorHeight)) {
588
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700589 if (x < 0) {
590 width += x;
591 x = 0;
592 }
593 if (y < 0) {
594 height += y;
595 y = 0;
596 }
597 if (width < 0) {
598 width = 0;
599 }
600 if (height < 0) {
601 height = 0;
602 }
Romain Guy8f85e802011-12-14 19:23:32 -0800603 glScissor(x, y, width, height);
604
605 mScissorX = x;
606 mScissorY = y;
607 mScissorWidth = width;
608 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700609
610 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800611 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700612 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800613}
614
Romain Guy8a4ac612012-07-17 17:32:48 -0700615bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700616 if (!scissorEnabled) {
617 glEnable(GL_SCISSOR_TEST);
618 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700619 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700620 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700621 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700622 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700623}
624
Romain Guy8a4ac612012-07-17 17:32:48 -0700625bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700626 if (scissorEnabled) {
627 glDisable(GL_SCISSOR_TEST);
628 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700629 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700630 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700631 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700632}
633
634void Caches::setScissorEnabled(bool enabled) {
635 if (scissorEnabled != enabled) {
636 if (enabled) glEnable(GL_SCISSOR_TEST);
637 else glDisable(GL_SCISSOR_TEST);
638 scissorEnabled = enabled;
639 }
640}
641
Romain Guy82bc7a72012-01-03 14:13:39 -0800642void Caches::resetScissor() {
643 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
644}
645
Romain Guy85ef80d2012-09-13 20:26:50 -0700646///////////////////////////////////////////////////////////////////////////////
647// Tiling
648///////////////////////////////////////////////////////////////////////////////
649
Romain Guyf735c8e2013-01-31 17:45:55 -0800650void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800651 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800652 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700653 }
654}
655
656void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800657 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700658 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700659 }
660}
661
Romain Guy54c1a642012-09-27 17:55:46 -0700662bool Caches::hasRegisteredFunctors() {
663 return mFunctorsCount > 0;
664}
665
666void Caches::registerFunctors(uint32_t functorCount) {
667 mFunctorsCount += functorCount;
668}
669
670void Caches::unregisterFunctors(uint32_t functorCount) {
671 if (functorCount > mFunctorsCount) {
672 mFunctorsCount = 0;
673 } else {
674 mFunctorsCount -= functorCount;
675 }
676}
677
Romain Guy85ef80d2012-09-13 20:26:50 -0700678///////////////////////////////////////////////////////////////////////////////
679// Regions
680///////////////////////////////////////////////////////////////////////////////
681
Romain Guy5b3b3522010-10-27 18:57:51 -0700682TextureVertex* Caches::getRegionMesh() {
683 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
684 if (!mRegionMesh) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800685 mRegionMesh.reset(new TextureVertex[gMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700686 }
687
Chris Craik51d6a3d2014-12-22 17:16:56 -0800688 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700689}
690
Chris Craikba9b6132013-12-15 17:10:19 -0800691///////////////////////////////////////////////////////////////////////////////
692// Temporary Properties
693///////////////////////////////////////////////////////////////////////////////
694
695void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700696 propertyLightDiameter = -1.0f;
697 propertyLightPosY = -1.0f;
698 propertyLightPosZ = -1.0f;
699 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700700 propertyAmbientShadowStrength = -1;
701 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800702}
703
704void Caches::setTempProperty(const char* name, const char* value) {
705 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700706 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700707 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
708 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800709 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700710 } else if (!strcmp(name, "lightDiameter")) {
711 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
712 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800713 return;
Chris Craik59744b72014-07-01 17:56:52 -0700714 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700715 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
716 ALOGD("lightPos Y = %.2f", propertyLightPosY);
717 return;
Chris Craik59744b72014-07-01 17:56:52 -0700718 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700719 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
720 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800721 return;
ztenghui14a4e352014-08-13 10:44:39 -0700722 } else if (!strcmp(name, "ambientShadowStrength")) {
723 propertyAmbientShadowStrength = atoi(value);
724 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
725 return;
726 } else if (!strcmp(name, "spotShadowStrength")) {
727 propertySpotShadowStrength = atoi(value);
728 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
729 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800730 }
731 ALOGD(" failed");
732}
733
Chet Haasedd78cca2010-10-22 18:59:26 -0700734}; // namespace uirenderer
735}; // namespace android