blob: a819d9a908640ff382e3ae7c522ec25145a2f605 [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
47 mCaches = &Caches::createInstance(*this);
48 mCaches->init();
49 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070050}
51
John Reck49bc4ac2015-01-29 12:53:38 -080052static void layerLostGlContext(Layer* layer) {
53 layer->onGlContextLost();
54}
55
Chris Craik1d477422014-08-26 17:30:15 -070056void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070057/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070058 size_t size = mActiveLayers.size();
59 if (CC_UNLIKELY(size != 0)) {
60 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
61 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070062 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070063 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
64 cit != mRegisteredContexts.end(); cit++) {
65 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070066 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
67 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070068 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
69 pit != context->mPrefetechedLayers.end(); pit++) {
70 (*pit)->debugDumpLayers(" ");
71 }
72 context->mRootRenderNode->debugDumpLayers(" ");
73 }
Chris Craik599e2542014-09-05 15:17:11 -070074
Chris Craikbfd1cd62014-09-10 13:04:31 -070075
76 if (mActiveLayers.begin() == mActiveLayers.end()) {
77 ALOGE("set has become empty. wat.");
78 }
Chris Craik599e2542014-09-05 15:17:11 -070079 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
80 lit != mActiveLayers.end(); lit++) {
81 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070082 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
83 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070084 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070085 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070086 }
Chris Craik21029ef2014-09-12 09:29:10 -070087*/
John Reck49bc4ac2015-01-29 12:53:38 -080088
Chris Craik96a5c4c2015-01-27 15:46:35 -080089 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080090 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080091 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080092
Chris Craik44eb2c02015-01-29 09:45:09 -080093 mCaches->terminate();
94
95 delete mBlend;
96 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080097 delete mMeshState;
98 mMeshState = nullptr;
99 delete mScissor;
100 mScissor = nullptr;
101 delete mStencil;
102 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700103}
104
John Reck3b202512014-06-23 13:13:08 -0700105void RenderState::setViewport(GLsizei width, GLsizei height) {
106 mViewportWidth = width;
107 mViewportHeight = height;
108 glViewport(0, 0, mViewportWidth, mViewportHeight);
109}
110
111
112void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
113 *outWidth = mViewportWidth;
114 *outHeight = mViewportHeight;
115}
116
117void RenderState::bindFramebuffer(GLuint fbo) {
118 if (mFramebuffer != fbo) {
119 mFramebuffer = fbo;
120 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
121 }
122}
123
124void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
125 interruptForFunctorInvoke();
126 (*functor)(mode, info);
127 resumeFromFunctorInvoke();
128}
129
130void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800131 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800132 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800133 meshState().unbindMeshBuffer();
134 meshState().unbindIndicesBuffer();
135 meshState().resetVertexPointers();
136 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700137 debugOverdraw(false, false);
138}
139
140void RenderState::resumeFromFunctorInvoke() {
141 glViewport(0, 0, mViewportWidth, mViewportHeight);
142 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
143 debugOverdraw(false, false);
144
145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
146
Chris Craik65fe5ee2015-01-26 18:06:29 -0800147 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800148 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700149
Chris Craik44eb2c02015-01-29 09:45:09 -0800150 mCaches->textureState().activateTexture(0);
151 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700152}
153
154void RenderState::debugOverdraw(bool enable, bool clear) {
155 if (mCaches->debugOverdraw && mFramebuffer == 0) {
156 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800157 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800158 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700159 }
160 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800161 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700162 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800163 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700164 }
165 }
166}
167
John Reck0e89e2b2014-10-31 14:49:06 -0700168void RenderState::requireGLContext() {
169 assertOnGLThread();
170 mRenderThread.eglManager().requireGlContext();
171}
172
173void RenderState::assertOnGLThread() {
174 pthread_t curr = pthread_self();
175 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
176}
177
John Reck0e89e2b2014-10-31 14:49:06 -0700178class DecStrongTask : public renderthread::RenderTask {
179public:
180 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
181
Chris Craikd41c4d82015-01-05 15:51:13 -0800182 virtual void run() override {
183 mObject->decStrong(nullptr);
184 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700185 delete this;
186 }
187
188private:
189 VirtualLightRefBase* mObject;
190};
191
192void RenderState::postDecStrong(VirtualLightRefBase* object) {
193 mRenderThread.queue(new DecStrongTask(object));
194}
195
Chris Craik6c15ffa2015-02-02 13:50:55 -0800196///////////////////////////////////////////////////////////////////////////////
197// Render
198///////////////////////////////////////////////////////////////////////////////
199
200/*
201 * Not yet supported:
202 *
203 * Textures + coordinates
204 * SkiaShader
205 * ColorFilter
206 *
207 // TODO: texture coord
208 // TODO: texture support
209 // TODO: skiashader support
210 // TODO: color filter support
211 */
212
213void RenderState::render(const Glop& glop) {
214 const Glop::Mesh& mesh = glop.mesh;
215 const Glop::Fill& shader = glop.fill;
216
Chris Craik117bdbc2015-02-05 10:12:38 -0800217 // --------------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800218 // ---------- Shader + uniform setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800219 // --------------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800220 mCaches->setProgram(shader.program);
221
Chris Craik117bdbc2015-02-05 10:12:38 -0800222 Glop::FloatColor color = shader.color;
Chris Craik03188872015-02-02 18:39:33 -0800223 shader.program->setColor(color.r, color.g, color.b, color.a);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800224
225 shader.program->set(glop.transform.ortho,
226 glop.transform.modelView,
227 glop.transform.canvas,
Chris Craik117bdbc2015-02-05 10:12:38 -0800228 glop.transform.fudgingOffset);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800229
Chris Craik117bdbc2015-02-05 10:12:38 -0800230 if (glop.fill.filterMode == ProgramDescription::kColorBlend) {
231 const Glop::FloatColor& color = glop.fill.filter.color;
232 glUniform4f(mCaches->program().getUniform("colorBlend"),
233 color.r, color.g, color.b, color.a);
234 } else if (glop.fill.filterMode == ProgramDescription::kColorMatrix) {
235 glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
236 glop.fill.filter.matrix.matrix);
237 glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
238 glop.fill.filter.matrix.vector);
239 }
240
241 // --------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800242 // ---------- Mesh setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800243 // --------------------------------
244 // vertices
245 bool force = meshState().bindMeshBufferInternal(mesh.vertexBufferObject)
246 || (mesh.vertices != nullptr);
247 meshState().bindPositionVertexPointer(force, mesh.vertices, mesh.stride);
248
249 // indices
250 meshState().bindIndicesBufferInternal(mesh.indexBufferObject);
251
Chris Craik6c15ffa2015-02-02 13:50:55 -0800252 if (glop.mesh.vertexFlags & kTextureCoord_Attrib) {
253 // TODO: support textures
254 LOG_ALWAYS_FATAL("textures not yet supported");
255 } else {
256 meshState().disableTexCoordsVertexArray();
257 }
258 if (glop.mesh.vertexFlags & kColor_Attrib) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800259 LOG_ALWAYS_FATAL("color vertex attribute not yet supported");
Chris Craik6c15ffa2015-02-02 13:50:55 -0800260 // TODO: enable color, disable when done
261 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800262 int alphaSlot = -1;
Chris Craik6c15ffa2015-02-02 13:50:55 -0800263 if (glop.mesh.vertexFlags & kAlpha_Attrib) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800264 const void* alphaCoords = ((const GLbyte*) glop.mesh.vertices) + kVertexAlphaOffset;
265 alphaSlot = shader.program->getAttrib("vtxAlpha");
266 glEnableVertexAttribArray(alphaSlot);
267 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800268 }
269
Chris Craik117bdbc2015-02-05 10:12:38 -0800270 // ------------------------------------
Chris Craik6c15ffa2015-02-02 13:50:55 -0800271 // ---------- GL state setup ----------
Chris Craik117bdbc2015-02-05 10:12:38 -0800272 // ------------------------------------
Chris Craik03188872015-02-02 18:39:33 -0800273 blend().setFactors(glop.blend.src, glop.blend.dst);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800274
Chris Craik117bdbc2015-02-05 10:12:38 -0800275 // ------------------------------------
276 // ---------- GL state setup ----------
277 // ------------------------------------
278 if (mesh.indexBufferObject || mesh.indices) {
279 glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount,
280 GL_UNSIGNED_SHORT, mesh.indices);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800281 } else {
Chris Craik117bdbc2015-02-05 10:12:38 -0800282 glDrawArrays(glop.mesh.primitiveMode, 0, glop.mesh.vertexCount);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800283 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800284
285 if (glop.mesh.vertexFlags & kAlpha_Attrib) {
286 glDisableVertexAttribArray(alphaSlot);
287 }
288}
289
290void RenderState::dump() {
291 blend().dump();
292 meshState().dump();
293 scissor().dump();
294 stencil().dump();
Chris Craik6c15ffa2015-02-02 13:50:55 -0800295}
296
John Reck3b202512014-06-23 13:13:08 -0700297} /* namespace uirenderer */
298} /* namespace android */