ISurfaceComposer.cpp revision 6c913be9ca95fd6b556d056e165a4ba6dc69795b
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
41namespace android {
42
43class IDisplayEventConnection;
44
45class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
46{
47public:
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            uint32_t reqWidth, uint32_t reqHeight,
108            uint32_t minLayerZ, uint32_t maxLayerZ)
109    {
110        Parcel data, reply;
111        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
112        data.writeStrongBinder(display);
113        data.writeStrongBinder(producer->asBinder());
114        data.writeInt32(reqWidth);
115        data.writeInt32(reqHeight);
116        data.writeInt32(minLayerZ);
117        data.writeInt32(maxLayerZ);
118        remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
119        return reply.readInt32();
120    }
121
122    virtual bool authenticateSurfaceTexture(
123            const sp<IGraphicBufferProducer>& bufferProducer) const
124    {
125        Parcel data, reply;
126        int err = NO_ERROR;
127        err = data.writeInterfaceToken(
128                ISurfaceComposer::getInterfaceDescriptor());
129        if (err != NO_ERROR) {
130            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
131                    "interface descriptor: %s (%d)", strerror(-err), -err);
132            return false;
133        }
134        err = data.writeStrongBinder(bufferProducer->asBinder());
135        if (err != NO_ERROR) {
136            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
137                    "strong binder to parcel: %s (%d)", strerror(-err), -err);
138            return false;
139        }
140        err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
141                &reply);
142        if (err != NO_ERROR) {
143            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
144                    "performing transaction: %s (%d)", strerror(-err), -err);
145            return false;
146        }
147        int32_t result = 0;
148        err = reply.readInt32(&result);
149        if (err != NO_ERROR) {
150            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
151                    "retrieving result: %s (%d)", strerror(-err), -err);
152            return false;
153        }
154        return result != 0;
155    }
156
157    virtual sp<IDisplayEventConnection> createDisplayEventConnection()
158    {
159        Parcel data, reply;
160        sp<IDisplayEventConnection> result;
161        int err = data.writeInterfaceToken(
162                ISurfaceComposer::getInterfaceDescriptor());
163        if (err != NO_ERROR) {
164            return result;
165        }
166        err = remote()->transact(
167                BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
168                data, &reply);
169        if (err != NO_ERROR) {
170            ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
171                    "transaction: %s (%d)", strerror(-err), -err);
172            return result;
173        }
174        result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
175        return result;
176    }
177
178    virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
179    {
180        Parcel data, reply;
181        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
182        data.writeString8(displayName);
183        data.writeInt32(secure ? 1 : 0);
184        remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
185        return reply.readStrongBinder();
186    }
187
188    virtual void destroyDisplay(const sp<IBinder>& display)
189    {
190        Parcel data, reply;
191        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
192        data.writeStrongBinder(display);
193        remote()->transact(BnSurfaceComposer::DESTROY_DISPLAY, data, &reply);
194    }
195
196    virtual sp<IBinder> getBuiltInDisplay(int32_t id)
197    {
198        Parcel data, reply;
199        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
200        data.writeInt32(id);
201        remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
202        return reply.readStrongBinder();
203    }
204
205    virtual void blank(const sp<IBinder>& display)
206    {
207        Parcel data, reply;
208        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
209        data.writeStrongBinder(display);
210        remote()->transact(BnSurfaceComposer::BLANK, data, &reply);
211    }
212
213    virtual void unblank(const sp<IBinder>& display)
214    {
215        Parcel data, reply;
216        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
217        data.writeStrongBinder(display);
218        remote()->transact(BnSurfaceComposer::UNBLANK, data, &reply);
219    }
220
221    virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info)
222    {
223        Parcel data, reply;
224        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
225        data.writeStrongBinder(display);
226        remote()->transact(BnSurfaceComposer::GET_DISPLAY_INFO, data, &reply);
227        memcpy(info, reply.readInplace(sizeof(DisplayInfo)), sizeof(DisplayInfo));
228        return reply.readInt32();
229    }
230};
231
232IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
233
234// ----------------------------------------------------------------------
235
236status_t BnSurfaceComposer::onTransact(
237    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
238{
239    switch(code) {
240        case CREATE_CONNECTION: {
241            CHECK_INTERFACE(ISurfaceComposer, data, reply);
242            sp<IBinder> b = createConnection()->asBinder();
243            reply->writeStrongBinder(b);
244            return NO_ERROR;
245        }
246        case CREATE_GRAPHIC_BUFFER_ALLOC: {
247            CHECK_INTERFACE(ISurfaceComposer, data, reply);
248            sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
249            reply->writeStrongBinder(b);
250            return NO_ERROR;
251        }
252        case SET_TRANSACTION_STATE: {
253            CHECK_INTERFACE(ISurfaceComposer, data, reply);
254            size_t count = data.readInt32();
255            ComposerState s;
256            Vector<ComposerState> state;
257            state.setCapacity(count);
258            for (size_t i=0 ; i<count ; i++) {
259                s.read(data);
260                state.add(s);
261            }
262            count = data.readInt32();
263            DisplayState d;
264            Vector<DisplayState> displays;
265            displays.setCapacity(count);
266            for (size_t i=0 ; i<count ; i++) {
267                d.read(data);
268                displays.add(d);
269            }
270            uint32_t flags = data.readInt32();
271            setTransactionState(state, displays, flags);
272            return NO_ERROR;
273        }
274        case BOOT_FINISHED: {
275            CHECK_INTERFACE(ISurfaceComposer, data, reply);
276            bootFinished();
277            return NO_ERROR;
278        }
279        case CAPTURE_SCREEN: {
280            CHECK_INTERFACE(ISurfaceComposer, data, reply);
281            sp<IBinder> display = data.readStrongBinder();
282            sp<IGraphicBufferProducer> producer =
283                    interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
284            uint32_t reqWidth = data.readInt32();
285            uint32_t reqHeight = data.readInt32();
286            uint32_t minLayerZ = data.readInt32();
287            uint32_t maxLayerZ = data.readInt32();
288            status_t res = captureScreen(display, producer,
289                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
290            reply->writeInt32(res);
291            return NO_ERROR;
292        }
293        case AUTHENTICATE_SURFACE: {
294            CHECK_INTERFACE(ISurfaceComposer, data, reply);
295            sp<IGraphicBufferProducer> bufferProducer =
296                    interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
297            int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
298            reply->writeInt32(result);
299            return NO_ERROR;
300        }
301        case CREATE_DISPLAY_EVENT_CONNECTION: {
302            CHECK_INTERFACE(ISurfaceComposer, data, reply);
303            sp<IDisplayEventConnection> connection(createDisplayEventConnection());
304            reply->writeStrongBinder(connection->asBinder());
305            return NO_ERROR;
306        }
307        case CREATE_DISPLAY: {
308            CHECK_INTERFACE(ISurfaceComposer, data, reply);
309            String8 displayName = data.readString8();
310            bool secure = bool(data.readInt32());
311            sp<IBinder> display(createDisplay(displayName, secure));
312            reply->writeStrongBinder(display);
313            return NO_ERROR;
314        }
315        case DESTROY_DISPLAY: {
316            CHECK_INTERFACE(ISurfaceComposer, data, reply);
317            sp<IBinder> display = data.readStrongBinder();
318            destroyDisplay(display);
319            return NO_ERROR;
320        }
321        case GET_BUILT_IN_DISPLAY: {
322            CHECK_INTERFACE(ISurfaceComposer, data, reply);
323            int32_t id = data.readInt32();
324            sp<IBinder> display(getBuiltInDisplay(id));
325            reply->writeStrongBinder(display);
326            return NO_ERROR;
327        }
328        case BLANK: {
329            CHECK_INTERFACE(ISurfaceComposer, data, reply);
330            sp<IBinder> display = data.readStrongBinder();
331            blank(display);
332            return NO_ERROR;
333        }
334        case UNBLANK: {
335            CHECK_INTERFACE(ISurfaceComposer, data, reply);
336            sp<IBinder> display = data.readStrongBinder();
337            unblank(display);
338            return NO_ERROR;
339        }
340        case GET_DISPLAY_INFO: {
341            CHECK_INTERFACE(ISurfaceComposer, data, reply);
342            DisplayInfo info;
343            sp<IBinder> display = data.readStrongBinder();
344            status_t result = getDisplayInfo(display, &info);
345            memcpy(reply->writeInplace(sizeof(DisplayInfo)), &info, sizeof(DisplayInfo));
346            reply->writeInt32(result);
347            return NO_ERROR;
348        }
349        default: {
350            return BBinder::onTransact(code, data, reply, flags);
351        }
352    }
353    // should be unreachable
354    return NO_ERROR;
355}
356
357// ----------------------------------------------------------------------------
358
359};
360