• 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 "JankTracker.h"
26 #include "LayerUpdateQueue.h"
27 #include "Lighting.h"
28 #include "ReliableSurface.h"
29 #include "RenderNode.h"
30 #include "renderthread/RenderTask.h"
31 #include "renderthread/RenderThread.h"
32 #include "utils/RingBuffer.h"
33 #include "ColorMode.h"
34 
35 #include <SkBitmap.h>
36 #include <SkRect.h>
37 #include <SkSize.h>
38 #include <cutils/compiler.h>
39 #include <utils/Functor.h>
40 #include <utils/Mutex.h>
41 
42 #include <functional>
43 #include <future>
44 #include <set>
45 #include <string>
46 #include <utility>
47 #include <vector>
48 
49 namespace android {
50 namespace uirenderer {
51 
52 class AnimationContext;
53 class DeferredLayerUpdater;
54 class ErrorHandler;
55 class Layer;
56 class Rect;
57 class RenderState;
58 
59 namespace renderthread {
60 
61 class Frame;
62 
63 // This per-renderer class manages the bridge between the global EGL context
64 // and the render surface.
65 // TODO: Rename to Renderer or some other per-window, top-level manager
66 class CanvasContext : public IFrameCallback {
67 public:
68     static CanvasContext* create(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
69                                  IContextFactory* contextFactory);
70     virtual ~CanvasContext();
71 
72     /**
73      * Update or create a layer specific for the provided RenderNode. The layer
74      * attached to the node will be specific to the RenderPipeline used by this
75      * context
76      *
77      *  @return true if the layer has been created or updated
78      */
createOrUpdateLayer(RenderNode * node,const DamageAccumulator & dmgAccumulator,ErrorHandler * errorHandler)79     bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& dmgAccumulator,
80                              ErrorHandler* errorHandler) {
81         return mRenderPipeline->createOrUpdateLayer(node, dmgAccumulator, errorHandler);
82     }
83 
84     /**
85      * Pin any mutable images to the GPU cache. A pinned images is guaranteed to
86      * remain in the cache until it has been unpinned. We leverage this feature
87      * to avoid making a CPU copy of the pixels.
88      *
89      * @return true if all images have been successfully pinned to the GPU cache
90      *         and false otherwise (e.g. cache limits have been exceeded).
91      */
pinImages(std::vector<SkImage * > & mutableImages)92     bool pinImages(std::vector<SkImage*>& mutableImages) {
93         if (!Properties::isDrawingEnabled()) {
94             return true;
95         }
96         return mRenderPipeline->pinImages(mutableImages);
97     }
pinImages(LsaVector<sk_sp<Bitmap>> & images)98     bool pinImages(LsaVector<sk_sp<Bitmap>>& images) {
99         if (!Properties::isDrawingEnabled()) {
100             return true;
101         }
102         return mRenderPipeline->pinImages(images);
103     }
104 
105     /**
106      * Unpin any image that had be previously pinned to the GPU cache
107      */
unpinImages()108     void unpinImages() { mRenderPipeline->unpinImages(); }
109 
110     static void invokeFunctor(const RenderThread& thread, Functor* functor);
111 
112     static void prepareToDraw(const RenderThread& thread, Bitmap* bitmap);
113 
114     /*
115      * If Properties::isSkiaEnabled() is true then this will return the Skia
116      * grContext associated with the current RenderPipeline.
117      */
getGrContext()118     GrDirectContext* getGrContext() const { return mRenderThread.getGrContext(); }
119 
getSurfaceControl()120     ASurfaceControl* getSurfaceControl() const { return mSurfaceControl; }
getSurfaceControlGenerationId()121     int32_t getSurfaceControlGenerationId() const { return mSurfaceControlGenerationId; }
122 
123     // Won't take effect until next EGLSurface creation
124     void setSwapBehavior(SwapBehavior swapBehavior);
125 
126     void setSurface(ANativeWindow* window, bool enableTimeout = true);
127     void setSurfaceControl(ASurfaceControl* surfaceControl);
128     bool pauseSurface();
129     void setStopped(bool stopped);
hasSurface()130     bool hasSurface() const { return mNativeSurface.get(); }
131     void allocateBuffers();
132 
133     void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
134     void setLightGeometry(const Vector3& lightCenter, float lightRadius);
135     void setOpaque(bool opaque);
136     void setColorMode(ColorMode mode);
137     bool makeCurrent();
138     void prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued, RenderNode* target);
139     // Returns the DequeueBufferDuration.
140     nsecs_t draw();
141     void destroy();
142 
143     // IFrameCallback, Choreographer-driven frame callback entry point
144     virtual void doFrame() override;
145     void prepareAndDraw(RenderNode* node);
146 
147     void buildLayer(RenderNode* node);
148     void markLayerInUse(RenderNode* node);
149 
150     void destroyHardwareResources();
151     static void trimMemory(RenderThread& thread, int level);
152 
153     DeferredLayerUpdater* createTextureLayer();
154 
155     void stopDrawing();
156     void notifyFramePending();
157 
profiler()158     FrameInfoVisualizer& profiler() { return mProfiler; }
159 
160     void dumpFrames(int fd);
161     void resetFrameStats();
162 
163     void setName(const std::string&& name);
164 
165     void addRenderNode(RenderNode* node, bool placeFront);
166     void removeRenderNode(RenderNode* node);
167 
setContentDrawBounds(const Rect & bounds)168     void setContentDrawBounds(const Rect& bounds) { mContentDrawBounds = bounds; }
169 
170     void addFrameMetricsObserver(FrameMetricsObserver* observer);
171     void removeFrameMetricsObserver(FrameMetricsObserver* observer);
172 
173     // Used to queue up work that needs to be completed before this frame completes
174     void enqueueFrameWork(std::function<void()>&& func);
175 
176     uint64_t getFrameNumber();
177 
178     void waitOnFences();
179 
getRenderPipeline()180     IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); }
181 
addFrameCommitListener(std::function<void (bool)> && func)182     void addFrameCommitListener(std::function<void(bool)>&& func) {
183         mFrameCommitCallbacks.push_back(std::move(func));
184     }
185 
setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)186     void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) {
187         mRenderPipeline->setPictureCapturedCallback(callback);
188     }
189 
setForceDark(bool enable)190     void setForceDark(bool enable) { mUseForceDark = enable; }
191 
useForceDark()192     bool useForceDark() {
193         return mUseForceDark;
194     }
195 
196     SkISize getNextFrameSize() const;
197 
198     // Returns the matrix to use to nudge non-AA'd points/lines towards the fragment center
199     const SkM44& getPixelSnapMatrix() const;
200 
201     // Called when SurfaceStats are available.
202     static void onSurfaceStatsAvailable(void* context, int32_t surfaceControlId,
203                                         ASurfaceControlStats* stats);
204 
setASurfaceTransactionCallback(const std::function<bool (int64_t,int64_t,int64_t)> & callback)205     void setASurfaceTransactionCallback(
206             const std::function<bool(int64_t, int64_t, int64_t)>& callback) {
207         mASurfaceTransactionCallback = callback;
208     }
209 
210     bool mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control);
211 
setPrepareSurfaceControlForWebviewCallback(const std::function<void ()> & callback)212     void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback) {
213         mPrepareSurfaceControlForWebviewCallback = callback;
214     }
215 
216     void prepareSurfaceControlForWebview();
217 
218     static CanvasContext* getActiveContext();
219 
220 private:
221     CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
222                   IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline);
223 
224     friend class RegisterFrameCallbackTask;
225     // TODO: Replace with something better for layer & other GL object
226     // lifecycle tracking
227     friend class android::uirenderer::RenderState;
228 
229     void freePrefetchedLayers();
230 
231     bool isSwapChainStuffed();
232     bool surfaceRequiresRedraw();
233     void setupPipelineSurface();
234 
235     SkRect computeDirtyRect(const Frame& frame, SkRect* dirty);
236     void finishFrame(FrameInfo* frameInfo);
237 
238     /**
239      * Invoke 'reportFrameMetrics' on the last frame stored in 'mLast4FrameInfos'.
240      * Populate the 'presentTime' field before calling.
241      */
242     void reportMetricsWithPresentTime();
243 
244     struct FrameMetricsInfo {
245         FrameInfo* frameInfo;
246         int64_t frameNumber;
247         int32_t surfaceId;
248     };
249 
250     FrameInfo* getFrameInfoFromLast4(uint64_t frameNumber, uint32_t surfaceControlId);
251 
252     // The same type as Frame.mWidth and Frame.mHeight
253     int32_t mLastFrameWidth = 0;
254     int32_t mLastFrameHeight = 0;
255 
256     RenderThread& mRenderThread;
257     std::unique_ptr<ReliableSurface> mNativeSurface;
258     // The SurfaceControl reference is passed from ViewRootImpl, can be set to
259     // NULL to remove the reference
260     ASurfaceControl* mSurfaceControl = nullptr;
261     // id to track surface control changes and WebViewFunctor uses it to determine
262     // whether reparenting is needed also used by FrameMetricsReporter to determine
263     // if a frame is from an "old" surface (i.e. one that existed before the
264     // observer was attched) and therefore shouldn't be reported.
265     // NOTE: It is important that this is an increasing counter.
266     int32_t mSurfaceControlGenerationId = 0;
267     // stopped indicates the CanvasContext will reject actual redraw operations,
268     // and defer repaint until it is un-stopped
269     bool mStopped = false;
270     // Incremented each time the CanvasContext is stopped. Used to ignore
271     // delayed messages that are triggered after stopping.
272     int mGenerationID;
273     // CanvasContext is dirty if it has received an update that it has not
274     // painted onto its surface.
275     bool mIsDirty = false;
276     SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default;
277     struct SwapHistory {
278         SkRect damage;
279         nsecs_t vsyncTime;
280         nsecs_t swapCompletedTime;
281         nsecs_t dequeueDuration;
282         nsecs_t queueDuration;
283     };
284 
285     // Need at least 4 because we do quad buffer. Add a few more for good measure.
286     RingBuffer<SwapHistory, 7> mSwapHistory;
287     // Frame numbers start at 1, 0 means uninitialized
288     uint64_t mFrameNumber = 0;
289     int64_t mDamageId = 0;
290 
291     // last vsync for a dropped frame due to stuffed queue
292     nsecs_t mLastDropVsync = 0;
293 
294     bool mOpaque;
295     bool mUseForceDark = false;
296     LightInfo mLightInfo;
297     LightGeometry mLightGeometry = {{0, 0, 0}, 0};
298 
299     bool mHaveNewSurface = false;
300     DamageAccumulator mDamageAccumulator;
301     LayerUpdateQueue mLayerUpdateQueue;
302     std::unique_ptr<AnimationContext> mAnimationContext;
303 
304     std::vector<sp<RenderNode>> mRenderNodes;
305 
306     FrameInfo* mCurrentFrameInfo = nullptr;
307 
308     // List of data of frames that are awaiting GPU completion reporting. Used to compute frame
309     // metrics and determine whether or not to report the metrics.
310     RingBuffer<FrameMetricsInfo, 4> mLast4FrameMetricsInfos
311             GUARDED_BY(mLast4FrameMetricsInfosMutex);
312     std::mutex mLast4FrameMetricsInfosMutex;
313 
314     std::string mName;
315     JankTracker mJankTracker;
316     FrameInfoVisualizer mProfiler;
317     std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter
318             GUARDED_BY(mFrameMetricsReporterMutex);
319     std::mutex mFrameMetricsReporterMutex;
320 
321     std::set<RenderNode*> mPrefetchedLayers;
322 
323     // Stores the bounds of the main content.
324     Rect mContentDrawBounds;
325 
326     std::vector<std::future<void>> mFrameFences;
327     std::unique_ptr<IRenderPipeline> mRenderPipeline;
328 
329     std::vector<std::function<void(bool)>> mFrameCommitCallbacks;
330 
331     // If set to true, we expect that callbacks into onSurfaceStatsAvailable
332     bool mExpectSurfaceStats = false;
333 
334     std::function<bool(int64_t, int64_t, int64_t)> mASurfaceTransactionCallback;
335     std::function<void()> mPrepareSurfaceControlForWebviewCallback;
336 
337     void cleanupResources();
338 };
339 
340 } /* namespace renderthread */
341 } /* namespace uirenderer */
342 } /* namespace android */
343