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#include <binder/IMemory.h>
25
26#include <utils/RefBase.h>
27#include <utils/Singleton.h>
28#include <utils/SortedVector.h>
29#include <utils/threads.h>
30
31#include <ui/FrameStats.h>
32#include <ui/PixelFormat.h>
33
34#include <gui/CpuConsumer.h>
35#include <gui/SurfaceControl.h>
36
37namespace android {
38
39// ---------------------------------------------------------------------------
40
41class DisplayInfo;
42class Composer;
43class HdrCapabilities;
44class ISurfaceComposerClient;
45class IGraphicBufferProducer;
46class Region;
47
48// ---------------------------------------------------------------------------
49
50class SurfaceComposerClient : public RefBase
51{
52    friend class Composer;
53public:
54                SurfaceComposerClient();
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    /* Triggers screen on/off or low power mode and waits for it to complete */
87    static void setDisplayPowerMode(const sp<IBinder>& display, int mode);
88
89    // ------------------------------------------------------------------------
90    // surface creation / destruction
91
92    //! Create a surface
93    sp<SurfaceControl> createSurface(
94            const String8& name,// name of the surface
95            uint32_t w,         // width in pixel
96            uint32_t h,         // height in pixel
97            PixelFormat format, // pixel-format desired
98            uint32_t flags = 0  // usage flags
99    );
100
101    //! Create a virtual display
102    static sp<IBinder> createDisplay(const String8& displayName, bool secure);
103
104    //! Destroy a virtual display
105    static void destroyDisplay(const sp<IBinder>& display);
106
107    //! Get the token for the existing default displays.
108    //! Possible values for id are eDisplayIdMain and eDisplayIdHdmi.
109    static sp<IBinder> getBuiltInDisplay(int32_t id);
110
111    // ------------------------------------------------------------------------
112    // Composer parameters
113    // All composer parameters must be changed within a transaction
114    // several surfaces can be updated in one transaction, all changes are
115    // committed at once when the transaction is closed.
116    // closeGlobalTransaction() requires an IPC with the server.
117
118    //! Open a composer transaction on all active SurfaceComposerClients.
119    static void openGlobalTransaction();
120
121    //! Close a composer transaction on all active SurfaceComposerClients.
122    static void closeGlobalTransaction(bool synchronous = false);
123
124    //! Flag the currently open transaction as an animation transaction.
125    static void setAnimationTransaction();
126
127    status_t    hide(const sp<IBinder>& id);
128    status_t    show(const sp<IBinder>& id);
129    status_t    setFlags(const sp<IBinder>& id, uint32_t flags, uint32_t mask);
130    status_t    setTransparentRegionHint(const sp<IBinder>& id, const Region& transparent);
131    status_t    setLayer(const sp<IBinder>& id, uint32_t layer);
132    status_t    setAlpha(const sp<IBinder>& id, float alpha=1.0f);
133    status_t    setMatrix(const sp<IBinder>& id, float dsdx, float dtdx, float dsdy, float dtdy);
134    status_t    setPosition(const sp<IBinder>& id, float x, float y);
135    status_t    setSize(const sp<IBinder>& id, uint32_t w, uint32_t h);
136    status_t    setCrop(const sp<IBinder>& id, const Rect& crop);
137    status_t    setFinalCrop(const sp<IBinder>& id, const Rect& crop);
138    status_t    setLayerStack(const sp<IBinder>& id, uint32_t layerStack);
139    status_t    deferTransactionUntil(const sp<IBinder>& id,
140            const sp<IBinder>& handle, uint64_t frameNumber);
141    status_t    setOverrideScalingMode(const sp<IBinder>& id,
142            int32_t overrideScalingMode);
143    status_t    setPositionAppliesWithResize(const sp<IBinder>& id);
144
145    status_t    destroySurface(const sp<IBinder>& id);
146
147    status_t clearLayerFrameStats(const sp<IBinder>& token) const;
148    status_t getLayerFrameStats(const sp<IBinder>& token, FrameStats* outStats) const;
149
150    static status_t clearAnimationFrameStats();
151    static status_t getAnimationFrameStats(FrameStats* outStats);
152
153    static status_t getHdrCapabilities(const sp<IBinder>& display,
154            HdrCapabilities* outCapabilities);
155
156    static void setDisplaySurface(const sp<IBinder>& token,
157            const sp<IGraphicBufferProducer>& bufferProducer);
158    static void setDisplayLayerStack(const sp<IBinder>& token,
159            uint32_t layerStack);
160    static void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
161
162    /* setDisplayProjection() defines the projection of layer stacks
163     * to a given display.
164     *
165     * - orientation defines the display's orientation.
166     * - layerStackRect defines which area of the window manager coordinate
167     * space will be used.
168     * - displayRect defines where on the display will layerStackRect be
169     * mapped to. displayRect is specified post-orientation, that is
170     * it uses the orientation seen by the end-user.
171     */
172    static void setDisplayProjection(const sp<IBinder>& token,
173            uint32_t orientation,
174            const Rect& layerStackRect,
175            const Rect& displayRect);
176
177private:
178    virtual void onFirstRef();
179    Composer& getComposer();
180
181    mutable     Mutex                       mLock;
182                status_t                    mStatus;
183                sp<ISurfaceComposerClient>  mClient;
184                Composer&                   mComposer;
185};
186
187// ---------------------------------------------------------------------------
188
189class ScreenshotClient
190{
191public:
192    // if cropping isn't required, callers may pass in a default Rect, e.g.:
193    //   capture(display, producer, Rect(), reqWidth, ...);
194    static status_t capture(
195            const sp<IBinder>& display,
196            const sp<IGraphicBufferProducer>& producer,
197            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
198            uint32_t minLayerZ, uint32_t maxLayerZ,
199            bool useIdentityTransform);
200
201private:
202    mutable sp<CpuConsumer> mCpuConsumer;
203    mutable sp<IGraphicBufferProducer> mProducer;
204    CpuConsumer::LockedBuffer mBuffer;
205    bool mHaveBuffer;
206
207public:
208    ScreenshotClient();
209    ~ScreenshotClient();
210
211    // frees the previous screenshot and captures a new one
212    // if cropping isn't required, callers may pass in a default Rect, e.g.:
213    //   update(display, Rect(), useIdentityTransform);
214    status_t update(const sp<IBinder>& display,
215            Rect sourceCrop, bool useIdentityTransform);
216    status_t update(const sp<IBinder>& display,
217            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
218            bool useIdentityTransform);
219    status_t update(const sp<IBinder>& display,
220            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
221            uint32_t minLayerZ, uint32_t maxLayerZ,
222            bool useIdentityTransform);
223    status_t update(const sp<IBinder>& display,
224            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
225            uint32_t minLayerZ, uint32_t maxLayerZ,
226            bool useIdentityTransform, uint32_t rotation);
227
228    sp<CpuConsumer> getCpuConsumer() const;
229
230    // release memory occupied by the screenshot
231    void release();
232
233    // pixels are valid until this object is freed or
234    // release() or update() is called
235    void const* getPixels() const;
236
237    uint32_t getWidth() const;
238    uint32_t getHeight() const;
239    PixelFormat getFormat() const;
240    uint32_t getStride() const;
241    // size of allocated memory in bytes
242    size_t getSize() const;
243};
244
245// ---------------------------------------------------------------------------
246}; // namespace android
247
248#endif // ANDROID_GUI_SURFACE_COMPOSER_CLIENT_H
249