13b20251a355c88193c439f928a84ae69483fb488John Reck/*
23b20251a355c88193c439f928a84ae69483fb488John Reck * Copyright (C) 2014 The Android Open Source Project
33b20251a355c88193c439f928a84ae69483fb488John Reck *
43b20251a355c88193c439f928a84ae69483fb488John Reck * Licensed under the Apache License, Version 2.0 (the "License");
53b20251a355c88193c439f928a84ae69483fb488John Reck * you may not use this file except in compliance with the License.
63b20251a355c88193c439f928a84ae69483fb488John Reck * You may obtain a copy of the License at
73b20251a355c88193c439f928a84ae69483fb488John Reck *
83b20251a355c88193c439f928a84ae69483fb488John Reck *      http://www.apache.org/licenses/LICENSE-2.0
93b20251a355c88193c439f928a84ae69483fb488John Reck *
103b20251a355c88193c439f928a84ae69483fb488John Reck * Unless required by applicable law or agreed to in writing, software
113b20251a355c88193c439f928a84ae69483fb488John Reck * distributed under the License is distributed on an "AS IS" BASIS,
123b20251a355c88193c439f928a84ae69483fb488John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133b20251a355c88193c439f928a84ae69483fb488John Reck * See the License for the specific language governing permissions and
143b20251a355c88193c439f928a84ae69483fb488John Reck * limitations under the License.
153b20251a355c88193c439f928a84ae69483fb488John Reck */
1638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <GpuMemoryTracker.h>
1796a5c4c7bab6718524de7253da8309143ab48befChris Craik#include "renderstate/RenderState.h"
183b20251a355c88193c439f928a84ae69483fb488John Reck
19443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck#include "renderthread/CanvasContext.h"
200e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck#include "renderthread/EglManager.h"
21117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik#include "utils/GLUtils.h"
229db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <algorithm>
239db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik
243b20251a355c88193c439f928a84ae69483fb488John Recknamespace android {
253b20251a355c88193c439f928a84ae69483fb488John Recknamespace uirenderer {
263b20251a355c88193c439f928a84ae69483fb488John Reck
270e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John ReckRenderState::RenderState(renderthread::RenderThread& thread)
280e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        : mRenderThread(thread)
293b20251a355c88193c439f928a84ae69483fb488John Reck        , mViewportWidth(0)
303b20251a355c88193c439f928a84ae69483fb488John Reck        , mViewportHeight(0)
313b20251a355c88193c439f928a84ae69483fb488John Reck        , mFramebuffer(0) {
320e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    mThreadId = pthread_self();
333b20251a355c88193c439f928a84ae69483fb488John Reck}
343b20251a355c88193c439f928a84ae69483fb488John Reck
353b20251a355c88193c439f928a84ae69483fb488John ReckRenderState::~RenderState() {
3644eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
3796a5c4c7bab6718524de7253da8309143ab48befChris Craik            "State object lifecycle not managed correctly");
383b20251a355c88193c439f928a84ae69483fb488John Reck}
393b20251a355c88193c439f928a84ae69483fb488John Reck
403b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::onGLContextCreated() {
4144eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    LOG_ALWAYS_FATAL_IF(mBlend || mMeshState || mScissor || mStencil,
4296a5c4c7bab6718524de7253da8309143ab48befChris Craik            "State object lifecycle not managed correctly");
4338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onGLContextCreated();
4438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
4544eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mBlend = new Blend();
4696a5c4c7bab6718524de7253da8309143ab48befChris Craik    mMeshState = new MeshState();
4796a5c4c7bab6718524de7253da8309143ab48befChris Craik    mScissor = new Scissor();
4896a5c4c7bab6718524de7253da8309143ab48befChris Craik    mStencil = new Stencil();
4996a5c4c7bab6718524de7253da8309143ab48befChris Craik
503b20251a355c88193c439f928a84ae69483fb488John Reck    // This is delayed because the first access of Caches makes GL calls
51ff5c8e8097e3eff910632a568195b798798cccccChris Craik    if (!mCaches) {
52ff5c8e8097e3eff910632a568195b798798cccccChris Craik        mCaches = &Caches::createInstance(*this);
53ff5c8e8097e3eff910632a568195b798798cccccChris Craik    }
543b20251a355c88193c439f928a84ae69483fb488John Reck    mCaches->init();
55ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck    mCaches->textureCache.setAssetAtlas(&mAssetAtlas);
563b20251a355c88193c439f928a84ae69483fb488John Reck}
573b20251a355c88193c439f928a84ae69483fb488John Reck
5857998017ff137f7d4ec33df21b6596141f8c4547John Reckstatic void layerLostGlContext(Layer* layer) {
5957998017ff137f7d4ec33df21b6596141f8c4547John Reck    layer->onGlContextLost();
6057998017ff137f7d4ec33df21b6596141f8c4547John Reck}
6157998017ff137f7d4ec33df21b6596141f8c4547John Reck
621d4774233304c484673e2af2c1de2ab41021c979Chris Craikvoid RenderState::onGLContextDestroyed() {
6321029ef131d6a98862ae6faf3305bee2872e9c5fChris Craik/*
64bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik    size_t size = mActiveLayers.size();
65bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik    if (CC_UNLIKELY(size != 0)) {
66bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik        ALOGE("Crashing, have %d contexts and %d layers at context destruction. isempty %d",
67bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik                mRegisteredContexts.size(), size, mActiveLayers.empty());
6817035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck        mCaches->dumpMemoryUsage();
69443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck        for (std::set<renderthread::CanvasContext*>::iterator cit = mRegisteredContexts.begin();
70443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck                cit != mRegisteredContexts.end(); cit++) {
71443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck            renderthread::CanvasContext* context = *cit;
72bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik            ALOGE("Context: %p (root = %p)", context, context->mRootRenderNode.get());
73bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik            ALOGE("  Prefeteched layers: %zu", context->mPrefetechedLayers.size());
74443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck            for (std::set<RenderNode*>::iterator pit = context->mPrefetechedLayers.begin();
75443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck                    pit != context->mPrefetechedLayers.end(); pit++) {
76443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck                (*pit)->debugDumpLayers("    ");
77443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck            }
78443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck            context->mRootRenderNode->debugDumpLayers("  ");
79443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck        }
80599e254ea33231b1809466ae765dbee53dc4685cChris Craik
81bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik
82bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik        if (mActiveLayers.begin() == mActiveLayers.end()) {
83bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik            ALOGE("set has become empty. wat.");
84bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik        }
85599e254ea33231b1809466ae765dbee53dc4685cChris Craik        for (std::set<const Layer*>::iterator lit = mActiveLayers.begin();
86599e254ea33231b1809466ae765dbee53dc4685cChris Craik             lit != mActiveLayers.end(); lit++) {
87599e254ea33231b1809466ae765dbee53dc4685cChris Craik            const Layer* layer = *(lit);
88bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik            ALOGE("Layer %p, state %d, texlayer %d, fbo %d, buildlayered %d",
89bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik                    layer, layer->state, layer->isTextureLayer(), layer->getFbo(), layer->wasBuildLayered);
90599e254ea33231b1809466ae765dbee53dc4685cChris Craik        }
91bfd1cd620991ac2fa9202fdce6c00ec47d071935Chris Craik        LOG_ALWAYS_FATAL("%d layers have survived gl context destruction", size);
9217035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    }
9321029ef131d6a98862ae6faf3305bee2872e9c5fChris Craik*/
9449bc4acfadf9c5b1e520217278ccb38010d38c89John Reck
959fded232a9548a304e0145011df8849fba0dcda7Chris Craik    mLayerPool.clear();
969fded232a9548a304e0145011df8849fba0dcda7Chris Craik
9796a5c4c7bab6718524de7253da8309143ab48befChris Craik    // TODO: reset all cached state in state objects
9857998017ff137f7d4ec33df21b6596141f8c4547John Reck    std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
99ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck    mAssetAtlas.terminate();
10096a5c4c7bab6718524de7253da8309143ab48befChris Craik
10144eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mCaches->terminate();
10244eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik
10344eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    delete mBlend;
10444eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mBlend = nullptr;
10596a5c4c7bab6718524de7253da8309143ab48befChris Craik    delete mMeshState;
10696a5c4c7bab6718524de7253da8309143ab48befChris Craik    mMeshState = nullptr;
10796a5c4c7bab6718524de7253da8309143ab48befChris Craik    delete mScissor;
10896a5c4c7bab6718524de7253da8309143ab48befChris Craik    mScissor = nullptr;
10996a5c4c7bab6718524de7253da8309143ab48befChris Craik    delete mStencil;
11096a5c4c7bab6718524de7253da8309143ab48befChris Craik    mStencil = nullptr;
11138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
11238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onGLContextDestroyed();
1131d4774233304c484673e2af2c1de2ab41021c979Chris Craik}
1141d4774233304c484673e2af2c1de2ab41021c979Chris Craik
1159fded232a9548a304e0145011df8849fba0dcda7Chris Craikvoid RenderState::flush(Caches::FlushMode mode) {
1169fded232a9548a304e0145011df8849fba0dcda7Chris Craik    switch (mode) {
1179fded232a9548a304e0145011df8849fba0dcda7Chris Craik        case Caches::FlushMode::Full:
1189fded232a9548a304e0145011df8849fba0dcda7Chris Craik            // fall through
1199fded232a9548a304e0145011df8849fba0dcda7Chris Craik        case Caches::FlushMode::Moderate:
1209fded232a9548a304e0145011df8849fba0dcda7Chris Craik            // fall through
1219fded232a9548a304e0145011df8849fba0dcda7Chris Craik        case Caches::FlushMode::Layers:
1229fded232a9548a304e0145011df8849fba0dcda7Chris Craik            mLayerPool.clear();
1239fded232a9548a304e0145011df8849fba0dcda7Chris Craik            break;
1249fded232a9548a304e0145011df8849fba0dcda7Chris Craik    }
1259fded232a9548a304e0145011df8849fba0dcda7Chris Craik    mCaches->flush(mode);
1269fded232a9548a304e0145011df8849fba0dcda7Chris Craik}
1279fded232a9548a304e0145011df8849fba0dcda7Chris Craik
1283b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::setViewport(GLsizei width, GLsizei height) {
1293b20251a355c88193c439f928a84ae69483fb488John Reck    mViewportWidth = width;
1303b20251a355c88193c439f928a84ae69483fb488John Reck    mViewportHeight = height;
1313b20251a355c88193c439f928a84ae69483fb488John Reck    glViewport(0, 0, mViewportWidth, mViewportHeight);
1323b20251a355c88193c439f928a84ae69483fb488John Reck}
1333b20251a355c88193c439f928a84ae69483fb488John Reck
1343b20251a355c88193c439f928a84ae69483fb488John Reck
1353b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::getViewport(GLsizei* outWidth, GLsizei* outHeight) {
1363b20251a355c88193c439f928a84ae69483fb488John Reck    *outWidth = mViewportWidth;
1373b20251a355c88193c439f928a84ae69483fb488John Reck    *outHeight = mViewportHeight;
1383b20251a355c88193c439f928a84ae69483fb488John Reck}
1393b20251a355c88193c439f928a84ae69483fb488John Reck
1403b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::bindFramebuffer(GLuint fbo) {
1413b20251a355c88193c439f928a84ae69483fb488John Reck    if (mFramebuffer != fbo) {
1423b20251a355c88193c439f928a84ae69483fb488John Reck        mFramebuffer = fbo;
1433b20251a355c88193c439f928a84ae69483fb488John Reck        glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
1443b20251a355c88193c439f928a84ae69483fb488John Reck    }
1453b20251a355c88193c439f928a84ae69483fb488John Reck}
1463b20251a355c88193c439f928a84ae69483fb488John Reck
1470b8d0677be2289bbc9e0b48c0878fb67d1cc0ebdJohn ReckGLuint RenderState::createFramebuffer() {
148818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    GLuint ret;
149818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    glGenFramebuffers(1, &ret);
150818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    return ret;
151818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik}
152818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
153818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craikvoid RenderState::deleteFramebuffer(GLuint fbo) {
154818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    if (mFramebuffer == fbo) {
155818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik        // GL defines that deleting the currently bound FBO rebinds FBO 0.
156818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik        // Reflect this in our cached value.
157818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik        mFramebuffer = 0;
158818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    }
159818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik    glDeleteFramebuffers(1, &fbo);
160818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik}
161818c9fbf1d76d5df19253ba4eb964efa939ec9ecChris Craik
1623b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
16395cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck    if (mode == DrawGlInfo::kModeProcessNoContext) {
16495cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        // If there's no context we don't need to interrupt as there's
16595cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        // no gl state to save/restore
16695cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        (*functor)(mode, info);
16795cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck    } else {
16895cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        interruptForFunctorInvoke();
16995cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        (*functor)(mode, info);
17095cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck        resumeFromFunctorInvoke();
17195cd24bb9d000eb541bc7ec7e6b53d1c7e313076John Reck    }
1723b20251a355c88193c439f928a84ae69483fb488John Reck}
1733b20251a355c88193c439f928a84ae69483fb488John Reck
1743b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::interruptForFunctorInvoke() {
1756c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    mCaches->setProgram(nullptr);
17644eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mCaches->textureState().resetActiveTexture();
17796a5c4c7bab6718524de7253da8309143ab48befChris Craik    meshState().unbindMeshBuffer();
17896a5c4c7bab6718524de7253da8309143ab48befChris Craik    meshState().unbindIndicesBuffer();
17996a5c4c7bab6718524de7253da8309143ab48befChris Craik    meshState().resetVertexPointers();
18096a5c4c7bab6718524de7253da8309143ab48befChris Craik    meshState().disableTexCoordsVertexArray();
1813b20251a355c88193c439f928a84ae69483fb488John Reck    debugOverdraw(false, false);
1823b20251a355c88193c439f928a84ae69483fb488John Reck}
1833b20251a355c88193c439f928a84ae69483fb488John Reck
1843b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::resumeFromFunctorInvoke() {
1853b20251a355c88193c439f928a84ae69483fb488John Reck    glViewport(0, 0, mViewportWidth, mViewportHeight);
1863b20251a355c88193c439f928a84ae69483fb488John Reck    glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
1873b20251a355c88193c439f928a84ae69483fb488John Reck    debugOverdraw(false, false);
1883b20251a355c88193c439f928a84ae69483fb488John Reck
1893b20251a355c88193c439f928a84ae69483fb488John Reck    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1903b20251a355c88193c439f928a84ae69483fb488John Reck
19165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik    scissor().invalidate();
19244eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    blend().invalidate();
1933b20251a355c88193c439f928a84ae69483fb488John Reck
19444eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mCaches->textureState().activateTexture(0);
19544eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik    mCaches->textureState().resetBoundTextures();
1963b20251a355c88193c439f928a84ae69483fb488John Reck}
1973b20251a355c88193c439f928a84ae69483fb488John Reck
1983b20251a355c88193c439f928a84ae69483fb488John Reckvoid RenderState::debugOverdraw(bool enable, bool clear) {
1992507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    if (Properties::debugOverdraw && mFramebuffer == 0) {
2003b20251a355c88193c439f928a84ae69483fb488John Reck        if (clear) {
20165fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik            scissor().setEnabled(false);
20296a5c4c7bab6718524de7253da8309143ab48befChris Craik            stencil().clear();
2033b20251a355c88193c439f928a84ae69483fb488John Reck        }
2043b20251a355c88193c439f928a84ae69483fb488John Reck        if (enable) {
20596a5c4c7bab6718524de7253da8309143ab48befChris Craik            stencil().enableDebugWrite();
2063b20251a355c88193c439f928a84ae69483fb488John Reck        } else {
20796a5c4c7bab6718524de7253da8309143ab48befChris Craik            stencil().disable();
2083b20251a355c88193c439f928a84ae69483fb488John Reck        }
2093b20251a355c88193c439f928a84ae69483fb488John Reck    }
2103b20251a355c88193c439f928a84ae69483fb488John Reck}
2113b20251a355c88193c439f928a84ae69483fb488John Reck
2120e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckclass DecStrongTask : public renderthread::RenderTask {
2130e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckpublic:
2140e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    DecStrongTask(VirtualLightRefBase* object) : mObject(object) {}
2150e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
216d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    virtual void run() override {
217d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mObject->decStrong(nullptr);
218d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mObject = nullptr;
2190e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        delete this;
2200e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    }
2210e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
2220e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckprivate:
2230e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    VirtualLightRefBase* mObject;
2240e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck};
2250e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
2260e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckvoid RenderState::postDecStrong(VirtualLightRefBase* object) {
227a55b5d6c65cde2b7cc28bb3ea160bfaaef7a446aJohn Reck    if (pthread_equal(mThreadId, pthread_self())) {
228a55b5d6c65cde2b7cc28bb3ea160bfaaef7a446aJohn Reck        object->decStrong(nullptr);
229a55b5d6c65cde2b7cc28bb3ea160bfaaef7a446aJohn Reck    } else {
230a55b5d6c65cde2b7cc28bb3ea160bfaaef7a446aJohn Reck        mRenderThread.queue(new DecStrongTask(object));
231a55b5d6c65cde2b7cc28bb3ea160bfaaef7a446aJohn Reck    }
2320e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck}
2330e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
2346c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik///////////////////////////////////////////////////////////////////////////////
2356c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik// Render
2366c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik///////////////////////////////////////////////////////////////////////////////
2376c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
23812efe649d3f5df8e81f4b78179939c1d488673a0Chris Craikvoid RenderState::render(const Glop& glop, const Matrix4& orthoMatrix) {
2396c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    const Glop::Mesh& mesh = glop.mesh;
240ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    const Glop::Mesh::Vertices& vertices = mesh.vertices;
241ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    const Glop::Mesh::Indices& indices = mesh.indices;
2420519c810a56bded1284fcb2ae40f438878c6585fChris Craik    const Glop::Fill& fill = glop.fill;
2436c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
244975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
2459372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
2460519c810a56bded1284fcb2ae40f438878c6585fChris Craik    // ---------------------------------------------
2470519c810a56bded1284fcb2ae40f438878c6585fChris Craik    // ---------- Program + uniform setup ----------
2480519c810a56bded1284fcb2ae40f438878c6585fChris Craik    // ---------------------------------------------
2490519c810a56bded1284fcb2ae40f438878c6585fChris Craik    mCaches->setProgram(fill.program);
2506c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
2510519c810a56bded1284fcb2ae40f438878c6585fChris Craik    if (fill.colorEnabled) {
2520519c810a56bded1284fcb2ae40f438878c6585fChris Craik        fill.program->setColor(fill.color);
2530519c810a56bded1284fcb2ae40f438878c6585fChris Craik    }
2546c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
25512efe649d3f5df8e81f4b78179939c1d488673a0Chris Craik    fill.program->set(orthoMatrix,
2566c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik            glop.transform.modelView,
25753e51e4aa933f9603587e1780f446c18816bf9beChris Craik            glop.transform.meshTransform(),
25853e51e4aa933f9603587e1780f446c18816bf9beChris Craik            glop.transform.transformFlags & TransformFlags::OffsetByFudgeFactor);
259117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
2600519c810a56bded1284fcb2ae40f438878c6585fChris Craik    // Color filter uniforms
261b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (fill.filterMode == ProgramDescription::ColorFilterMode::Blend) {
262ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        const FloatColor& color = fill.filter.color;
263117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        glUniform4f(mCaches->program().getUniform("colorBlend"),
264117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik                color.r, color.g, color.b, color.a);
265b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    } else if (fill.filterMode == ProgramDescription::ColorFilterMode::Matrix) {
266117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        glUniformMatrix4fv(mCaches->program().getUniform("colorMatrix"), 1, GL_FALSE,
267ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik                fill.filter.matrix.matrix);
268117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik        glUniform4fv(mCaches->program().getUniform("colorMatrixVector"), 1,
269ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik                fill.filter.matrix.vector);
270117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    }
2716c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
2720519c810a56bded1284fcb2ae40f438878c6585fChris Craik    // Round rect clipping uniforms
2730519c810a56bded1284fcb2ae40f438878c6585fChris Craik    if (glop.roundRectClipState) {
2740519c810a56bded1284fcb2ae40f438878c6585fChris Craik        // TODO: avoid query, and cache values (or RRCS ptr) in program
2750519c810a56bded1284fcb2ae40f438878c6585fChris Craik        const RoundRectClipState* state = glop.roundRectClipState;
2760519c810a56bded1284fcb2ae40f438878c6585fChris Craik        const Rect& innerRect = state->innerRect;
2770519c810a56bded1284fcb2ae40f438878c6585fChris Craik        glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
2780519c810a56bded1284fcb2ae40f438878c6585fChris Craik                innerRect.left, innerRect.top,
2790519c810a56bded1284fcb2ae40f438878c6585fChris Craik                innerRect.right, innerRect.bottom);
2800519c810a56bded1284fcb2ae40f438878c6585fChris Craik        glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
2810519c810a56bded1284fcb2ae40f438878c6585fChris Craik                1, GL_FALSE, &state->matrix.data[0]);
2820519c810a56bded1284fcb2ae40f438878c6585fChris Craik
2830519c810a56bded1284fcb2ae40f438878c6585fChris Craik        // add half pixel to round out integer rect space to cover pixel centers
2840519c810a56bded1284fcb2ae40f438878c6585fChris Craik        float roundedOutRadius = state->radius + 0.5f;
2850519c810a56bded1284fcb2ae40f438878c6585fChris Craik        glUniform1f(fill.program->getUniform("roundRectRadius"),
2860519c810a56bded1284fcb2ae40f438878c6585fChris Craik                roundedOutRadius);
2870519c810a56bded1284fcb2ae40f438878c6585fChris Craik    }
288ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik
289975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
2909372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
291117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // --------------------------------
2926c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    // ---------- Mesh setup ----------
293117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // --------------------------------
294117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // vertices
2951b7db4000eabb570697f4c5097588acbfa4df62bChris Craik    meshState().bindMeshBuffer(vertices.bufferObject);
2961b7db4000eabb570697f4c5097588acbfa4df62bChris Craik    meshState().bindPositionVertexPointer(vertices.position, vertices.stride);
297117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
298117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // indices
2991b7db4000eabb570697f4c5097588acbfa4df62bChris Craik    meshState().bindIndicesBuffer(indices.bufferObject);
300117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
301138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    // texture
302138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    if (fill.texture.texture != nullptr) {
30326bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        const Glop::Fill::TextureData& texture = fill.texture;
30426bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        // texture always takes slot 0, shader samplers increment from there
3050519c810a56bded1284fcb2ae40f438878c6585fChris Craik        mCaches->textureState().activateTexture(0);
3060519c810a56bded1284fcb2ae40f438878c6585fChris Craik
3071b7db4000eabb570697f4c5097588acbfa4df62bChris Craik        mCaches->textureState().bindTexture(texture.target, texture.texture->id());
30826bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        if (texture.clamp != GL_INVALID_ENUM) {
3091b7db4000eabb570697f4c5097588acbfa4df62bChris Craik            texture.texture->setWrap(texture.clamp, false, false, texture.target);
31030036092b40badecbe64d9c2bff4850132147f78Chris Craik        }
31126bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        if (texture.filter != GL_INVALID_ENUM) {
3121b7db4000eabb570697f4c5097588acbfa4df62bChris Craik            texture.texture->setFilter(texture.filter, false, false, texture.target);
31330036092b40badecbe64d9c2bff4850132147f78Chris Craik        }
3140519c810a56bded1284fcb2ae40f438878c6585fChris Craik
31526bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        if (texture.textureTransform) {
31626bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik            glUniformMatrix4fv(fill.program->getUniform("mainTextureTransform"), 1,
31726bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik                    GL_FALSE, &texture.textureTransform->data[0]);
31826bf34200e40a0fa8c66366559aa016380cd8c6fChris Craik        }
319138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    }
320138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik
321138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    // vertex attributes (tex coord, color, alpha)
322138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik    if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
323138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik        meshState().enableTexCoordsVertexArray();
324138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik        meshState().bindTexCoordsVertexPointer(vertices.texCoord, vertices.stride);
3256c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    } else {
3266c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik        meshState().disableTexCoordsVertexArray();
3276c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    }
328ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    int colorLocation = -1;
32953e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (vertices.attribFlags & VertexAttribFlags::Color) {
330ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        colorLocation = fill.program->getAttrib("colors");
331ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glEnableVertexAttribArray(colorLocation);
332ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, vertices.stride, vertices.color);
3336c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    }
334ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    int alphaLocation = -1;
33553e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (vertices.attribFlags & VertexAttribFlags::Alpha) {
336ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        // NOTE: alpha vertex position is computed assuming no VBO
337ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        const void* alphaCoords = ((const GLbyte*) vertices.position) + kVertexAlphaOffset;
338ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        alphaLocation = fill.program->getAttrib("vtxAlpha");
339ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glEnableVertexAttribArray(alphaLocation);
340ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glVertexAttribPointer(alphaLocation, 1, GL_FLOAT, GL_FALSE, vertices.stride, alphaCoords);
3416c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    }
342922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    // Shader uniforms
343ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    SkiaShader::apply(*mCaches, fill.skiaShaderData);
344922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
345975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
346c5a3efd28668a62df3e3b364b49624c5af7549b6Dohyun Lee    Texture* texture = (fill.skiaShaderData.skiaShaderType & kBitmap_SkiaShaderType) ?
347c5a3efd28668a62df3e3b364b49624c5af7549b6Dohyun Lee            fill.skiaShaderData.bitmapData.bitmapTexture : nullptr;
348c5a3efd28668a62df3e3b364b49624c5af7549b6Dohyun Lee    const AutoTexture autoCleanup(texture);
3499372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
350117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // ------------------------------------
3516c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    // ---------- GL state setup ----------
352117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // ------------------------------------
353031888744e24b5c7243ac99ec98b78aff5db1c78Chris Craik    blend().setFactors(glop.blend.src, glop.blend.dst);
3546c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
355975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
3569372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
357117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // ------------------------------------
3582ab95d780b023152556d9f8659de734ec7b55047Chris Craik    // ---------- Actual drawing ----------
359117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    // ------------------------------------
360ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    if (indices.bufferObject == meshState().getQuadListIBO()) {
3612ab95d780b023152556d9f8659de734ec7b55047Chris Craik        // Since the indexed quad list is of limited length, we loop over
3622ab95d780b023152556d9f8659de734ec7b55047Chris Craik        // the glDrawXXX method while updating the vertex pointer
3630519c810a56bded1284fcb2ae40f438878c6585fChris Craik        GLsizei elementsCount = mesh.elementCount;
364ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        const GLbyte* vertexData = static_cast<const GLbyte*>(vertices.position);
3652ab95d780b023152556d9f8659de734ec7b55047Chris Craik        while (elementsCount > 0) {
3669db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik            GLsizei drawCount = std::min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
3671b7db4000eabb570697f4c5097588acbfa4df62bChris Craik            meshState().bindPositionVertexPointer(vertexData, vertices.stride);
36853e51e4aa933f9603587e1780f446c18816bf9beChris Craik            if (vertices.attribFlags & VertexAttribFlags::TextureCoord) {
3691b7db4000eabb570697f4c5097588acbfa4df62bChris Craik                meshState().bindTexCoordsVertexPointer(
370ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik                        vertexData + kMeshTextureOffset, vertices.stride);
371f27133df2d179c99d6bc1ae644af09e9153a0071Chris Craik            }
372f27133df2d179c99d6bc1ae644af09e9153a0071Chris Craik
3732ab95d780b023152556d9f8659de734ec7b55047Chris Craik            glDrawElements(mesh.primitiveMode, drawCount, GL_UNSIGNED_SHORT, nullptr);
3742ab95d780b023152556d9f8659de734ec7b55047Chris Craik            elementsCount -= drawCount;
375ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik            vertexData += (drawCount / 6) * 4 * vertices.stride;
3762ab95d780b023152556d9f8659de734ec7b55047Chris Craik        }
377ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    } else if (indices.bufferObject || indices.indices) {
378ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glDrawElements(mesh.primitiveMode, mesh.elementCount, GL_UNSIGNED_SHORT, indices.indices);
3796c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    } else {
3800519c810a56bded1284fcb2ae40f438878c6585fChris Craik        glDrawArrays(mesh.primitiveMode, 0, mesh.elementCount);
381117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    }
382117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
383975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
3849372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
3852ab95d780b023152556d9f8659de734ec7b55047Chris Craik    // -----------------------------------
3862ab95d780b023152556d9f8659de734ec7b55047Chris Craik    // ---------- Mesh teardown ----------
3872ab95d780b023152556d9f8659de734ec7b55047Chris Craik    // -----------------------------------
38853e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (vertices.attribFlags & VertexAttribFlags::Alpha) {
389ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glDisableVertexAttribArray(alphaLocation);
390ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik    }
39153e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (vertices.attribFlags & VertexAttribFlags::Color) {
392ef2507439c08f4e9c4c9bba1c6243ca9df2ee827Chris Craik        glDisableVertexAttribArray(colorLocation);
3936c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik    }
3949372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
395975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
3966c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik}
3976c15ffa196fc9b7724c189d833c3435d8db12266Chris Craik
398117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craikvoid RenderState::dump() {
399117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    blend().dump();
400117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    meshState().dump();
401117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    scissor().dump();
402117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    stencil().dump();
403117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik}
404117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik
4053b20251a355c88193c439f928a84ae69483fb488John Reck} /* namespace uirenderer */
4063b20251a355c88193c439f928a84ae69483fb488John Reck} /* namespace android */
407