blob: 478a3ec1970ee9bf7bf3ddf5433d2ef1a9a5ae8a [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
32#ifdef USE_OPENGL_RENDERER
33using namespace uirenderer;
34ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
35#endif
36
37namespace uirenderer {
38
39///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070040// Macros
41///////////////////////////////////////////////////////////////////////////////
42
43#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000044 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070045#else
46 #define FLUSH_LOGD(...)
47#endif
48
49///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070050// Constructors/destructor
51///////////////////////////////////////////////////////////////////////////////
52
Romain Guy8aa195d2013-06-04 18:00:09 -070053Caches::Caches(): Singleton<Caches>(),
John Reck17035b02014-09-03 07:39:53 -070054 mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(NULL) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080055 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070056 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070057 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070058 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070059 initStaticProperties();
Romain Guy0f6675332013-03-01 14:31:04 -080060 initExtensions();
Chris Craikba9b6132013-12-15 17:10:19 -080061 initTempProperties();
Romain Guye190aa62010-11-10 19:01:29 -080062
63 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000064 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070065}
66
Romain Guy3b748a42013-04-17 18:54:38 -070067bool Caches::init() {
68 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080069
70 glGenBuffers(1, &meshBuffer);
71 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
72 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
73
74 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080075 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080076 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070077 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080078 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070079 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080080
Romain Guy15bc6432011-12-13 13:11:32 -080081 mTexCoordsArrayEnabled = false;
82
Romain Guyb1d0a4e2012-07-13 18:25:35 -070083 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070084 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080085 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
86
Romain Guya1d3c912011-12-13 14:55:06 -080087 glActiveTexture(gTextureUnits[0]);
88 mTextureUnit = 0;
89
Romain Guy8ff6b9e2011-11-09 20:10:18 -080090 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070091 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080092 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080093 blend = false;
94 lastSrcMode = GL_ZERO;
95 lastDstMode = GL_ZERO;
96 currentProgram = NULL;
97
Romain Guy54c1a642012-09-27 17:55:46 -070098 mFunctorsCount = 0;
99
Romain Guyc2a97212013-02-06 15:29:46 -0800100 debugLayersUpdates = false;
101 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800102 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800103
Romain Guy3b748a42013-04-17 18:54:38 -0700104 patchCache.init(*this);
105
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800106 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700107
Romain Guy8aa195d2013-06-04 18:00:09 -0700108 resetBoundTextures();
109
Romain Guy3b748a42013-04-17 18:54:38 -0700110 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800111}
112
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700113void Caches::initFont() {
114 fontRenderer = GammaFontRenderer::createRenderer();
115}
116
Romain Guydfa10462012-05-12 16:18:58 -0700117void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800118 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700119 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-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 Guy0f6675332013-03-01 14:31:04 -0800129 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700130 setLabel = glLabelObjectEXT;
131 getLabel = glGetObjectLabelEXT;
132 } else {
133 setLabel = setLabelNull;
134 getLabel = getLabelNull;
135 }
136}
137
138void Caches::initConstraints() {
139 GLint maxTextureUnits;
140 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
141 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
142 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
143 }
144
145 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
146}
147
Romain Guyf9f00162013-05-09 11:50:12 -0700148void Caches::initStaticProperties() {
149 gpuPixelBuffersEnabled = false;
150
151 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700152 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700153 char property[PROPERTY_VALUE_MAX];
154 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
155 gpuPixelBuffersEnabled = !strcmp(property, "true");
156 }
157 }
158}
159
Romain Guy5bb3c732012-11-29 17:52:58 -0800160bool Caches::initProperties() {
161 bool prevDebugLayersUpdates = debugLayersUpdates;
162 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800163 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800164
Romain Guy4ff0cf42012-08-06 14:51:10 -0700165 char property[PROPERTY_VALUE_MAX];
166 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
167 INIT_LOGD(" Layers updates debug enabled: %s", property);
168 debugLayersUpdates = !strcmp(property, "true");
169 } else {
170 debugLayersUpdates = false;
171 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700172
Romain Guy627c6fd2013-08-21 11:53:18 -0700173 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700174 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
175 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700176 if (!strcmp(property, "show")) {
177 debugOverdraw = true;
178 mOverdrawDebugColorSet = kColorSet_Default;
179 } else if (!strcmp(property, "show_deuteranomaly")) {
180 debugOverdraw = true;
181 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
182 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700183 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800184
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800185 // See Properties.h for valid values
186 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
187 INIT_LOGD(" Stencil clip debug enabled: %s", property);
188 if (!strcmp(property, "hide")) {
189 debugStencilClip = kStencilHide;
190 } else if (!strcmp(property, "highlight")) {
191 debugStencilClip = kStencilShowHighlight;
192 } else if (!strcmp(property, "region")) {
193 debugStencilClip = kStencilShowRegion;
194 }
195 } else {
196 debugStencilClip = kStencilHide;
197 }
198
Romain Guy0f6675332013-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 Guy0f6675332013-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 Guy0f6675332013-03-01 14:31:04 -0800212 INIT_LOGD(" Draw reorder enabled");
213 }
214
Romain Guy5bb3c732012-11-29 17:52:58 -0800215 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800216 (prevDebugOverdraw != debugOverdraw) ||
217 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700218}
219
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800220void Caches::terminate() {
221 if (!mInitialized) return;
222
223 glDeleteBuffers(1, &meshBuffer);
224 mCurrentBuffer = 0;
225
Romain Guy3b748a42013-04-17 18:54:38 -0700226 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700227 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700228 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800229 mRegionMesh = NULL;
230
ztenghui63d41ab2014-02-14 13:13:41 -0800231 glDeleteBuffers(1, &mShadowStripsIndices);
232 mShadowStripsIndices = 0;
233
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800234 fboCache.clear();
235
236 programCache.clear();
237 currentProgram = NULL;
238
Romain Guy3b748a42013-04-17 18:54:38 -0700239 assetAtlas.terminate();
240
241 patchCache.clear();
242
Romain Guy46bfc482013-08-16 18:38:29 -0700243 clearGarbage();
244
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800245 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700246}
247
248///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800249// Debug
250///////////////////////////////////////////////////////////////////////////////
251
Romain Guy627c6fd2013-08-21 11:53:18 -0700252uint32_t Caches::getOverdrawColor(uint32_t amount) const {
253 static uint32_t sOverdrawColors[2][4] = {
254 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
255 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
256 };
257 if (amount < 1) amount = 1;
258 if (amount > 4) amount = 4;
259 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
260}
261
Romain Guyc15008e2010-11-10 11:59:15 -0800262void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700263 String8 stringLog;
264 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000265 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700266}
267
268void Caches::dumpMemoryUsage(String8 &log) {
269 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());
274 log.appendFormat(" Garbage layers %8zu\n", mLayerGarbage.size());
275 log.appendFormat(" Active layers %8zu\n",
276 mRenderState ? mRenderState->mActiveLayers.size() : 0);
Romain Guy8d4aeb72013-02-12 16:08:55 -0800277 log.appendFormat(" RenderBufferCache %8d / %8d\n",
278 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700279 log.appendFormat(" GradientCache %8d / %8d\n",
280 gradientCache.getSize(), gradientCache.getMaxSize());
281 log.appendFormat(" PathCache %8d / %8d\n",
282 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700283 log.appendFormat(" TessellationCache %8d / %8d\n",
284 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700285 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800286 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700287 log.appendFormat(" PatchCache %8d / %8d\n",
288 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700289 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700290 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
291 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
292 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
293 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
294 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
295 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800296 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700297 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700298 log.appendFormat(" FboCache %8d / %8d\n",
299 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800300
301 uint32_t total = 0;
302 total += textureCache.getSize();
303 total += layerCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800304 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800305 total += gradientCache.getSize();
306 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700307 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800308 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700309 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700310 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700311 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
312 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800313 }
314
Chet Haase9c1e23b2011-03-24 10:51:31 -0700315 log.appendFormat("Total memory usage:\n");
316 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800317}
318
319///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800320// Memory management
321///////////////////////////////////////////////////////////////////////////////
322
323void Caches::clearGarbage() {
324 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800325 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700326 patchCache.clearGarbage();
Romain Guy57066eb2011-01-12 12:53:32 -0800327
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700328 Vector<Layer*> layers;
Romain Guy57066eb2011-01-12 12:53:32 -0800329
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700330 { // scope for the lock
331 Mutex::Autolock _l(mGarbageLock);
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700332 layers = mLayerGarbage;
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700333 mLayerGarbage.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800334 }
Romain Guybb0acdf2012-03-05 13:44:35 -0800335
John Reck087bc0c2014-04-04 16:20:08 -0700336 size_t count = layers.size();
Mathias Agopian17ef62c2012-09-25 22:52:40 -0700337 for (size_t i = 0; i < count; i++) {
338 Layer* layer = layers.itemAt(i);
339 delete layer;
340 }
341 layers.clear();
Romain Guy57066eb2011-01-12 12:53:32 -0800342}
343
Romain Guyada830f2011-01-13 12:13:20 -0800344void Caches::deleteLayerDeferred(Layer* layer) {
Romain Guy57066eb2011-01-12 12:53:32 -0800345 Mutex::Autolock _l(mGarbageLock);
Chris Craikbfd1cd62014-09-10 13:04:31 -0700346 layer->state = Layer::kState_InGarbageList;
Romain Guyada830f2011-01-13 12:13:20 -0800347 mLayerGarbage.push(layer);
Romain Guyfe48f652010-11-11 15:36:56 -0800348}
349
Romain Guybdf76092011-07-18 15:00:43 -0700350void Caches::flush(FlushMode mode) {
351 FLUSH_LOGD("Flushing caches (mode %d)", mode);
352
Romain Guyb0a41ed2013-08-16 14:44:38 -0700353 // We must stop tasks before clearing caches
354 if (mode > kFlushMode_Layers) {
355 tasks.stop();
356 }
357
Romain Guybdf76092011-07-18 15:00:43 -0700358 switch (mode) {
359 case kFlushMode_Full:
360 textureCache.clear();
361 patchCache.clear();
362 dropShadowCache.clear();
363 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700364 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700365 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700366 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700367 // fall through
368 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700369 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700370 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700371 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700372 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700373 // fall through
374 case kFlushMode_Layers:
375 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800376 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700377 break;
378 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700379
380 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700381 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700382}
383
Romain Guyfe48f652010-11-11 15:36:56 -0800384///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700385// VBO
386///////////////////////////////////////////////////////////////////////////////
387
Romain Guyf3a910b42011-12-12 20:35:21 -0800388bool Caches::bindMeshBuffer() {
389 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700390}
391
Romain Guyf3a910b42011-12-12 20:35:21 -0800392bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700393 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700394 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700395 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800396 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700397 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800398 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700399}
400
Romain Guyf3a910b42011-12-12 20:35:21 -0800401bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700402 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700403 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700404 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800405 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700406 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800407 return false;
408}
409
ztenghui63d41ab2014-02-14 13:13:41 -0800410bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800411 if (mCurrentIndicesBuffer != buffer) {
412 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
413 mCurrentIndicesBuffer = buffer;
414 return true;
415 }
416 return false;
417}
418
ztenghui63d41ab2014-02-14 13:13:41 -0800419bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700420 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700421 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
422 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700423 uint16_t quad = i * 4;
424 int index = i * 6;
425 regionIndices[index ] = quad; // top-left
426 regionIndices[index + 1] = quad + 1; // top-right
427 regionIndices[index + 2] = quad + 2; // bottom-left
428 regionIndices[index + 3] = quad + 2; // bottom-left
429 regionIndices[index + 4] = quad + 1; // top-right
430 regionIndices[index + 5] = quad + 3; // bottom-right
431 }
432
433 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800434 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700435 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700436 regionIndices, GL_STATIC_DRAW);
437
438 delete[] regionIndices;
439 return force;
440 }
441
ztenghui63d41ab2014-02-14 13:13:41 -0800442 return bindIndicesBufferInternal(mMeshIndices);
443}
444
445bool Caches::bindShadowIndicesBuffer() {
446 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700447 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800448 ShadowTessellator::generateShadowIndices(shadowIndices);
449 glGenBuffers(1, &mShadowStripsIndices);
450 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700451 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800452 shadowIndices, GL_STATIC_DRAW);
453
454 delete[] shadowIndices;
455 return force;
456 }
457
458 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700459}
460
Romain Guy15bc6432011-12-13 13:11:32 -0800461bool Caches::unbindIndicesBuffer() {
462 if (mCurrentIndicesBuffer) {
463 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
464 mCurrentIndicesBuffer = 0;
465 return true;
466 }
467 return false;
468}
469
Romain Guy85ef80d2012-09-13 20:26:50 -0700470///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700471// PBO
472///////////////////////////////////////////////////////////////////////////////
473
474bool Caches::bindPixelBuffer(const GLuint buffer) {
475 if (mCurrentPixelBuffer != buffer) {
476 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
477 mCurrentPixelBuffer = buffer;
478 return true;
479 }
480 return false;
481}
482
483bool Caches::unbindPixelBuffer() {
484 if (mCurrentPixelBuffer) {
485 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
486 mCurrentPixelBuffer = 0;
487 return true;
488 }
489 return false;
490}
491
492///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700493// Meshes and textures
494///////////////////////////////////////////////////////////////////////////////
495
ztenghui55bfb4e2013-12-03 10:38:55 -0800496void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700497 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
498 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800499 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
500 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700501 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800502 }
503}
504
ztenghui55bfb4e2013-12-03 10:38:55 -0800505void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800506 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700507 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800508 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800509 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800510 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800511 }
512}
513
514void Caches::resetVertexPointers() {
515 mCurrentPositionPointer = this;
516 mCurrentTexCoordsPointer = this;
517}
518
519void Caches::resetTexCoordsVertexPointer() {
520 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700521}
522
Romain Guy15bc6432011-12-13 13:11:32 -0800523void Caches::enableTexCoordsVertexArray() {
524 if (!mTexCoordsArrayEnabled) {
525 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800526 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800527 mTexCoordsArrayEnabled = true;
528 }
529}
530
Romain Guyff316ec2013-02-13 18:39:43 -0800531void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800532 if (mTexCoordsArrayEnabled) {
533 glDisableVertexAttribArray(Program::kBindingTexCoords);
534 mTexCoordsArrayEnabled = false;
535 }
536}
537
Romain Guya1d3c912011-12-13 14:55:06 -0800538void Caches::activeTexture(GLuint textureUnit) {
539 if (mTextureUnit != textureUnit) {
540 glActiveTexture(gTextureUnits[textureUnit]);
541 mTextureUnit = textureUnit;
542 }
543}
544
Chris Craik9ab2d182013-07-22 16:16:06 -0700545void Caches::resetActiveTexture() {
546 mTextureUnit = -1;
547}
548
Romain Guy8aa195d2013-06-04 18:00:09 -0700549void Caches::bindTexture(GLuint texture) {
550 if (mBoundTextures[mTextureUnit] != texture) {
551 glBindTexture(GL_TEXTURE_2D, texture);
552 mBoundTextures[mTextureUnit] = texture;
553 }
554}
555
556void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500557 if (target == GL_TEXTURE_2D) {
558 bindTexture(texture);
559 } else {
560 // GLConsumer directly calls glBindTexture() with
561 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
562 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700563 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700564 }
565}
566
Romain Guybe1b1272013-06-06 14:02:54 -0700567void Caches::deleteTexture(GLuint texture) {
568 // When glDeleteTextures() is called on a currently bound texture,
569 // OpenGL ES specifies that the texture is then considered unbound
570 // Consider the following series of calls:
571 //
572 // glGenTextures -> creates texture name 2
573 // glBindTexture(2)
574 // glDeleteTextures(2) -> 2 is now unbound
575 // glGenTextures -> can return 2 again
576 //
577 // If we don't call glBindTexture(2) after the second glGenTextures
578 // call, any texture operation will be performed on the default
579 // texture (name=0)
580
jiayuanr4a473c7d2014-06-10 17:41:49 +0800581 unbindTexture(texture);
582
Romain Guybe1b1272013-06-06 14:02:54 -0700583 glDeleteTextures(1, &texture);
584}
585
Romain Guy8aa195d2013-06-04 18:00:09 -0700586void Caches::resetBoundTextures() {
587 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
588}
589
jiayuanr4a473c7d2014-06-10 17:41:49 +0800590void Caches::unbindTexture(GLuint texture) {
591 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
592 if (mBoundTextures[i] == texture) {
593 mBoundTextures[i] = 0;
594 }
595 }
596}
597
Romain Guy85ef80d2012-09-13 20:26:50 -0700598///////////////////////////////////////////////////////////////////////////////
599// Scissor
600///////////////////////////////////////////////////////////////////////////////
601
Romain Guy8a4ac612012-07-17 17:32:48 -0700602bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700603 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
604 width != mScissorWidth || height != mScissorHeight)) {
605
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700606 if (x < 0) {
607 width += x;
608 x = 0;
609 }
610 if (y < 0) {
611 height += y;
612 y = 0;
613 }
614 if (width < 0) {
615 width = 0;
616 }
617 if (height < 0) {
618 height = 0;
619 }
Romain Guy8f85e802011-12-14 19:23:32 -0800620 glScissor(x, y, width, height);
621
622 mScissorX = x;
623 mScissorY = y;
624 mScissorWidth = width;
625 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700626
627 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800628 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700629 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800630}
631
Romain Guy8a4ac612012-07-17 17:32:48 -0700632bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700633 if (!scissorEnabled) {
634 glEnable(GL_SCISSOR_TEST);
635 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700636 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700637 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700638 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700639 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700640}
641
Romain Guy8a4ac612012-07-17 17:32:48 -0700642bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700643 if (scissorEnabled) {
644 glDisable(GL_SCISSOR_TEST);
645 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700646 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700647 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700648 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700649}
650
651void Caches::setScissorEnabled(bool enabled) {
652 if (scissorEnabled != enabled) {
653 if (enabled) glEnable(GL_SCISSOR_TEST);
654 else glDisable(GL_SCISSOR_TEST);
655 scissorEnabled = enabled;
656 }
657}
658
Romain Guy82bc7a72012-01-03 14:13:39 -0800659void Caches::resetScissor() {
660 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
661}
662
Romain Guy85ef80d2012-09-13 20:26:50 -0700663///////////////////////////////////////////////////////////////////////////////
664// Tiling
665///////////////////////////////////////////////////////////////////////////////
666
Romain Guyf735c8e2013-01-31 17:45:55 -0800667void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800668 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800669 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700670 }
671}
672
673void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800674 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700675 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700676 }
677}
678
Romain Guy54c1a642012-09-27 17:55:46 -0700679bool Caches::hasRegisteredFunctors() {
680 return mFunctorsCount > 0;
681}
682
683void Caches::registerFunctors(uint32_t functorCount) {
684 mFunctorsCount += functorCount;
685}
686
687void Caches::unregisterFunctors(uint32_t functorCount) {
688 if (functorCount > mFunctorsCount) {
689 mFunctorsCount = 0;
690 } else {
691 mFunctorsCount -= functorCount;
692 }
693}
694
Romain Guy85ef80d2012-09-13 20:26:50 -0700695///////////////////////////////////////////////////////////////////////////////
696// Regions
697///////////////////////////////////////////////////////////////////////////////
698
Romain Guy5b3b3522010-10-27 18:57:51 -0700699TextureVertex* Caches::getRegionMesh() {
700 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
701 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700702 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700703 }
704
705 return mRegionMesh;
706}
707
Chris Craikba9b6132013-12-15 17:10:19 -0800708///////////////////////////////////////////////////////////////////////////////
709// Temporary Properties
710///////////////////////////////////////////////////////////////////////////////
711
712void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700713 propertyLightDiameter = -1.0f;
714 propertyLightPosY = -1.0f;
715 propertyLightPosZ = -1.0f;
716 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700717 propertyAmbientShadowStrength = -1;
718 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800719}
720
721void Caches::setTempProperty(const char* name, const char* value) {
722 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700723 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700724 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
725 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800726 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700727 } else if (!strcmp(name, "lightDiameter")) {
728 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
729 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800730 return;
Chris Craik59744b72014-07-01 17:56:52 -0700731 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700732 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
733 ALOGD("lightPos Y = %.2f", propertyLightPosY);
734 return;
Chris Craik59744b72014-07-01 17:56:52 -0700735 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700736 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
737 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800738 return;
ztenghui14a4e352014-08-13 10:44:39 -0700739 } else if (!strcmp(name, "ambientShadowStrength")) {
740 propertyAmbientShadowStrength = atoi(value);
741 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
742 return;
743 } else if (!strcmp(name, "spotShadowStrength")) {
744 propertySpotShadowStrength = atoi(value);
745 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
746 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800747 }
748 ALOGD(" failed");
749}
750
Chet Haasedd78cca2010-10-22 18:59:26 -0700751}; // namespace uirenderer
752}; // namespace android