CanvasContext.h revision 8a33e4019991c58b06adf2e3a9ac1eeeccd8fa94
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#pragma once 18 19#include "BakedOpDispatcher.h" 20#include "BakedOpRenderer.h" 21#include "DamageAccumulator.h" 22#include "FrameBuilder.h" 23#include "FrameInfo.h" 24#include "FrameInfoVisualizer.h" 25#include "FrameMetricsReporter.h" 26#include "IContextFactory.h" 27#include "LayerUpdateQueue.h" 28#include "RenderNode.h" 29#include "thread/Task.h" 30#include "thread/TaskProcessor.h" 31#include "utils/RingBuffer.h" 32#include "renderthread/RenderTask.h" 33#include "renderthread/RenderThread.h" 34 35#include <cutils/compiler.h> 36#include <EGL/egl.h> 37#include <SkBitmap.h> 38#include <SkRect.h> 39#include <utils/Functor.h> 40#include <gui/Surface.h> 41 42#include <functional> 43#include <set> 44#include <string> 45#include <vector> 46 47namespace android { 48namespace uirenderer { 49 50class AnimationContext; 51class DeferredLayerUpdater; 52class Layer; 53class Rect; 54class RenderState; 55 56namespace renderthread { 57 58class EglManager; 59 60enum SwapBehavior { 61 kSwap_default, 62 kSwap_discardBuffer, 63}; 64 65// This per-renderer class manages the bridge between the global EGL context 66// and the render surface. 67// TODO: Rename to Renderer or some other per-window, top-level manager 68class CanvasContext : public IFrameCallback { 69public: 70 static CanvasContext* create(RenderThread& thread, bool translucent, 71 RenderNode* rootRenderNode, IContextFactory* contextFactory); 72 virtual ~CanvasContext(); 73 74 // Won't take effect until next EGLSurface creation 75 void setSwapBehavior(SwapBehavior swapBehavior); 76 77 void initialize(Surface* surface); 78 void updateSurface(Surface* surface); 79 bool pauseSurface(Surface* surface); 80 void setStopped(bool stopped); 81 bool hasSurface() { return mNativeSurface.get(); } 82 83 void setup(float lightRadius, 84 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 85 void setLightCenter(const Vector3& lightCenter); 86 void setOpaque(bool opaque); 87 bool makeCurrent(); 88 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, 89 int64_t syncQueued, RenderNode* target); 90 void draw(); 91 void destroy(TreeObserver* observer); 92 93 // IFrameCallback, Choreographer-driven frame callback entry point 94 virtual void doFrame() override; 95 void prepareAndDraw(RenderNode* node); 96 97 void buildLayer(RenderNode* node, TreeObserver* observer); 98 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap); 99 void markLayerInUse(RenderNode* node); 100 101 void destroyHardwareResources(TreeObserver* observer); 102 static void trimMemory(RenderThread& thread, int level); 103 104 static void invokeFunctor(RenderThread& thread, Functor* functor); 105 106 Layer* createTextureLayer(); 107 108 ANDROID_API static void setTextureAtlas(RenderThread& thread, 109 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); 110 111 void stopDrawing(); 112 void notifyFramePending(); 113 114 FrameInfoVisualizer& profiler() { return mProfiler; } 115 116 void dumpFrames(int fd); 117 void resetFrameStats(); 118 119 void setName(const std::string&& name) { mName = name; } 120 const std::string& name() { return mName; } 121 122 void serializeDisplayListTree(); 123 124 void addRenderNode(RenderNode* node, bool placeFront) { 125 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size()); 126 mRenderNodes.emplace(mRenderNodes.begin() + pos, node); 127 } 128 129 void removeRenderNode(RenderNode* node) { 130 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node), 131 mRenderNodes.end()); 132 } 133 134 void setContentDrawBounds(int left, int top, int right, int bottom) { 135 mContentDrawBounds.set(left, top, right, bottom); 136 } 137 138 RenderState& getRenderState() { 139 return mRenderThread.renderState(); 140 } 141 142 void addFrameMetricsObserver(FrameMetricsObserver* observer) { 143 if (mFrameMetricsReporter.get() == nullptr) { 144 mFrameMetricsReporter.reset(new FrameMetricsReporter()); 145 } 146 147 mFrameMetricsReporter->addObserver(observer); 148 } 149 150 void removeFrameMetricsObserver(FrameMetricsObserver* observer) { 151 if (mFrameMetricsReporter.get() != nullptr) { 152 mFrameMetricsReporter->removeObserver(observer); 153 if (!mFrameMetricsReporter->hasObservers()) { 154 mFrameMetricsReporter.reset(nullptr); 155 } 156 } 157 } 158 159 // Used to queue up work that needs to be completed before this frame completes 160 ANDROID_API void enqueueFrameWork(std::function<void()>&& func); 161 162 ANDROID_API int64_t getFrameNumber(); 163 164private: 165 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 166 IContextFactory* contextFactory); 167 168 friend class RegisterFrameCallbackTask; 169 // TODO: Replace with something better for layer & other GL object 170 // lifecycle tracking 171 friend class android::uirenderer::RenderState; 172 173 void setSurface(Surface* window); 174 175 void freePrefetchedLayers(TreeObserver* observer); 176 177 void waitOnFences(); 178 179 bool isSwapChainStuffed(); 180 181 EGLint mLastFrameWidth = 0; 182 EGLint mLastFrameHeight = 0; 183 184 RenderThread& mRenderThread; 185 EglManager& mEglManager; 186 sp<Surface> mNativeSurface; 187 EGLSurface mEglSurface = EGL_NO_SURFACE; 188 // stopped indicates the CanvasContext will reject actual redraw operations, 189 // and defer repaint until it is un-stopped 190 bool mStopped = false; 191 // CanvasContext is dirty if it has received an update that it has not 192 // painted onto its surface. 193 bool mIsDirty = false; 194 bool mBufferPreserved = false; 195 SwapBehavior mSwapBehavior = kSwap_default; 196 struct SwapHistory { 197 SkRect damage; 198 nsecs_t vsyncTime; 199 nsecs_t swapCompletedTime; 200 nsecs_t dequeueDuration; 201 nsecs_t queueDuration; 202 }; 203 204 RingBuffer<SwapHistory, 3> mSwapHistory; 205 int64_t mFrameNumber = -1; 206 207 bool mOpaque; 208 BakedOpRenderer::LightInfo mLightInfo; 209 FrameBuilder::LightGeometry mLightGeometry = { {0, 0, 0}, 0 }; 210 211 bool mHaveNewSurface = false; 212 DamageAccumulator mDamageAccumulator; 213 LayerUpdateQueue mLayerUpdateQueue; 214 std::unique_ptr<AnimationContext> mAnimationContext; 215 216 std::vector< sp<RenderNode> > mRenderNodes; 217 218 FrameInfo* mCurrentFrameInfo = nullptr; 219 // Ring buffer large enough for 2 seconds worth of frames 220 RingBuffer<FrameInfo, 120> mFrames; 221 std::string mName; 222 JankTracker mJankTracker; 223 FrameInfoVisualizer mProfiler; 224 std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter; 225 226 std::set<RenderNode*> mPrefetchedLayers; 227 228 // Stores the bounds of the main content. 229 Rect mContentDrawBounds; 230 231 // TODO: This is really a Task<void> but that doesn't really work 232 // when Future<> expects to be able to get/set a value 233 struct FuncTask : public Task<bool> { 234 std::function<void()> func; 235 }; 236 class FuncTaskProcessor; 237 238 std::vector< sp<FuncTask> > mFrameFences; 239 sp<TaskProcessor<bool> > mFrameWorkProcessor; 240}; 241 242} /* namespace renderthread */ 243} /* namespace uirenderer */ 244} /* namespace android */ 245