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 
34 #include <SkBitmap.h>
35 #include <SkRect.h>
36 #include <SkSize.h>
37 #include <cutils/compiler.h>
38 #include <utils/Functor.h>
39 
40 #include <functional>
41 #include <future>
42 #include <set>
43 #include <string>
44 #include <utility>
45 #include <vector>
46 
47 namespace android {
48 namespace uirenderer {
49 
50 class AnimationContext;
51 class DeferredLayerUpdater;
52 class ErrorHandler;
53 class Layer;
54 class Rect;
55 class RenderState;
56 
57 namespace renderthread {
58 
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(ANativeWindow* window, bool enableTimeout = true);
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 
addFrameMetricsObserver(FrameMetricsObserver * observer)155     void addFrameMetricsObserver(FrameMetricsObserver* observer) {
156         if (mFrameMetricsReporter.get() == nullptr) {
157             mFrameMetricsReporter.reset(new FrameMetricsReporter());
158         }
159 
160         mFrameMetricsReporter->addObserver(observer);
161     }
162 
removeFrameMetricsObserver(FrameMetricsObserver * observer)163     void removeFrameMetricsObserver(FrameMetricsObserver* observer) {
164         if (mFrameMetricsReporter.get() != nullptr) {
165             mFrameMetricsReporter->removeObserver(observer);
166             if (!mFrameMetricsReporter->hasObservers()) {
167                 mFrameMetricsReporter.reset(nullptr);
168             }
169         }
170     }
171 
172     // Used to queue up work that needs to be completed before this frame completes
173     ANDROID_API void enqueueFrameWork(std::function<void()>&& func);
174 
175     ANDROID_API int64_t getFrameNumber();
176 
177     void waitOnFences();
178 
getRenderPipeline()179     IRenderPipeline* getRenderPipeline() { return mRenderPipeline.get(); }
180 
addFrameCompleteListener(std::function<void (int64_t)> && func)181     void addFrameCompleteListener(std::function<void(int64_t)>&& func) {
182         mFrameCompleteCallbacks.push_back(std::move(func));
183     }
184 
setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)185     void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback) {
186         mRenderPipeline->setPictureCapturedCallback(callback);
187     }
188 
setForceDark(bool enable)189     void setForceDark(bool enable) { mUseForceDark = enable; }
190 
useForceDark()191     bool useForceDark() {
192         return mUseForceDark;
193     }
194 
195     // Must be called before setSurface
196     void setRenderAheadDepth(int renderAhead);
197 
198     SkISize getNextFrameSize() const;
199 
200 private:
201     CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
202                   IContextFactory* contextFactory, std::unique_ptr<IRenderPipeline> renderPipeline);
203 
204     friend class RegisterFrameCallbackTask;
205     // TODO: Replace with something better for layer & other GL object
206     // lifecycle tracking
207     friend class android::uirenderer::RenderState;
208 
209     void freePrefetchedLayers();
210 
211     bool isSwapChainStuffed();
212     bool surfaceRequiresRedraw();
213     void setPresentTime();
214 
215     SkRect computeDirtyRect(const Frame& frame, SkRect* dirty);
216 
217     // The same type as Frame.mWidth and Frame.mHeight
218     int32_t mLastFrameWidth = 0;
219     int32_t mLastFrameHeight = 0;
220 
221     RenderThread& mRenderThread;
222     std::unique_ptr<ReliableSurface> mNativeSurface;
223     // stopped indicates the CanvasContext will reject actual redraw operations,
224     // and defer repaint until it is un-stopped
225     bool mStopped = false;
226     // Incremented each time the CanvasContext is stopped. Used to ignore
227     // delayed messages that are triggered after stopping.
228     int mGenerationID;
229     // CanvasContext is dirty if it has received an update that it has not
230     // painted onto its surface.
231     bool mIsDirty = false;
232     SwapBehavior mSwapBehavior = SwapBehavior::kSwap_default;
233     bool mFixedRenderAhead = false;
234     uint32_t mRenderAheadDepth = 0;
235     uint32_t mRenderAheadCapacity = 0;
236     struct SwapHistory {
237         SkRect damage;
238         nsecs_t vsyncTime;
239         nsecs_t swapCompletedTime;
240         nsecs_t dequeueDuration;
241         nsecs_t queueDuration;
242     };
243 
244     // Need at least 4 because we do quad buffer. Add a 5th for good measure.
245     RingBuffer<SwapHistory, 5> mSwapHistory;
246     int64_t mFrameNumber = -1;
247     int64_t mDamageId = 0;
248 
249     // last vsync for a dropped frame due to stuffed queue
250     nsecs_t mLastDropVsync = 0;
251 
252     bool mOpaque;
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     RingBuffer<std::pair<FrameInfo*, int64_t>, 4> mLast4FrameInfos;
266     std::string mName;
267     JankTracker mJankTracker;
268     FrameInfoVisualizer mProfiler;
269     std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter;
270 
271     std::set<RenderNode*> mPrefetchedLayers;
272 
273     // Stores the bounds of the main content.
274     Rect mContentDrawBounds;
275 
276     std::vector<std::future<void>> mFrameFences;
277     std::unique_ptr<IRenderPipeline> mRenderPipeline;
278 
279     std::vector<std::function<void(int64_t)>> mFrameCompleteCallbacks;
280 };
281 
282 } /* namespace renderthread */
283 } /* namespace uirenderer */
284 } /* namespace android */
285