CanvasContext.h revision 3e8249568cc428296ac76c7ddce3f0382d40fe5b
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            uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
58    void setOpaque(bool opaque);
59    void makeCurrent();
60    void processLayerUpdate(DeferredLayerUpdater* layerUpdater);
61    void prepareTree(TreeInfo& info);
62    void draw();
63    void destroyCanvasAndSurface();
64
65    // IFrameCallback, Chroreographer-driven frame callback entry point
66    virtual void doFrame();
67
68    void buildLayer(RenderNode* node);
69    bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
70
71    void destroyHardwareResources();
72    static void trimMemory(RenderThread& thread, int level);
73
74    static void invokeFunctor(RenderThread& thread, Functor* functor);
75
76    void runWithGlContext(RenderTask* task);
77
78    Layer* createRenderLayer(int width, int height);
79    Layer* createTextureLayer();
80
81    ANDROID_API static void setTextureAtlas(RenderThread& thread,
82            const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
83
84    void stopDrawing();
85    void notifyFramePending();
86
87    DrawProfiler& profiler() { return mProfiler; }
88
89private:
90    friend class RegisterFrameCallbackTask;
91
92    void setSurface(ANativeWindow* window);
93    void swapBuffers();
94    void requireSurface();
95
96    void requireGlContext();
97
98    RenderThread& mRenderThread;
99    EglManager& mEglManager;
100    sp<ANativeWindow> mNativeWindow;
101    EGLSurface mEglSurface;
102    bool mDirtyRegionsEnabled;
103
104    bool mOpaque;
105    OpenGLRenderer* mCanvas;
106    bool mHaveNewSurface;
107    DamageAccumulator mDamageAccumulator;
108
109    const sp<RenderNode> mRootRenderNode;
110
111    DrawProfiler mProfiler;
112};
113
114} /* namespace renderthread */
115} /* namespace uirenderer */
116} /* namespace android */
117#endif /* CANVASCONTEXT_H_ */
118