CanvasContext.h revision e248bd1b2c3fcf8088429507e73b31f45ee2544b
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 "DamageAccumulator.h" 21#include "IContextFactory.h" 22#include "FrameInfo.h" 23#include "FrameInfoVisualizer.h" 24#include "RenderNode.h" 25#include "utils/RingBuffer.h" 26#include "renderthread/RenderTask.h" 27#include "renderthread/RenderThread.h" 28 29#include <cutils/compiler.h> 30#include <EGL/egl.h> 31#include <SkBitmap.h> 32#include <SkRect.h> 33#include <utils/Functor.h> 34 35#include <set> 36#include <string> 37 38namespace android { 39namespace uirenderer { 40 41class AnimationContext; 42class DeferredLayerUpdater; 43class OpenGLRenderer; 44class Rect; 45class Layer; 46class RenderState; 47 48namespace renderthread { 49 50class EglManager; 51 52enum SwapBehavior { 53 kSwap_default, 54 kSwap_discardBuffer, 55}; 56 57// This per-renderer class manages the bridge between the global EGL context 58// and the render surface. 59// TODO: Rename to Renderer or some other per-window, top-level manager 60class CanvasContext : public IFrameCallback { 61public: 62 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 63 IContextFactory* contextFactory); 64 virtual ~CanvasContext(); 65 66 // Won't take effect until next EGLSurface creation 67 void setSwapBehavior(SwapBehavior swapBehavior); 68 69 bool initialize(ANativeWindow* window); 70 void updateSurface(ANativeWindow* window); 71 bool pauseSurface(ANativeWindow* window); 72 bool hasSurface() { return mNativeWindow.get(); } 73 74 void setup(int width, int height, float lightRadius, 75 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 76 void setLightCenter(const Vector3& lightCenter); 77 void setOpaque(bool opaque); 78 void makeCurrent(); 79 void processLayerUpdate(DeferredLayerUpdater* layerUpdater); 80 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued); 81 void draw(); 82 void destroy(); 83 84 // IFrameCallback, Chroreographer-driven frame callback entry point 85 virtual void doFrame() override; 86 87 void buildLayer(RenderNode* node); 88 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap); 89 void markLayerInUse(RenderNode* node); 90 91 void destroyHardwareResources(); 92 static void trimMemory(RenderThread& thread, int level); 93 94 static void invokeFunctor(RenderThread& thread, Functor* functor); 95 96 void runWithGlContext(RenderTask* task); 97 98 Layer* createTextureLayer(); 99 100 ANDROID_API static void setTextureAtlas(RenderThread& thread, 101 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); 102 103 void stopDrawing(); 104 void notifyFramePending(); 105 106 FrameInfoVisualizer& profiler() { return mProfiler; } 107 108 void dumpFrames(int fd); 109 void resetFrameStats(); 110 111 void setName(const std::string&& name) { mName = name; } 112 const std::string& name() { return mName; } 113 114 void serializeDisplayListTree(); 115 116private: 117 friend class RegisterFrameCallbackTask; 118 // TODO: Replace with something better for layer & other GL object 119 // lifecycle tracking 120 friend class android::uirenderer::RenderState; 121 122 void setSurface(ANativeWindow* window); 123 void requireSurface(); 124 125 void freePrefetechedLayers(); 126 127 RenderThread& mRenderThread; 128 EglManager& mEglManager; 129 sp<ANativeWindow> mNativeWindow; 130 EGLSurface mEglSurface = EGL_NO_SURFACE; 131 bool mBufferPreserved = false; 132 SwapBehavior mSwapBehavior = kSwap_default; 133 RingBuffer<SkRect, 3> mDamageHistory; 134 135 bool mOpaque; 136 OpenGLRenderer* mCanvas = nullptr; 137 bool mHaveNewSurface = false; 138 DamageAccumulator mDamageAccumulator; 139 std::unique_ptr<AnimationContext> mAnimationContext; 140 141 const sp<RenderNode> mRootRenderNode; 142 143 FrameInfo* mCurrentFrameInfo = nullptr; 144 // Ring buffer large enough for 2 seconds worth of frames 145 RingBuffer<FrameInfo, 120> mFrames; 146 std::string mName; 147 JankTracker mJankTracker; 148 FrameInfoVisualizer mProfiler; 149 150 std::set<RenderNode*> mPrefetechedLayers; 151}; 152 153} /* namespace renderthread */ 154} /* namespace uirenderer */ 155} /* namespace android */ 156#endif /* CANVASCONTEXT_H_ */ 157