CanvasContext.h revision f7d9c1dc84671d4e99657ef071d275700d85bb11
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 "RenderTask.h"
27
28#define FUNCTOR_PROCESS_DELAY 4
29
30namespace android {
31namespace uirenderer {
32
33class DeferredLayerUpdater;
34class RenderNode;
35class DisplayListData;
36class OpenGLRenderer;
37class Rect;
38class Layer;
39
40namespace renderthread {
41
42class GlobalContext;
43class CanvasContext;
44class RenderThread;
45
46class InvokeFunctorsTask : public RenderTask {
47public:
48    InvokeFunctorsTask(CanvasContext* context)
49        : mContext(context) {}
50
51    virtual void run();
52
53private:
54    CanvasContext* mContext;
55};
56
57// This per-renderer class manages the bridge between the global EGL context
58// and the render surface.
59class CanvasContext {
60public:
61    CanvasContext(bool translucent);
62    ~CanvasContext();
63
64    bool initialize(EGLNativeWindowType window);
65    void updateSurface(EGLNativeWindowType window);
66    void pauseSurface(EGLNativeWindowType window);
67    void setup(int width, int height);
68    void setDisplayListData(RenderNode* displayList, DisplayListData* newData);
69    void processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters);
70    void drawDisplayList(RenderNode* displayList, Rect* dirty);
71    void destroyCanvas();
72
73    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
74
75    void attachFunctor(Functor* functor);
76    void detachFunctor(Functor* functor);
77    void invokeFunctor(Functor* functor);
78
79    void runWithGlContext(RenderTask* task);
80
81    Layer* createRenderLayer(int width, int height);
82    Layer* createTextureLayer();
83
84private:
85    void setSurface(EGLNativeWindowType window);
86    void swapBuffers();
87    void requireSurface();
88
89    friend class InvokeFunctorsTask;
90    void invokeFunctors();
91    void removeFunctorsTask();
92    void queueFunctorsTask(int delayMs = FUNCTOR_PROCESS_DELAY);
93
94    void requireGlContext();
95
96    GlobalContext* mGlobalContext;
97    RenderThread& mRenderThread;
98    EGLSurface mEglSurface;
99    bool mDirtyRegionsEnabled;
100
101    bool mOpaque;
102    OpenGLRenderer* mCanvas;
103    bool mHaveNewSurface;
104
105    bool mInvokeFunctorsPending;
106    InvokeFunctorsTask mInvokeFunctorsTask;
107
108};
109
110} /* namespace renderthread */
111} /* namespace uirenderer */
112} /* namespace android */
113#endif /* CANVASCONTEXT_H_ */
114