blob: f0bf7b22ff013344008cb8eb57291afe64246ff8 [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
69 glGenBuffers(1, &meshBuffer);
70 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
71 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
72
73 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080074 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080075 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070076 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070078 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080079
Romain Guy15bc6432011-12-13 13:11:32 -080080 mTexCoordsArrayEnabled = false;
81
Romain Guyb1d0a4e2012-07-13 18:25:35 -070082 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070083 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080084 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
85
Romain Guya1d3c912011-12-13 14:55:06 -080086 glActiveTexture(gTextureUnits[0]);
87 mTextureUnit = 0;
88
Romain Guy8ff6b9e2011-11-09 20:10:18 -080089 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070090 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080091 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080092 blend = false;
93 lastSrcMode = GL_ZERO;
94 lastDstMode = GL_ZERO;
95 currentProgram = NULL;
96
Romain Guy54c1a642012-09-27 17:55:46 -070097 mFunctorsCount = 0;
98
Romain Guyc2a97212013-02-06 15:29:46 -080099 debugLayersUpdates = false;
100 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800101 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800102
Romain Guy3b748a42013-04-17 18:54:38 -0700103 patchCache.init(*this);
104
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800105 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700106
Romain Guy8aa195d2013-06-04 18:00:09 -0700107 resetBoundTextures();
108
Romain Guy3b748a42013-04-17 18:54:38 -0700109 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800110}
111
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700112void Caches::initFont() {
113 fontRenderer = GammaFontRenderer::createRenderer();
114}
115
Romain Guydfa10462012-05-12 16:18:58 -0700116void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800117 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700118 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800119
Chris Craikff785832013-03-08 13:12:16 -0800120 startMark = glPushGroupMarkerEXT;
121 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700122 } else {
123 eventMark = eventMarkNull;
124 startMark = startMarkNull;
125 endMark = endMarkNull;
126 }
127
Romain Guy0f667532013-03-01 14:31:04 -0800128 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700129 setLabel = glLabelObjectEXT;
130 getLabel = glGetObjectLabelEXT;
131 } else {
132 setLabel = setLabelNull;
133 getLabel = getLabelNull;
134 }
135}
136
137void Caches::initConstraints() {
138 GLint maxTextureUnits;
139 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
140 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
141 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
142 }
143
144 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
145}
146
Romain Guyf9f00162013-05-09 11:50:12 -0700147void Caches::initStaticProperties() {
148 gpuPixelBuffersEnabled = false;
149
150 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700151 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700152 char property[PROPERTY_VALUE_MAX];
153 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
154 gpuPixelBuffersEnabled = !strcmp(property, "true");
155 }
156 }
157}
158
Romain Guy5bb3c732012-11-29 17:52:58 -0800159bool Caches::initProperties() {
160 bool prevDebugLayersUpdates = debugLayersUpdates;
161 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800162 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800163
Romain Guy4ff0cf42012-08-06 14:51:10 -0700164 char property[PROPERTY_VALUE_MAX];
165 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
166 INIT_LOGD(" Layers updates debug enabled: %s", property);
167 debugLayersUpdates = !strcmp(property, "true");
168 } else {
169 debugLayersUpdates = false;
170 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700171
Romain Guy627c6fd2013-08-21 11:53:18 -0700172 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700173 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
174 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700175 if (!strcmp(property, "show")) {
176 debugOverdraw = true;
177 mOverdrawDebugColorSet = kColorSet_Default;
178 } else if (!strcmp(property, "show_deuteranomaly")) {
179 debugOverdraw = true;
180 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
181 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700182 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800183
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800184 // See Properties.h for valid values
185 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
186 INIT_LOGD(" Stencil clip debug enabled: %s", property);
187 if (!strcmp(property, "hide")) {
188 debugStencilClip = kStencilHide;
189 } else if (!strcmp(property, "highlight")) {
190 debugStencilClip = kStencilShowHighlight;
191 } else if (!strcmp(property, "region")) {
192 debugStencilClip = kStencilShowRegion;
193 }
194 } else {
195 debugStencilClip = kStencilHide;
196 }
197
Romain Guy0f667532013-03-01 14:31:04 -0800198 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
199 drawDeferDisabled = !strcasecmp(property, "true");
200 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
201 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700202 drawDeferDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800203 INIT_LOGD(" Draw defer enabled");
204 }
205
206 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
207 drawReorderDisabled = !strcasecmp(property, "true");
208 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
209 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700210 drawReorderDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800211 INIT_LOGD(" Draw reorder enabled");
212 }
213
Romain Guy5bb3c732012-11-29 17:52:58 -0800214 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800215 (prevDebugOverdraw != debugOverdraw) ||
216 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700217}
218
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800219void Caches::terminate() {
220 if (!mInitialized) return;
221
222 glDeleteBuffers(1, &meshBuffer);
223 mCurrentBuffer = 0;
224
Romain Guy3b748a42013-04-17 18:54:38 -0700225 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700226 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700227 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800228 mRegionMesh = NULL;
229
ztenghui63d41ab2014-02-14 13:13:41 -0800230 glDeleteBuffers(1, &mShadowStripsIndices);
231 mShadowStripsIndices = 0;
232
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800233 fboCache.clear();
234
235 programCache.clear();
236 currentProgram = NULL;
237
Romain Guy3b748a42013-04-17 18:54:38 -0700238 assetAtlas.terminate();
239
240 patchCache.clear();
241
Romain Guy46bfc482013-08-16 18:38:29 -0700242 clearGarbage();
243
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800244 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700245}
246
247///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800248// Debug
249///////////////////////////////////////////////////////////////////////////////
250
Romain Guy627c6fd2013-08-21 11:53:18 -0700251uint32_t Caches::getOverdrawColor(uint32_t amount) const {
252 static uint32_t sOverdrawColors[2][4] = {
253 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
254 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
255 };
256 if (amount < 1) amount = 1;
257 if (amount > 4) amount = 4;
258 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
259}
260
Romain Guyc15008e2010-11-10 11:59:15 -0800261void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700262 String8 stringLog;
263 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000264 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700265}
266
267void Caches::dumpMemoryUsage(String8 &log) {
268 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
269 log.appendFormat(" TextureCache %8d / %8d\n",
270 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700271 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
272 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
273 log.appendFormat(" Garbage layers %8zu\n", mLayerGarbage.size());
274 log.appendFormat(" Active layers %8zu\n",
275 mRenderState ? mRenderState->mActiveLayers.size() : 0);
Romain Guy8d4aeb72013-02-12 16:08:55 -0800276 log.appendFormat(" RenderBufferCache %8d / %8d\n",
277 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700278 log.appendFormat(" GradientCache %8d / %8d\n",
279 gradientCache.getSize(), gradientCache.getMaxSize());
280 log.appendFormat(" PathCache %8d / %8d\n",
281 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700282 log.appendFormat(" TessellationCache %8d / %8d\n",
283 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700284 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800285 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700286 log.appendFormat(" PatchCache %8d / %8d\n",
287 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700288 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700289 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
290 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
291 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
292 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
293 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
294 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800295 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700296 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700297 log.appendFormat(" FboCache %8d / %8d\n",
298 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800299
300 uint32_t total = 0;
301 total += textureCache.getSize();
302 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800303 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800304 total += gradientCache.getSize();
305 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700306 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800307 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700308 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700309 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700310 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
311 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800312 }
313
Chet Haase9c1e23b2011-03-24 10:51:31 -0700314 log.appendFormat("Total memory usage:\n");
315 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800316}
317
318///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800319// Memory management
320///////////////////////////////////////////////////////////////////////////////
321
322void Caches::clearGarbage() {
323 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800324 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700325 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800326
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700327 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800328
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700329 { // scope for the lock
330 Mutex::Autolock _l(mGarbageLock);
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700331 layers = mLayerGarbage;
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700332 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800333 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800334
John Reck087bc0c2014-04-04 16:20:08 -0700335 size_t count = layers.size();
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700336 for (size_t i = 0; i < count; i++) {
337 Layer* layer = layers.itemAt(i);
338 delete layer;
339 }
340 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800341}
342
Romain Guyada830f2011-01-13 12:13:20 -0800343void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800344 Mutex::Autolock _l(mGarbageLock);
Chris Craikbfd1cd62014-09-10 13:04:31 -0700345 layer->state = Layer::kState_InGarbageList;
Romain Guyada830f2011-01-13 12:13:20 -0800346 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800347}
348
Romain Guybdf76092011-07-18 15:00:43 -0700349void Caches::flush(FlushMode mode) {
350 FLUSH_LOGD("Flushing caches (mode %d)", mode);
351
Romain Guyb0a41ed2013-08-16 14:44:38 -0700352 // We must stop tasks before clearing caches
353 if (mode > kFlushMode_Layers) {
354 tasks.stop();
355 }
356
Romain Guybdf76092011-07-18 15:00:43 -0700357 switch (mode) {
358 case kFlushMode_Full:
359 textureCache.clear();
360 patchCache.clear();
361 dropShadowCache.clear();
362 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700363 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700364 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700365 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700366 // fall through
367 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700368 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700369 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700370 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700371 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700372 // fall through
373 case kFlushMode_Layers:
374 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800375 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700376 break;
377 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700378
379 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700380 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700381}
382
Romain Guyfe48f652010-11-11 15:36:56 -0800383///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700384// VBO
385///////////////////////////////////////////////////////////////////////////////
386
Romain Guyf3a910b42011-12-12 20:35:21 -0800387bool Caches::bindMeshBuffer() {
388 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700389}
390
Romain Guyf3a910b42011-12-12 20:35:21 -0800391bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700392 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700393 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700394 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800395 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700396 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800397 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700398}
399
Romain Guyf3a910b42011-12-12 20:35:21 -0800400bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700401 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700402 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700403 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800404 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700405 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800406 return false;
407}
408
ztenghui63d41ab2014-02-14 13:13:41 -0800409bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800410 if (mCurrentIndicesBuffer != buffer) {
411 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
412 mCurrentIndicesBuffer = buffer;
413 return true;
414 }
415 return false;
416}
417
ztenghui63d41ab2014-02-14 13:13:41 -0800418bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700419 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700420 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
421 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700422 uint16_t quad = i * 4;
423 int index = i * 6;
424 regionIndices[index ] = quad; // top-left
425 regionIndices[index + 1] = quad + 1; // top-right
426 regionIndices[index + 2] = quad + 2; // bottom-left
427 regionIndices[index + 3] = quad + 2; // bottom-left
428 regionIndices[index + 4] = quad + 1; // top-right
429 regionIndices[index + 5] = quad + 3; // bottom-right
430 }
431
432 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800433 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700434 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700435 regionIndices, GL_STATIC_DRAW);
436
437 delete[] regionIndices;
438 return force;
439 }
440
ztenghui63d41ab2014-02-14 13:13:41 -0800441 return bindIndicesBufferInternal(mMeshIndices);
442}
443
444bool Caches::bindShadowIndicesBuffer() {
445 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700446 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800447 ShadowTessellator::generateShadowIndices(shadowIndices);
448 glGenBuffers(1, &mShadowStripsIndices);
449 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700450 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800451 shadowIndices, GL_STATIC_DRAW);
452
453 delete[] shadowIndices;
454 return force;
455 }
456
457 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700458}
459
Romain Guy15bc6432011-12-13 13:11:32 -0800460bool Caches::unbindIndicesBuffer() {
461 if (mCurrentIndicesBuffer) {
462 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
463 mCurrentIndicesBuffer = 0;
464 return true;
465 }
466 return false;
467}
468
Romain Guy85ef80d2012-09-13 20:26:50 -0700469///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700470// PBO
471///////////////////////////////////////////////////////////////////////////////
472
473bool Caches::bindPixelBuffer(const GLuint buffer) {
474 if (mCurrentPixelBuffer != buffer) {
475 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
476 mCurrentPixelBuffer = buffer;
477 return true;
478 }
479 return false;
480}
481
482bool Caches::unbindPixelBuffer() {
483 if (mCurrentPixelBuffer) {
484 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
485 mCurrentPixelBuffer = 0;
486 return true;
487 }
488 return false;
489}
490
491///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700492// Meshes and textures
493///////////////////////////////////////////////////////////////////////////////
494
ztenghui55bfb4e2013-12-03 10:38:55 -0800495void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700496 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
497 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800498 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
499 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700500 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800501 }
502}
503
ztenghui55bfb4e2013-12-03 10:38:55 -0800504void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800505 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700506 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800507 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800508 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800509 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800510 }
511}
512
513void Caches::resetVertexPointers() {
514 mCurrentPositionPointer = this;
515 mCurrentTexCoordsPointer = this;
516}
517
518void Caches::resetTexCoordsVertexPointer() {
519 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700520}
521
Romain Guy15bc6432011-12-13 13:11:32 -0800522void Caches::enableTexCoordsVertexArray() {
523 if (!mTexCoordsArrayEnabled) {
524 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800525 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800526 mTexCoordsArrayEnabled = true;
527 }
528}
529
Romain Guyff316ec2013-02-13 18:39:43 -0800530void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800531 if (mTexCoordsArrayEnabled) {
532 glDisableVertexAttribArray(Program::kBindingTexCoords);
533 mTexCoordsArrayEnabled = false;
534 }
535}
536
Romain Guya1d3c912011-12-13 14:55:06 -0800537void Caches::activeTexture(GLuint textureUnit) {
538 if (mTextureUnit != textureUnit) {
539 glActiveTexture(gTextureUnits[textureUnit]);
540 mTextureUnit = textureUnit;
541 }
542}
543
Chris Craik9ab2d182013-07-22 16:16:06 -0700544void Caches::resetActiveTexture() {
545 mTextureUnit = -1;
546}
547
Romain Guy8aa195d2013-06-04 18:00:09 -0700548void Caches::bindTexture(GLuint texture) {
549 if (mBoundTextures[mTextureUnit] != texture) {
550 glBindTexture(GL_TEXTURE_2D, texture);
551 mBoundTextures[mTextureUnit] = texture;
552 }
553}
554
555void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500556 if (target == GL_TEXTURE_2D) {
557 bindTexture(texture);
558 } else {
559 // GLConsumer directly calls glBindTexture() with
560 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
561 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700562 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700563 }
564}
565
Romain Guybe1b1272013-06-06 14:02:54 -0700566void Caches::deleteTexture(GLuint texture) {
567 // When glDeleteTextures() is called on a currently bound texture,
568 // OpenGL ES specifies that the texture is then considered unbound
569 // Consider the following series of calls:
570 //
571 // glGenTextures -> creates texture name 2
572 // glBindTexture(2)
573 // glDeleteTextures(2) -> 2 is now unbound
574 // glGenTextures -> can return 2 again
575 //
576 // If we don't call glBindTexture(2) after the second glGenTextures
577 // call, any texture operation will be performed on the default
578 // texture (name=0)
579
jiayuanr4a473c7d2014-06-10 17:41:49 +0800580 unbindTexture(texture);
581
Romain Guybe1b1272013-06-06 14:02:54 -0700582 glDeleteTextures(1, &texture);
583}
584
Romain Guy8aa195d2013-06-04 18:00:09 -0700585void Caches::resetBoundTextures() {
586 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
587}
588
jiayuanr4a473c7d2014-06-10 17:41:49 +0800589void Caches::unbindTexture(GLuint texture) {
590 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
591 if (mBoundTextures[i] == texture) {
592 mBoundTextures[i] = 0;
593 }
594 }
595}
596
Romain Guy85ef80d2012-09-13 20:26:50 -0700597///////////////////////////////////////////////////////////////////////////////
598// Scissor
599///////////////////////////////////////////////////////////////////////////////
600
Romain Guy8a4ac612012-07-17 17:32:48 -0700601bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700602 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
603 width != mScissorWidth || height != mScissorHeight)) {
604
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700605 if (x < 0) {
606 width += x;
607 x = 0;
608 }
609 if (y < 0) {
610 height += y;
611 y = 0;
612 }
613 if (width < 0) {
614 width = 0;
615 }
616 if (height < 0) {
617 height = 0;
618 }
Romain Guy8f85e802011-12-14 19:23:32 -0800619 glScissor(x, y, width, height);
620
621 mScissorX = x;
622 mScissorY = y;
623 mScissorWidth = width;
624 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700625
626 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800627 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700628 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800629}
630
Romain Guy8a4ac612012-07-17 17:32:48 -0700631bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700632 if (!scissorEnabled) {
633 glEnable(GL_SCISSOR_TEST);
634 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700635 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700636 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700637 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700638 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700639}
640
Romain Guy8a4ac612012-07-17 17:32:48 -0700641bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700642 if (scissorEnabled) {
643 glDisable(GL_SCISSOR_TEST);
644 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700645 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700646 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700647 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700648}
649
650void Caches::setScissorEnabled(bool enabled) {
651 if (scissorEnabled != enabled) {
652 if (enabled) glEnable(GL_SCISSOR_TEST);
653 else glDisable(GL_SCISSOR_TEST);
654 scissorEnabled = enabled;
655 }
656}
657
Romain Guy82bc7a72012-01-03 14:13:39 -0800658void Caches::resetScissor() {
659 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
660}
661
Romain Guy85ef80d2012-09-13 20:26:50 -0700662///////////////////////////////////////////////////////////////////////////////
663// Tiling
664///////////////////////////////////////////////////////////////////////////////
665
Romain Guyf735c8e2013-01-31 17:45:55 -0800666void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800667 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800668 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700669 }
670}
671
672void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800673 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700674 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700675 }
676}
677
Romain Guy54c1a642012-09-27 17:55:46 -0700678bool Caches::hasRegisteredFunctors() {
679 return mFunctorsCount > 0;
680}
681
682void Caches::registerFunctors(uint32_t functorCount) {
683 mFunctorsCount += functorCount;
684}
685
686void Caches::unregisterFunctors(uint32_t functorCount) {
687 if (functorCount > mFunctorsCount) {
688 mFunctorsCount = 0;
689 } else {
690 mFunctorsCount -= functorCount;
691 }
692}
693
Romain Guy85ef80d2012-09-13 20:26:50 -0700694///////////////////////////////////////////////////////////////////////////////
695// Regions
696///////////////////////////////////////////////////////////////////////////////
697
Romain Guy5b3b3522010-10-27 18:57:51 -0700698TextureVertex* Caches::getRegionMesh() {
699 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
700 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700701 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700702 }
703
704 return mRegionMesh;
705}
706
Chris Craikba9b6132013-12-15 17:10:19 -0800707///////////////////////////////////////////////////////////////////////////////
708// Temporary Properties
709///////////////////////////////////////////////////////////////////////////////
710
711void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700712 propertyLightDiameter = -1.0f;
713 propertyLightPosY = -1.0f;
714 propertyLightPosZ = -1.0f;
715 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700716 propertyAmbientShadowStrength = -1;
717 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800718}
719
720void Caches::setTempProperty(const char* name, const char* value) {
721 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700722 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700723 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
724 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800725 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700726 } else if (!strcmp(name, "lightDiameter")) {
727 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
728 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800729 return;
Chris Craik59744b72014-07-01 17:56:52 -0700730 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700731 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
732 ALOGD("lightPos Y = %.2f", propertyLightPosY);
733 return;
Chris Craik59744b72014-07-01 17:56:52 -0700734 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700735 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
736 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800737 return;
ztenghui14a4e352014-08-13 10:44:39 -0700738 } else if (!strcmp(name, "ambientShadowStrength")) {
739 propertyAmbientShadowStrength = atoi(value);
740 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
741 return;
742 } else if (!strcmp(name, "spotShadowStrength")) {
743 propertySpotShadowStrength = atoi(value);
744 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
745 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800746 }
747 ALOGD(" failed");
748}
749
Chet Haasedd78cca2010-10-22 18:59:26 -0700750}; // namespace uirenderer
751}; // namespace android