blob: 77b66f213712bf5614523ff6d47e127dee72f6e7 [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"
ywenf087c822015-05-28 14:10:53 +080028#include "utils/GLUtils.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
John Reckfbc8df02014-11-14 16:18:41 -080070 ATRACE_NAME("Caches::init");
71
Romain Guy8ff6b9e2011-11-09 20:10:18 -080072 glGenBuffers(1, &meshBuffer);
73 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
74 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
75
76 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080077 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080078 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070079 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080080 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070081 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080082
Romain Guy15bc6432011-12-13 13:11:32 -080083 mTexCoordsArrayEnabled = false;
84
Romain Guyb1d0a4e2012-07-13 18:25:35 -070085 glDisable(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -070086 scissorEnabled = false;
Romain Guy8f85e802011-12-14 19:23:32 -080087 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
88
Romain Guya1d3c912011-12-13 14:55:06 -080089 glActiveTexture(gTextureUnits[0]);
90 mTextureUnit = 0;
91
Romain Guy8ff6b9e2011-11-09 20:10:18 -080092 mRegionMesh = NULL;
Romain Guy3b748a42013-04-17 18:54:38 -070093 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080094 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080095 blend = false;
96 lastSrcMode = GL_ZERO;
97 lastDstMode = GL_ZERO;
98 currentProgram = NULL;
99
Romain Guy54c1a642012-09-27 17:55:46 -0700100 mFunctorsCount = 0;
101
Romain Guyc2a97212013-02-06 15:29:46 -0800102 debugLayersUpdates = false;
103 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800104 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800105
Romain Guy3b748a42013-04-17 18:54:38 -0700106 patchCache.init(*this);
107
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700109
Romain Guy8aa195d2013-06-04 18:00:09 -0700110 resetBoundTextures();
111
Romain Guy3b748a42013-04-17 18:54:38 -0700112 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800113}
114
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700115void Caches::initFont() {
116 fontRenderer = GammaFontRenderer::createRenderer();
117}
118
Romain Guydfa10462012-05-12 16:18:58 -0700119void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800120 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700121 eventMark = glInsertEventMarkerEXT;
Romain Guy0f6675332013-03-01 14:31:04 -0800122
Chris Craikff785832013-03-08 13:12:16 -0800123 startMark = glPushGroupMarkerEXT;
124 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700125 } else {
126 eventMark = eventMarkNull;
127 startMark = startMarkNull;
128 endMark = endMarkNull;
129 }
130
Romain Guy0f6675332013-03-01 14:31:04 -0800131 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700132 setLabel = glLabelObjectEXT;
133 getLabel = glGetObjectLabelEXT;
134 } else {
135 setLabel = setLabelNull;
136 getLabel = getLabelNull;
137 }
138}
139
140void Caches::initConstraints() {
141 GLint maxTextureUnits;
142 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
143 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
144 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
145 }
146
147 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
148}
149
Romain Guyf9f00162013-05-09 11:50:12 -0700150void Caches::initStaticProperties() {
151 gpuPixelBuffersEnabled = false;
152
153 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700154 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700155 char property[PROPERTY_VALUE_MAX];
156 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
157 gpuPixelBuffersEnabled = !strcmp(property, "true");
158 }
159 }
160}
161
Romain Guy5bb3c732012-11-29 17:52:58 -0800162bool Caches::initProperties() {
163 bool prevDebugLayersUpdates = debugLayersUpdates;
164 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800165 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800166
Romain Guy4ff0cf42012-08-06 14:51:10 -0700167 char property[PROPERTY_VALUE_MAX];
168 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, NULL) > 0) {
169 INIT_LOGD(" Layers updates debug enabled: %s", property);
170 debugLayersUpdates = !strcmp(property, "true");
171 } else {
172 debugLayersUpdates = false;
173 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700174
Romain Guy627c6fd2013-08-21 11:53:18 -0700175 debugOverdraw = false;
Romain Guy7c450aa2012-09-21 19:15:00 -0700176 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, NULL) > 0) {
177 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700178 if (!strcmp(property, "show")) {
179 debugOverdraw = true;
180 mOverdrawDebugColorSet = kColorSet_Default;
181 } else if (!strcmp(property, "show_deuteranomaly")) {
182 debugOverdraw = true;
183 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
184 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700185 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800186
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800187 // See Properties.h for valid values
188 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, NULL) > 0) {
189 INIT_LOGD(" Stencil clip debug enabled: %s", property);
190 if (!strcmp(property, "hide")) {
191 debugStencilClip = kStencilHide;
192 } else if (!strcmp(property, "highlight")) {
193 debugStencilClip = kStencilShowHighlight;
194 } else if (!strcmp(property, "region")) {
195 debugStencilClip = kStencilShowRegion;
196 }
197 } else {
198 debugStencilClip = kStencilHide;
199 }
200
Romain Guy0f6675332013-03-01 14:31:04 -0800201 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
202 drawDeferDisabled = !strcasecmp(property, "true");
203 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
204 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700205 drawDeferDisabled = false;
Romain Guy0f6675332013-03-01 14:31:04 -0800206 INIT_LOGD(" Draw defer enabled");
207 }
208
209 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
210 drawReorderDisabled = !strcasecmp(property, "true");
211 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
212 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700213 drawReorderDisabled = false;
Romain Guy0f6675332013-03-01 14:31:04 -0800214 INIT_LOGD(" Draw reorder enabled");
215 }
216
Romain Guy5bb3c732012-11-29 17:52:58 -0800217 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800218 (prevDebugOverdraw != debugOverdraw) ||
219 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700220}
221
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800222void Caches::terminate() {
223 if (!mInitialized) return;
224
225 glDeleteBuffers(1, &meshBuffer);
226 mCurrentBuffer = 0;
227
Romain Guy3b748a42013-04-17 18:54:38 -0700228 glDeleteBuffers(1, &mMeshIndices);
Romain Guy5b3b3522010-10-27 18:57:51 -0700229 delete[] mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700230 mMeshIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800231 mRegionMesh = NULL;
232
ztenghui63d41ab2014-02-14 13:13:41 -0800233 glDeleteBuffers(1, &mShadowStripsIndices);
234 mShadowStripsIndices = 0;
235
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800236 fboCache.clear();
237
238 programCache.clear();
239 currentProgram = NULL;
240
Romain Guy3b748a42013-04-17 18:54:38 -0700241 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) {
John Reck0e89e2b2014-10-31 14:49:06 -0700269 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700270 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
271 log.appendFormat(" TextureCache %8d / %8d\n",
272 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700273 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
274 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700275 if (mRenderState) {
276 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800277 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700278 it != mRenderState->mActiveLayers.end(); it++) {
279 const Layer* layer = *it;
280 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
281 layer->getWidth(), layer->getHeight(),
282 layer->isTextureLayer(), layer->getTexture(),
283 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800284 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700285 }
286 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
287 memused, mRenderState->mActiveLayers.size());
288 total += memused;
289 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800290 log.appendFormat(" RenderBufferCache %8d / %8d\n",
291 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700292 log.appendFormat(" GradientCache %8d / %8d\n",
293 gradientCache.getSize(), gradientCache.getMaxSize());
294 log.appendFormat(" PathCache %8d / %8d\n",
295 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700296 log.appendFormat(" TessellationCache %8d / %8d\n",
297 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700298 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800299 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700300 log.appendFormat(" PatchCache %8d / %8d\n",
301 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700302 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700303 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
304 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
305 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
306 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
307 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
308 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800309 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700310 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700311 log.appendFormat(" FboCache %8d / %8d\n",
312 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800313
Romain Guyc15008e2010-11-10 11:59:15 -0800314 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800315 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800316 total += gradientCache.getSize();
317 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700318 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800319 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700320 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700321 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700322 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
323 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800324 }
325
Chet Haase9c1e23b2011-03-24 10:51:31 -0700326 log.appendFormat("Total memory usage:\n");
327 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800328}
329
330///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800331// Memory management
332///////////////////////////////////////////////////////////////////////////////
333
334void Caches::clearGarbage() {
335 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800336 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700337 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800338}
339
Romain Guybdf76092011-07-18 15:00:43 -0700340void Caches::flush(FlushMode mode) {
341 FLUSH_LOGD("Flushing caches (mode %d)", mode);
342
Romain Guyb0a41ed2013-08-16 14:44:38 -0700343 // We must stop tasks before clearing caches
344 if (mode > kFlushMode_Layers) {
345 tasks.stop();
346 }
347
Romain Guybdf76092011-07-18 15:00:43 -0700348 switch (mode) {
349 case kFlushMode_Full:
350 textureCache.clear();
351 patchCache.clear();
352 dropShadowCache.clear();
353 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700354 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700355 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700356 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700357 // fall through
358 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700359 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700360 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700361 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700362 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700363 // fall through
364 case kFlushMode_Layers:
365 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800366 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700367 break;
368 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700369
370 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700371 glFinish();
ywenf087c822015-05-28 14:10:53 +0800372
373 // glFinish() need dequeue buffer, and it is not 100% success
374 // It generates gl error sometimes, this error will be there
375 // until glGetError called. Call GLUtils::dumpGLErrors to clean
376 // the error in case it leaks to other functions
377 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700378}
379
Romain Guyfe48f652010-11-11 15:36:56 -0800380///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700381// VBO
382///////////////////////////////////////////////////////////////////////////////
383
Romain Guyf3a910b42011-12-12 20:35:21 -0800384bool Caches::bindMeshBuffer() {
385 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700386}
387
Romain Guyf3a910b42011-12-12 20:35:21 -0800388bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700389 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700390 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700391 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800392 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700393 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800394 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700395}
396
Romain Guyf3a910b42011-12-12 20:35:21 -0800397bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700398 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700399 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700400 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800401 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700402 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800403 return false;
404}
405
ztenghui63d41ab2014-02-14 13:13:41 -0800406bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800407 if (mCurrentIndicesBuffer != buffer) {
408 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
409 mCurrentIndicesBuffer = buffer;
410 return true;
411 }
412 return false;
413}
414
ztenghui63d41ab2014-02-14 13:13:41 -0800415bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700416 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700417 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
418 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700419 uint16_t quad = i * 4;
420 int index = i * 6;
421 regionIndices[index ] = quad; // top-left
422 regionIndices[index + 1] = quad + 1; // top-right
423 regionIndices[index + 2] = quad + 2; // bottom-left
424 regionIndices[index + 3] = quad + 2; // bottom-left
425 regionIndices[index + 4] = quad + 1; // top-right
426 regionIndices[index + 5] = quad + 3; // bottom-right
427 }
428
429 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800430 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700431 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700432 regionIndices, GL_STATIC_DRAW);
433
434 delete[] regionIndices;
435 return force;
436 }
437
ztenghui63d41ab2014-02-14 13:13:41 -0800438 return bindIndicesBufferInternal(mMeshIndices);
439}
440
441bool Caches::bindShadowIndicesBuffer() {
442 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700443 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800444 ShadowTessellator::generateShadowIndices(shadowIndices);
445 glGenBuffers(1, &mShadowStripsIndices);
446 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700447 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800448 shadowIndices, GL_STATIC_DRAW);
449
450 delete[] shadowIndices;
451 return force;
452 }
453
454 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700455}
456
Romain Guy15bc6432011-12-13 13:11:32 -0800457bool Caches::unbindIndicesBuffer() {
458 if (mCurrentIndicesBuffer) {
459 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
460 mCurrentIndicesBuffer = 0;
461 return true;
462 }
463 return false;
464}
465
Romain Guy85ef80d2012-09-13 20:26:50 -0700466///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700467// PBO
468///////////////////////////////////////////////////////////////////////////////
469
470bool Caches::bindPixelBuffer(const GLuint buffer) {
471 if (mCurrentPixelBuffer != buffer) {
472 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
473 mCurrentPixelBuffer = buffer;
474 return true;
475 }
476 return false;
477}
478
479bool Caches::unbindPixelBuffer() {
480 if (mCurrentPixelBuffer) {
481 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
482 mCurrentPixelBuffer = 0;
483 return true;
484 }
485 return false;
486}
487
488///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700489// Meshes and textures
490///////////////////////////////////////////////////////////////////////////////
491
ztenghui55bfb4e2013-12-03 10:38:55 -0800492void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700493 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
494 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800495 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
496 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700497 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800498 }
499}
500
ztenghui55bfb4e2013-12-03 10:38:55 -0800501void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800502 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700503 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800504 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800505 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800506 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800507 }
508}
509
510void Caches::resetVertexPointers() {
511 mCurrentPositionPointer = this;
512 mCurrentTexCoordsPointer = this;
513}
514
515void Caches::resetTexCoordsVertexPointer() {
516 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700517}
518
Romain Guy15bc6432011-12-13 13:11:32 -0800519void Caches::enableTexCoordsVertexArray() {
520 if (!mTexCoordsArrayEnabled) {
521 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800522 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800523 mTexCoordsArrayEnabled = true;
524 }
525}
526
Romain Guyff316ec2013-02-13 18:39:43 -0800527void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800528 if (mTexCoordsArrayEnabled) {
529 glDisableVertexAttribArray(Program::kBindingTexCoords);
530 mTexCoordsArrayEnabled = false;
531 }
532}
533
Romain Guya1d3c912011-12-13 14:55:06 -0800534void Caches::activeTexture(GLuint textureUnit) {
535 if (mTextureUnit != textureUnit) {
536 glActiveTexture(gTextureUnits[textureUnit]);
537 mTextureUnit = textureUnit;
538 }
539}
540
Chris Craik9ab2d182013-07-22 16:16:06 -0700541void Caches::resetActiveTexture() {
542 mTextureUnit = -1;
543}
544
Romain Guy8aa195d2013-06-04 18:00:09 -0700545void Caches::bindTexture(GLuint texture) {
546 if (mBoundTextures[mTextureUnit] != texture) {
547 glBindTexture(GL_TEXTURE_2D, texture);
548 mBoundTextures[mTextureUnit] = texture;
549 }
550}
551
552void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500553 if (target == GL_TEXTURE_2D) {
554 bindTexture(texture);
555 } else {
556 // GLConsumer directly calls glBindTexture() with
557 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
558 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700559 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700560 }
561}
562
Romain Guybe1b1272013-06-06 14:02:54 -0700563void Caches::deleteTexture(GLuint texture) {
564 // When glDeleteTextures() is called on a currently bound texture,
565 // OpenGL ES specifies that the texture is then considered unbound
566 // Consider the following series of calls:
567 //
568 // glGenTextures -> creates texture name 2
569 // glBindTexture(2)
570 // glDeleteTextures(2) -> 2 is now unbound
571 // glGenTextures -> can return 2 again
572 //
573 // If we don't call glBindTexture(2) after the second glGenTextures
574 // call, any texture operation will be performed on the default
575 // texture (name=0)
576
jiayuanr4a473c7d2014-06-10 17:41:49 +0800577 unbindTexture(texture);
578
Romain Guybe1b1272013-06-06 14:02:54 -0700579 glDeleteTextures(1, &texture);
580}
581
Romain Guy8aa195d2013-06-04 18:00:09 -0700582void Caches::resetBoundTextures() {
583 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
584}
585
jiayuanr4a473c7d2014-06-10 17:41:49 +0800586void Caches::unbindTexture(GLuint texture) {
587 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
588 if (mBoundTextures[i] == texture) {
589 mBoundTextures[i] = 0;
590 }
591 }
592}
593
Romain Guy85ef80d2012-09-13 20:26:50 -0700594///////////////////////////////////////////////////////////////////////////////
595// Scissor
596///////////////////////////////////////////////////////////////////////////////
597
Romain Guy8a4ac612012-07-17 17:32:48 -0700598bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700599 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
600 width != mScissorWidth || height != mScissorHeight)) {
601
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700602 if (x < 0) {
603 width += x;
604 x = 0;
605 }
606 if (y < 0) {
607 height += y;
608 y = 0;
609 }
610 if (width < 0) {
611 width = 0;
612 }
613 if (height < 0) {
614 height = 0;
615 }
Romain Guy8f85e802011-12-14 19:23:32 -0800616 glScissor(x, y, width, height);
617
618 mScissorX = x;
619 mScissorY = y;
620 mScissorWidth = width;
621 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700622
623 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800624 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700625 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800626}
627
Romain Guy8a4ac612012-07-17 17:32:48 -0700628bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700629 if (!scissorEnabled) {
630 glEnable(GL_SCISSOR_TEST);
631 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700632 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700633 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700634 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700635 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700636}
637
Romain Guy8a4ac612012-07-17 17:32:48 -0700638bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700639 if (scissorEnabled) {
640 glDisable(GL_SCISSOR_TEST);
641 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700642 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700643 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700644 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700645}
646
647void Caches::setScissorEnabled(bool enabled) {
648 if (scissorEnabled != enabled) {
649 if (enabled) glEnable(GL_SCISSOR_TEST);
650 else glDisable(GL_SCISSOR_TEST);
651 scissorEnabled = enabled;
652 }
653}
654
Romain Guy82bc7a72012-01-03 14:13:39 -0800655void Caches::resetScissor() {
656 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
657}
658
Romain Guy85ef80d2012-09-13 20:26:50 -0700659///////////////////////////////////////////////////////////////////////////////
660// Tiling
661///////////////////////////////////////////////////////////////////////////////
662
Romain Guyf735c8e2013-01-31 17:45:55 -0800663void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800664 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800665 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700666 }
667}
668
669void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800670 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700671 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700672 }
673}
674
Romain Guy54c1a642012-09-27 17:55:46 -0700675bool Caches::hasRegisteredFunctors() {
676 return mFunctorsCount > 0;
677}
678
679void Caches::registerFunctors(uint32_t functorCount) {
680 mFunctorsCount += functorCount;
681}
682
683void Caches::unregisterFunctors(uint32_t functorCount) {
684 if (functorCount > mFunctorsCount) {
685 mFunctorsCount = 0;
686 } else {
687 mFunctorsCount -= functorCount;
688 }
689}
690
Romain Guy85ef80d2012-09-13 20:26:50 -0700691///////////////////////////////////////////////////////////////////////////////
692// Regions
693///////////////////////////////////////////////////////////////////////////////
694
Romain Guy5b3b3522010-10-27 18:57:51 -0700695TextureVertex* Caches::getRegionMesh() {
696 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
697 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700698 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 }
700
701 return mRegionMesh;
702}
703
Chris Craikba9b6132013-12-15 17:10:19 -0800704///////////////////////////////////////////////////////////////////////////////
705// Temporary Properties
706///////////////////////////////////////////////////////////////////////////////
707
708void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700709 propertyLightDiameter = -1.0f;
710 propertyLightPosY = -1.0f;
711 propertyLightPosZ = -1.0f;
712 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700713 propertyAmbientShadowStrength = -1;
714 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800715}
716
717void Caches::setTempProperty(const char* name, const char* value) {
718 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700719 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700720 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
721 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800722 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700723 } else if (!strcmp(name, "lightDiameter")) {
724 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
725 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800726 return;
Chris Craik59744b72014-07-01 17:56:52 -0700727 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700728 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
729 ALOGD("lightPos Y = %.2f", propertyLightPosY);
730 return;
Chris Craik59744b72014-07-01 17:56:52 -0700731 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700732 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
733 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800734 return;
ztenghui14a4e352014-08-13 10:44:39 -0700735 } else if (!strcmp(name, "ambientShadowStrength")) {
736 propertyAmbientShadowStrength = atoi(value);
737 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
738 return;
739 } else if (!strcmp(name, "spotShadowStrength")) {
740 propertySpotShadowStrength = atoi(value);
741 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
742 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800743 }
744 ALOGD(" failed");
745}
746
Chet Haasedd78cca2010-10-22 18:59:26 -0700747}; // namespace uirenderer
748}; // namespace android