blob: 8727154246760a8fa09aa3277a91b74c4c1a936e [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
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 assetAtlas.terminate();
242
243 patchCache.clear();
244
Romain Guy46bfc482013-08-16 18:38:29 -0700245 clearGarbage();
246
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800247 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700248}
249
250///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800251// Debug
252///////////////////////////////////////////////////////////////////////////////
253
Romain Guy627c6fd2013-08-21 11:53:18 -0700254uint32_t Caches::getOverdrawColor(uint32_t amount) const {
255 static uint32_t sOverdrawColors[2][4] = {
256 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
257 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
258 };
259 if (amount < 1) amount = 1;
260 if (amount > 4) amount = 4;
261 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
262}
263
Romain Guyc15008e2010-11-10 11:59:15 -0800264void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700265 String8 stringLog;
266 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000267 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700268}
269
270void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700271 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700272 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
273 log.appendFormat(" TextureCache %8d / %8d\n",
274 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700275 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
276 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700277 if (mRenderState) {
278 int memused = 0;
279 for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin();
280 it != mRenderState->mActiveLayers.end(); it++) {
281 const Layer* layer = *it;
282 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
283 layer->getWidth(), layer->getHeight(),
284 layer->isTextureLayer(), layer->getTexture(),
285 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800286 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700287 }
288 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
289 memused, mRenderState->mActiveLayers.size());
290 total += memused;
291 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800292 log.appendFormat(" RenderBufferCache %8d / %8d\n",
293 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700294 log.appendFormat(" GradientCache %8d / %8d\n",
295 gradientCache.getSize(), gradientCache.getMaxSize());
296 log.appendFormat(" PathCache %8d / %8d\n",
297 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700298 log.appendFormat(" TessellationCache %8d / %8d\n",
299 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700300 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800301 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700302 log.appendFormat(" PatchCache %8d / %8d\n",
303 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700304 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700305 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
306 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
307 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
308 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
309 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
310 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800311 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700312 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700313 log.appendFormat(" FboCache %8d / %8d\n",
314 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800315
Romain Guyc15008e2010-11-10 11:59:15 -0800316 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800317 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800318 total += gradientCache.getSize();
319 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700320 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800321 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700322 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700323 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700324 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
325 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800326 }
327
Chet Haase9c1e23b2011-03-24 10:51:31 -0700328 log.appendFormat("Total memory usage:\n");
329 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800330}
331
332///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800333// Memory management
334///////////////////////////////////////////////////////////////////////////////
335
336void Caches::clearGarbage() {
337 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800338 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700339 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800340}
341
Romain Guybdf76092011-07-18 15:00:43 -0700342void Caches::flush(FlushMode mode) {
343 FLUSH_LOGD("Flushing caches (mode %d)", mode);
344
Romain Guyb0a41ed2013-08-16 14:44:38 -0700345 // We must stop tasks before clearing caches
346 if (mode > kFlushMode_Layers) {
347 tasks.stop();
348 }
349
Romain Guybdf76092011-07-18 15:00:43 -0700350 switch (mode) {
351 case kFlushMode_Full:
352 textureCache.clear();
353 patchCache.clear();
354 dropShadowCache.clear();
355 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700356 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700357 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700358 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700359 // fall through
360 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700361 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700362 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700363 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700364 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700365 // fall through
366 case kFlushMode_Layers:
367 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800368 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700369 break;
370 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700371
372 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700373 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700374}
375
Romain Guyfe48f652010-11-11 15:36:56 -0800376///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700377// VBO
378///////////////////////////////////////////////////////////////////////////////
379
Romain Guyf3a910b42011-12-12 20:35:21 -0800380bool Caches::bindMeshBuffer() {
381 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700382}
383
Romain Guyf3a910b42011-12-12 20:35:21 -0800384bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700385 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700386 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700387 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800388 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700389 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800390 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700391}
392
Romain Guyf3a910b42011-12-12 20:35:21 -0800393bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700394 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700395 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700396 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800397 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700398 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800399 return false;
400}
401
ztenghui63d41ab2014-02-14 13:13:41 -0800402bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800403 if (mCurrentIndicesBuffer != buffer) {
404 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
405 mCurrentIndicesBuffer = buffer;
406 return true;
407 }
408 return false;
409}
410
ztenghui63d41ab2014-02-14 13:13:41 -0800411bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700412 if (!mMeshIndices) {
Romain Guy31e08e92013-06-18 15:53:53 -0700413 uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
414 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700415 uint16_t quad = i * 4;
416 int index = i * 6;
417 regionIndices[index ] = quad; // top-left
418 regionIndices[index + 1] = quad + 1; // top-right
419 regionIndices[index + 2] = quad + 2; // bottom-left
420 regionIndices[index + 3] = quad + 2; // bottom-left
421 regionIndices[index + 4] = quad + 1; // top-right
422 regionIndices[index + 5] = quad + 3; // bottom-right
423 }
424
425 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800426 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700427 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Romain Guy3b748a42013-04-17 18:54:38 -0700428 regionIndices, GL_STATIC_DRAW);
429
430 delete[] regionIndices;
431 return force;
432 }
433
ztenghui63d41ab2014-02-14 13:13:41 -0800434 return bindIndicesBufferInternal(mMeshIndices);
435}
436
437bool Caches::bindShadowIndicesBuffer() {
438 if (!mShadowStripsIndices) {
ztenghui50ecf842014-03-11 16:52:30 -0700439 uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
ztenghui63d41ab2014-02-14 13:13:41 -0800440 ShadowTessellator::generateShadowIndices(shadowIndices);
441 glGenBuffers(1, &mShadowStripsIndices);
442 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700443 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
ztenghui63d41ab2014-02-14 13:13:41 -0800444 shadowIndices, GL_STATIC_DRAW);
445
446 delete[] shadowIndices;
447 return force;
448 }
449
450 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700451}
452
Romain Guy15bc6432011-12-13 13:11:32 -0800453bool Caches::unbindIndicesBuffer() {
454 if (mCurrentIndicesBuffer) {
455 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
456 mCurrentIndicesBuffer = 0;
457 return true;
458 }
459 return false;
460}
461
Romain Guy85ef80d2012-09-13 20:26:50 -0700462///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700463// PBO
464///////////////////////////////////////////////////////////////////////////////
465
466bool Caches::bindPixelBuffer(const GLuint buffer) {
467 if (mCurrentPixelBuffer != buffer) {
468 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
469 mCurrentPixelBuffer = buffer;
470 return true;
471 }
472 return false;
473}
474
475bool Caches::unbindPixelBuffer() {
476 if (mCurrentPixelBuffer) {
477 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
478 mCurrentPixelBuffer = 0;
479 return true;
480 }
481 return false;
482}
483
484///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700485// Meshes and textures
486///////////////////////////////////////////////////////////////////////////////
487
ztenghui55bfb4e2013-12-03 10:38:55 -0800488void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700489 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
490 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800491 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
492 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700493 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800494 }
495}
496
ztenghui55bfb4e2013-12-03 10:38:55 -0800497void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800498 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700499 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800500 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800501 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800502 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800503 }
504}
505
506void Caches::resetVertexPointers() {
507 mCurrentPositionPointer = this;
508 mCurrentTexCoordsPointer = this;
509}
510
511void Caches::resetTexCoordsVertexPointer() {
512 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700513}
514
Romain Guy15bc6432011-12-13 13:11:32 -0800515void Caches::enableTexCoordsVertexArray() {
516 if (!mTexCoordsArrayEnabled) {
517 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800518 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800519 mTexCoordsArrayEnabled = true;
520 }
521}
522
Romain Guyff316ec2013-02-13 18:39:43 -0800523void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800524 if (mTexCoordsArrayEnabled) {
525 glDisableVertexAttribArray(Program::kBindingTexCoords);
526 mTexCoordsArrayEnabled = false;
527 }
528}
529
Romain Guya1d3c912011-12-13 14:55:06 -0800530void Caches::activeTexture(GLuint textureUnit) {
531 if (mTextureUnit != textureUnit) {
532 glActiveTexture(gTextureUnits[textureUnit]);
533 mTextureUnit = textureUnit;
534 }
535}
536
Chris Craik9ab2d182013-07-22 16:16:06 -0700537void Caches::resetActiveTexture() {
538 mTextureUnit = -1;
539}
540
Romain Guy8aa195d2013-06-04 18:00:09 -0700541void Caches::bindTexture(GLuint texture) {
542 if (mBoundTextures[mTextureUnit] != texture) {
543 glBindTexture(GL_TEXTURE_2D, texture);
544 mBoundTextures[mTextureUnit] = texture;
545 }
546}
547
548void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500549 if (target == GL_TEXTURE_2D) {
550 bindTexture(texture);
551 } else {
552 // GLConsumer directly calls glBindTexture() with
553 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
554 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700555 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700556 }
557}
558
Romain Guybe1b1272013-06-06 14:02:54 -0700559void Caches::deleteTexture(GLuint texture) {
560 // When glDeleteTextures() is called on a currently bound texture,
561 // OpenGL ES specifies that the texture is then considered unbound
562 // Consider the following series of calls:
563 //
564 // glGenTextures -> creates texture name 2
565 // glBindTexture(2)
566 // glDeleteTextures(2) -> 2 is now unbound
567 // glGenTextures -> can return 2 again
568 //
569 // If we don't call glBindTexture(2) after the second glGenTextures
570 // call, any texture operation will be performed on the default
571 // texture (name=0)
572
jiayuanr4a473c7d2014-06-10 17:41:49 +0800573 unbindTexture(texture);
574
Romain Guybe1b1272013-06-06 14:02:54 -0700575 glDeleteTextures(1, &texture);
576}
577
Romain Guy8aa195d2013-06-04 18:00:09 -0700578void Caches::resetBoundTextures() {
579 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
580}
581
jiayuanr4a473c7d2014-06-10 17:41:49 +0800582void Caches::unbindTexture(GLuint texture) {
583 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
584 if (mBoundTextures[i] == texture) {
585 mBoundTextures[i] = 0;
586 }
587 }
588}
589
Romain Guy85ef80d2012-09-13 20:26:50 -0700590///////////////////////////////////////////////////////////////////////////////
591// Scissor
592///////////////////////////////////////////////////////////////////////////////
593
Romain Guy8a4ac612012-07-17 17:32:48 -0700594bool Caches::setScissor(GLint x, GLint y, GLint width, GLint height) {
Romain Guy586cae32012-07-13 15:28:31 -0700595 if (scissorEnabled && (x != mScissorX || y != mScissorY ||
596 width != mScissorWidth || height != mScissorHeight)) {
597
Chet Haaseaa42c9a2012-10-16 17:36:16 -0700598 if (x < 0) {
599 width += x;
600 x = 0;
601 }
602 if (y < 0) {
603 height += y;
604 y = 0;
605 }
606 if (width < 0) {
607 width = 0;
608 }
609 if (height < 0) {
610 height = 0;
611 }
Romain Guy8f85e802011-12-14 19:23:32 -0800612 glScissor(x, y, width, height);
613
614 mScissorX = x;
615 mScissorY = y;
616 mScissorWidth = width;
617 mScissorHeight = height;
Romain Guy8a4ac612012-07-17 17:32:48 -0700618
619 return true;
Romain Guy8f85e802011-12-14 19:23:32 -0800620 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700621 return false;
Romain Guy8f85e802011-12-14 19:23:32 -0800622}
623
Romain Guy8a4ac612012-07-17 17:32:48 -0700624bool Caches::enableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700625 if (!scissorEnabled) {
626 glEnable(GL_SCISSOR_TEST);
627 scissorEnabled = true;
Romain Guy50ae66a2012-10-07 14:05:59 -0700628 resetScissor();
Romain Guy8a4ac612012-07-17 17:32:48 -0700629 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700630 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700631 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700632}
633
Romain Guy8a4ac612012-07-17 17:32:48 -0700634bool Caches::disableScissor() {
Romain Guy586cae32012-07-13 15:28:31 -0700635 if (scissorEnabled) {
636 glDisable(GL_SCISSOR_TEST);
637 scissorEnabled = false;
Romain Guy8a4ac612012-07-17 17:32:48 -0700638 return true;
Romain Guy586cae32012-07-13 15:28:31 -0700639 }
Romain Guy8a4ac612012-07-17 17:32:48 -0700640 return false;
Romain Guy586cae32012-07-13 15:28:31 -0700641}
642
643void Caches::setScissorEnabled(bool enabled) {
644 if (scissorEnabled != enabled) {
645 if (enabled) glEnable(GL_SCISSOR_TEST);
646 else glDisable(GL_SCISSOR_TEST);
647 scissorEnabled = enabled;
648 }
649}
650
Romain Guy82bc7a72012-01-03 14:13:39 -0800651void Caches::resetScissor() {
652 mScissorX = mScissorY = mScissorWidth = mScissorHeight = 0;
653}
654
Romain Guy85ef80d2012-09-13 20:26:50 -0700655///////////////////////////////////////////////////////////////////////////////
656// Tiling
657///////////////////////////////////////////////////////////////////////////////
658
Romain Guyf735c8e2013-01-31 17:45:55 -0800659void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800660 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800661 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700662 }
663}
664
665void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800666 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700667 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700668 }
669}
670
Romain Guy54c1a642012-09-27 17:55:46 -0700671bool Caches::hasRegisteredFunctors() {
672 return mFunctorsCount > 0;
673}
674
675void Caches::registerFunctors(uint32_t functorCount) {
676 mFunctorsCount += functorCount;
677}
678
679void Caches::unregisterFunctors(uint32_t functorCount) {
680 if (functorCount > mFunctorsCount) {
681 mFunctorsCount = 0;
682 } else {
683 mFunctorsCount -= functorCount;
684 }
685}
686
Romain Guy85ef80d2012-09-13 20:26:50 -0700687///////////////////////////////////////////////////////////////////////////////
688// Regions
689///////////////////////////////////////////////////////////////////////////////
690
Romain Guy5b3b3522010-10-27 18:57:51 -0700691TextureVertex* Caches::getRegionMesh() {
692 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
693 if (!mRegionMesh) {
Romain Guy31e08e92013-06-18 15:53:53 -0700694 mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
Romain Guy5b3b3522010-10-27 18:57:51 -0700695 }
696
697 return mRegionMesh;
698}
699
Chris Craikba9b6132013-12-15 17:10:19 -0800700///////////////////////////////////////////////////////////////////////////////
701// Temporary Properties
702///////////////////////////////////////////////////////////////////////////////
703
704void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700705 propertyLightDiameter = -1.0f;
706 propertyLightPosY = -1.0f;
707 propertyLightPosZ = -1.0f;
708 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700709 propertyAmbientShadowStrength = -1;
710 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800711}
712
713void Caches::setTempProperty(const char* name, const char* value) {
714 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700715 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700716 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
717 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800718 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700719 } else if (!strcmp(name, "lightDiameter")) {
720 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
721 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800722 return;
Chris Craik59744b72014-07-01 17:56:52 -0700723 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700724 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
725 ALOGD("lightPos Y = %.2f", propertyLightPosY);
726 return;
Chris Craik59744b72014-07-01 17:56:52 -0700727 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700728 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
729 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800730 return;
ztenghui14a4e352014-08-13 10:44:39 -0700731 } else if (!strcmp(name, "ambientShadowStrength")) {
732 propertyAmbientShadowStrength = atoi(value);
733 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
734 return;
735 } else if (!strcmp(name, "spotShadowStrength")) {
736 propertySpotShadowStrength = atoi(value);
737 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
738 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800739 }
740 ALOGD(" failed");
741}
742
Chet Haasedd78cca2010-10-22 18:59:26 -0700743}; // namespace uirenderer
744}; // namespace android