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