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