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