OSDN Git Service

Merge "Add new system APK locations." into lmp-dev
[android-x86/frameworks-native.git] / libs / gui / ISurfaceComposer.cpp
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 // tag as surfaceflinger
18 #define LOG_TAG "SurfaceFlinger"
19
20 #include <stdint.h>
21 #include <sys/types.h>
22
23 #include <binder/Parcel.h>
24 #include <binder/IMemory.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27
28 #include <gui/BitTube.h>
29 #include <gui/IDisplayEventConnection.h>
30 #include <gui/ISurfaceComposer.h>
31 #include <gui/IGraphicBufferProducer.h>
32
33 #include <private/gui/LayerState.h>
34
35 #include <ui/DisplayInfo.h>
36
37 #include <utils/Log.h>
38
39 // ---------------------------------------------------------------------------
40
41 namespace android {
42
43 class IDisplayEventConnection;
44
45 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
46 {
47 public:
48     BpSurfaceComposer(const sp<IBinder>& impl)
49         : BpInterface<ISurfaceComposer>(impl)
50     {
51     }
52
53     virtual sp<ISurfaceComposerClient> createConnection()
54     {
55         uint32_t n;
56         Parcel data, reply;
57         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
58         remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
59         return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
60     }
61
62     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc()
63     {
64         uint32_t n;
65         Parcel data, reply;
66         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
67         remote()->transact(BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, data, &reply);
68         return interface_cast<IGraphicBufferAlloc>(reply.readStrongBinder());
69     }
70
71     virtual void setTransactionState(
72             const Vector<ComposerState>& state,
73             const Vector<DisplayState>& displays,
74             uint32_t flags)
75     {
76         Parcel data, reply;
77         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
78         {
79             Vector<ComposerState>::const_iterator b(state.begin());
80             Vector<ComposerState>::const_iterator e(state.end());
81             data.writeInt32(state.size());
82             for ( ; b != e ; ++b ) {
83                 b->write(data);
84             }
85         }
86         {
87             Vector<DisplayState>::const_iterator b(displays.begin());
88             Vector<DisplayState>::const_iterator e(displays.end());
89             data.writeInt32(displays.size());
90             for ( ; b != e ; ++b ) {
91                 b->write(data);
92             }
93         }
94         data.writeInt32(flags);
95         remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
96     }
97
98     virtual void bootFinished()
99     {
100         Parcel data, reply;
101         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
102         remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
103     }
104
105     virtual status_t captureScreen(const sp<IBinder>& display,
106             const sp<IGraphicBufferProducer>& producer,
107             Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
108             uint32_t minLayerZ, uint32_t maxLayerZ,
109             bool useIdentityTransform)
110     {
111         Parcel data, reply;
112         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
113         data.writeStrongBinder(display);
114         data.writeStrongBinder(producer->asBinder());
115         data.write(sourceCrop);
116         data.writeInt32(reqWidth);
117         data.writeInt32(reqHeight);
118         data.writeInt32(minLayerZ);
119         data.writeInt32(maxLayerZ);
120         data.writeInt32(static_cast<int32_t>(useIdentityTransform));
121         remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
122         return reply.readInt32();
123     }
124
125     virtual bool authenticateSurfaceTexture(
126             const sp<IGraphicBufferProducer>& bufferProducer) const
127     {
128         Parcel data, reply;
129         int err = NO_ERROR;
130         err = data.writeInterfaceToken(
131                 ISurfaceComposer::getInterfaceDescriptor());
132         if (err != NO_ERROR) {
133             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
134                     "interface descriptor: %s (%d)", strerror(-err), -err);
135             return false;
136         }
137         err = data.writeStrongBinder(bufferProducer->asBinder());
138         if (err != NO_ERROR) {
139             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
140                     "strong binder to parcel: %s (%d)", strerror(-err), -err);
141             return false;
142         }
143         err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
144                 &reply);
145         if (err != NO_ERROR) {
146             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
147                     "performing transaction: %s (%d)", strerror(-err), -err);
148             return false;
149         }
150         int32_t result = 0;
151         err = reply.readInt32(&result);
152         if (err != NO_ERROR) {
153             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
154                     "retrieving result: %s (%d)", strerror(-err), -err);
155             return false;
156         }
157         return result != 0;
158     }
159
160     virtual sp<IDisplayEventConnection> createDisplayEventConnection()
161     {
162         Parcel data, reply;
163         sp<IDisplayEventConnection> result;
164         int err = data.writeInterfaceToken(
165                 ISurfaceComposer::getInterfaceDescriptor());
166         if (err != NO_ERROR) {
167             return result;
168         }
169         err = remote()->transact(
170                 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
171                 data, &reply);
172         if (err != NO_ERROR) {
173             ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
174                     "transaction: %s (%d)", strerror(-err), -err);
175             return result;
176         }
177         result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
178         return result;
179     }
180
181     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
182     {
183         Parcel data, reply;
184         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
185         data.writeString8(displayName);
186         data.writeInt32(secure ? 1 : 0);
187         remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
188         return reply.readStrongBinder();
189     }
190
191     virtual void destroyDisplay(const sp<IBinder>& display)
192     {
193         Parcel data, reply;
194         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
195         data.writeStrongBinder(display);
196         remote()->transact(BnSurfaceComposer::DESTROY_DISPLAY, data, &reply);
197     }
198
199     virtual sp<IBinder> getBuiltInDisplay(int32_t id)
200     {
201         Parcel data, reply;
202         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
203         data.writeInt32(id);
204         remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
205         return reply.readStrongBinder();
206     }
207
208     virtual void setPowerMode(const sp<IBinder>& display, int mode)
209     {
210         Parcel data, reply;
211         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
212         data.writeStrongBinder(display);
213         data.writeInt32(mode);
214         remote()->transact(BnSurfaceComposer::SET_POWER_MODE, data, &reply);
215     }
216
217     virtual status_t getDisplayConfigs(const sp<IBinder>& display,
218             Vector<DisplayInfo>* configs)
219     {
220         Parcel data, reply;
221         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
222         data.writeStrongBinder(display);
223         remote()->transact(BnSurfaceComposer::GET_DISPLAY_CONFIGS, data, &reply);
224         status_t result = reply.readInt32();
225         if (result == NO_ERROR) {
226             size_t numConfigs = static_cast<size_t>(reply.readInt32());
227             configs->clear();
228             configs->resize(numConfigs);
229             for (size_t c = 0; c < numConfigs; ++c) {
230                 memcpy(&(configs->editItemAt(c)),
231                         reply.readInplace(sizeof(DisplayInfo)),
232                         sizeof(DisplayInfo));
233             }
234         }
235         return result;
236     }
237
238     virtual int getActiveConfig(const sp<IBinder>& display)
239     {
240         Parcel data, reply;
241         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
242         data.writeStrongBinder(display);
243         remote()->transact(BnSurfaceComposer::GET_ACTIVE_CONFIG, data, &reply);
244         return reply.readInt32();
245     }
246
247     virtual status_t setActiveConfig(const sp<IBinder>& display, int id)
248     {
249         Parcel data, reply;
250         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
251         data.writeStrongBinder(display);
252         data.writeInt32(id);
253         remote()->transact(BnSurfaceComposer::SET_ACTIVE_CONFIG, data, &reply);
254         return reply.readInt32();
255     }
256
257     virtual status_t clearAnimationFrameStats() {
258         Parcel data, reply;
259         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
260         remote()->transact(BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS, data, &reply);
261         return reply.readInt32();
262     }
263
264     virtual status_t getAnimationFrameStats(FrameStats* outStats) const {
265         Parcel data, reply;
266         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
267         remote()->transact(BnSurfaceComposer::GET_ANIMATION_FRAME_STATS, data, &reply);
268         reply.read(*outStats);
269         return reply.readInt32();
270     }
271 };
272
273 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
274
275 // ----------------------------------------------------------------------
276
277 status_t BnSurfaceComposer::onTransact(
278     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
279 {
280     switch(code) {
281         case CREATE_CONNECTION: {
282             CHECK_INTERFACE(ISurfaceComposer, data, reply);
283             sp<IBinder> b = createConnection()->asBinder();
284             reply->writeStrongBinder(b);
285             return NO_ERROR;
286         }
287         case CREATE_GRAPHIC_BUFFER_ALLOC: {
288             CHECK_INTERFACE(ISurfaceComposer, data, reply);
289             sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
290             reply->writeStrongBinder(b);
291             return NO_ERROR;
292         }
293         case SET_TRANSACTION_STATE: {
294             CHECK_INTERFACE(ISurfaceComposer, data, reply);
295             size_t count = data.readInt32();
296             ComposerState s;
297             Vector<ComposerState> state;
298             state.setCapacity(count);
299             for (size_t i=0 ; i<count ; i++) {
300                 s.read(data);
301                 state.add(s);
302             }
303             count = data.readInt32();
304             DisplayState d;
305             Vector<DisplayState> displays;
306             displays.setCapacity(count);
307             for (size_t i=0 ; i<count ; i++) {
308                 d.read(data);
309                 displays.add(d);
310             }
311             uint32_t flags = data.readInt32();
312             setTransactionState(state, displays, flags);
313             return NO_ERROR;
314         }
315         case BOOT_FINISHED: {
316             CHECK_INTERFACE(ISurfaceComposer, data, reply);
317             bootFinished();
318             return NO_ERROR;
319         }
320         case CAPTURE_SCREEN: {
321             CHECK_INTERFACE(ISurfaceComposer, data, reply);
322             sp<IBinder> display = data.readStrongBinder();
323             sp<IGraphicBufferProducer> producer =
324                     interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
325             Rect sourceCrop;
326             data.read(sourceCrop);
327             uint32_t reqWidth = data.readInt32();
328             uint32_t reqHeight = data.readInt32();
329             uint32_t minLayerZ = data.readInt32();
330             uint32_t maxLayerZ = data.readInt32();
331             bool useIdentityTransform = static_cast<bool>(data.readInt32());
332
333             status_t res = captureScreen(display, producer,
334                     sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
335                     useIdentityTransform);
336             reply->writeInt32(res);
337             return NO_ERROR;
338         }
339         case AUTHENTICATE_SURFACE: {
340             CHECK_INTERFACE(ISurfaceComposer, data, reply);
341             sp<IGraphicBufferProducer> bufferProducer =
342                     interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
343             int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
344             reply->writeInt32(result);
345             return NO_ERROR;
346         }
347         case CREATE_DISPLAY_EVENT_CONNECTION: {
348             CHECK_INTERFACE(ISurfaceComposer, data, reply);
349             sp<IDisplayEventConnection> connection(createDisplayEventConnection());
350             reply->writeStrongBinder(connection->asBinder());
351             return NO_ERROR;
352         }
353         case CREATE_DISPLAY: {
354             CHECK_INTERFACE(ISurfaceComposer, data, reply);
355             String8 displayName = data.readString8();
356             bool secure = bool(data.readInt32());
357             sp<IBinder> display(createDisplay(displayName, secure));
358             reply->writeStrongBinder(display);
359             return NO_ERROR;
360         }
361         case DESTROY_DISPLAY: {
362             CHECK_INTERFACE(ISurfaceComposer, data, reply);
363             sp<IBinder> display = data.readStrongBinder();
364             destroyDisplay(display);
365             return NO_ERROR;
366         }
367         case GET_BUILT_IN_DISPLAY: {
368             CHECK_INTERFACE(ISurfaceComposer, data, reply);
369             int32_t id = data.readInt32();
370             sp<IBinder> display(getBuiltInDisplay(id));
371             reply->writeStrongBinder(display);
372             return NO_ERROR;
373         }
374         case GET_DISPLAY_CONFIGS: {
375             CHECK_INTERFACE(ISurfaceComposer, data, reply);
376             Vector<DisplayInfo> configs;
377             sp<IBinder> display = data.readStrongBinder();
378             status_t result = getDisplayConfigs(display, &configs);
379             reply->writeInt32(result);
380             if (result == NO_ERROR) {
381                 reply->writeInt32(static_cast<int32_t>(configs.size()));
382                 for (size_t c = 0; c < configs.size(); ++c) {
383                     memcpy(reply->writeInplace(sizeof(DisplayInfo)),
384                             &configs[c], sizeof(DisplayInfo));
385                 }
386             }
387             return NO_ERROR;
388         }
389         case GET_ACTIVE_CONFIG: {
390             CHECK_INTERFACE(ISurfaceComposer, data, reply);
391             sp<IBinder> display = data.readStrongBinder();
392             int id = getActiveConfig(display);
393             reply->writeInt32(id);
394             return NO_ERROR;
395         }
396         case SET_ACTIVE_CONFIG: {
397             CHECK_INTERFACE(ISurfaceComposer, data, reply);
398             sp<IBinder> display = data.readStrongBinder();
399             int id = data.readInt32();
400             status_t result = setActiveConfig(display, id);
401             reply->writeInt32(result);
402             return NO_ERROR;
403         }
404         case CLEAR_ANIMATION_FRAME_STATS: {
405             CHECK_INTERFACE(ISurfaceComposer, data, reply);
406             status_t result = clearAnimationFrameStats();
407             reply->writeInt32(result);
408             return NO_ERROR;
409         }
410         case GET_ANIMATION_FRAME_STATS: {
411             CHECK_INTERFACE(ISurfaceComposer, data, reply);
412             FrameStats stats;
413             status_t result = getAnimationFrameStats(&stats);
414             reply->write(stats);
415             reply->writeInt32(result);
416             return NO_ERROR;
417         }
418         case SET_POWER_MODE: {
419             CHECK_INTERFACE(ISurfaceComposer, data, reply);
420             sp<IBinder> display = data.readStrongBinder();
421             int32_t mode = data.readInt32();
422             setPowerMode(display, mode);
423             return NO_ERROR;
424         }
425         default: {
426             return BBinder::onTransact(code, data, reply, flags);
427         }
428     }
429     // should be unreachable
430     return NO_ERROR;
431 }
432
433 // ----------------------------------------------------------------------------
434
435 };