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 "FrameInfo.h"
22#include "FrameInfoVisualizer.h"
23#include "FrameMetricsReporter.h"
24#include "IContextFactory.h"
25#include "LayerUpdateQueue.h"
26#include "RenderNode.h"
27#include "thread/Task.h"
28#include "thread/TaskProcessor.h"
29#include "utils/RingBuffer.h"
30#include "renderthread/RenderTask.h"
31#include "renderthread/RenderThread.h"
32
33#if HWUI_NEW_OPS
34#include "BakedOpDispatcher.h"
35#include "BakedOpRenderer.h"
36#include "FrameBuilder.h"
37#endif
38
39#include <cutils/compiler.h>
40#include <EGL/egl.h>
41#include <SkBitmap.h>
42#include <SkRect.h>
43#include <utils/Functor.h>
44#include <gui/Surface.h>
45
46#include <functional>
47#include <set>
48#include <string>
49#include <vector>
50
51namespace android {
52namespace uirenderer {
53
54class AnimationContext;
55class DeferredLayerUpdater;
56class OpenGLRenderer;
57class Rect;
58class Layer;
59class RenderState;
60
61namespace renderthread {
62
63class EglManager;
64
65enum SwapBehavior {
66    kSwap_default,
67    kSwap_discardBuffer,
68};
69
70// This per-renderer class manages the bridge between the global EGL context
71// and the render surface.
72// TODO: Rename to Renderer or some other per-window, top-level manager
73class CanvasContext : public IFrameCallback {
74public:
75    CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
76            IContextFactory* contextFactory);
77    virtual ~CanvasContext();
78
79    // Won't take effect until next EGLSurface creation
80    void setSwapBehavior(SwapBehavior swapBehavior);
81
82    void initialize(Surface* surface);
83    void updateSurface(Surface* surface);
84    bool pauseSurface(Surface* surface);
85    void setStopped(bool stopped);
86    bool hasSurface() { return mNativeSurface.get(); }
87
88    void setup(int width, int height, float lightRadius,
89            uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
90    void setLightCenter(const Vector3& lightCenter);
91    void setOpaque(bool opaque);
92    bool makeCurrent();
93    void prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
94            int64_t syncQueued, RenderNode* target);
95    void draw();
96    void destroy(TreeObserver* observer);
97
98    // IFrameCallback, Choreographer-driven frame callback entry point
99    virtual void doFrame() override;
100    void prepareAndDraw(RenderNode* node);
101
102    void buildLayer(RenderNode* node, TreeObserver* observer);
103    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
104    void markLayerInUse(RenderNode* node);
105
106    void destroyHardwareResources(TreeObserver* observer);
107    static void trimMemory(RenderThread& thread, int level);
108
109    static void invokeFunctor(RenderThread& thread, Functor* functor);
110
111    void runWithGlContext(RenderTask* task);
112
113    Layer* createTextureLayer();
114
115    ANDROID_API static void setTextureAtlas(RenderThread& thread,
116            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
117
118    void stopDrawing();
119    void notifyFramePending();
120
121    FrameInfoVisualizer& profiler() { return mProfiler; }
122
123    void dumpFrames(int fd);
124    void resetFrameStats();
125
126    void setName(const std::string&& name) { mName = name; }
127    const std::string& name() { return mName; }
128
129    void serializeDisplayListTree();
130
131    void addRenderNode(RenderNode* node, bool placeFront) {
132        int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size());
133        mRenderNodes.emplace(mRenderNodes.begin() + pos, node);
134    }
135
136    void removeRenderNode(RenderNode* node) {
137        mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node),
138                mRenderNodes.end());
139    }
140
141    void setContentDrawBounds(int left, int top, int right, int bottom) {
142        mContentDrawBounds.set(left, top, right, bottom);
143    }
144
145    RenderState& getRenderState() {
146        return mRenderThread.renderState();
147    }
148
149    void addFrameMetricsObserver(FrameMetricsObserver* observer) {
150        if (mFrameMetricsReporter.get() == nullptr) {
151            mFrameMetricsReporter.reset(new FrameMetricsReporter());
152        }
153
154        mFrameMetricsReporter->addObserver(observer);
155    }
156
157    void removeFrameMetricsObserver(FrameMetricsObserver* observer) {
158        if (mFrameMetricsReporter.get() != nullptr) {
159            mFrameMetricsReporter->removeObserver(observer);
160            if (!mFrameMetricsReporter->hasObservers()) {
161                mFrameMetricsReporter.reset(nullptr);
162            }
163        }
164    }
165
166    // Used to queue up work that needs to be completed before this frame completes
167    ANDROID_API void enqueueFrameWork(std::function<void()>&& func);
168
169    ANDROID_API int64_t getFrameNumber();
170
171private:
172    friend class RegisterFrameCallbackTask;
173    // TODO: Replace with something better for layer & other GL object
174    // lifecycle tracking
175    friend class android::uirenderer::RenderState;
176
177    void setSurface(Surface* window);
178
179    void freePrefetchedLayers(TreeObserver* observer);
180
181    void waitOnFences();
182
183    EGLint mLastFrameWidth = 0;
184    EGLint mLastFrameHeight = 0;
185
186    RenderThread& mRenderThread;
187    EglManager& mEglManager;
188    sp<Surface> mNativeSurface;
189    EGLSurface mEglSurface = EGL_NO_SURFACE;
190    // stopped indicates the CanvasContext will reject actual redraw operations,
191    // and defer repaint until it is un-stopped
192    bool mStopped = false;
193    // CanvasContext is dirty if it has received an update that it has not
194    // painted onto its surface.
195    bool mIsDirty = false;
196    bool mBufferPreserved = false;
197    SwapBehavior mSwapBehavior = kSwap_default;
198    struct SwapHistory {
199        SkRect damage;
200        nsecs_t vsyncTime;
201        nsecs_t swapTime;
202    };
203
204    RingBuffer<SwapHistory, 3> mSwapHistory;
205    int64_t mFrameNumber = -1;
206
207    bool mOpaque;
208#if HWUI_NEW_OPS
209    BakedOpRenderer::LightInfo mLightInfo;
210    FrameBuilder::LightGeometry mLightGeometry = { {0, 0, 0}, 0 };
211#else
212    OpenGLRenderer* mCanvas = nullptr;
213#endif
214
215    bool mHaveNewSurface = false;
216    DamageAccumulator mDamageAccumulator;
217    LayerUpdateQueue mLayerUpdateQueue;
218    std::unique_ptr<AnimationContext> mAnimationContext;
219
220    std::vector< sp<RenderNode> > mRenderNodes;
221
222    FrameInfo* mCurrentFrameInfo = nullptr;
223    // Ring buffer large enough for 2 seconds worth of frames
224    RingBuffer<FrameInfo, 120> mFrames;
225    std::string mName;
226    JankTracker mJankTracker;
227    FrameInfoVisualizer mProfiler;
228    std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter;
229
230    std::set<RenderNode*> mPrefetchedLayers;
231
232    // Stores the bounds of the main content.
233    Rect mContentDrawBounds;
234
235    // TODO: This is really a Task<void> but that doesn't really work
236    // when Future<> expects to be able to get/set a value
237    struct FuncTask : public Task<bool> {
238        std::function<void()> func;
239    };
240    class FuncTaskProcessor;
241
242    std::vector< sp<FuncTask> > mFrameFences;
243    sp<TaskProcessor<bool> > mFrameWorkProcessor;
244};
245
246} /* namespace renderthread */
247} /* namespace uirenderer */
248} /* namespace android */
249#endif /* CANVASCONTEXT_H_ */
250