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