1768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev/*
2768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * Copyright (C) 2016 The Android Open Source Project
3768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev *
4768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * Licensed under the Apache License, Version 2.0 (the "License");
5768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * you may not use this file except in compliance with the License.
6768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * You may obtain a copy of the License at
7768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev *
8768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev *      http://www.apache.org/licenses/LICENSE-2.0
9768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev *
10768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * Unless required by applicable law or agreed to in writing, software
11768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * distributed under the License is distributed on an "AS IS" BASIS,
12768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * See the License for the specific language governing permissions and
14768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev * limitations under the License.
15768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev */
16768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
17768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include "OpenGLPipeline.h"
18768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
19768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include "DeferredLayerUpdater.h"
20768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include "EglManager.h"
21cd55852fcd840f7f4c4d7a0a7253a2995c77afa2Greg Daniel#include "Frame.h"
228cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel#include "GlLayer.h"
23de97307362c26b64e2376b21ccde8414088cdc8bMatt Sarett#include "ProfileRenderer.h"
24768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include "renderstate/RenderState.h"
25c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger#include "OpenGLReadback.h"
26768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
27768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include <cutils/properties.h>
28768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#include <strings.h>
29768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
30768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievnamespace android {
31768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievnamespace uirenderer {
32768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievnamespace renderthread {
33768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
34768e39335b668e8ef25fef30ab42b2d6d29a4735Stan IlievOpenGLPipeline::OpenGLPipeline(RenderThread& thread)
35b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger        :  mEglManager(thread.eglManager())
36b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger        , mRenderThread(thread) {
37768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
38768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
39768e39335b668e8ef25fef30ab42b2d6d29a4735Stan IlievMakeCurrentResult OpenGLPipeline::makeCurrent() {
40768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    // TODO: Figure out why this workaround is needed, see b/13913604
41768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
42768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    EGLint error = 0;
43768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    bool haveNewSurface = mEglManager.makeCurrent(mEglSurface, &error);
44768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
45768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    Caches::getInstance().textureCache.resetMarkInUse(this);
46768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (!haveNewSurface) {
47768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        return MakeCurrentResult::AlreadyCurrent;
48768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
49768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
50768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
51768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
52768e39335b668e8ef25fef30ab42b2d6d29a4735Stan IlievFrame OpenGLPipeline::getFrame() {
53768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
54768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev                "drawRenderNode called on a context with no surface!");
55768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return mEglManager.beginFrame(mEglSurface);
56768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
57768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
58768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
59768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const FrameBuilder::LightGeometry& lightGeometry,
60768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        LayerUpdateQueue* layerUpdateQueue,
61768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const Rect& contentDrawBounds, bool opaque,
62768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const BakedOpRenderer::LightInfo& lightInfo,
63768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const std::vector< sp<RenderNode> >& renderNodes,
64768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        FrameInfoVisualizer* profiler) {
65768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
66768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    mEglManager.damageFrame(frame, dirty);
67768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
68768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    bool drew = false;
69768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
70768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
71768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    auto& caches = Caches::getInstance();
72768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    FrameBuilder frameBuilder(dirty, frame.width(), frame.height(), lightGeometry, caches);
73768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
74768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    frameBuilder.deferLayers(*layerUpdateQueue);
75768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    layerUpdateQueue->clear();
76768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
77768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    frameBuilder.deferRenderNodeScene(renderNodes, contentDrawBounds);
78768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
79768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
80768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev            opaque, lightInfo);
81768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
82de97307362c26b64e2376b21ccde8414088cdc8bMatt Sarett    ProfileRenderer profileRenderer(renderer);
83de97307362c26b64e2376b21ccde8414088cdc8bMatt Sarett    profiler->draw(profileRenderer);
84768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    drew = renderer.didDraw();
85768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
86768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    // post frame cleanup
87768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    caches.clearGarbage();
88768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    caches.pathCache.trim();
89768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    caches.tessellationCache.trim();
90768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
91768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#if DEBUG_MEMORY_USAGE
92116c376c737fafff0e69f4b6c30d77fd21127ffcDaichi Hirono    caches.dumpMemoryUsage();
93768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#else
94768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (CC_UNLIKELY(Properties::debugLevel & kDebugMemory)) {
95768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        caches.dumpMemoryUsage();
96768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
97768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev#endif
98768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
99768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return drew;
100768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
101768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
102768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
103768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        FrameInfo* currentFrameInfo, bool* requireSwap) {
104768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
105768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    GL_CHECKPOINT(LOW);
106768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
107768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    // Even if we decided to cancel the frame, from the perspective of jank
108768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    // metrics the frame was swapped at this point
109768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    currentFrameInfo->markSwapBuffers();
110768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
111768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    *requireSwap = drew || mEglManager.damageRequiresSwap();
112768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
113768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
114768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        return false;
115768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
116768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
117768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return *requireSwap;
118768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
119768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
120768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
121c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    ATRACE_CALL();
1222f1aaf7e24590e3b1dde62e0cb6d8d2978c9d5daChris Craik    // acquire most recent buffer for drawing
1232f1aaf7e24590e3b1dde62e0cb6d8d2978c9d5daChris Craik    layer->updateTexImage();
124768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    layer->apply();
1258cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel    return OpenGLReadbackImpl::copyLayerInto(mRenderThread,
1268cd3edfa15cc9cdbffa935d19ab894426b08d174Greg Daniel            static_cast<GlLayer&>(*layer->backingLayer()), bitmap);
127768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
128768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
1293e9999bd866fac71c72e6b484a9836c87c328a08sergeyvstatic Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
1303e9999bd866fac71c72e6b484a9836c87c328a08sergeyv        SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
1313e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha,
1323e9999bd866fac71c72e6b484a9836c87c328a08sergeyv            mode, blend);
13356ad6ec42f814e9e61030ff819cac4e5d31def8bDerek Sollenberger    Caches::getInstance().textureState().activateTexture(0);
13456ad6ec42f814e9e61030ff819cac4e5d31def8bDerek Sollenberger    layer->generateTexture();
1353e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    return layer;
1363e9999bd866fac71c72e6b484a9836c87c328a08sergeyv}
13756ad6ec42f814e9e61030ff819cac4e5d31def8bDerek Sollenberger
1383e9999bd866fac71c72e6b484a9836c87c328a08sergeyvDeferredLayerUpdater* OpenGLPipeline::createTextureLayer() {
1393e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    mEglManager.initialize();
1403e9999bd866fac71c72e6b484a9836c87c328a08sergeyv    return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
141768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
142768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
143768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievvoid OpenGLPipeline::onStop() {
144768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (mEglManager.isCurrent(mEglSurface)) {
145768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        mEglManager.makeCurrent(EGL_NO_SURFACE);
146768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
147768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
148768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
149768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
150768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
151768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
152768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        mEglManager.destroySurface(mEglSurface);
153768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        mEglSurface = EGL_NO_SURFACE;
154768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
155768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
156768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (surface) {
157768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        mEglSurface = mEglManager.createSurface(surface);
158768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
159768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
160768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    if (mEglSurface != EGL_NO_SURFACE) {
161768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
162768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
163768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        return true;
164768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    }
165768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
166768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return false;
167768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
168768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
169768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::isSurfaceReady() {
170768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
171768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
172768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
173768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievbool OpenGLPipeline::isContextReady() {
174768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return CC_LIKELY(mEglManager.hasEglContext());
175768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
176768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
177768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievvoid OpenGLPipeline::onDestroyHardwareResources() {
178daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    Caches& caches = Caches::getInstance();
179daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    // Make sure to release all the textures we were owning as there won't
180daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    // be another draw
181daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    caches.textureCache.resetMarkInUse(this);
182768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    mRenderThread.renderState().flush(Caches::FlushMode::Layers);
183768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
184768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
185768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Ilievvoid OpenGLPipeline::renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
186768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        LayerUpdateQueue* layerUpdateQueue, bool opaque,
187768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev        const BakedOpRenderer::LightInfo& lightInfo) {
188768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    static const std::vector< sp<RenderNode> > emptyNodeList;
189768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    auto& caches = Caches::getInstance();
190768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    FrameBuilder frameBuilder(*layerUpdateQueue, lightGeometry, caches);
191768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    layerUpdateQueue->clear();
192768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
193768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev            opaque, lightInfo);
194768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    LOG_ALWAYS_FATAL_IF(renderer.didDraw(), "shouldn't draw in buildlayer case");
195768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
196768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
197768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
198768e39335b668e8ef25fef30ab42b2d6d29a4735Stan IlievTaskManager* OpenGLPipeline::getTaskManager() {
199768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev    return &Caches::getInstance().tasks;
200768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev}
201768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev
2026a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenbergerstatic bool layerMatchesWH(OffscreenBuffer* layer, int width, int height) {
2036a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    return layer->viewportWidth == (uint32_t)width && layer->viewportHeight == (uint32_t)height;
2046a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger}
2056a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger
2066a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenbergerbool OpenGLPipeline::createOrUpdateLayer(RenderNode* node,
2076a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        const DamageAccumulator& damageAccumulator) {
2086a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    RenderState& renderState = mRenderThread.renderState();
2096a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    OffscreenBufferPool& layerPool = renderState.layerPool();
2106a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    bool transformUpdateNeeded = false;
2116a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    if (node->getLayer() == nullptr) {
2126a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        node->setLayer(layerPool.get(renderState, node->getWidth(), node->getHeight()));
2136a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        transformUpdateNeeded = true;
2146a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    } else if (!layerMatchesWH(node->getLayer(), node->getWidth(), node->getHeight())) {
2156a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering)
2166a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        // Or, ideally, maintain damage between frames on node/layer so ordering is always correct
2176a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        if (node->properties().fitsOnLayer()) {
2186a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger            node->setLayer(layerPool.resize(node->getLayer(), node->getWidth(), node->getHeight()));
2196a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        } else {
2206a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger            destroyLayer(node);
2216a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        }
2226a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        transformUpdateNeeded = true;
2236a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    }
2246a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger
2256a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    if (transformUpdateNeeded && node->getLayer()) {
2266a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        // update the transform in window of the layer to reset its origin wrt light source position
2276a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        Matrix4 windowTransform;
2286a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        damageAccumulator.computeCurrentTransform(&windowTransform);
2296a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        node->getLayer()->setWindowTransform(windowTransform);
2306a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    }
2316a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger
2326a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    return transformUpdateNeeded;
2336a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger}
2346a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger
235b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenbergerbool OpenGLPipeline::pinImages(LsaVector<sk_sp<Bitmap>>& images) {
236b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    TextureCache& cache = Caches::getInstance().textureCache;
237b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    bool prefetchSucceeded = true;
238b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    for (auto& bitmapResource : images) {
239b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger        prefetchSucceeded &= cache.prefetchAndMarkInUse(this, bitmapResource.get());
240b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    }
241b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    return prefetchSucceeded;
242b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger}
243b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger
244b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenbergervoid OpenGLPipeline::unpinImages() {
245b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger    Caches::getInstance().textureCache.resetMarkInUse(this);
246b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger}
247b7d34b64dd32e3d84bd43344c9c3d9ad098129afDerek Sollenberger
2486a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenbergervoid OpenGLPipeline::destroyLayer(RenderNode* node) {
2496a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    if (OffscreenBuffer* layer = node->getLayer()) {
2506a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        layer->renderState.layerPool().putOrDelete(layer);
2516a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger        node->setLayer(nullptr);
2526a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger    }
2536a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger}
2546a21ca5a4c6b62ae277ae6dcb14bc187460321a0Derek Sollenberger
255daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenbergervoid OpenGLPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
256daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    if (Caches::hasInstance() && thread.eglManager().hasEglContext()) {
257daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger        ATRACE_NAME("Bitmap#prepareToDraw task");
258daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger        Caches::getInstance().textureCache.prefetch(bitmap);
259daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    }
260daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger}
261daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger
262daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenbergervoid OpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
263daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
264daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    if (thread.eglManager().hasEglContext()) {
265daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger        mode = DrawGlInfo::kModeProcess;
266daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    }
267daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    thread.renderState().invokeFunctor(functor, mode, nullptr);
268daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger}
269daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger
270768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev} /* namespace renderthread */
271768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev} /* namespace uirenderer */
272768e39335b668e8ef25fef30ab42b2d6d29a4735Stan Iliev} /* namespace android */
273