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