CanvasContext.h revision 189e87498f666e94dc8c8201e7bac56bb09b9251
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 "IRenderPipeline.h" 28#include "LayerUpdateQueue.h" 29#include "RenderNode.h" 30#include "thread/Task.h" 31#include "thread/TaskProcessor.h" 32#include "utils/RingBuffer.h" 33#include "renderthread/RenderTask.h" 34#include "renderthread/RenderThread.h" 35 36#include <cutils/compiler.h> 37#include <EGL/egl.h> 38#include <SkBitmap.h> 39#include <SkRect.h> 40#include <utils/Functor.h> 41#include <gui/Surface.h> 42 43#include <functional> 44#include <set> 45#include <string> 46#include <vector> 47 48namespace android { 49namespace uirenderer { 50 51class AnimationContext; 52class DeferredLayerUpdater; 53class Layer; 54class Rect; 55class RenderState; 56 57namespace renderthread { 58 59class EglManager; 60class Frame; 61 62// This per-renderer class manages the bridge between the global EGL context 63// and the render surface. 64// TODO: Rename to Renderer or some other per-window, top-level manager 65class CanvasContext : public IFrameCallback { 66public: 67 static CanvasContext* create(RenderThread& thread, bool translucent, 68 RenderNode* rootRenderNode, IContextFactory* contextFactory); 69 virtual ~CanvasContext(); 70 71 /** 72 * Update or create a layer specific for the provided RenderNode. The layer 73 * attached to the node will be specific to the RenderPipeline used by this 74 * context 75 * 76 * @return true if the layer has been created or updated 77 */ 78 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator) { 79 return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator); 80 } 81 82 /** 83 * Pin any mutable images to the GPU cache. A pinned images is guaranteed to 84 * remain in the cache until it has been unpinned. We leverage this feature 85 * to avoid making a CPU copy of the pixels. 86 * 87 * @return true if all images have been successfully pinned to the GPU cache 88 * and false otherwise (e.g. cache limits have been exceeded). 89 */ 90 bool pinImages(std::vector<SkImage*>& mutableImages) { 91 return mRenderPipeline->pinImages(mutableImages); 92 } 93 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) { 94 return mRenderPipeline->pinImages(images); 95 } 96 97 /** 98 * Unpin any image that had be previously pinned to the GPU cache 99 */ 100 void unpinImages() { mRenderPipeline->unpinImages(); } 101 102 /** 103 * Destroy any layers that have been attached to the provided RenderNode removing 104 * any state that may have been set during createOrUpdateLayer(). 105 */ 106 static void destroyLayer(RenderNode* node); 107 108 static void invokeFunctor(const RenderThread& thread, Functor* functor); 109 110 static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap); 111 112 /* 113 * If Properties::isSkiaEnabled() is true then this will return the Skia 114 * grContext associated with the current RenderPipeline. 115 */ 116 GrContext* getGrContext() const { return mRenderThread.getGrContext(); } 117 118 // Won't take effect until next EGLSurface creation 119 void setSwapBehavior(SwapBehavior swapBehavior); 120 121 void initialize(Surface* surface); 122 void updateSurface(Surface* surface); 123 bool pauseSurface(Surface* surface); 124 void setStopped(bool stopped); 125 bool hasSurface() { return mNativeSurface.get(); } 126 127 void setup(float lightRadius, 128 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 129 void setLightCenter(const Vector3& lightCenter); 130 void setOpaque(bool opaque); 131 bool makeCurrent(); 132 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, 133 int64_t syncQueued, RenderNode* target); 134 void draw(); 135 void destroy(TreeObserver* observer); 136 137 // IFrameCallback, Choreographer-driven frame callback entry point 138 virtual void doFrame() override; 139 void prepareAndDraw(RenderNode* node); 140 141 void buildLayer(RenderNode* node, TreeObserver* observer); 142 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap); 143 void markLayerInUse(RenderNode* node); 144 145 void destroyHardwareResources(TreeObserver* observer); 146 static void trimMemory(RenderThread& thread, int level); 147 148 DeferredLayerUpdater* createTextureLayer(); 149 150 void stopDrawing(); 151 void notifyFramePending(); 152 153 FrameInfoVisualizer& profiler() { return mProfiler; } 154 155 void dumpFrames(int fd); 156 void resetFrameStats(); 157 158 void setName(const std::string&& name) { mName = name; } 159 const std::string& name() { return mName; } 160 161 void serializeDisplayListTree(); 162 163 void addRenderNode(RenderNode* node, bool placeFront) { 164 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size()); 165 mRenderNodes.emplace(mRenderNodes.begin() + pos, node); 166 } 167 168 void removeRenderNode(RenderNode* node) { 169 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node), 170 mRenderNodes.end()); 171 } 172 173 void setContentDrawBounds(int left, int top, int right, int bottom) { 174 mContentDrawBounds.set(left, top, right, bottom); 175 } 176 177 RenderState& getRenderState() { 178 return mRenderThread.renderState(); 179 } 180 181 void addFrameMetricsObserver(FrameMetricsObserver* observer) { 182 if (mFrameMetricsReporter.get() == nullptr) { 183 mFrameMetricsReporter.reset(new FrameMetricsReporter()); 184 } 185 186 mFrameMetricsReporter->addObserver(observer); 187 } 188 189 void removeFrameMetricsObserver(FrameMetricsObserver* observer) { 190 if (mFrameMetricsReporter.get() != nullptr) { 191 mFrameMetricsReporter->removeObserver(observer); 192 if (!mFrameMetricsReporter->hasObservers()) { 193 mFrameMetricsReporter.reset(nullptr); 194 } 195 } 196 } 197 198 // Used to queue up work that needs to be completed before this frame completes 199 ANDROID_API void enqueueFrameWork(std::function<void()>&& func); 200 201 ANDROID_API int64_t getFrameNumber(); 202 203 void waitOnFences(); 204 205private: 206 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 207 IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline); 208 209 friend class RegisterFrameCallbackTask; 210 // TODO: Replace with something better for layer & other GL object 211 // lifecycle tracking 212 friend class android::uirenderer::RenderState; 213 214 void setSurface(Surface* window); 215 216 void freePrefetchedLayers(TreeObserver* observer); 217 218 bool isSwapChainStuffed(); 219 220 SkRect computeDirtyRect(const Frame& frame, SkRect* dirty); 221 222 EGLint mLastFrameWidth = 0; 223 EGLint mLastFrameHeight = 0; 224 225 RenderThread& mRenderThread; 226 sp<Surface> mNativeSurface; 227 // stopped indicates the CanvasContext will reject actual redraw operations, 228 // and defer repaint until it is un-stopped 229 bool mStopped = false; 230 // CanvasContext is dirty if it has received an update that it has not 231 // painted onto its surface. 232 bool mIsDirty = false; 233 SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default; 234 struct SwapHistory { 235 SkRect damage; 236 nsecs_t vsyncTime; 237 nsecs_t swapCompletedTime; 238 nsecs_t dequeueDuration; 239 nsecs_t queueDuration; 240 }; 241 242 RingBuffer<SwapHistory, 3> mSwapHistory; 243 int64_t mFrameNumber = -1; 244 245 // last vsync for a dropped frame due to stuffed queue 246 nsecs_t mLastDropVsync = 0; 247 248 bool mOpaque; 249 BakedOpRenderer::LightInfo mLightInfo; 250 FrameBuilder::LightGeometry mLightGeometry = { {0, 0, 0}, 0 }; 251 252 bool mHaveNewSurface = false; 253 DamageAccumulator mDamageAccumulator; 254 LayerUpdateQueue mLayerUpdateQueue; 255 std::unique_ptr<AnimationContext> mAnimationContext; 256 257 std::vector< sp<RenderNode> > mRenderNodes; 258 259 FrameInfo* mCurrentFrameInfo = nullptr; 260 // Ring buffer large enough for 2 seconds worth of frames 261 RingBuffer<FrameInfo, 120> mFrames; 262 std::string mName; 263 JankTracker mJankTracker; 264 FrameInfoVisualizer mProfiler; 265 std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter; 266 267 std::set<RenderNode*> mPrefetchedLayers; 268 269 // Stores the bounds of the main content. 270 Rect mContentDrawBounds; 271 272 // TODO: This is really a Task<void> but that doesn't really work 273 // when Future<> expects to be able to get/set a value 274 struct FuncTask : public Task<bool> { 275 std::function<void()> func; 276 }; 277 class FuncTaskProcessor; 278 279 std::vector< sp<FuncTask> > mFrameFences; 280 sp<TaskProcessor<bool> > mFrameWorkProcessor; 281 std::unique_ptr<IRenderPipeline> mRenderPipeline; 282}; 283 284} /* namespace renderthread */ 285} /* namespace uirenderer */ 286} /* namespace android */ 287