CanvasContext.h revision e18264b079481a244b30e3f71012c53bbd861f92
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 handleFunctorStatus(int status, const Rect& redrawClip);
86    void removeFunctorsTask();
87    void queueFunctorsTask(int delayMs = FUNCTOR_PROCESS_DELAY);
88
89    void requireGlContext();
90
91    GlobalContext* mGlobalContext;
92    RenderThread& mRenderThread;
93    EGLSurface mEglSurface;
94    bool mDirtyRegionsEnabled;
95
96    bool mOpaque;
97    OpenGLRenderer* mCanvas;
98    bool mHaveNewSurface;
99
100    bool mInvokeFunctorsPending;
101    InvokeFunctorsTask mInvokeFunctorsTask;
102
103};
104
105} /* namespace renderthread */
106} /* namespace uirenderer */
107} /* namespace android */
108#endif /* CANVASCONTEXT_H_ */
109