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