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