123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck/*
223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Copyright (C) 2014 The Android Open Source Project
323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Licensed under the Apache License, Version 2.0 (the "License");
523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * you may not use this file except in compliance with the License.
623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * You may obtain a copy of the License at
723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *      http://www.apache.org/licenses/LICENSE-2.0
923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
1023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Unless required by applicable law or agreed to in writing, software
1123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * distributed under the License is distributed on an "AS IS" BASIS,
1223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * See the License for the specific language governing permissions and
1423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * limitations under the License.
1523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck */
1623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
1723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#ifndef CANVASCONTEXT_H_
1823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#define CANVASCONTEXT_H_
1923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
200e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include <set>
21998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
2223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include <cutils/compiler.h>
2323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include <EGL/egl.h>
2419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck#include <SkBitmap.h>
254f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include <utils/Functor.h>
2619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck#include <utils/Vector.h>
274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
280e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "../DamageAccumulator.h"
290e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "../DrawProfiler.h"
300e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "../IContextFactory.h"
310e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "../RenderNode.h"
320e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "RenderTask.h"
330e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#include "RenderThread.h"
340e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck
350e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck#define FUNCTOR_PROCESS_DELAY 4
3623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
3723b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace android {
3823b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace uirenderer {
394f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
40119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext;
4119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reckclass DeferredLayerUpdater;
424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckclass OpenGLRenderer;
434f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckclass Rect;
441949e7928eeec22cd3f74b5f763a4eb433238453John Reckclass Layer;
45443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reckclass RenderState;
464f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
4723b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace renderthread {
4823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
493b20251a355c88193c439f928a84ae69483fb488John Reckclass EglManager;
504f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
511125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckenum SwapBehavior {
521125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    kSwap_default,
531125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    kSwap_discardBuffer,
541125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck};
551125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
5623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck// This per-renderer class manages the bridge between the global EGL context
5723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck// and the render surface.
58119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck// TODO: Rename to Renderer or some other per-window, top-level manager
59e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckclass CanvasContext : public IFrameCallback {
6023b797ab5151eb2474f3bdd679f2f07bfd723042John Reckpublic:
61119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
62119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck            IContextFactory* contextFactory);
63e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    virtual ~CanvasContext();
6423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
651125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    // Won't take effect until next EGLSurface creation
661125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    void setSwapBehavior(SwapBehavior swapBehavior);
671125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
68a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    bool initialize(ANativeWindow* window);
69a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    void updateSurface(ANativeWindow* window);
7001a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    bool pauseSurface(ANativeWindow* window);
71aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    bool hasSurface() { return mNativeWindow.get(); }
72aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck
73058fc640017c90120c599d378a4cbc55668b05b7Chris Craik    void setup(int width, int height, const Vector3& lightCenter, float lightRadius,
74058fc640017c90120c599d378a4cbc55668b05b7Chris Craik            uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
7563a06673253914510bbeebd500655008682dade1John Reck    void setOpaque(bool opaque);
76860d155f866cc15a725e7ce03763280987f24901John Reck    void makeCurrent();
7768bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    void processLayerUpdate(DeferredLayerUpdater* layerUpdater);
780e40462e11d27eb859b829b112cecb8c6f0d7afbJohn Reck    void prepareTree(TreeInfo& info);
79e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    void draw();
8017035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    void destroy();
8123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
82e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    // IFrameCallback, Chroreographer-driven frame callback entry point
8318f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck    virtual void doFrame();
84e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
853e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    void buildLayer(RenderNode* node);
8619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
87998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    void markLayerInUse(RenderNode* node);
8819b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
89f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    void destroyHardwareResources();
90f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    static void trimMemory(RenderThread& thread, int level);
91e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
923b20251a355c88193c439f928a84ae69483fb488John Reck    static void invokeFunctor(RenderThread& thread, Functor* functor);
9323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
94fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reck    void runWithGlContext(RenderTask* task);
95fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reck
961949e7928eeec22cd3f74b5f763a4eb433238453John Reck    Layer* createTextureLayer();
971949e7928eeec22cd3f74b5f763a4eb433238453John Reck
983b20251a355c88193c439f928a84ae69483fb488John Reck    ANDROID_API static void setTextureAtlas(RenderThread& thread,
993b20251a355c88193c439f928a84ae69483fb488John Reck            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
10066f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck
101f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    void stopDrawing();
102a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    void notifyFramePending();
103a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
104fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    DrawProfiler& profiler() { return mProfiler; }
105fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
10623b797ab5151eb2474f3bdd679f2f07bfd723042John Reckprivate:
107a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    friend class RegisterFrameCallbackTask;
108443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    // TODO: Replace with something better for layer & other GL object
109443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    // lifecycle tracking
110443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    friend class android::uirenderer::RenderState;
111a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
112a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    void setSurface(ANativeWindow* window);
1134f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    void swapBuffers();
114f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    void requireSurface();
1154f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
11619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    void requireGlContext();
11719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
118998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    void freePrefetechedLayers();
119998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
1204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    RenderThread& mRenderThread;
1213b20251a355c88193c439f928a84ae69483fb488John Reck    EglManager& mEglManager;
122a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    sp<ANativeWindow> mNativeWindow;
12323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    EGLSurface mEglSurface;
1241125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    bool mBufferPreserved;
1251125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    SwapBehavior mSwapBehavior;
1264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    bool mOpaque;
1284f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    OpenGLRenderer* mCanvas;
1294f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    bool mHaveNewSurface;
130e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    DamageAccumulator mDamageAccumulator;
131119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext* mAnimationContext;
132e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
133e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    const sp<RenderNode> mRootRenderNode;
134fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
135fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    DrawProfiler mProfiler;
136998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
137998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    std::set<RenderNode*> mPrefetechedLayers;
13823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck};
13923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
14023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace renderthread */
14123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace uirenderer */
14223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace android */
14323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#endif /* CANVASCONTEXT_H_ */
144