blob: cdf81508d7c4c77e9ded50428b788c9fb2f051ea [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 patchCache.clear();
241
Romain Guy46bfc482013-08-16 18:38:29 -0700242 clearGarbage();
243
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800244 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700245}
246
247///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800248// Debug
249///////////////////////////////////////////////////////////////////////////////
250
Romain Guy627c6fd2013-08-21 11:53:18 -0700251uint32_t Caches::getOverdrawColor(uint32_t amount) const {
252 static uint32_t sOverdrawColors[2][4] = {
253 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
254 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
255 };
256 if (amount < 1) amount = 1;
257 if (amount > 4) amount = 4;
258 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
259}
260
Romain Guyc15008e2010-11-10 11:59:15 -0800261void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700262 String8 stringLog;
263 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000264 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700265}
266
267void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700268 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700269 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
270 log.appendFormat(" TextureCache %8d / %8d\n",
271 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700272 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
273 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700274 if (mRenderState) {
275 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800276 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700277 it != mRenderState->mActiveLayers.end(); it++) {
278 const Layer* layer = *it;
279 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
280 layer->getWidth(), layer->getHeight(),
281 layer->isTextureLayer(), layer->getTexture(),
282 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800283 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700284 }
285 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
286 memused, mRenderState->mActiveLayers.size());
287 total += memused;
288 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800289 log.appendFormat(" RenderBufferCache %8d / %8d\n",
290 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700291 log.appendFormat(" GradientCache %8d / %8d\n",
292 gradientCache.getSize(), gradientCache.getMaxSize());
293 log.appendFormat(" PathCache %8d / %8d\n",
294 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700295 log.appendFormat(" TessellationCache %8d / %8d\n",
296 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700297 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800298 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700299 log.appendFormat(" PatchCache %8d / %8d\n",
300 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700301 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700302 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
303 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
304 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
305 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
306 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
307 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800308 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700309 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700310 log.appendFormat(" FboCache %8d / %8d\n",
311 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800312
Romain Guyc15008e2010-11-10 11:59:15 -0800313 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800314 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800315 total += gradientCache.getSize();
316 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700317 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800318 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700319 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700320 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700321 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
322 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800323 }
324
Chet Haase9c1e23b2011-03-24 10:51:31 -0700325 log.appendFormat("Total memory usage:\n");
326 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800327}
328
329///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800330// Memory management
331///////////////////////////////////////////////////////////////////////////////
332
333void Caches::clearGarbage() {
334 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800335 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700336 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800337}
338
Romain Guybdf76092011-07-18 15:00:43 -0700339void Caches::flush(FlushMode mode) {
340 FLUSH_LOGD("Flushing caches (mode %d)", mode);
341
Romain Guyb0a41ed2013-08-16 14:44:38 -0700342 // We must stop tasks before clearing caches
343 if (mode > kFlushMode_Layers) {
344 tasks.stop();
345 }
346
Romain Guybdf76092011-07-18 15:00:43 -0700347 switch (mode) {
348 case kFlushMode_Full:
349 textureCache.clear();
350 patchCache.clear();
351 dropShadowCache.clear();
352 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700353 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700354 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700355 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700356 // fall through
357 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700358 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700359 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700360 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700361 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700362 // fall through
363 case kFlushMode_Layers:
364 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800365 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700366 break;
367 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700368
369 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700370 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700371}
372
Romain Guyfe48f652010-11-11 15:36:56 -0800373///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700374// VBO
375///////////////////////////////////////////////////////////////////////////////
376
Romain Guyf3a910b42011-12-12 20:35:21 -0800377bool Caches::bindMeshBuffer() {
378 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700379}
380
Romain Guyf3a910b42011-12-12 20:35:21 -0800381bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700382 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700383 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700384 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800385 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700386 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800387 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700388}
389
Romain Guyf3a910b42011-12-12 20:35:21 -0800390bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700391 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700392 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700393 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800394 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700395 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800396 return false;
397}
398
ztenghui63d41ab2014-02-14 13:13:41 -0800399bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800400 if (mCurrentIndicesBuffer != buffer) {
401 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
402 mCurrentIndicesBuffer = buffer;
403 return true;
404 }
405 return false;
406}
407
ztenghui63d41ab2014-02-14 13:13:41 -0800408bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700409 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700410 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
411 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700412 uint16_t quad = i * 4;
413 int index = i * 6;
414 regionIndices[index ] = quad; // top-left
415 regionIndices[index + 1] = quad + 1; // top-right
416 regionIndices[index + 2] = quad + 2; // bottom-left
417 regionIndices[index + 3] = quad + 2; // bottom-left
418 regionIndices[index + 4] = quad + 1; // top-right
419 regionIndices[index + 5] = quad + 3; // bottom-right
420 }
421
422 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800423 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700424 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700425 regionIndices, GL_STATIC_DRAW);
426
427 delete[] regionIndices;
428 return force;
429 }
430
ztenghui63d41ab2014-02-14 13:13:41 -0800431 return bindIndicesBufferInternal(mMeshIndices);
432}
433
434bool Caches::bindShadowIndicesBuffer() {
435 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700436 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800437 ShadowTessellator::generateShadowIndices(shadowIndices);
438 glGenBuffers(1, &mShadowStripsIndices);
439 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700440 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800441 shadowIndices, GL_STATIC_DRAW);
442
443 delete[] shadowIndices;
444 return force;
445 }
446
447 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700448}
449
Romain Guy15bc6432011-12-13 13:11:32 -0800450bool Caches::unbindIndicesBuffer() {
451 if (mCurrentIndicesBuffer) {
452 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
453 mCurrentIndicesBuffer = 0;
454 return true;
455 }
456 return false;
457}
458
Romain Guy85ef80d2012-09-13 20:26:50 -0700459///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700460// PBO
461///////////////////////////////////////////////////////////////////////////////
462
463bool Caches::bindPixelBuffer(const GLuint buffer) {
464 if (mCurrentPixelBuffer != buffer) {
465 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
466 mCurrentPixelBuffer = buffer;
467 return true;
468 }
469 return false;
470}
471
472bool Caches::unbindPixelBuffer() {
473 if (mCurrentPixelBuffer) {
474 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
475 mCurrentPixelBuffer = 0;
476 return true;
477 }
478 return false;
479}
480
481///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700482// Meshes and textures
483///////////////////////////////////////////////////////////////////////////////
484
ztenghui55bfb4e2013-12-03 10:38:55 -0800485void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700486 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
487 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800488 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
489 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700490 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800491 }
492}
493
ztenghui55bfb4e2013-12-03 10:38:55 -0800494void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800495 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700496 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800497 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800498 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800499 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800500 }
501}
502
503void Caches::resetVertexPointers() {
504 mCurrentPositionPointer = this;
505 mCurrentTexCoordsPointer = this;
506}
507
508void Caches::resetTexCoordsVertexPointer() {
509 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700510}
511
Romain Guy15bc6432011-12-13 13:11:32 -0800512void Caches::enableTexCoordsVertexArray() {
513 if (!mTexCoordsArrayEnabled) {
514 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800515 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800516 mTexCoordsArrayEnabled = true;
517 }
518}
519
Romain Guyff316ec2013-02-13 18:39:43 -0800520void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800521 if (mTexCoordsArrayEnabled) {
522 glDisableVertexAttribArray(Program::kBindingTexCoords);
523 mTexCoordsArrayEnabled = false;
524 }
525}
526
Romain Guya1d3c912011-12-13 14:55:06 -0800527void Caches::activeTexture(GLuint textureUnit) {
528 if (mTextureUnit != textureUnit) {
529 glActiveTexture(gTextureUnits[textureUnit]);
530 mTextureUnit = textureUnit;
531 }
532}
533
Chris Craik9ab2d182013-07-22 16:16:06 -0700534void Caches::resetActiveTexture() {
535 mTextureUnit = -1;
536}
537
Romain Guy8aa195d2013-06-04 18:00:09 -0700538void Caches::bindTexture(GLuint texture) {
539 if (mBoundTextures[mTextureUnit] != texture) {
540 glBindTexture(GL_TEXTURE_2D, texture);
541 mBoundTextures[mTextureUnit] = texture;
542 }
543}
544
545void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500546 if (target == GL_TEXTURE_2D) {
547 bindTexture(texture);
548 } else {
549 // GLConsumer directly calls glBindTexture() with
550 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
551 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700552 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700553 }
554}
555
Romain Guybe1b1272013-06-06 14:02:54 -0700556void Caches::deleteTexture(GLuint texture) {
557 // When glDeleteTextures() is called on a currently bound texture,
558 // OpenGL ES specifies that the texture is then considered unbound
559 // Consider the following series of calls:
560 //
561 // glGenTextures -> creates texture name 2
562 // glBindTexture(2)
563 // glDeleteTextures(2) -> 2 is now unbound
564 // glGenTextures -> can return 2 again
565 //
566 // If we don't call glBindTexture(2) after the second glGenTextures
567 // call, any texture operation will be performed on the default
568 // texture (name=0)
569
jiayuanr4a473c7d2014-06-10 17:41:49 +0800570 unbindTexture(texture);
571
Romain Guybe1b1272013-06-06 14:02:54 -0700572 glDeleteTextures(1, &texture);
573}
574
Romain Guy8aa195d2013-06-04 18:00:09 -0700575void Caches::resetBoundTextures() {
576 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
577}
578
jiayuanr4a473c7d2014-06-10 17:41:49 +0800579void Caches::unbindTexture(GLuint texture) {
580 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
581 if (mBoundTextures[i] == texture) {
582 mBoundTextures[i] = 0;
583 }
584 }
585}
586
Romain Guy85ef80d2012-09-13 20:26:50 -0700587///////////////////////////////////////////////////////////////////////////////
588// Scissor
589///////////////////////////////////////////////////////////////////////////////
590
Romain Guy8a4ac612012-07-17 17:32:48 -0700591bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700592 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
593 width != mScissorWidth || height != mScissorHeight)) {
594
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700595 if (x < 0) {
596 width += x;
597 x = 0;
598 }
599 if (y < 0) {
600 height += y;
601 y = 0;
602 }
603 if (width < 0) {
604 width = 0;
605 }
606 if (height < 0) {
607 height = 0;
608 }
Romain Guy8f85e802011-12-14 19:23:32 -0800609 glScissor(x, y, width, height);
610
611 mScissorX = x;
612 mScissorY = y;
613 mScissorWidth = width;
614 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700615
616 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800617 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700618 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800619}
620
Romain Guy8a4ac612012-07-17 17:32:48 -0700621bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700622 if (!scissorEnabled) {
623 glEnable(GL_SCISSOR_TEST);
624 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700625 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700626 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700627 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700628 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700629}
630
Romain Guy8a4ac612012-07-17 17:32:48 -0700631bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700632 if (scissorEnabled) {
633 glDisable(GL_SCISSOR_TEST);
634 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700635 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700636 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700637 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700638}
639
640void Caches::setScissorEnabled(bool enabled) {
641 if (scissorEnabled != enabled) {
642 if (enabled) glEnable(GL_SCISSOR_TEST);
643 else glDisable(GL_SCISSOR_TEST);
644 scissorEnabled = enabled;
645 }
646}
647
Romain Guy82bc7a72012-01-03 14:13:39 -0800648void Caches::resetScissor() {
649 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
650}
651
Romain Guy85ef80d2012-09-13 20:26:50 -0700652///////////////////////////////////////////////////////////////////////////////
653// Tiling
654///////////////////////////////////////////////////////////////////////////////
655
Romain Guyf735c8e2013-01-31 17:45:55 -0800656void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800657 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800658 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700659 }
660}
661
662void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800663 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700664 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700665 }
666}
667
Romain Guy54c1a642012-09-27 17:55:46 -0700668bool Caches::hasRegisteredFunctors() {
669 return mFunctorsCount > 0;
670}
671
672void Caches::registerFunctors(uint32_t functorCount) {
673 mFunctorsCount += functorCount;
674}
675
676void Caches::unregisterFunctors(uint32_t functorCount) {
677 if (functorCount > mFunctorsCount) {
678 mFunctorsCount = 0;
679 } else {
680 mFunctorsCount -= functorCount;
681 }
682}
683
Romain Guy85ef80d2012-09-13 20:26:50 -0700684///////////////////////////////////////////////////////////////////////////////
685// Regions
686///////////////////////////////////////////////////////////////////////////////
687
Romain Guy5b3b3522010-10-27 18:57:51 -0700688TextureVertex* Caches::getRegionMesh() {
689 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
690 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700691 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700692 }
693
694 return mRegionMesh;
695}
696
Chris Craikba9b6132013-12-15 17:10:19 -0800697///////////////////////////////////////////////////////////////////////////////
698// Temporary Properties
699///////////////////////////////////////////////////////////////////////////////
700
701void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700702 propertyLightDiameter = -1.0f;
703 propertyLightPosY = -1.0f;
704 propertyLightPosZ = -1.0f;
705 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700706 propertyAmbientShadowStrength = -1;
707 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800708}
709
710void Caches::setTempProperty(const char* name, const char* value) {
711 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700712 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700713 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
714 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800715 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700716 } else if (!strcmp(name, "lightDiameter")) {
717 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
718 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800719 return;
Chris Craik59744b72014-07-01 17:56:52 -0700720 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700721 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
722 ALOGD("lightPos Y = %.2f", propertyLightPosY);
723 return;
Chris Craik59744b72014-07-01 17:56:52 -0700724 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700725 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
726 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800727 return;
ztenghui14a4e352014-08-13 10:44:39 -0700728 } else if (!strcmp(name, "ambientShadowStrength")) {
729 propertyAmbientShadowStrength = atoi(value);
730 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
731 return;
732 } else if (!strcmp(name, "spotShadowStrength")) {
733 propertySpotShadowStrength = atoi(value);
734 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
735 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800736 }
737 ALOGD(" failed");
738}
739
Chet Haasedd78cca2010-10-22 18:59:26 -0700740}; // namespace uirenderer
741}; // namespace android