blob: 20dd21c510d3064aef883e5a603283cf91411726 [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
19#include "Caches.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080020
Romain Guybb0acdf2012-03-05 13:44:35 -080021#include "DisplayListRenderer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "GammaFontRenderer.h"
Romain Guy09b7c912011-02-02 20:28:09 -080023#include "LayerRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "Properties.h"
25#include "renderstate/RenderState.h"
ztenghui63d41ab2014-02-14 13:13:41 -080026#include "ShadowTessellator.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080027
28#include <utils/Log.h>
29#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070030
31namespace android {
32
Chet Haasedd78cca2010-10-22 18:59:26 -070033using namespace uirenderer;
34ANDROID_SINGLETON_STATIC_INSTANCE(Caches);
Chet Haasedd78cca2010-10-22 18:59:26 -070035
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070039// Macros
40///////////////////////////////////////////////////////////////////////////////
41
42#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000043 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070044#else
45 #define FLUSH_LOGD(...)
46#endif
47
48///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070049// Constructors/destructor
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy8aa195d2013-06-04 18:00:09 -070052Caches::Caches(): Singleton<Caches>(),
Chris Craikd41c4d82015-01-05 15:51:13 -080053 mExtensions(Extensions::getInstance()), mInitialized(false), mRenderState(nullptr) {
Romain Guy8ff6b9e2011-11-09 20:10:18 -080054 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070055 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070056 initConstraints();
Romain Guy4ff0cf42012-08-06 14:51:10 -070057 initProperties();
Romain Guyf9f00162013-05-09 11:50:12 -070058 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080059 initExtensions();
Chris Craikba9b6132013-12-15 17:10:19 -080060 initTempProperties();
Romain Guye190aa62010-11-10 19:01:29 -080061
62 mDebugLevel = readDebugLevel();
Steve Block5baa3a62011-12-20 16:23:08 +000063 ALOGD("Enabling debug mode %d", mDebugLevel);
Chet Haasedd78cca2010-10-22 18:59:26 -070064}
65
Romain Guy3b748a42013-04-17 18:54:38 -070066bool Caches::init() {
67 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080068
John Reckfbc8df02014-11-14 16:18:41 -080069 ATRACE_NAME("Caches::init");
70
Romain Guy8ff6b9e2011-11-09 20:10:18 -080071 glGenBuffers(1, &meshBuffer);
72 glBindBuffer(GL_ARRAY_BUFFER, meshBuffer);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);
74
75 mCurrentBuffer = meshBuffer;
Romain Guy15bc6432011-12-13 13:11:32 -080076 mCurrentIndicesBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080077 mCurrentPositionPointer = this;
Chris Craikcb4d6002012-09-25 12:00:29 -070078 mCurrentPositionStride = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080079 mCurrentTexCoordsPointer = this;
Romain Guycf51a412013-04-08 19:40:31 -070080 mCurrentPixelBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -080081
Romain Guy15bc6432011-12-13 13:11:32 -080082 mTexCoordsArrayEnabled = false;
83
Romain Guya1d3c912011-12-13 14:55:06 -080084 glActiveTexture(gTextureUnits[0]);
85 mTextureUnit = 0;
86
Chris Craikd41c4d82015-01-05 15:51:13 -080087 mRegionMesh = nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070088 mMeshIndices = 0;
ztenghui63d41ab2014-02-14 13:13:41 -080089 mShadowStripsIndices = 0;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080090 blend = false;
91 lastSrcMode = GL_ZERO;
92 lastDstMode = GL_ZERO;
Chris Craikd41c4d82015-01-05 15:51:13 -080093 currentProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080094
Romain Guy54c1a642012-09-27 17:55:46 -070095 mFunctorsCount = 0;
96
Romain Guyc2a97212013-02-06 15:29:46 -080097 debugLayersUpdates = false;
98 debugOverdraw = false;
Romain Guy3ff0bfd2013-02-25 14:15:37 -080099 debugStencilClip = kStencilHide;
Romain Guyc2a97212013-02-06 15:29:46 -0800100
Romain Guy3b748a42013-04-17 18:54:38 -0700101 patchCache.init(*this);
102
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800103 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -0700104
Romain Guy8aa195d2013-06-04 18:00:09 -0700105 resetBoundTextures();
106
Romain Guy3b748a42013-04-17 18:54:38 -0700107 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800108}
109
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700110void Caches::initFont() {
111 fontRenderer = GammaFontRenderer::createRenderer();
112}
113
Romain Guydfa10462012-05-12 16:18:58 -0700114void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800115 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -0700116 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -0800117
Chris Craikff785832013-03-08 13:12:16 -0800118 startMark = glPushGroupMarkerEXT;
119 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -0700120 } else {
121 eventMark = eventMarkNull;
122 startMark = startMarkNull;
123 endMark = endMarkNull;
124 }
125
Romain Guy0f667532013-03-01 14:31:04 -0800126 if (mExtensions.hasDebugLabel() && (drawDeferDisabled || drawReorderDisabled)) {
Romain Guydfa10462012-05-12 16:18:58 -0700127 setLabel = glLabelObjectEXT;
128 getLabel = glGetObjectLabelEXT;
129 } else {
130 setLabel = setLabelNull;
131 getLabel = getLabelNull;
132 }
133}
134
135void Caches::initConstraints() {
136 GLint maxTextureUnits;
137 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
138 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
139 ALOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
140 }
141
142 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
143}
144
Romain Guyf9f00162013-05-09 11:50:12 -0700145void Caches::initStaticProperties() {
146 gpuPixelBuffersEnabled = false;
147
148 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700149 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700150 char property[PROPERTY_VALUE_MAX];
151 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
152 gpuPixelBuffersEnabled = !strcmp(property, "true");
153 }
154 }
155}
156
Romain Guy5bb3c732012-11-29 17:52:58 -0800157bool Caches::initProperties() {
158 bool prevDebugLayersUpdates = debugLayersUpdates;
159 bool prevDebugOverdraw = debugOverdraw;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800160 StencilClipDebug prevDebugStencilClip = debugStencilClip;
Romain Guy5bb3c732012-11-29 17:52:58 -0800161
Romain Guy4ff0cf42012-08-06 14:51:10 -0700162 char property[PROPERTY_VALUE_MAX];
Chris Craikd41c4d82015-01-05 15:51:13 -0800163 if (property_get(PROPERTY_DEBUG_LAYERS_UPDATES, property, nullptr) > 0) {
Romain Guy4ff0cf42012-08-06 14:51:10 -0700164 INIT_LOGD(" Layers updates debug enabled: %s", property);
165 debugLayersUpdates = !strcmp(property, "true");
166 } else {
167 debugLayersUpdates = false;
168 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700169
Romain Guy627c6fd2013-08-21 11:53:18 -0700170 debugOverdraw = false;
Chris Craikd41c4d82015-01-05 15:51:13 -0800171 if (property_get(PROPERTY_DEBUG_OVERDRAW, property, nullptr) > 0) {
Romain Guy7c450aa2012-09-21 19:15:00 -0700172 INIT_LOGD(" Overdraw debug enabled: %s", property);
Romain Guy627c6fd2013-08-21 11:53:18 -0700173 if (!strcmp(property, "show")) {
174 debugOverdraw = true;
175 mOverdrawDebugColorSet = kColorSet_Default;
176 } else if (!strcmp(property, "show_deuteranomaly")) {
177 debugOverdraw = true;
178 mOverdrawDebugColorSet = kColorSet_Deuteranomaly;
179 }
Romain Guy7c450aa2012-09-21 19:15:00 -0700180 }
Romain Guy5bb3c732012-11-29 17:52:58 -0800181
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800182 // See Properties.h for valid values
Chris Craikd41c4d82015-01-05 15:51:13 -0800183 if (property_get(PROPERTY_DEBUG_STENCIL_CLIP, property, nullptr) > 0) {
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800184 INIT_LOGD(" Stencil clip debug enabled: %s", property);
185 if (!strcmp(property, "hide")) {
186 debugStencilClip = kStencilHide;
187 } else if (!strcmp(property, "highlight")) {
188 debugStencilClip = kStencilShowHighlight;
189 } else if (!strcmp(property, "region")) {
190 debugStencilClip = kStencilShowRegion;
191 }
192 } else {
193 debugStencilClip = kStencilHide;
194 }
195
Romain Guy0f667532013-03-01 14:31:04 -0800196 if (property_get(PROPERTY_DISABLE_DRAW_DEFER, property, "false")) {
197 drawDeferDisabled = !strcasecmp(property, "true");
198 INIT_LOGD(" Draw defer %s", drawDeferDisabled ? "disabled" : "enabled");
199 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700200 drawDeferDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800201 INIT_LOGD(" Draw defer enabled");
202 }
203
204 if (property_get(PROPERTY_DISABLE_DRAW_REORDER, property, "false")) {
205 drawReorderDisabled = !strcasecmp(property, "true");
206 INIT_LOGD(" Draw reorder %s", drawReorderDisabled ? "disabled" : "enabled");
207 } else {
Chris Craik58ddced2014-07-16 19:11:46 -0700208 drawReorderDisabled = false;
Romain Guy0f667532013-03-01 14:31:04 -0800209 INIT_LOGD(" Draw reorder enabled");
210 }
211
Romain Guy5bb3c732012-11-29 17:52:58 -0800212 return (prevDebugLayersUpdates != debugLayersUpdates) ||
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800213 (prevDebugOverdraw != debugOverdraw) ||
214 (prevDebugStencilClip != debugStencilClip);
Romain Guy4ff0cf42012-08-06 14:51:10 -0700215}
216
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800217void Caches::terminate() {
218 if (!mInitialized) return;
219
220 glDeleteBuffers(1, &meshBuffer);
221 mCurrentBuffer = 0;
222
Romain Guy3b748a42013-04-17 18:54:38 -0700223 glDeleteBuffers(1, &mMeshIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700224 mMeshIndices = 0;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800225 mRegionMesh.release();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800226
ztenghui63d41ab2014-02-14 13:13:41 -0800227 glDeleteBuffers(1, &mShadowStripsIndices);
228 mShadowStripsIndices = 0;
229
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800230 fboCache.clear();
231
232 programCache.clear();
Chris Craikd41c4d82015-01-05 15:51:13 -0800233 currentProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800234
Romain Guy3b748a42013-04-17 18:54:38 -0700235 patchCache.clear();
236
Romain Guy46bfc482013-08-16 18:38:29 -0700237 clearGarbage();
238
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800239 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700240}
241
242///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800243// Debug
244///////////////////////////////////////////////////////////////////////////////
245
Romain Guy627c6fd2013-08-21 11:53:18 -0700246uint32_t Caches::getOverdrawColor(uint32_t amount) const {
247 static uint32_t sOverdrawColors[2][4] = {
248 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
249 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
250 };
251 if (amount < 1) amount = 1;
252 if (amount > 4) amount = 4;
253 return sOverdrawColors[mOverdrawDebugColorSet][amount - 1];
254}
255
Romain Guyc15008e2010-11-10 11:59:15 -0800256void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700257 String8 stringLog;
258 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000259 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700260}
261
262void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700263 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700264 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
265 log.appendFormat(" TextureCache %8d / %8d\n",
266 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700267 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
268 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700269 if (mRenderState) {
270 int memused = 0;
271 for (std::set<const Layer*>::iterator it = mRenderState->mActiveLayers.begin();
272 it != mRenderState->mActiveLayers.end(); it++) {
273 const Layer* layer = *it;
274 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
275 layer->getWidth(), layer->getHeight(),
276 layer->isTextureLayer(), layer->getTexture(),
277 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800278 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700279 }
280 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
281 memused, mRenderState->mActiveLayers.size());
282 total += memused;
283 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800284 log.appendFormat(" RenderBufferCache %8d / %8d\n",
285 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700286 log.appendFormat(" GradientCache %8d / %8d\n",
287 gradientCache.getSize(), gradientCache.getMaxSize());
288 log.appendFormat(" PathCache %8d / %8d\n",
289 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700290 log.appendFormat(" TessellationCache %8d / %8d\n",
291 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700292 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800293 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700294 log.appendFormat(" PatchCache %8d / %8d\n",
295 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700296 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700297 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
298 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
299 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
300 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
301 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
302 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800303 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700304 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700305 log.appendFormat(" FboCache %8d / %8d\n",
306 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800307
Romain Guyc15008e2010-11-10 11:59:15 -0800308 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800309 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800310 total += gradientCache.getSize();
311 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700312 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800313 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700314 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700315 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700316 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
317 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800318 }
319
Chet Haase9c1e23b2011-03-24 10:51:31 -0700320 log.appendFormat("Total memory usage:\n");
321 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800322}
323
324///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800325// Memory management
326///////////////////////////////////////////////////////////////////////////////
327
328void Caches::clearGarbage() {
329 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800330 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700331 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800332}
333
Romain Guybdf76092011-07-18 15:00:43 -0700334void Caches::flush(FlushMode mode) {
335 FLUSH_LOGD("Flushing caches (mode %d)", mode);
336
Romain Guyb0a41ed2013-08-16 14:44:38 -0700337 // We must stop tasks before clearing caches
338 if (mode > kFlushMode_Layers) {
339 tasks.stop();
340 }
341
Romain Guybdf76092011-07-18 15:00:43 -0700342 switch (mode) {
343 case kFlushMode_Full:
344 textureCache.clear();
345 patchCache.clear();
346 dropShadowCache.clear();
347 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700348 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700349 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700350 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700351 // fall through
352 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700353 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700354 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700355 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700356 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700357 // fall through
358 case kFlushMode_Layers:
359 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800360 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700361 break;
362 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700363
364 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700365 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700366}
367
Romain Guyfe48f652010-11-11 15:36:56 -0800368///////////////////////////////////////////////////////////////////////////////
Romain Guy5b3b3522010-10-27 18:57:51 -0700369// VBO
370///////////////////////////////////////////////////////////////////////////////
371
Romain Guyf3a910b42011-12-12 20:35:21 -0800372bool Caches::bindMeshBuffer() {
373 return bindMeshBuffer(meshBuffer);
Chet Haasedd78cca2010-10-22 18:59:26 -0700374}
375
Romain Guyf3a910b42011-12-12 20:35:21 -0800376bool Caches::bindMeshBuffer(const GLuint buffer) {
Romain Guy9bca4792010-10-25 18:42:25 -0700377 if (mCurrentBuffer != buffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700378 glBindBuffer(GL_ARRAY_BUFFER, buffer);
Romain Guy9bca4792010-10-25 18:42:25 -0700379 mCurrentBuffer = buffer;
Romain Guyf3a910b42011-12-12 20:35:21 -0800380 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700381 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800382 return false;
Chet Haasedd78cca2010-10-22 18:59:26 -0700383}
384
Romain Guyf3a910b42011-12-12 20:35:21 -0800385bool Caches::unbindMeshBuffer() {
Romain Guy9bca4792010-10-25 18:42:25 -0700386 if (mCurrentBuffer) {
Chet Haasedd78cca2010-10-22 18:59:26 -0700387 glBindBuffer(GL_ARRAY_BUFFER, 0);
Romain Guy9bca4792010-10-25 18:42:25 -0700388 mCurrentBuffer = 0;
Romain Guyf3a910b42011-12-12 20:35:21 -0800389 return true;
Chet Haasedd78cca2010-10-22 18:59:26 -0700390 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800391 return false;
392}
393
ztenghui63d41ab2014-02-14 13:13:41 -0800394bool Caches::bindIndicesBufferInternal(const GLuint buffer) {
Romain Guy15bc6432011-12-13 13:11:32 -0800395 if (mCurrentIndicesBuffer != buffer) {
396 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
397 mCurrentIndicesBuffer = buffer;
398 return true;
399 }
400 return false;
401}
402
ztenghui63d41ab2014-02-14 13:13:41 -0800403bool Caches::bindQuadIndicesBuffer() {
Romain Guy3b748a42013-04-17 18:54:38 -0700404 if (!mMeshIndices) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800405 std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[gMaxNumberOfQuads * 6]);
Romain Guy31e08e92013-06-18 15:53:53 -0700406 for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
Romain Guy3b748a42013-04-17 18:54:38 -0700407 uint16_t quad = i * 4;
408 int index = i * 6;
409 regionIndices[index ] = quad; // top-left
410 regionIndices[index + 1] = quad + 1; // top-right
411 regionIndices[index + 2] = quad + 2; // bottom-left
412 regionIndices[index + 3] = quad + 2; // bottom-left
413 regionIndices[index + 4] = quad + 1; // top-right
414 regionIndices[index + 5] = quad + 3; // bottom-right
415 }
416
417 glGenBuffers(1, &mMeshIndices);
ztenghui63d41ab2014-02-14 13:13:41 -0800418 bool force = bindIndicesBufferInternal(mMeshIndices);
Romain Guy31e08e92013-06-18 15:53:53 -0700419 glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
Chris Craik51d6a3d2014-12-22 17:16:56 -0800420 regionIndices.get(), GL_STATIC_DRAW);
Romain Guy3b748a42013-04-17 18:54:38 -0700421 return force;
422 }
423
ztenghui63d41ab2014-02-14 13:13:41 -0800424 return bindIndicesBufferInternal(mMeshIndices);
425}
426
427bool Caches::bindShadowIndicesBuffer() {
428 if (!mShadowStripsIndices) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800429 std::unique_ptr<uint16_t[]> shadowIndices(new uint16_t[MAX_SHADOW_INDEX_COUNT]);
430 ShadowTessellator::generateShadowIndices(shadowIndices.get());
ztenghui63d41ab2014-02-14 13:13:41 -0800431 glGenBuffers(1, &mShadowStripsIndices);
432 bool force = bindIndicesBufferInternal(mShadowStripsIndices);
ztenghui50ecf842014-03-11 16:52:30 -0700433 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
Chris Craik51d6a3d2014-12-22 17:16:56 -0800434 shadowIndices.get(), GL_STATIC_DRAW);
ztenghui63d41ab2014-02-14 13:13:41 -0800435 return force;
436 }
437
438 return bindIndicesBufferInternal(mShadowStripsIndices);
Romain Guy3b748a42013-04-17 18:54:38 -0700439}
440
Romain Guy15bc6432011-12-13 13:11:32 -0800441bool Caches::unbindIndicesBuffer() {
442 if (mCurrentIndicesBuffer) {
443 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
444 mCurrentIndicesBuffer = 0;
445 return true;
446 }
447 return false;
448}
449
Romain Guy85ef80d2012-09-13 20:26:50 -0700450///////////////////////////////////////////////////////////////////////////////
Romain Guycf51a412013-04-08 19:40:31 -0700451// PBO
452///////////////////////////////////////////////////////////////////////////////
453
454bool Caches::bindPixelBuffer(const GLuint buffer) {
455 if (mCurrentPixelBuffer != buffer) {
456 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
457 mCurrentPixelBuffer = buffer;
458 return true;
459 }
460 return false;
461}
462
463bool Caches::unbindPixelBuffer() {
464 if (mCurrentPixelBuffer) {
465 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
466 mCurrentPixelBuffer = 0;
467 return true;
468 }
469 return false;
470}
471
472///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700473// Meshes and textures
474///////////////////////////////////////////////////////////////////////////////
475
ztenghui55bfb4e2013-12-03 10:38:55 -0800476void Caches::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700477 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
478 GLuint slot = currentProgram->position;
Romain Guyf3a910b42011-12-12 20:35:21 -0800479 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
480 mCurrentPositionPointer = vertices;
Chris Craikcb4d6002012-09-25 12:00:29 -0700481 mCurrentPositionStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800482 }
483}
484
ztenghui55bfb4e2013-12-03 10:38:55 -0800485void Caches::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Romain Guyff316ec2013-02-13 18:39:43 -0800486 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craikcb4d6002012-09-25 12:00:29 -0700487 GLuint slot = currentProgram->texCoords;
Romain Guyff316ec2013-02-13 18:39:43 -0800488 glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Romain Guyf3a910b42011-12-12 20:35:21 -0800489 mCurrentTexCoordsPointer = vertices;
Romain Guyff316ec2013-02-13 18:39:43 -0800490 mCurrentTexCoordsStride = stride;
Romain Guyf3a910b42011-12-12 20:35:21 -0800491 }
492}
493
494void Caches::resetVertexPointers() {
495 mCurrentPositionPointer = this;
496 mCurrentTexCoordsPointer = this;
497}
498
499void Caches::resetTexCoordsVertexPointer() {
500 mCurrentTexCoordsPointer = this;
Chet Haasedd78cca2010-10-22 18:59:26 -0700501}
502
Romain Guy15bc6432011-12-13 13:11:32 -0800503void Caches::enableTexCoordsVertexArray() {
504 if (!mTexCoordsArrayEnabled) {
505 glEnableVertexAttribArray(Program::kBindingTexCoords);
Romain Guyec31f832011-12-13 18:39:19 -0800506 mCurrentTexCoordsPointer = this;
Romain Guy15bc6432011-12-13 13:11:32 -0800507 mTexCoordsArrayEnabled = true;
508 }
509}
510
Romain Guyff316ec2013-02-13 18:39:43 -0800511void Caches::disableTexCoordsVertexArray() {
Romain Guy15bc6432011-12-13 13:11:32 -0800512 if (mTexCoordsArrayEnabled) {
513 glDisableVertexAttribArray(Program::kBindingTexCoords);
514 mTexCoordsArrayEnabled = false;
515 }
516}
517
Romain Guya1d3c912011-12-13 14:55:06 -0800518void Caches::activeTexture(GLuint textureUnit) {
519 if (mTextureUnit != textureUnit) {
520 glActiveTexture(gTextureUnits[textureUnit]);
521 mTextureUnit = textureUnit;
522 }
523}
524
Chris Craik9ab2d182013-07-22 16:16:06 -0700525void Caches::resetActiveTexture() {
526 mTextureUnit = -1;
527}
528
Romain Guy8aa195d2013-06-04 18:00:09 -0700529void Caches::bindTexture(GLuint texture) {
530 if (mBoundTextures[mTextureUnit] != texture) {
531 glBindTexture(GL_TEXTURE_2D, texture);
532 mBoundTextures[mTextureUnit] = texture;
533 }
534}
535
536void Caches::bindTexture(GLenum target, GLuint texture) {
Fred Fettinger70735bd2014-08-29 14:02:31 -0500537 if (target == GL_TEXTURE_2D) {
538 bindTexture(texture);
539 } else {
540 // GLConsumer directly calls glBindTexture() with
541 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
542 // since the cached state could be stale
Romain Guy8aa195d2013-06-04 18:00:09 -0700543 glBindTexture(target, texture);
Romain Guy8aa195d2013-06-04 18:00:09 -0700544 }
545}
546
Romain Guybe1b1272013-06-06 14:02:54 -0700547void Caches::deleteTexture(GLuint texture) {
548 // When glDeleteTextures() is called on a currently bound texture,
549 // OpenGL ES specifies that the texture is then considered unbound
550 // Consider the following series of calls:
551 //
552 // glGenTextures -> creates texture name 2
553 // glBindTexture(2)
554 // glDeleteTextures(2) -> 2 is now unbound
555 // glGenTextures -> can return 2 again
556 //
557 // If we don't call glBindTexture(2) after the second glGenTextures
558 // call, any texture operation will be performed on the default
559 // texture (name=0)
560
jiayuanr4a473c7d2014-06-10 17:41:49 +0800561 unbindTexture(texture);
562
Romain Guybe1b1272013-06-06 14:02:54 -0700563 glDeleteTextures(1, &texture);
564}
565
Romain Guy8aa195d2013-06-04 18:00:09 -0700566void Caches::resetBoundTextures() {
567 memset(mBoundTextures, 0, REQUIRED_TEXTURE_UNITS_COUNT * sizeof(GLuint));
568}
569
jiayuanr4a473c7d2014-06-10 17:41:49 +0800570void Caches::unbindTexture(GLuint texture) {
571 for (int i = 0; i < REQUIRED_TEXTURE_UNITS_COUNT; i++) {
572 if (mBoundTextures[i] == texture) {
573 mBoundTextures[i] = 0;
574 }
575 }
576}
577
Romain Guy85ef80d2012-09-13 20:26:50 -0700578///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700579// Tiling
580///////////////////////////////////////////////////////////////////////////////
581
Romain Guyf735c8e2013-01-31 17:45:55 -0800582void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Romain Guy3bbacf22013-02-06 16:51:04 -0800583 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800584 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700585 }
586}
587
588void Caches::endTiling() {
Romain Guy3bbacf22013-02-06 16:51:04 -0800589 if (mExtensions.hasTiledRendering() && !debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700590 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700591 }
592}
593
Romain Guy54c1a642012-09-27 17:55:46 -0700594bool Caches::hasRegisteredFunctors() {
595 return mFunctorsCount > 0;
596}
597
598void Caches::registerFunctors(uint32_t functorCount) {
599 mFunctorsCount += functorCount;
600}
601
602void Caches::unregisterFunctors(uint32_t functorCount) {
603 if (functorCount > mFunctorsCount) {
604 mFunctorsCount = 0;
605 } else {
606 mFunctorsCount -= functorCount;
607 }
608}
609
Romain Guy85ef80d2012-09-13 20:26:50 -0700610///////////////////////////////////////////////////////////////////////////////
611// Regions
612///////////////////////////////////////////////////////////////////////////////
613
Romain Guy5b3b3522010-10-27 18:57:51 -0700614TextureVertex* Caches::getRegionMesh() {
615 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
616 if (!mRegionMesh) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800617 mRegionMesh.reset(new TextureVertex[gMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700618 }
619
Chris Craik51d6a3d2014-12-22 17:16:56 -0800620 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700621}
622
Chris Craikba9b6132013-12-15 17:10:19 -0800623///////////////////////////////////////////////////////////////////////////////
624// Temporary Properties
625///////////////////////////////////////////////////////////////////////////////
626
627void Caches::initTempProperties() {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700628 propertyLightDiameter = -1.0f;
629 propertyLightPosY = -1.0f;
630 propertyLightPosZ = -1.0f;
631 propertyAmbientRatio = -1.0f;
ztenghui14a4e352014-08-13 10:44:39 -0700632 propertyAmbientShadowStrength = -1;
633 propertySpotShadowStrength = -1;
Chris Craikba9b6132013-12-15 17:10:19 -0800634}
635
636void Caches::setTempProperty(const char* name, const char* value) {
637 ALOGD("setting property %s to %s", name, value);
Chris Craika736cd92014-08-04 13:18:38 -0700638 if (!strcmp(name, "ambientRatio")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700639 propertyAmbientRatio = fmin(fmax(atof(value), 0.0), 10.0);
640 ALOGD("ambientRatio = %.2f", propertyAmbientRatio);
ztenghuicc3c2562014-01-17 10:34:10 -0800641 return;
Chris Craikf5be3ca2014-04-30 18:20:03 -0700642 } else if (!strcmp(name, "lightDiameter")) {
643 propertyLightDiameter = fmin(fmax(atof(value), 0.0), 3000.0);
644 ALOGD("lightDiameter = %.2f", propertyLightDiameter);
ztenghuicc3c2562014-01-17 10:34:10 -0800645 return;
Chris Craik59744b72014-07-01 17:56:52 -0700646 } else if (!strcmp(name, "lightPosY")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700647 propertyLightPosY = fmin(fmax(atof(value), 0.0), 3000.0);
648 ALOGD("lightPos Y = %.2f", propertyLightPosY);
649 return;
Chris Craik59744b72014-07-01 17:56:52 -0700650 } else if (!strcmp(name, "lightPosZ")) {
Chris Craikf5be3ca2014-04-30 18:20:03 -0700651 propertyLightPosZ = fmin(fmax(atof(value), 0.0), 3000.0);
652 ALOGD("lightPos Z = %.2f", propertyLightPosZ);
ztenghuicc3c2562014-01-17 10:34:10 -0800653 return;
ztenghui14a4e352014-08-13 10:44:39 -0700654 } else if (!strcmp(name, "ambientShadowStrength")) {
655 propertyAmbientShadowStrength = atoi(value);
656 ALOGD("ambient shadow strength = 0x%x out of 0xff", propertyAmbientShadowStrength);
657 return;
658 } else if (!strcmp(name, "spotShadowStrength")) {
659 propertySpotShadowStrength = atoi(value);
660 ALOGD("spot shadow strength = 0x%x out of 0xff", propertySpotShadowStrength);
661 return;
Chris Craikba9b6132013-12-15 17:10:19 -0800662 }
663 ALOGD(" failed");
664}
665
Chet Haasedd78cca2010-10-22 18:59:26 -0700666}; // namespace uirenderer
667}; // namespace android