blob: 3ebd57b33c8101079248cb3fd149ff57dc3b5188 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 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 */
Chris Craik96a5c4c2015-01-27 15:46:35 -080016#include "renderstate/RenderState.h"
John Reck3b202512014-06-23 13:13:08 -070017
John Reck443a7142014-09-04 17:40:05 -070018#include "renderthread/CanvasContext.h"
John Reck0e89e2b2014-10-31 14:49:06 -070019#include "renderthread/EglManager.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080020#include "utils/GLUtils.h"
John Reck443a7142014-09-04 17:40:05 -070021
John Reck3b202512014-06-23 13:13:08 -070022namespace android {
23namespace uirenderer {
24
John Reck0e89e2b2014-10-31 14:49:06 -070025RenderState::RenderState(renderthread::RenderThread& thread)
26 : mRenderThread(thread)
John Reck3b202512014-06-23 13:13:08 -070027 , mViewportWidth(0)
28 , mViewportHeight(0)
29 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070030 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070031}
32
33RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080034 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080035 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070036}
37
38void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080039 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080040 "State object lifecycle not managed correctly");
Chris Craik44eb2c02015-01-29 09:45:09 -080041 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080042 mMeshState = new MeshState();
43 mScissor = new Scissor();
44 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080045
Chris Craik96a5c4c2015-01-27 15:46:35 -080046 // This is delayed because the first access of Caches makes GL calls
Chris Craikff5c8e82015-01-30 09:46:18 -080047 if (!mCaches) {
48 mCaches = &Caches::createInstance(*this);
49 }
Chris Craik96a5c4c2015-01-27 15:46:35 -080050 mCaches->init();
51 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070052}
53
John Reck49bc4ac2015-01-29 12:53:38 -080054static void layerLostGlContext(Layer* layer) {
55 layer->onGlContextLost();
56}
57
Chris Craik1d477422014-08-26 17:30:15 -070058void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070059/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070060 size_t size = mActiveLayers.size();
61 if (CC_UNLIKELY(size != 0)) {
62 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
63 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070064 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070065 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
66 cit != mRegisteredContexts.end(); cit++) {
67 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070068 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
69 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070070 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
71 pit != context->mPrefetechedLayers.end(); pit++) {
72 (*pit)->debugDumpLayers(" ");
73 }
74 context->mRootRenderNode->debugDumpLayers(" ");
75 }
Chris Craik599e2542014-09-05 15:17:11 -070076
Chris Craikbfd1cd62014-09-10 13:04:31 -070077
78 if (mActiveLayers.begin() == mActiveLayers.end()) {
79 ALOGE("set has become empty. wat.");
80 }
Chris Craik599e2542014-09-05 15:17:11 -070081 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
82 lit != mActiveLayers.end(); lit++) {
83 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070084 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
85 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070086 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070087 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070088 }
Chris Craik21029ef2014-09-12 09:29:10 -070089*/
John Reck49bc4ac2015-01-29 12:53:38 -080090
Chris Craik96a5c4c2015-01-27 15:46:35 -080091 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080092 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080093 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080094
Chris Craik44eb2c02015-01-29 09:45:09 -080095 mCaches->terminate();
96
97 delete mBlend;
98 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080099 delete mMeshState;
100 mMeshState = nullptr;
101 delete mScissor;
102 mScissor = nullptr;
103 delete mStencil;
104 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700105}
106
John Reck3b202512014-06-23 13:13:08 -0700107void RenderState::setViewport(GLsizei width, GLsizei height) {
108 mViewportWidth = width;
109 mViewportHeight = height;
110 glViewport(0, 0, mViewportWidth, mViewportHeight);
111}
112
113
114void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
115 *outWidth = mViewportWidth;
116 *outHeight = mViewportHeight;
117}
118
119void RenderState::bindFramebuffer(GLuint fbo) {
120 if (mFramebuffer != fbo) {
121 mFramebuffer = fbo;
122 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
123 }
124}
125
126void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
127 interruptForFunctorInvoke();
128 (*functor)(mode, info);
129 resumeFromFunctorInvoke();
130}
131
132void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800133 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800134 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800135 meshState().unbindMeshBuffer();
136 meshState().unbindIndicesBuffer();
137 meshState().resetVertexPointers();
138 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700139 debugOverdraw(false, false);
140}
141
142void RenderState::resumeFromFunctorInvoke() {
143 glViewport(0, 0, mViewportWidth, mViewportHeight);
144 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
145 debugOverdraw(false, false);
146
147 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
148
Chris Craik65fe5ee2015-01-26 18:06:29 -0800149 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800150 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700151
Chris Craik44eb2c02015-01-29 09:45:09 -0800152 mCaches->textureState().activateTexture(0);
153 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700154}
155
156void RenderState::debugOverdraw(bool enable, bool clear) {
Chris Craik2507c342015-05-04 14:36:49 -0700157 if (Properties::debugOverdraw && mFramebuffer == 0) {
John Reck3b202512014-06-23 13:13:08 -0700158 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800159 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800160 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700161 }
162 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800163 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700164 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800165 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700166 }
167 }
168}
169
John Reck0e89e2b2014-10-31 14:49:06 -0700170void RenderState::requireGLContext() {
171 assertOnGLThread();
John Reckd7db4d72015-05-20 07:18:55 -0700172 LOG_ALWAYS_FATAL_IF(!mRenderThread.eglManager().hasEglContext(),
173 "No GL context!");
John Reck0e89e2b2014-10-31 14:49:06 -0700174}
175
176void RenderState::assertOnGLThread() {
177 pthread_t curr = pthread_self();
178 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
179}
180
John Reck0e89e2b2014-10-31 14:49:06 -0700181class DecStrongTask : public renderthread::RenderTask {
182public:
183 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
184
Chris Craikd41c4d82015-01-05 15:51:13 -0800185 virtual void run() override {
186 mObject->decStrong(nullptr);
187 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700188 delete this;
189 }
190
191private:
192 VirtualLightRefBase* mObject;
193};
194
195void RenderState::postDecStrong(VirtualLightRefBase* object) {
196 mRenderThread.queue(new DecStrongTask(object));
197}
198
Chris Craik6c15ffa2015-02-02 13:50:55 -0800199///////////////////////////////////////////////////////////////////////////////
200// Render
201///////////////////////////////////////////////////////////////////////////////
202
Chris Craik6c15ffa2015-02-02 13:50:55 -0800203void RenderState::render(const Glop& glop) {
204 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800205 const Glop::Mesh::Vertices& vertices = mesh.vertices;
206 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c8102015-02-11 13:17:06 -0800207 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800208
Chris Craik0519c8102015-02-11 13:17:06 -0800209 // ---------------------------------------------
210 // ---------- Program + uniform setup ----------
211 // ---------------------------------------------
212 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800213
Chris Craik0519c8102015-02-11 13:17:06 -0800214 if (fill.colorEnabled) {
215 fill.program->setColor(fill.color);
216 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800217
Chris Craik0519c8102015-02-11 13:17:06 -0800218 fill.program->set(glop.transform.ortho,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800219 glop.transform.modelView,
Chris Craik53e51e42015-06-01 10:35:35 -0700220 glop.transform.meshTransform(),
221 glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800222
Chris Craik0519c8102015-02-11 13:17:06 -0800223 // Color filter uniforms
Chris Craikef250742015-02-25 17:16:16 -0800224 if (fill.filterMode == ProgramDescription::kColorBlend) {
225 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800226 glUniform4f(mCaches->program().getUniform("colorBlend"),
227 color.r, color.g, color.b, color.a);
Chris Craikef250742015-02-25 17:16:16 -0800228 } else if (fill.filterMode == ProgramDescription::kColorMatrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800229 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800230 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800231 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800232 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800233 }
234
Chris Craik0519c8102015-02-11 13:17:06 -0800235 // Round rect clipping uniforms
236 if (glop.roundRectClipState) {
237 // TODO: avoid query, and cache values (or RRCS ptr) in program
238 const RoundRectClipState* state = glop.roundRectClipState;
239 const Rect& innerRect = state->innerRect;
240 glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
241 innerRect.left, innerRect.top,
242 innerRect.right, innerRect.bottom);
243 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
244 1, GL_FALSE, &state->matrix.data[0]);
245
246 // add half pixel to round out integer rect space to cover pixel centers
247 float roundedOutRadius = state->radius + 0.5f;
248 glUniform1f(fill.program->getUniform("roundRectRadius"),
249 roundedOutRadius);
250 }
Chris Craikef250742015-02-25 17:16:16 -0800251
Chris Craik117bdbc2015-02-05 10:12:38 -0800252 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800253 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800254 // --------------------------------
255 // vertices
Chris Craikef250742015-02-25 17:16:16 -0800256 const bool force = meshState().bindMeshBufferInternal(vertices.bufferObject)
257 || (vertices.position != nullptr);
258 meshState().bindPositionVertexPointer(force, vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800259
260 // indices
Chris Craikef250742015-02-25 17:16:16 -0800261 meshState().bindIndicesBufferInternal(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800262
Chris Craik53e51e42015-06-01 10:35:35 -0700263 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik26bf3422015-02-26 16:28:17 -0800264 const Glop::Fill::TextureData& texture = fill.texture;
265 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c8102015-02-11 13:17:06 -0800266 mCaches->textureState().activateTexture(0);
267
Chris Craik26bf3422015-02-26 16:28:17 -0800268 if (texture.clamp != GL_INVALID_ENUM) {
Chris Craik5f1356c2015-05-27 11:06:21 -0700269 texture.texture->setWrap(texture.clamp, true, false, texture.target);
Chris Craik30036092015-02-12 10:41:39 -0800270 }
Chris Craik26bf3422015-02-26 16:28:17 -0800271 if (texture.filter != GL_INVALID_ENUM) {
Chris Craik5f1356c2015-05-27 11:06:21 -0700272 texture.texture->setFilter(texture.filter, true, false, texture.target);
Chris Craik30036092015-02-12 10:41:39 -0800273 }
Chris Craik0519c8102015-02-11 13:17:06 -0800274
Chris Craik26bf3422015-02-26 16:28:17 -0800275 mCaches->textureState().bindTexture(texture.target, texture.texture->id);
Chris Craik0519c8102015-02-11 13:17:06 -0800276 meshState().enableTexCoordsVertexArray();
Chris Craikef250742015-02-25 17:16:16 -0800277 meshState().bindTexCoordsVertexPointer(force, vertices.texCoord, vertices.stride);
Chris Craik26bf3422015-02-26 16:28:17 -0800278
279 if (texture.textureTransform) {
280 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
281 GL_FALSE, &texture.textureTransform->data[0]);
282 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800283 } else {
284 meshState().disableTexCoordsVertexArray();
285 }
Chris Craikef250742015-02-25 17:16:16 -0800286 int colorLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700287 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800288 colorLocation = fill.program->getAttrib("colors");
289 glEnableVertexAttribArray(colorLocation);
290 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800291 }
Chris Craikef250742015-02-25 17:16:16 -0800292 int alphaLocation = -1;
Chris Craik53e51e42015-06-01 10:35:35 -0700293 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800294 // NOTE: alpha vertex position is computed assuming no VBO
295 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
296 alphaLocation = fill.program->getAttrib("vtxAlpha");
297 glEnableVertexAttribArray(alphaLocation);
298 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800299 }
Chris Craik922d3a72015-02-13 17:47:21 -0800300 // Shader uniforms
Chris Craikef250742015-02-25 17:16:16 -0800301 SkiaShader::apply(*mCaches, fill.skiaShaderData);
Chris Craik922d3a72015-02-13 17:47:21 -0800302
Chris Craik117bdbc2015-02-05 10:12:38 -0800303 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800304 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800305 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800306 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800307
Chris Craik117bdbc2015-02-05 10:12:38 -0800308 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800309 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800310 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800311 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800312 // Since the indexed quad list is of limited length, we loop over
313 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c8102015-02-11 13:17:06 -0800314 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800315 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800316 while (elementsCount > 0) {
317 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
318
Chris Craikf27133d2015-02-19 09:51:53 -0800319 // rebind pointers without forcing, since initial bind handled above
Chris Craikef250742015-02-25 17:16:16 -0800320 meshState().bindPositionVertexPointer(false, vertexData, vertices.stride);
Chris Craik53e51e42015-06-01 10:35:35 -0700321 if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craikf27133d2015-02-19 09:51:53 -0800322 meshState().bindTexCoordsVertexPointer(false,
Chris Craikef250742015-02-25 17:16:16 -0800323 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800324 }
325
Chris Craik2ab95d72015-02-06 15:25:51 -0800326 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
327 elementsCount -= drawCount;
Chris Craikef250742015-02-25 17:16:16 -0800328 vertexData += (drawCount / 6) * 4 * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800329 }
Chris Craikef250742015-02-25 17:16:16 -0800330 } else if (indices.bufferObject || indices.indices) {
331 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800332 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800333 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800334 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800335
Chris Craik2ab95d72015-02-06 15:25:51 -0800336 // -----------------------------------
337 // ---------- Mesh teardown ----------
338 // -----------------------------------
Chris Craik53e51e42015-06-01 10:35:35 -0700339 if (vertices.attribFlags & VertexAttribFlags::Alpha) {
Chris Craikef250742015-02-25 17:16:16 -0800340 glDisableVertexAttribArray(alphaLocation);
341 }
Chris Craik53e51e42015-06-01 10:35:35 -0700342 if (vertices.attribFlags & VertexAttribFlags::Color) {
Chris Craikef250742015-02-25 17:16:16 -0800343 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800344 }
345}
346
347void RenderState::dump() {
348 blend().dump();
349 meshState().dump();
350 scissor().dump();
351 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800352}
353
John Reck3b202512014-06-23 13:13:08 -0700354} /* namespace uirenderer */
355} /* namespace android */