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
31namespace android {
32class GraphicBuffer;
33
34namespace uirenderer {
35
36class DeferredLayerUpdater;
37class RenderNode;
38class Rect;
39
40namespace renderthread {
41
42class CanvasContext;
43class RenderThread;
44class RenderProxyBridge;
45
46namespace DumpFlags {
47enum {
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 */
62class ANDROID_API RenderProxy {
63public:
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 initialize(const sp<Surface>& surface);
73    ANDROID_API void updateSurface(const sp<Surface>& surface);
74    ANDROID_API bool pauseSurface(const sp<Surface>& surface);
75    ANDROID_API void setStopped(bool stopped);
76    ANDROID_API void setup(float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
77    ANDROID_API void setLightCenter(const Vector3& lightCenter);
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
86    ANDROID_API DeferredLayerUpdater* createTextureLayer();
87    ANDROID_API void buildLayer(RenderNode* node);
88    ANDROID_API bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap);
89    ANDROID_API void pushLayerUpdate(DeferredLayerUpdater* layer);
90    ANDROID_API void cancelLayerUpdate(DeferredLayerUpdater* layer);
91    ANDROID_API void detachSurfaceTexture(DeferredLayerUpdater* layer);
92
93    ANDROID_API void destroyHardwareResources();
94    ANDROID_API static void trimMemory(int level);
95    ANDROID_API static void overrideProperty(const char* name, const char* value);
96
97    ANDROID_API void fence();
98    ANDROID_API static void staticFence();
99    ANDROID_API void stopDrawing();
100    ANDROID_API void notifyFramePending();
101
102    ANDROID_API void dumpProfileInfo(int fd, int dumpFlags);
103    // Not exported, only used for testing
104    void resetProfileInfo();
105    uint32_t frameTimePercentile(int p);
106    ANDROID_API static void dumpGraphicsMemory(int fd);
107
108    ANDROID_API static void rotateProcessStatsBuffer();
109    ANDROID_API static void setProcessStatsBuffer(int fd);
110    ANDROID_API int getRenderThreadTid();
111
112    ANDROID_API void serializeDisplayListTree();
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 setFrameCallback(std::function<void(int64_t)>&& callback);
119    ANDROID_API void setFrameCompleteCallback(std::function<void(int64_t)>&& callback);
120
121    ANDROID_API void addFrameMetricsObserver(FrameMetricsObserver* observer);
122    ANDROID_API void removeFrameMetricsObserver(FrameMetricsObserver* observer);
123    ANDROID_API long getDroppedFrameReportCount();
124
125    ANDROID_API static int copySurfaceInto(sp<Surface>& surface, int left, int top, int right,
126                                           int bottom, SkBitmap* bitmap);
127    ANDROID_API static void prepareToDraw(Bitmap& bitmap);
128
129    static sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& bitmap);
130
131    static int copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap);
132
133    static void onBitmapDestroyed(uint32_t pixelRefId);
134
135    ANDROID_API static void disableVsync();
136
137    static void repackVectorDrawableAtlas();
138
139    static void releaseVDAtlasEntries();
140
141private:
142    RenderThread& mRenderThread;
143    CanvasContext* mContext;
144
145    DrawFrameTask mDrawFrameTask;
146
147    void destroyContext();
148
149    // Friend class to help with bridging
150    friend class RenderProxyBridge;
151};
152
153} /* namespace renderthread */
154} /* namespace uirenderer */
155} /* namespace android */
156#endif /* RENDERPROXY_H_ */
157