• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "DamageAccumulator.h"
20 #include "FrameInfo.h"
21 #include "FrameInfoVisualizer.h"
22 #include "FrameMetricsReporter.h"
23 #include "IContextFactory.h"
24 #include "IRenderPipeline.h"
25 #include "LayerUpdateQueue.h"
26 #include "Lighting.h"
27 #include "ReliableSurface.h"
28 #include "RenderNode.h"
29 #include "renderthread/RenderTask.h"
30 #include "renderthread/RenderThread.h"
31 
32 #include <EGL/egl.h>
33 #include <SkBitmap.h>
34 #include <SkRect.h>
35 #include <SkSize.h>
36 #include <cutils/compiler.h>
37 #include <gui/Surface.h>
38 #include <utils/Functor.h>
39 
40 #include <functional>
41 #include <future>
42 #include <set>
43 #include <string>
44 #include <vector>
45 
46 namespace android {
47 namespace uirenderer {
48 
49 class AnimationContext;
50 class DeferredLayerUpdater;
51 class ErrorHandler;
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, RenderNode* rootRenderNode,
67                                  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,ErrorHandler * errorHandler)77     bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator,
78                              ErrorHandler* errorHandler) {
79         return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator, errorHandler);
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      */
pinImages(std::vector<SkImage * > & mutableImages)90     bool pinImages(std::vector<SkImage*>& mutableImages) {
91         return mRenderPipeline->pinImages(mutableImages);
92     }
pinImages(LsaVector<sk_sp<Bitmap>> & images)93     bool pinImages(LsaVector<sk_sp<Bitmap>>& images) { return mRenderPipeline->pinImages(images); }
94 
95     /**
96      * Unpin any image that had be previously pinned to the GPU cache
97      */
unpinImages()98     void unpinImages() { mRenderPipeline->unpinImages(); }
99 
100     static void invokeFunctor(const RenderThread& thread, Functor* functor);
101 
102     static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap);
103 
104     /*
105      * If Properties::isSkiaEnabled() is true then this will return the Skia
106      * grContext associated with the current RenderPipeline.
107      */
getGrContext()108     GrContext* getGrContext() const { return mRenderThread.getGrContext(); }
109 
110     // Won't take effect until next EGLSurface creation
111     void setSwapBehavior(SwapBehavior swapBehavior);
112 
113     void setSurface(sp<Surface>&& surface);
114     bool pauseSurface();
115     void setStopped(bool stopped);
hasSurface()116     bool hasSurface() const { return mNativeSurface.get(); }
117     void allocateBuffers();
118 
119     void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
120     void setLightGeometry(const Vector3& lightCenter, float lightRadius);
121     void setOpaque(bool opaque);
122     void setWideGamut(bool wideGamut);
123     bool makeCurrent();
124     void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued, RenderNode* target);
125     void draw();
126     void destroy();
127 
128     // IFrameCallback, Choreographer-driven frame callback entry point
129     virtual void doFrame() override;
130     void prepareAndDraw(RenderNode* node);
131 
132     void buildLayer(RenderNode* node);
133     void markLayerInUse(RenderNode* node);
134 
135     void destroyHardwareResources();
136     static void trimMemory(RenderThread& thread, int level);
137 
138     DeferredLayerUpdater* createTextureLayer();
139 
140     void stopDrawing();
141     void notifyFramePending();
142 
profiler()143     FrameInfoVisualizer& profiler() { return mProfiler; }
144 
145     void dumpFrames(int fd);
146     void resetFrameStats();
147 
148     void setName(const std::string&& name);
149 
150     void addRenderNode(RenderNode* node, bool placeFront);
151     void removeRenderNode(RenderNode* node);
152 
setContentDrawBounds(const Rect & bounds)153     void setContentDrawBounds(const Rect& bounds) { mContentDrawBounds = bounds; }
154 
getRenderState()155     RenderState& getRenderState() { return mRenderThread.renderState(); }
156 
addFrameMetricsObserver(FrameMetricsObserver * observer)157     void addFrameMetricsObserver(FrameMetricsObserver* observer) {
158         if (mFrameMetricsReporter.get() == nullptr) {
159             mFrameMetricsReporter.reset(new FrameMetricsReporter());
160         }
161 
162         mFrameMetricsReporter->addObserver(observer);
163     }
164 
removeFrameMetricsObserver(FrameMetricsObserver * observer)165     void removeFrameMetricsObserver(FrameMetricsObserver* observer) {
166         if (mFrameMetricsReporter.get() != nullptr) {
167             mFrameMetricsReporter->removeObserver(observer);
168             if (!mFrameMetricsReporter->hasObservers()) {
169                 mFrameMetricsReporter.reset(nullptr);
170             }
171         }
172     }
173 
174     // Used to queue up work that needs to be completed before this frame completes
175     ANDROID_API void enqueueFrameWork(std::function<void()>&& func);
176 
177     ANDROID_API int64_t getFrameNumber();
178 
179     void waitOnFences();
180 
getRenderPipeline()181     IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); }
182 
addFrameCompleteListener(std::function<void (int64_t)> && func)183     void addFrameCompleteListener(std::function<void(int64_t)>&& func) {
184         mFrameCompleteCallbacks.push_back(std::move(func));
185     }
186 
setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)187     void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) {
188         mRenderPipeline->setPictureCapturedCallback(callback);
189     }
190 
setForceDark(bool enable)191     void setForceDark(bool enable) { mUseForceDark = enable; }
192 
useForceDark()193     bool useForceDark() {
194         return mUseForceDark;
195     }
196 
197     // Must be called before setSurface
198     void setRenderAheadDepth(int renderAhead);
199 
200     SkISize getNextFrameSize() const;
201 
202 private:
203     CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
204                   IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline);
205 
206     friend class RegisterFrameCallbackTask;
207     // TODO: Replace with something better for layer & other GL object
208     // lifecycle tracking
209     friend class android::uirenderer::RenderState;
210 
211     void freePrefetchedLayers();
212 
213     bool isSwapChainStuffed();
214     bool surfaceRequiresRedraw();
215     void setPresentTime();
216 
217     SkRect computeDirtyRect(const Frame& frame, SkRect* dirty);
218 
219     EGLint mLastFrameWidth = 0;
220     EGLint mLastFrameHeight = 0;
221 
222     RenderThread& mRenderThread;
223     sp<ReliableSurface> mNativeSurface;
224     // stopped indicates the CanvasContext will reject actual redraw operations,
225     // and defer repaint until it is un-stopped
226     bool mStopped = false;
227     // Incremented each time the CanvasContext is stopped. Used to ignore
228     // delayed messages that are triggered after stopping.
229     int mGenerationID;
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     bool mFixedRenderAhead = false;
235     uint32_t mRenderAheadDepth = 0;
236     uint32_t mRenderAheadCapacity = 0;
237     struct SwapHistory {
238         SkRect damage;
239         nsecs_t vsyncTime;
240         nsecs_t swapCompletedTime;
241         nsecs_t dequeueDuration;
242         nsecs_t queueDuration;
243     };
244 
245     RingBuffer<SwapHistory, 3> mSwapHistory;
246     int64_t mFrameNumber = -1;
247 
248     // last vsync for a dropped frame due to stuffed queue
249     nsecs_t mLastDropVsync = 0;
250 
251     bool mOpaque;
252     bool mWideColorGamut = false;
253     bool mUseForceDark = false;
254     LightInfo mLightInfo;
255     LightGeometry mLightGeometry = {{0, 0, 0}, 0};
256 
257     bool mHaveNewSurface = false;
258     DamageAccumulator mDamageAccumulator;
259     LayerUpdateQueue mLayerUpdateQueue;
260     std::unique_ptr<AnimationContext> mAnimationContext;
261 
262     std::vector<sp<RenderNode>> mRenderNodes;
263 
264     FrameInfo* mCurrentFrameInfo = nullptr;
265     std::string mName;
266     JankTracker mJankTracker;
267     FrameInfoVisualizer mProfiler;
268     std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter;
269 
270     std::set<RenderNode*> mPrefetchedLayers;
271 
272     // Stores the bounds of the main content.
273     Rect mContentDrawBounds;
274 
275     std::vector<std::future<void>> mFrameFences;
276     std::unique_ptr<IRenderPipeline> mRenderPipeline;
277 
278     std::vector<std::function<void(int64_t)>> mFrameCompleteCallbacks;
279 };
280 
281 } /* namespace renderthread */
282 } /* namespace uirenderer */
283 } /* namespace android */
284