1/*
2 * Copyright (C) 2007 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 ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
18#define ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <binder/IBinder.h>
24
25#include <utils/RefBase.h>
26#include <utils/Singleton.h>
27#include <utils/SortedVector.h>
28#include <utils/threads.h>
29
30#include <ui/FrameStats.h>
31#include <ui/PixelFormat.h>
32
33#include <gui/CpuConsumer.h>
34#include <gui/SurfaceControl.h>
35
36namespace android {
37
38// ---------------------------------------------------------------------------
39
40struct DisplayInfo;
41class Composer;
42class HdrCapabilities;
43class ISurfaceComposerClient;
44class IGraphicBufferProducer;
45class Region;
46
47// ---------------------------------------------------------------------------
48
49class SurfaceComposerClient : public RefBase
50{
51    friend class Composer;
52public:
53                SurfaceComposerClient();
54                SurfaceComposerClient(const sp<IGraphicBufferProducer>& parent);
55    virtual     ~SurfaceComposerClient();
56
57    // Always make sure we could initialize
58    status_t    initCheck() const;
59
60    // Return the connection of this client
61    sp<IBinder> connection() const;
62
63    // Forcibly remove connection before all references have gone away.
64    void        dispose();
65
66    // callback when the composer is dies
67    status_t linkToComposerDeath(const sp<IBinder::DeathRecipient>& recipient,
68            void* cookie = NULL, uint32_t flags = 0);
69
70    // Get a list of supported configurations for a given display
71    static status_t getDisplayConfigs(const sp<IBinder>& display,
72            Vector<DisplayInfo>* configs);
73
74    // Get the DisplayInfo for the currently-active configuration
75    static status_t getDisplayInfo(const sp<IBinder>& display,
76            DisplayInfo* info);
77
78    // Get the index of the current active configuration (relative to the list
79    // returned by getDisplayInfo)
80    static int getActiveConfig(const sp<IBinder>& display);
81
82    // Set a new active configuration using an index relative to the list
83    // returned by getDisplayInfo
84    static status_t setActiveConfig(const sp<IBinder>& display, int id);
85
86    // Gets the list of supported color modes for the given display
87    static status_t getDisplayColorModes(const sp<IBinder>& display,
88            Vector<android_color_mode_t>* outColorModes);
89
90    // Gets the active color mode for the given display
91    static android_color_mode_t getActiveColorMode(const sp<IBinder>& display);
92
93    // Sets the active color mode for the given display
94    static status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode);
95
96    /* Triggers screen on/off or low power mode and waits for it to complete */
97    static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
98
99    // ------------------------------------------------------------------------
100    // surface creation / destruction
101
102    //! Create a surface
103    sp<SurfaceControl> createSurface(
104            const String8& name,// name of the surface
105            uint32_t w,         // width in pixel
106            uint32_t h,         // height in pixel
107            PixelFormat format, // pixel-format desired
108            uint32_t flags = 0, // usage flags
109            SurfaceControl* parent = nullptr, // parent
110            uint32_t windowType = 0, // from WindowManager.java (STATUS_BAR, INPUT_METHOD, etc.)
111            uint32_t ownerUid = 0 // UID of the task
112    );
113
114    //! Create a virtual display
115    static sp<IBinder> createDisplay(const String8& displayName, bool secure);
116
117    //! Destroy a virtual display
118    static void destroyDisplay(const sp<IBinder>& display);
119
120    //! Get the token for the existing default displays.
121    //! Possible values for id are eDisplayIdMain and eDisplayIdHdmi.
122    static sp<IBinder> getBuiltInDisplay(int32_t id);
123
124    // ------------------------------------------------------------------------
125    // Composer parameters
126    // All composer parameters must be changed within a transaction
127    // several surfaces can be updated in one transaction, all changes are
128    // committed at once when the transaction is closed.
129    // closeGlobalTransaction() requires an IPC with the server.
130
131    //! Open a composer transaction on all active SurfaceComposerClients.
132    static void openGlobalTransaction();
133
134    //! Close a composer transaction on all active SurfaceComposerClients.
135    static void closeGlobalTransaction(bool synchronous = false);
136
137    static status_t enableVSyncInjections(bool enable);
138
139    static status_t injectVSync(nsecs_t when);
140
141    //! Flag the currently open transaction as an animation transaction.
142    static void setAnimationTransaction();
143
144    status_t    hide(const sp<IBinder>& id);
145    status_t    show(const sp<IBinder>& id);
146    status_t    setFlags(const sp<IBinder>& id, uint32_t flags, uint32_t mask);
147    status_t    setTransparentRegionHint(const sp<IBinder>& id, const Region& transparent);
148    status_t    setLayer(const sp<IBinder>& id, int32_t layer);
149    status_t    setRelativeLayer(const sp<IBinder>& id,
150            const sp<IBinder>& relativeTo, int32_t layer);
151    status_t    setAlpha(const sp<IBinder>& id, float alpha=1.0f);
152    status_t    setMatrix(const sp<IBinder>& id, float dsdx, float dtdx, float dtdy, float dsdy);
153    status_t    setPosition(const sp<IBinder>& id, float x, float y);
154    status_t    setSize(const sp<IBinder>& id, uint32_t w, uint32_t h);
155    status_t    setCrop(const sp<IBinder>& id, const Rect& crop);
156    status_t    setFinalCrop(const sp<IBinder>& id, const Rect& crop);
157    status_t    setLayerStack(const sp<IBinder>& id, uint32_t layerStack);
158    status_t    deferTransactionUntil(const sp<IBinder>& id,
159            const sp<IBinder>& handle, uint64_t frameNumber);
160    status_t    deferTransactionUntil(const sp<IBinder>& id,
161            const sp<Surface>& handle, uint64_t frameNumber);
162    status_t    reparentChildren(const sp<IBinder>& id,
163            const sp<IBinder>& newParentHandle);
164    status_t    detachChildren(const sp<IBinder>& id);
165    status_t    setOverrideScalingMode(const sp<IBinder>& id,
166            int32_t overrideScalingMode);
167    status_t    setGeometryAppliesWithResize(const sp<IBinder>& id);
168
169    status_t    destroySurface(const sp<IBinder>& id);
170
171    status_t clearLayerFrameStats(const sp<IBinder>& token) const;
172    status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
173
174    static status_t clearAnimationFrameStats();
175    static status_t getAnimationFrameStats(FrameStats* outStats);
176
177    static status_t getHdrCapabilities(const sp<IBinder>& display,
178            HdrCapabilities* outCapabilities);
179
180    static status_t setDisplaySurface(const sp<IBinder>& token,
181            sp<IGraphicBufferProducer> bufferProducer);
182    static void setDisplayLayerStack(const sp<IBinder>& token,
183            uint32_t layerStack);
184    static void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
185
186    /* setDisplayProjection() defines the projection of layer stacks
187     * to a given display.
188     *
189     * - orientation defines the display's orientation.
190     * - layerStackRect defines which area of the window manager coordinate
191     * space will be used.
192     * - displayRect defines where on the display will layerStackRect be
193     * mapped to. displayRect is specified post-orientation, that is
194     * it uses the orientation seen by the end-user.
195     */
196    static void setDisplayProjection(const sp<IBinder>& token,
197            uint32_t orientation,
198            const Rect& layerStackRect,
199            const Rect& displayRect);
200
201private:
202    virtual void onFirstRef();
203    Composer& getComposer();
204
205    mutable     Mutex                       mLock;
206                status_t                    mStatus;
207                sp<ISurfaceComposerClient>  mClient;
208                Composer&                   mComposer;
209                wp<IGraphicBufferProducer>  mParent;
210};
211
212// ---------------------------------------------------------------------------
213
214class ScreenshotClient
215{
216public:
217    // if cropping isn't required, callers may pass in a default Rect, e.g.:
218    //   capture(display, producer, Rect(), reqWidth, ...);
219    static status_t capture(
220            const sp<IBinder>& display,
221            const sp<IGraphicBufferProducer>& producer,
222            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
223            int32_t minLayerZ, int32_t maxLayerZ,
224            bool useIdentityTransform);
225    static status_t captureToBuffer(
226            const sp<IBinder>& display,
227            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
228            int32_t minLayerZ, int32_t maxLayerZ,
229            bool useIdentityTransform,
230            uint32_t rotation,
231            sp<GraphicBuffer>* outbuffer);
232private:
233    mutable sp<CpuConsumer> mCpuConsumer;
234    mutable sp<IGraphicBufferProducer> mProducer;
235    CpuConsumer::LockedBuffer mBuffer;
236    bool mHaveBuffer;
237
238public:
239    ScreenshotClient();
240    ~ScreenshotClient();
241
242    // frees the previous screenshot and captures a new one
243    // if cropping isn't required, callers may pass in a default Rect, e.g.:
244    //   update(display, Rect(), useIdentityTransform);
245    status_t update(const sp<IBinder>& display,
246            Rect sourceCrop, bool useIdentityTransform);
247    status_t update(const sp<IBinder>& display,
248            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
249            bool useIdentityTransform);
250    status_t update(const sp<IBinder>& display,
251            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
252            int32_t minLayerZ, int32_t maxLayerZ,
253            bool useIdentityTransform);
254    status_t update(const sp<IBinder>& display,
255            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
256            int32_t minLayerZ, int32_t maxLayerZ,
257            bool useIdentityTransform, uint32_t rotation);
258
259    sp<CpuConsumer> getCpuConsumer() const;
260
261    // release memory occupied by the screenshot
262    void release();
263
264    // pixels are valid until this object is freed or
265    // release() or update() is called
266    void const* getPixels() const;
267
268    uint32_t getWidth() const;
269    uint32_t getHeight() const;
270    PixelFormat getFormat() const;
271    uint32_t getStride() const;
272    // size of allocated memory in bytes
273    size_t getSize() const;
274};
275
276// ---------------------------------------------------------------------------
277}; // namespace android
278
279#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
280