RenderState.cpp revision c6baf563ba6aa207a48317c177b29f1d2b70cf3d
1/*
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 */
16#include "renderstate/RenderState.h"
17
18#include "renderthread/CanvasContext.h"
19#include "renderthread/EglManager.h"
20#include "utils/GLUtils.h"
21
22namespace android {
23namespace uirenderer {
24
25RenderState::RenderState(renderthread::RenderThread& thread)
26        : mRenderThread(thread)
27        , mViewportWidth(0)
28        , mViewportHeight(0)
29        , mFramebuffer(0) {
30    mThreadId = pthread_self();
31}
32
33RenderState::~RenderState() {
34    LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
35            "State object lifecycle not managed correctly");
36}
37
38void RenderState::onGLContextCreated() {
39    LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
40            "State object lifecycle not managed correctly");
41    mBlend = new Blend();
42    mMeshState = new MeshState();
43    mScissor = new Scissor();
44    mStencil = new Stencil();
45
46    // This is delayed because the first access of Caches makes GL calls
47    if (!mCaches) {
48        mCaches = &Caches::createInstance(*this);
49    }
50    mCaches->init();
51    mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
52}
53
54static void layerLostGlContext(Layer* layer) {
55    layer->onGlContextLost();
56}
57
58void RenderState::onGLContextDestroyed() {
59/*
60    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());
64        mCaches->dumpMemoryUsage();
65        for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
66                cit != mRegisteredContexts.end(); cit++) {
67            renderthread::CanvasContext* context = *cit;
68            ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
69            ALOGE("  Prefeteched layers: %zu", context->mPrefetechedLayers.size());
70            for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
71                    pit != context->mPrefetechedLayers.end(); pit++) {
72                (*pit)->debugDumpLayers("    ");
73            }
74            context->mRootRenderNode->debugDumpLayers("  ");
75        }
76
77
78        if (mActiveLayers.begin() == mActiveLayers.end()) {
79            ALOGE("set has become empty. wat.");
80        }
81        for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
82             lit != mActiveLayers.end(); lit++) {
83            const Layer* layer = *(lit);
84            ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
85                    layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
86        }
87        LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
88    }
89*/
90
91    // TODO: reset all cached state in state objects
92    std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
93    mAssetAtlas.terminate();
94
95    mCaches->terminate();
96
97    delete mBlend;
98    mBlend = nullptr;
99    delete mMeshState;
100    mMeshState = nullptr;
101    delete mScissor;
102    mScissor = nullptr;
103    delete mStencil;
104    mStencil = nullptr;
105}
106
107void 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    if (mode == DrawGlInfo::kModeProcessNoContext) {
128        // If there's no context we don't need to interrupt as there's
129        // no gl state to save/restore
130        (*functor)(mode, info);
131    } else {
132        interruptForFunctorInvoke();
133        (*functor)(mode, info);
134        resumeFromFunctorInvoke();
135    }
136}
137
138void RenderState::interruptForFunctorInvoke() {
139    mCaches->setProgram(nullptr);
140    mCaches->textureState().resetActiveTexture();
141    meshState().unbindMeshBuffer();
142    meshState().unbindIndicesBuffer();
143    meshState().resetVertexPointers();
144    meshState().disableTexCoordsVertexArray();
145    debugOverdraw(false, false);
146}
147
148void RenderState::resumeFromFunctorInvoke() {
149    glViewport(0, 0, mViewportWidth, mViewportHeight);
150    glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
151    debugOverdraw(false, false);
152
153    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155    scissor().invalidate();
156    blend().invalidate();
157
158    mCaches->textureState().activateTexture(0);
159    mCaches->textureState().resetBoundTextures();
160}
161
162void RenderState::debugOverdraw(bool enable, bool clear) {
163    if (Properties::debugOverdraw && mFramebuffer == 0) {
164        if (clear) {
165            scissor().setEnabled(false);
166            stencil().clear();
167        }
168        if (enable) {
169            stencil().enableDebugWrite();
170        } else {
171            stencil().disable();
172        }
173    }
174}
175
176void RenderState::requireGLContext() {
177    assertOnGLThread();
178    LOG_ALWAYS_FATAL_IF(!mRenderThread.eglManager().hasEglContext(),
179            "No GL context!");
180}
181
182void RenderState::assertOnGLThread() {
183    pthread_t curr = pthread_self();
184    LOG_ALWAYS_FATAL_IF(!pthread_equal(mThreadId, curr), "Wrong thread!");
185}
186
187class DecStrongTask : public renderthread::RenderTask {
188public:
189    explicit DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
190
191    virtual void run() override {
192        mObject->decStrong(nullptr);
193        mObject = nullptr;
194        delete this;
195    }
196
197private:
198    VirtualLightRefBase* mObject;
199};
200
201void RenderState::postDecStrong(VirtualLightRefBase* object) {
202    mRenderThread.queue(new DecStrongTask(object));
203}
204
205///////////////////////////////////////////////////////////////////////////////
206// Render
207///////////////////////////////////////////////////////////////////////////////
208
209void RenderState::render(const Glop& glop) {
210    const Glop::Mesh& mesh = glop.mesh;
211    const Glop::Mesh::Vertices& vertices = mesh.vertices;
212    const Glop::Mesh::Indices& indices = mesh.indices;
213    const Glop::Fill& fill = glop.fill;
214
215    // ---------------------------------------------
216    // ---------- Program + uniform setup ----------
217    // ---------------------------------------------
218    mCaches->setProgram(fill.program);
219
220    if (fill.colorEnabled) {
221        fill.program->setColor(fill.color);
222    }
223
224    fill.program->set(glop.transform.ortho,
225            glop.transform.modelView,
226            glop.transform.meshTransform(),
227            glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
228
229    // Color filter uniforms
230    if (fill.filterMode == ProgramDescription::kColorBlend) {
231        const FloatColor& color = fill.filter.color;
232        glUniform4f(mCaches->program().getUniform("colorBlend"),
233                color.r, color.g, color.b, color.a);
234    } else if (fill.filterMode == ProgramDescription::kColorMatrix) {
235        glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
236                fill.filter.matrix.matrix);
237        glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
238                fill.filter.matrix.vector);
239    }
240
241    // Round rect clipping uniforms
242    if (glop.roundRectClipState) {
243        // TODO: avoid query, and cache values (or RRCS ptr) in program
244        const RoundRectClipState* state = glop.roundRectClipState;
245        const Rect& innerRect = state->innerRect;
246        glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
247                innerRect.left, innerRect.top,
248                innerRect.right, innerRect.bottom);
249        glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
250                1, GL_FALSE, &state->matrix.data[0]);
251
252        // add half pixel to round out integer rect space to cover pixel centers
253        float roundedOutRadius = state->radius + 0.5f;
254        glUniform1f(fill.program->getUniform("roundRectRadius"),
255                roundedOutRadius);
256    }
257
258    // --------------------------------
259    // ---------- Mesh setup ----------
260    // --------------------------------
261    // vertices
262    const bool force = meshState().bindMeshBufferInternal(vertices.bufferObject)
263            || (vertices.position != nullptr);
264    meshState().bindPositionVertexPointer(force, vertices.position, vertices.stride);
265
266    // indices
267    meshState().bindIndicesBufferInternal(indices.bufferObject);
268
269    if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
270        const Glop::Fill::TextureData& texture = fill.texture;
271        // texture always takes slot 0, shader samplers increment from there
272        mCaches->textureState().activateTexture(0);
273
274        if (texture.clamp != GL_INVALID_ENUM) {
275            texture.texture->setWrap(texture.clamp, true, false, texture.target);
276        }
277        if (texture.filter != GL_INVALID_ENUM) {
278            texture.texture->setFilter(texture.filter, true, false, texture.target);
279        }
280
281        mCaches->textureState().bindTexture(texture.target, texture.texture->id);
282        meshState().enableTexCoordsVertexArray();
283        meshState().bindTexCoordsVertexPointer(force, vertices.texCoord, vertices.stride);
284
285        if (texture.textureTransform) {
286            glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
287                    GL_FALSE, &texture.textureTransform->data[0]);
288        }
289    } else {
290        meshState().disableTexCoordsVertexArray();
291    }
292    int colorLocation = -1;
293    if (vertices.attribFlags & VertexAttribFlags::Color) {
294        colorLocation = fill.program->getAttrib("colors");
295        glEnableVertexAttribArray(colorLocation);
296        glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
297    }
298    int alphaLocation = -1;
299    if (vertices.attribFlags & VertexAttribFlags::Alpha) {
300        // NOTE: alpha vertex position is computed assuming no VBO
301        const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
302        alphaLocation = fill.program->getAttrib("vtxAlpha");
303        glEnableVertexAttribArray(alphaLocation);
304        glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
305    }
306    // Shader uniforms
307    SkiaShader::apply(*mCaches, fill.skiaShaderData);
308
309    Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
310            fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
311    const AutoTexture autoCleanup(texture);
312
313    // ------------------------------------
314    // ---------- GL state setup ----------
315    // ------------------------------------
316    blend().setFactors(glop.blend.src, glop.blend.dst);
317
318    // ------------------------------------
319    // ---------- Actual drawing ----------
320    // ------------------------------------
321    if (indices.bufferObject == meshState().getQuadListIBO()) {
322        // Since the indexed quad list is of limited length, we loop over
323        // the glDrawXXX method while updating the vertex pointer
324        GLsizei elementsCount = mesh.elementCount;
325        const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
326        while (elementsCount > 0) {
327            GLsizei drawCount = MathUtils::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
328
329            // rebind pointers without forcing, since initial bind handled above
330            meshState().bindPositionVertexPointer(false, vertexData, vertices.stride);
331            if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
332                meshState().bindTexCoordsVertexPointer(false,
333                        vertexData + kMeshTextureOffset, vertices.stride);
334            }
335
336            glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
337            elementsCount -= drawCount;
338            vertexData += (drawCount / 6) * 4 * vertices.stride;
339        }
340    } else if (indices.bufferObject || indices.indices) {
341        glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
342    } else {
343        glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
344    }
345
346    // -----------------------------------
347    // ---------- Mesh teardown ----------
348    // -----------------------------------
349    if (vertices.attribFlags & VertexAttribFlags::Alpha) {
350        glDisableVertexAttribArray(alphaLocation);
351    }
352    if (vertices.attribFlags & VertexAttribFlags::Color) {
353        glDisableVertexAttribArray(colorLocation);
354    }
355}
356
357void RenderState::dump() {
358    blend().dump();
359    meshState().dump();
360    scissor().dump();
361    stencil().dump();
362}
363
364} /* namespace uirenderer */
365} /* namespace android */
366