blob: 7b44d6db9a819045cdbb19470b1c9fa229fca697 [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) {
157 if (mCaches->debugOverdraw && mFramebuffer == 0) {
158 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();
172 mRenderThread.eglManager().requireGlContext();
173}
174
175void RenderState::assertOnGLThread() {
176 pthread_t curr = pthread_self();
177 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
178}
179
John Reck0e89e2b2014-10-31 14:49:06 -0700180class DecStrongTask : public renderthread::RenderTask {
181public:
182 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
183
Chris Craikd41c4d82015-01-05 15:51:13 -0800184 virtual void run() override {
185 mObject->decStrong(nullptr);
186 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700187 delete this;
188 }
189
190private:
191 VirtualLightRefBase* mObject;
192};
193
194void RenderState::postDecStrong(VirtualLightRefBase* object) {
195 mRenderThread.queue(new DecStrongTask(object));
196}
197
Chris Craik6c15ffa2015-02-02 13:50:55 -0800198///////////////////////////////////////////////////////////////////////////////
199// Render
200///////////////////////////////////////////////////////////////////////////////
201
Chris Craik6c15ffa2015-02-02 13:50:55 -0800202void RenderState::render(const Glop& glop) {
203 const Glop::Mesh& mesh = glop.mesh;
Chris Craikef250742015-02-25 17:16:16 -0800204 const Glop::Mesh::Vertices& vertices = mesh.vertices;
205 const Glop::Mesh::Indices& indices = mesh.indices;
Chris Craik0519c812015-02-11 13:17:06 -0800206 const Glop::Fill& fill = glop.fill;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800207
Chris Craik0519c812015-02-11 13:17:06 -0800208 // ---------------------------------------------
209 // ---------- Program + uniform setup ----------
210 // ---------------------------------------------
211 mCaches->setProgram(fill.program);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800212
Chris Craik0519c812015-02-11 13:17:06 -0800213 if (fill.colorEnabled) {
214 fill.program->setColor(fill.color);
215 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800216
Chris Craik0519c812015-02-11 13:17:06 -0800217 fill.program->set(glop.transform.ortho,
Chris Craik6c15ffa2015-02-02 13:50:55 -0800218 glop.transform.modelView,
219 glop.transform.canvas,
Chris Craik117bdbc2015-02-05 10:12:38 -0800220 glop.transform.fudgingOffset);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800221
Chris Craik0519c812015-02-11 13:17:06 -0800222 // Color filter uniforms
Chris Craikef250742015-02-25 17:16:16 -0800223 if (fill.filterMode == ProgramDescription::kColorBlend) {
224 const FloatColor& color = fill.filter.color;
Chris Craik117bdbc2015-02-05 10:12:38 -0800225 glUniform4f(mCaches->program().getUniform("colorBlend"),
226 color.r, color.g, color.b, color.a);
Chris Craikef250742015-02-25 17:16:16 -0800227 } else if (fill.filterMode == ProgramDescription::kColorMatrix) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800228 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
Chris Craikef250742015-02-25 17:16:16 -0800229 fill.filter.matrix.matrix);
Chris Craik117bdbc2015-02-05 10:12:38 -0800230 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
Chris Craikef250742015-02-25 17:16:16 -0800231 fill.filter.matrix.vector);
Chris Craik117bdbc2015-02-05 10:12:38 -0800232 }
233
Chris Craik0519c812015-02-11 13:17:06 -0800234 // Round rect clipping uniforms
235 if (glop.roundRectClipState) {
236 // TODO: avoid query, and cache values (or RRCS ptr) in program
237 const RoundRectClipState* state = glop.roundRectClipState;
238 const Rect& innerRect = state->innerRect;
239 glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
240 innerRect.left, innerRect.top,
241 innerRect.right, innerRect.bottom);
242 glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
243 1, GL_FALSE, &state->matrix.data[0]);
244
245 // add half pixel to round out integer rect space to cover pixel centers
246 float roundedOutRadius = state->radius + 0.5f;
247 glUniform1f(fill.program->getUniform("roundRectRadius"),
248 roundedOutRadius);
249 }
Chris Craikef250742015-02-25 17:16:16 -0800250
Chris Craik117bdbc2015-02-05 10:12:38 -0800251 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800252 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800253 // --------------------------------
254 // vertices
Chris Craikef250742015-02-25 17:16:16 -0800255 const bool force = meshState().bindMeshBufferInternal(vertices.bufferObject)
256 || (vertices.position != nullptr);
257 meshState().bindPositionVertexPointer(force, vertices.position, vertices.stride);
Chris Craik117bdbc2015-02-05 10:12:38 -0800258
259 // indices
Chris Craikef250742015-02-25 17:16:16 -0800260 meshState().bindIndicesBufferInternal(indices.bufferObject);
Chris Craik117bdbc2015-02-05 10:12:38 -0800261
Chris Craikeb911c22015-03-06 17:30:11 -0800262 if (vertices.attribFlags & VertexAttribFlags::kTextureCoord) {
Chris Craik26bf3422015-02-26 16:28:17 -0800263 const Glop::Fill::TextureData& texture = fill.texture;
264 // texture always takes slot 0, shader samplers increment from there
Chris Craik0519c812015-02-11 13:17:06 -0800265 mCaches->textureState().activateTexture(0);
266
Chris Craik26bf3422015-02-26 16:28:17 -0800267 if (texture.clamp != GL_INVALID_ENUM) {
268 texture.texture->setWrap(texture.clamp, true);
Chris Craik30036092015-02-12 10:41:39 -0800269 }
Chris Craik26bf3422015-02-26 16:28:17 -0800270 if (texture.filter != GL_INVALID_ENUM) {
271 texture.texture->setFilter(texture.filter, true);
Chris Craik30036092015-02-12 10:41:39 -0800272 }
Chris Craik0519c812015-02-11 13:17:06 -0800273
Chris Craik26bf3422015-02-26 16:28:17 -0800274 mCaches->textureState().bindTexture(texture.target, texture.texture->id);
Chris Craik0519c812015-02-11 13:17:06 -0800275 meshState().enableTexCoordsVertexArray();
Chris Craikef250742015-02-25 17:16:16 -0800276 meshState().bindTexCoordsVertexPointer(force, vertices.texCoord, vertices.stride);
Chris Craik26bf3422015-02-26 16:28:17 -0800277
278 if (texture.textureTransform) {
279 glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
280 GL_FALSE, &texture.textureTransform->data[0]);
281 }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800282 } else {
283 meshState().disableTexCoordsVertexArray();
284 }
Chris Craikef250742015-02-25 17:16:16 -0800285 int colorLocation = -1;
Chris Craikeb911c22015-03-06 17:30:11 -0800286 if (vertices.attribFlags & VertexAttribFlags::kColor) {
Chris Craikef250742015-02-25 17:16:16 -0800287 colorLocation = fill.program->getAttrib("colors");
288 glEnableVertexAttribArray(colorLocation);
289 glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800290 }
Chris Craikef250742015-02-25 17:16:16 -0800291 int alphaLocation = -1;
Chris Craikeb911c22015-03-06 17:30:11 -0800292 if (vertices.attribFlags & VertexAttribFlags::kAlpha) {
Chris Craikef250742015-02-25 17:16:16 -0800293 // NOTE: alpha vertex position is computed assuming no VBO
294 const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
295 alphaLocation = fill.program->getAttrib("vtxAlpha");
296 glEnableVertexAttribArray(alphaLocation);
297 glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800298 }
Chris Craik922d3a72015-02-13 17:47:21 -0800299 // Shader uniforms
Chris Craikef250742015-02-25 17:16:16 -0800300 SkiaShader::apply(*mCaches, fill.skiaShaderData);
Chris Craik922d3a72015-02-13 17:47:21 -0800301
Chris Craik117bdbc2015-02-05 10:12:38 -0800302 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800303 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800304 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800305 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800306
Chris Craik117bdbc2015-02-05 10:12:38 -0800307 // ------------------------------------
Chris Craik2ab95d72015-02-06 15:25:51 -0800308 // ---------- Actual drawing ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800309 // ------------------------------------
Chris Craikef250742015-02-25 17:16:16 -0800310 if (indices.bufferObject == meshState().getQuadListIBO()) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800311 // Since the indexed quad list is of limited length, we loop over
312 // the glDrawXXX method while updating the vertex pointer
Chris Craik0519c812015-02-11 13:17:06 -0800313 GLsizei elementsCount = mesh.elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800314 const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
Chris Craik2ab95d72015-02-06 15:25:51 -0800315 while (elementsCount > 0) {
316 GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
317
Chris Craikf27133d2015-02-19 09:51:53 -0800318 // rebind pointers without forcing, since initial bind handled above
Chris Craikef250742015-02-25 17:16:16 -0800319 meshState().bindPositionVertexPointer(false, vertexData, vertices.stride);
Chris Craikeb911c22015-03-06 17:30:11 -0800320 if (vertices.attribFlags & VertexAttribFlags::kTextureCoord) {
Chris Craikf27133d2015-02-19 09:51:53 -0800321 meshState().bindTexCoordsVertexPointer(false,
Chris Craikef250742015-02-25 17:16:16 -0800322 vertexData + kMeshTextureOffset, vertices.stride);
Chris Craikf27133d2015-02-19 09:51:53 -0800323 }
324
Chris Craik2ab95d72015-02-06 15:25:51 -0800325 glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
326 elementsCount -= drawCount;
Chris Craikef250742015-02-25 17:16:16 -0800327 vertexData += (drawCount / 6) * 4 * vertices.stride;
Chris Craik2ab95d72015-02-06 15:25:51 -0800328 }
Chris Craikef250742015-02-25 17:16:16 -0800329 } else if (indices.bufferObject || indices.indices) {
330 glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800331 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800332 glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800333 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800334
Chris Craik2ab95d72015-02-06 15:25:51 -0800335 // -----------------------------------
336 // ---------- Mesh teardown ----------
337 // -----------------------------------
Chris Craikeb911c22015-03-06 17:30:11 -0800338 if (vertices.attribFlags & VertexAttribFlags::kAlpha) {
Chris Craikef250742015-02-25 17:16:16 -0800339 glDisableVertexAttribArray(alphaLocation);
340 }
Chris Craikeb911c22015-03-06 17:30:11 -0800341 if (vertices.attribFlags & VertexAttribFlags::kColor) {
Chris Craikef250742015-02-25 17:16:16 -0800342 glDisableVertexAttribArray(colorLocation);
Chris Craik117bdbc2015-02-05 10:12:38 -0800343 }
344}
345
346void RenderState::dump() {
347 blend().dump();
348 meshState().dump();
349 scissor().dump();
350 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800351}
352
John Reck3b202512014-06-23 13:13:08 -0700353} /* namespace uirenderer */
354} /* namespace android */