1 /*
2  * Copyright (C) 2013 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 #ifndef RENDERPROXY_H_
18 #define RENDERPROXY_H_
19 
20 #include <SkBitmap.h>
21 #include <cutils/compiler.h>
22 #include <gui/Surface.h>
23 #include <utils/Functor.h>
24 
25 #include "../FrameMetricsObserver.h"
26 #include "../IContextFactory.h"
27 #include "DrawFrameTask.h"
28 #include "SwapBehavior.h"
29 #include "hwui/Bitmap.h"
30 
31 namespace android {
32 class GraphicBuffer;
33 
34 namespace uirenderer {
35 
36 class DeferredLayerUpdater;
37 class RenderNode;
38 class Rect;
39 
40 namespace renderthread {
41 
42 class CanvasContext;
43 class RenderThread;
44 class RenderProxyBridge;
45 
46 namespace DumpFlags {
47 enum {
48     FrameStats = 1 << 0,
49     Reset = 1 << 1,
50     JankStats = 1 << 2,
51 };
52 }
53 
54 /*
55  * RenderProxy is strictly single threaded. All methods must be invoked on the owning
56  * thread. It is important to note that RenderProxy may be deleted while it has
57  * tasks post()'d as a result. Therefore any RenderTask that is post()'d must not
58  * reference RenderProxy or any of its fields. The exception here is that postAndWait()
59  * references RenderProxy fields. This is safe as RenderProxy cannot
60  * be deleted if it is blocked inside a call.
61  */
62 class ANDROID_API RenderProxy {
63 public:
64     ANDROID_API RenderProxy(bool opaque, RenderNode* rootNode, IContextFactory* contextFactory);
65     ANDROID_API virtual ~RenderProxy();
66 
67     // Won't take effect until next EGLSurface creation
68     ANDROID_API void setSwapBehavior(SwapBehavior swapBehavior);
69     ANDROID_API bool loadSystemProperties();
70     ANDROID_API void setName(const char* name);
71 
72     ANDROID_API void setSurface(const sp<Surface>& surface, bool enableTimeout = true);
73     ANDROID_API void allocateBuffers();
74     ANDROID_API bool pause();
75     ANDROID_API void setStopped(bool stopped);
76     ANDROID_API void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
77     ANDROID_API void setLightGeometry(const Vector3& lightCenter, float lightRadius);
78     ANDROID_API void setOpaque(bool opaque);
79     ANDROID_API void setWideGamut(bool wideGamut);
80     ANDROID_API int64_t* frameInfo();
81     ANDROID_API int syncAndDrawFrame();
82     ANDROID_API void destroy();
83 
84     ANDROID_API static void invokeFunctor(Functor* functor, bool waitForCompletion);
85     static void destroyFunctor(int functor);
86 
87     ANDROID_API DeferredLayerUpdater* createTextureLayer();
88     ANDROID_API void buildLayer(RenderNode* node);
89     ANDROID_API bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap);
90     ANDROID_API void pushLayerUpdate(DeferredLayerUpdater* layer);
91     ANDROID_API void cancelLayerUpdate(DeferredLayerUpdater* layer);
92     ANDROID_API void detachSurfaceTexture(DeferredLayerUpdater* layer);
93 
94     ANDROID_API void destroyHardwareResources();
95     ANDROID_API static void trimMemory(int level);
96     ANDROID_API static void overrideProperty(const char* name, const char* value);
97 
98     ANDROID_API void fence();
99     ANDROID_API static int maxTextureSize();
100     ANDROID_API void stopDrawing();
101     ANDROID_API void notifyFramePending();
102 
103     ANDROID_API void dumpProfileInfo(int fd, int dumpFlags);
104     // Not exported, only used for testing
105     void resetProfileInfo();
106     uint32_t frameTimePercentile(int p);
107     ANDROID_API static void dumpGraphicsMemory(int fd);
108 
109     ANDROID_API static void rotateProcessStatsBuffer();
110     ANDROID_API static void setProcessStatsBuffer(int fd);
111     ANDROID_API int getRenderThreadTid();
112 
113     ANDROID_API void addRenderNode(RenderNode* node, bool placeFront);
114     ANDROID_API void removeRenderNode(RenderNode* node);
115     ANDROID_API void drawRenderNode(RenderNode* node);
116     ANDROID_API void setContentDrawBounds(int left, int top, int right, int bottom);
117     ANDROID_API void setPictureCapturedCallback(
118             const std::function<void(sk_sp<SkPicture>&&)>& callback);
119     ANDROID_API void setFrameCallback(std::function<void(int64_t)>&& callback);
120     ANDROID_API void setFrameCompleteCallback(std::function<void(int64_t)>&& callback);
121 
122     ANDROID_API void addFrameMetricsObserver(FrameMetricsObserver* observer);
123     ANDROID_API void removeFrameMetricsObserver(FrameMetricsObserver* observer);
124     ANDROID_API void setForceDark(bool enable);
125 
126     /**
127      * Sets a render-ahead depth on the backing renderer. This will increase latency by
128      * <swapInterval> * renderAhead and increase memory usage by (3 + renderAhead) * <resolution>.
129      * In return the renderer will be less susceptible to jitter, resulting in a smoother animation.
130      *
131      * Not recommended to use in response to anything touch driven, but for canned animations
132      * where latency is not a concern careful use may be beneficial.
133      *
134      * Note that when increasing this there will be a frame gap of N frames where N is
135      * renderAhead - <current renderAhead>. When decreasing this if there are any pending
136      * frames they will retain their prior renderAhead value, so it will take a few frames
137      * for the decrease to flush through.
138      *
139      * @param renderAhead How far to render ahead, must be in the range [0..2]
140      */
141     ANDROID_API void setRenderAheadDepth(int renderAhead);
142 
143     ANDROID_API static int copySurfaceInto(sp<Surface>& surface, int left, int top, int right,
144                                            int bottom, SkBitmap* bitmap);
145     ANDROID_API static void prepareToDraw(Bitmap& bitmap);
146 
147     static int copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap);
148 
149     ANDROID_API static void disableVsync();
150 
151     ANDROID_API static void preload();
152 
153     static void repackVectorDrawableAtlas();
154 
155     static void releaseVDAtlasEntries();
156 
157 private:
158     RenderThread& mRenderThread;
159     CanvasContext* mContext;
160 
161     DrawFrameTask mDrawFrameTask;
162 
163     void destroyContext();
164 
165     // Friend class to help with bridging
166     friend class RenderProxyBridge;
167 };
168 
169 } /* namespace renderthread */
170 } /* namespace uirenderer */
171 } /* namespace android */
172 #endif /* RENDERPROXY_H_ */
173