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