CanvasContext.h revision 3b20251a355c88193c439f928a84ae69483fb488
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 <cutils/compiler.h>
21#include <EGL/egl.h>
22#include <SkBitmap.h>
23#include <utils/Functor.h>
24#include <utils/Vector.h>
25
26#include "../DamageAccumulator.h"
27#include "../DrawProfiler.h"
28#include "../RenderNode.h"
29#include "RenderTask.h"
30#include "RenderThread.h"
31
32#define FUNCTOR_PROCESS_DELAY 4
33
34namespace android {
35namespace uirenderer {
36
37class DeferredLayerUpdater;
38class OpenGLRenderer;
39class Rect;
40class Layer;
41
42namespace renderthread {
43
44class EglManager;
45
46// This per-renderer class manages the bridge between the global EGL context
47// and the render surface.
48class CanvasContext : public IFrameCallback {
49public:
50    CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode);
51    virtual ~CanvasContext();
52
53    bool initialize(ANativeWindow* window);
54    void updateSurface(ANativeWindow* window);
55    void pauseSurface(ANativeWindow* window);
56    void setup(int width, int height, const Vector3& lightCenter, float lightRadius);
57    void setOpaque(bool opaque);
58    void makeCurrent();
59    void processLayerUpdate(DeferredLayerUpdater* layerUpdater, TreeInfo& info);
60    void prepareTree(TreeInfo& info);
61    void draw();
62    void destroyCanvasAndSurface();
63
64    // IFrameCallback, Chroreographer-driven frame callback entry point
65    virtual void doFrame();
66
67    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
68
69    void flushCaches(Caches::FlushMode flushMode);
70
71    static void invokeFunctor(RenderThread& thread, Functor* functor);
72
73    void runWithGlContext(RenderTask* task);
74
75    Layer* createRenderLayer(int width, int height);
76    Layer* createTextureLayer();
77
78    ANDROID_API static void setTextureAtlas(RenderThread& thread,
79            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
80
81    void notifyFramePending();
82
83    DrawProfiler& profiler() { return mProfiler; }
84
85private:
86    friend class RegisterFrameCallbackTask;
87
88    void setSurface(ANativeWindow* window);
89    void swapBuffers();
90    void requireSurface();
91
92    void requireGlContext();
93
94    RenderThread& mRenderThread;
95    EglManager& mEglManager;
96    sp<ANativeWindow> mNativeWindow;
97    EGLSurface mEglSurface;
98    bool mDirtyRegionsEnabled;
99
100    bool mOpaque;
101    OpenGLRenderer* mCanvas;
102    bool mHaveNewSurface;
103    DamageAccumulator mDamageAccumulator;
104
105    const sp<RenderNode> mRootRenderNode;
106
107    DrawProfiler mProfiler;
108};
109
110} /* namespace renderthread */
111} /* namespace uirenderer */
112} /* namespace android */
113#endif /* CANVASCONTEXT_H_ */
114