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