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