CanvasContext.h revision f47a594f5250b1914c36423ee6b371f0b8db09d0
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);
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 destroyHardwareResources();
70    static void trimMemory(RenderThread& thread, int level);
71
72    static void invokeFunctor(RenderThread& thread, Functor* functor);
73
74    void runWithGlContext(RenderTask* task);
75
76    Layer* createRenderLayer(int width, int height);
77    Layer* createTextureLayer();
78
79    ANDROID_API static void setTextureAtlas(RenderThread& thread,
80            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
81
82    void stopDrawing();
83    void notifyFramePending();
84
85    DrawProfiler& profiler() { return mProfiler; }
86
87private:
88    friend class RegisterFrameCallbackTask;
89
90    void setSurface(ANativeWindow* window);
91    void swapBuffers();
92    void requireSurface();
93
94    void requireGlContext();
95
96    RenderThread& mRenderThread;
97    EglManager& mEglManager;
98    sp<ANativeWindow> mNativeWindow;
99    EGLSurface mEglSurface;
100    bool mDirtyRegionsEnabled;
101
102    bool mOpaque;
103    OpenGLRenderer* mCanvas;
104    bool mHaveNewSurface;
105    DamageAccumulator mDamageAccumulator;
106
107    const sp<RenderNode> mRootRenderNode;
108
109    DrawProfiler mProfiler;
110};
111
112} /* namespace renderthread */
113} /* namespace uirenderer */
114} /* namespace android */
115#endif /* CANVASCONTEXT_H_ */
116