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