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 <SkRefCnt.h>
21 #include <android/hardware_buffer.h>
22 #include <android/native_window.h>
23 #include <android/surface_control.h>
24 #include <cutils/compiler.h>
25 #include <utils/Functor.h>
26 
27 #include "../FrameMetricsObserver.h"
28 #include "../IContextFactory.h"
29 #include "ColorMode.h"
30 #include "CopyRequest.h"
31 #include "DrawFrameTask.h"
32 #include "SwapBehavior.h"
33 #include "hwui/Bitmap.h"
34 
35 class SkBitmap;
36 class SkPicture;
37 class SkImage;
38 
39 namespace android {
40 class GraphicBuffer;
41 class Surface;
42 
43 namespace uirenderer {
44 
45 class DeferredLayerUpdater;
46 class RenderNode;
47 class Rect;
48 
49 namespace renderthread {
50 
51 class CanvasContext;
52 class RenderThread;
53 class RenderProxyBridge;
54 
55 namespace DumpFlags {
56 enum {
57     FrameStats = 1 << 0,
58     Reset = 1 << 1,
59     JankStats = 1 << 2,
60 };
61 }
62 
63 /*
64  * RenderProxy is strictly single threaded. All methods must be invoked on the owning
65  * thread. It is important to note that RenderProxy may be deleted while it has
66  * tasks post()'d as a result. Therefore any RenderTask that is post()'d must not
67  * reference RenderProxy or any of its fields. The exception here is that postAndWait()
68  * references RenderProxy fields. This is safe as RenderProxy cannot
69  * be deleted if it is blocked inside a call.
70  */
71 class RenderProxy {
72 public:
73     RenderProxy(bool opaque, RenderNode* rootNode, IContextFactory* contextFactory);
74     virtual ~RenderProxy();
75 
76     // Won't take effect until next EGLSurface creation
77     void setSwapBehavior(SwapBehavior swapBehavior);
78     bool loadSystemProperties();
79     void setName(const char* name);
80     void setHardwareBuffer(AHardwareBuffer* buffer);
81     void setSurface(ANativeWindow* window, bool enableTimeout = true);
82     void setSurfaceControl(ASurfaceControl* surfaceControl);
83     void allocateBuffers();
84     bool pause();
85     void setStopped(bool stopped);
86     void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
87     void setLightGeometry(const Vector3& lightCenter, float lightRadius);
88     void setHardwareBufferRenderParams(const HardwareBufferRenderParams& params);
89     void setOpaque(bool opaque);
90     float setColorMode(ColorMode mode);
91     void setRenderSdrHdrRatio(float ratio);
92     int64_t* frameInfo();
93     void forceDrawNextFrame();
94     int syncAndDrawFrame();
95     void destroy();
96 
97     static void destroyFunctor(int functor);
98 
99     DeferredLayerUpdater* createTextureLayer();
100     void buildLayer(RenderNode* node);
101     bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap);
102     void pushLayerUpdate(DeferredLayerUpdater* layer);
103     void cancelLayerUpdate(DeferredLayerUpdater* layer);
104     void detachSurfaceTexture(DeferredLayerUpdater* layer);
105 
106     void destroyHardwareResources();
107     static void trimMemory(int level);
108     static void trimCaches(int level);
109     static void purgeCaches();
110     static void overrideProperty(const char* name, const char* value);
111 
112     void fence();
113     static int maxTextureSize();
114     void stopDrawing();
115     void notifyFramePending();
116     void notifyCallbackPending();
117     void notifyExpensiveFrame();
118 
119     void dumpProfileInfo(int fd, int dumpFlags);
120     // Not exported, only used for testing
121     void resetProfileInfo();
122     uint32_t frameTimePercentile(int p);
123     static void dumpGraphicsMemory(int fd, bool includeProfileData = true,
124                                    bool resetProfile = false);
125     static void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
126 
127     static void rotateProcessStatsBuffer();
128     static void setProcessStatsBuffer(int fd);
129     int getRenderThreadTid();
130 
131     void addRenderNode(RenderNode* node, bool placeFront);
132     void removeRenderNode(RenderNode* node);
133     void drawRenderNode(RenderNode* node);
134     void setContentDrawBounds(int left, int top, int right, int bottom);
135     void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback);
136     void setASurfaceTransactionCallback(
137             const std::function<bool(int64_t, int64_t, int64_t)>& callback);
138     void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback);
139     void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback);
140     void setFrameCommitCallback(std::function<void(bool)>&& callback);
141     void setFrameCompleteCallback(std::function<void()>&& callback);
142 
143     void addFrameMetricsObserver(FrameMetricsObserver* observer);
144     void removeFrameMetricsObserver(FrameMetricsObserver* observer);
145     void setForceDark(bool enable);
146 
147     static void copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request);
148     static void prepareToDraw(Bitmap& bitmap);
149 
150     static int copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap);
151     static int copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap);
152 
153     static void disableVsync();
154 
155     static void preload();
156 
157     static void setRtAnimationsEnabled(bool enabled);
158 
159 private:
160     RenderThread& mRenderThread;
161     CanvasContext* mContext;
162 
163     DrawFrameTask mDrawFrameTask;
164 
165     void destroyContext();
166 
167     // Friend class to help with bridging
168     friend class RenderProxyBridge;
169 };
170 
171 } /* namespace renderthread */
172 } /* namespace uirenderer */
173 } /* namespace android */
174 #endif /* RENDERPROXY_H_ */
175