CanvasContext.h revision ba6adf66d3c44c0aa2fd8a224862ff1901d64300
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
17#ifndef CANVASCONTEXT_H_
18#define CANVASCONTEXT_H_
19
20#include "DamageAccumulator.h"
21#include "DrawProfiler.h"
22#include "IContextFactory.h"
23#include "FrameInfo.h"
24#include "RenderNode.h"
25#include "utils/RingBuffer.h"
26#include "renderthread/RenderTask.h"
27#include "renderthread/RenderThread.h"
28
29#include <cutils/compiler.h>
30#include <EGL/egl.h>
31#include <SkBitmap.h>
32#include <utils/Functor.h>
33#include <utils/Vector.h>
34
35#include <set>
36
37namespace android {
38namespace uirenderer {
39
40class AnimationContext;
41class DeferredLayerUpdater;
42class OpenGLRenderer;
43class Rect;
44class Layer;
45class RenderState;
46
47namespace renderthread {
48
49class EglManager;
50
51enum SwapBehavior {
52    kSwap_default,
53    kSwap_discardBuffer,
54};
55
56// This per-renderer class manages the bridge between the global EGL context
57// and the render surface.
58// TODO: Rename to Renderer or some other per-window, top-level manager
59class CanvasContext : public IFrameCallback {
60public:
61    CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
62            IContextFactory* contextFactory);
63    virtual ~CanvasContext();
64
65    // Won't take effect until next EGLSurface creation
66    void setSwapBehavior(SwapBehavior swapBehavior);
67
68    bool initialize(ANativeWindow* window);
69    void updateSurface(ANativeWindow* window);
70    bool pauseSurface(ANativeWindow* window);
71    bool hasSurface() { return mNativeWindow.get(); }
72
73    void setup(int width, int height, const Vector3& lightCenter, float lightRadius,
74            uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
75    void setOpaque(bool opaque);
76    void makeCurrent();
77    void processLayerUpdate(DeferredLayerUpdater* layerUpdater);
78    void prepareTree(TreeInfo& info, int64_t* uiFrameInfo);
79    void draw();
80    void destroy();
81
82    // IFrameCallback, Chroreographer-driven frame callback entry point
83    virtual void doFrame() override;
84
85    void buildLayer(RenderNode* node);
86    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
87    void markLayerInUse(RenderNode* node);
88
89    void destroyHardwareResources();
90    static void trimMemory(RenderThread& thread, int level);
91
92    static void invokeFunctor(RenderThread& thread, Functor* functor);
93
94    void runWithGlContext(RenderTask* task);
95
96    Layer* createTextureLayer();
97
98    ANDROID_API static void setTextureAtlas(RenderThread& thread,
99            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
100
101    void stopDrawing();
102    void notifyFramePending();
103
104    DrawProfiler& profiler() { return mProfiler; }
105
106    void dumpFrames(int fd);
107    void resetFrameStats();
108
109private:
110    friend class RegisterFrameCallbackTask;
111    // TODO: Replace with something better for layer & other GL object
112    // lifecycle tracking
113    friend class android::uirenderer::RenderState;
114
115    void setSurface(ANativeWindow* window);
116    void swapBuffers();
117    void requireSurface();
118
119    void requireGlContext();
120
121    void freePrefetechedLayers();
122
123    RenderThread& mRenderThread;
124    EglManager& mEglManager;
125    sp<ANativeWindow> mNativeWindow;
126    EGLSurface mEglSurface;
127    bool mBufferPreserved;
128    SwapBehavior mSwapBehavior;
129
130    bool mOpaque;
131    OpenGLRenderer* mCanvas;
132    bool mHaveNewSurface;
133    DamageAccumulator mDamageAccumulator;
134    std::unique_ptr<AnimationContext> mAnimationContext;
135
136    const sp<RenderNode> mRootRenderNode;
137
138    DrawProfiler mProfiler;
139    FrameInfo* mCurrentFrameInfo;
140    // Ring buffer large enough for 1 second worth of frames
141    RingBuffer<FrameInfo, 60> mFrames;
142
143    std::set<RenderNode*> mPrefetechedLayers;
144};
145
146} /* namespace renderthread */
147} /* namespace uirenderer */
148} /* namespace android */
149#endif /* CANVASCONTEXT_H_ */
150