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