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