blob: 8eda7c97eaa063b315f2e711ad8f4d0945c84d76 [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"
John Reck443a7142014-09-04 17:40:05 -070020
John Reck3b202512014-06-23 13:13:08 -070021namespace android {
22namespace uirenderer {
23
John Reck0e89e2b2014-10-31 14:49:06 -070024RenderState::RenderState(renderthread::RenderThread& thread)
25 : mRenderThread(thread)
John Reck3b202512014-06-23 13:13:08 -070026 , mViewportWidth(0)
27 , mViewportHeight(0)
28 , mFramebuffer(0) {
John Reck0e89e2b2014-10-31 14:49:06 -070029 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -070030}
31
32RenderState::~RenderState() {
Chris Craik44eb2c02015-01-29 09:45:09 -080033 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080034 "State object lifecycle not managed correctly");
John Reck3b202512014-06-23 13:13:08 -070035}
36
37void RenderState::onGLContextCreated() {
Chris Craik44eb2c02015-01-29 09:45:09 -080038 LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
Chris Craik96a5c4c2015-01-27 15:46:35 -080039 "State object lifecycle not managed correctly");
Chris Craik44eb2c02015-01-29 09:45:09 -080040 mBlend = new Blend();
Chris Craik96a5c4c2015-01-27 15:46:35 -080041 mMeshState = new MeshState();
42 mScissor = new Scissor();
43 mStencil = new Stencil();
Chris Craik65fe5ee2015-01-26 18:06:29 -080044
Chris Craik96a5c4c2015-01-27 15:46:35 -080045 // This is delayed because the first access of Caches makes GL calls
46 mCaches = &Caches::createInstance(*this);
47 mCaches->init();
48 mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
John Reck3b202512014-06-23 13:13:08 -070049}
50
John Reck49bc4ac2015-01-29 12:53:38 -080051static void layerLostGlContext(Layer* layer) {
52 layer->onGlContextLost();
53}
54
Chris Craik1d477422014-08-26 17:30:15 -070055void RenderState::onGLContextDestroyed() {
Chris Craik21029ef2014-09-12 09:29:10 -070056/*
Chris Craikbfd1cd62014-09-10 13:04:31 -070057 size_t size = mActiveLayers.size();
58 if (CC_UNLIKELY(size != 0)) {
59 ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
60 mRegisteredContexts.size(), size, mActiveLayers.empty());
John Reck17035b02014-09-03 07:39:53 -070061 mCaches->dumpMemoryUsage();
John Reck443a7142014-09-04 17:40:05 -070062 for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
63 cit != mRegisteredContexts.end(); cit++) {
64 renderthread::CanvasContext* context = *cit;
Chris Craikbfd1cd62014-09-10 13:04:31 -070065 ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
66 ALOGE(" Prefeteched layers: %zu", context->mPrefetechedLayers.size());
John Reck443a7142014-09-04 17:40:05 -070067 for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
68 pit != context->mPrefetechedLayers.end(); pit++) {
69 (*pit)->debugDumpLayers(" ");
70 }
71 context->mRootRenderNode->debugDumpLayers(" ");
72 }
Chris Craik599e2542014-09-05 15:17:11 -070073
Chris Craikbfd1cd62014-09-10 13:04:31 -070074
75 if (mActiveLayers.begin() == mActiveLayers.end()) {
76 ALOGE("set has become empty. wat.");
77 }
Chris Craik599e2542014-09-05 15:17:11 -070078 for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
79 lit != mActiveLayers.end(); lit++) {
80 const Layer* layer = *(lit);
Chris Craikbfd1cd62014-09-10 13:04:31 -070081 ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
82 layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
Chris Craik599e2542014-09-05 15:17:11 -070083 }
Chris Craikbfd1cd62014-09-10 13:04:31 -070084 LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
John Reck17035b02014-09-03 07:39:53 -070085 }
Chris Craik21029ef2014-09-12 09:29:10 -070086*/
John Reck49bc4ac2015-01-29 12:53:38 -080087
Chris Craik96a5c4c2015-01-27 15:46:35 -080088 // TODO: reset all cached state in state objects
John Reck49bc4ac2015-01-29 12:53:38 -080089 std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
John Reckebd52612014-12-10 16:47:36 -080090 mAssetAtlas.terminate();
Chris Craik96a5c4c2015-01-27 15:46:35 -080091
Chris Craik44eb2c02015-01-29 09:45:09 -080092 mCaches->terminate();
93
94 delete mBlend;
95 mBlend = nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080096 delete mMeshState;
97 mMeshState = nullptr;
98 delete mScissor;
99 mScissor = nullptr;
100 delete mStencil;
101 mStencil = nullptr;
Chris Craik1d477422014-08-26 17:30:15 -0700102}
103
John Reck3b202512014-06-23 13:13:08 -0700104void RenderState::setViewport(GLsizei width, GLsizei height) {
105 mViewportWidth = width;
106 mViewportHeight = height;
107 glViewport(0, 0, mViewportWidth, mViewportHeight);
108}
109
110
111void RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
112 *outWidth = mViewportWidth;
113 *outHeight = mViewportHeight;
114}
115
116void RenderState::bindFramebuffer(GLuint fbo) {
117 if (mFramebuffer != fbo) {
118 mFramebuffer = fbo;
119 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
120 }
121}
122
123void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
124 interruptForFunctorInvoke();
125 (*functor)(mode, info);
126 resumeFromFunctorInvoke();
127}
128
129void RenderState::interruptForFunctorInvoke() {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800130 mCaches->setProgram(nullptr);
Chris Craik44eb2c02015-01-29 09:45:09 -0800131 mCaches->textureState().resetActiveTexture();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800132 meshState().unbindMeshBuffer();
133 meshState().unbindIndicesBuffer();
134 meshState().resetVertexPointers();
135 meshState().disableTexCoordsVertexArray();
John Reck3b202512014-06-23 13:13:08 -0700136 debugOverdraw(false, false);
137}
138
139void RenderState::resumeFromFunctorInvoke() {
140 glViewport(0, 0, mViewportWidth, mViewportHeight);
141 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
142 debugOverdraw(false, false);
143
144 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
145
Chris Craik65fe5ee2015-01-26 18:06:29 -0800146 scissor().invalidate();
Chris Craik44eb2c02015-01-29 09:45:09 -0800147 blend().invalidate();
John Reck3b202512014-06-23 13:13:08 -0700148
Chris Craik44eb2c02015-01-29 09:45:09 -0800149 mCaches->textureState().activateTexture(0);
150 mCaches->textureState().resetBoundTextures();
John Reck3b202512014-06-23 13:13:08 -0700151}
152
153void RenderState::debugOverdraw(bool enable, bool clear) {
154 if (mCaches->debugOverdraw && mFramebuffer == 0) {
155 if (clear) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800156 scissor().setEnabled(false);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800157 stencil().clear();
John Reck3b202512014-06-23 13:13:08 -0700158 }
159 if (enable) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800160 stencil().enableDebugWrite();
John Reck3b202512014-06-23 13:13:08 -0700161 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800162 stencil().disable();
John Reck3b202512014-06-23 13:13:08 -0700163 }
164 }
165}
166
John Reck0e89e2b2014-10-31 14:49:06 -0700167void RenderState::requireGLContext() {
168 assertOnGLThread();
169 mRenderThread.eglManager().requireGlContext();
170}
171
172void RenderState::assertOnGLThread() {
173 pthread_t curr = pthread_self();
174 LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
175}
176
John Reck0e89e2b2014-10-31 14:49:06 -0700177class DecStrongTask : public renderthread::RenderTask {
178public:
179 DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
180
Chris Craikd41c4d82015-01-05 15:51:13 -0800181 virtual void run() override {
182 mObject->decStrong(nullptr);
183 mObject = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700184 delete this;
185 }
186
187private:
188 VirtualLightRefBase* mObject;
189};
190
191void RenderState::postDecStrong(VirtualLightRefBase* object) {
192 mRenderThread.queue(new DecStrongTask(object));
193}
194
Chris Craik6c15ffa2015-02-02 13:50:55 -0800195///////////////////////////////////////////////////////////////////////////////
196// Render
197///////////////////////////////////////////////////////////////////////////////
198
199/*
200 * Not yet supported:
201 *
202 * Textures + coordinates
203 * SkiaShader
204 * ColorFilter
205 *
206 // TODO: texture coord
207 // TODO: texture support
208 // TODO: skiashader support
209 // TODO: color filter support
210 */
211
212void RenderState::render(const Glop& glop) {
213 const Glop::Mesh& mesh = glop.mesh;
214 const Glop::Fill& shader = glop.fill;
215
216 // ---------- Shader + uniform setup ----------
217 mCaches->setProgram(shader.program);
218
219 Glop::Fill::Color color = shader.color;
220 shader.program->setColor(color.a, color.r, color.g, color.b);
221
222 shader.program->set(glop.transform.ortho,
223 glop.transform.modelView,
224 glop.transform.canvas,
225 glop.transform.offset);
226
227 // ---------- Mesh setup ----------
228 if (glop.mesh.vertexFlags & kTextureCoord_Attrib) {
229 // TODO: support textures
230 LOG_ALWAYS_FATAL("textures not yet supported");
231 } else {
232 meshState().disableTexCoordsVertexArray();
233 }
234 if (glop.mesh.vertexFlags & kColor_Attrib) {
235 LOG_ALWAYS_FATAL("color attribute not yet supported");
236 // TODO: enable color, disable when done
237 }
238 if (glop.mesh.vertexFlags & kAlpha_Attrib) {
239 LOG_ALWAYS_FATAL("alpha attribute not yet supported");
240 // TODO: enable alpha attribute, disable when done
241 }
242
243 /**
244 * Hard-coded vertex assumptions:
245 * - required
246 * - xy floats
247 * - 0 offset
248 * - in VBO
249 */
250 bool force = meshState().bindMeshBuffer(mesh.vertexBufferObject);
251 meshState().bindPositionVertexPointer(force, nullptr, mesh.stride);
252
253 /**
254 * Hard-coded index assumptions:
255 * - optional
256 * - 0 offset
257 * - in IBO
258 */
259 meshState().bindIndicesBufferInternal(mesh.indexBufferObject);
260
261 // ---------- GL state setup ----------
262
263 if (glop.blend.mode != Glop::Blend::kDisable) {
264 blend().enable(glop.blend.mode, glop.blend.swapSrcDst);
265 } else {
266 blend().disable();
267 }
268
269 glDrawElements(glop.mesh.primitiveMode, glop.mesh.vertexCount, GL_UNSIGNED_BYTE, nullptr);
270}
271
John Reck3b202512014-06-23 13:13:08 -0700272} /* namespace uirenderer */
273} /* namespace android */