ISurfaceComposer.cpp revision d723bd7669b4fc88dc282d8bf8ba5ecb2849d22f
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 ~BpSurfaceComposer();
55
56    virtual sp<ISurfaceComposerClient> createConnection()
57    {
58        uint32_t n;
59        Parcel data, reply;
60        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
61        remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
62        return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
63    }
64
65    virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc()
66    {
67        uint32_t n;
68        Parcel data, reply;
69        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
70        remote()->transact(BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, data, &reply);
71        return interface_cast<IGraphicBufferAlloc>(reply.readStrongBinder());
72    }
73
74    virtual void setTransactionState(
75            const Vector<ComposerState>& state,
76            const Vector<DisplayState>& displays,
77            uint32_t flags)
78    {
79        Parcel data, reply;
80        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
81
82        data.writeUint32(static_cast<uint32_t>(state.size()));
83        for (const auto& s : state) {
84            s.write(data);
85        }
86
87        data.writeUint32(static_cast<uint32_t>(displays.size()));
88        for (const auto& d : displays) {
89            d.write(data);
90        }
91
92        data.writeUint32(flags);
93        remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
94    }
95
96    virtual void bootFinished()
97    {
98        Parcel data, reply;
99        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
100        remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
101    }
102
103    virtual status_t captureScreen(const sp<IBinder>& display,
104            const sp<IGraphicBufferProducer>& producer,
105            Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
106            uint32_t minLayerZ, uint32_t maxLayerZ,
107            bool useIdentityTransform,
108            ISurfaceComposer::Rotation rotation)
109    {
110        Parcel data, reply;
111        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
112        data.writeStrongBinder(display);
113        data.writeStrongBinder(IInterface::asBinder(producer));
114        data.write(sourceCrop);
115        data.writeUint32(reqWidth);
116        data.writeUint32(reqHeight);
117        data.writeUint32(minLayerZ);
118        data.writeUint32(maxLayerZ);
119        data.writeInt32(static_cast<int32_t>(useIdentityTransform));
120        data.writeInt32(static_cast<int32_t>(rotation));
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(IInterface::asBinder(bufferProducer));
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 = reply.readUint32();
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 status_t getDisplayStats(const sp<IBinder>& display,
239            DisplayStatInfo* stats)
240    {
241        Parcel data, reply;
242        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
243        data.writeStrongBinder(display);
244        remote()->transact(BnSurfaceComposer::GET_DISPLAY_STATS, data, &reply);
245        status_t result = reply.readInt32();
246        if (result == NO_ERROR) {
247            memcpy(stats,
248                    reply.readInplace(sizeof(DisplayStatInfo)),
249                    sizeof(DisplayStatInfo));
250        }
251        return result;
252    }
253
254    virtual int getActiveConfig(const sp<IBinder>& display)
255    {
256        Parcel data, reply;
257        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
258        data.writeStrongBinder(display);
259        remote()->transact(BnSurfaceComposer::GET_ACTIVE_CONFIG, data, &reply);
260        return reply.readInt32();
261    }
262
263    virtual status_t setActiveConfig(const sp<IBinder>& display, int id)
264    {
265        Parcel data, reply;
266        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
267        data.writeStrongBinder(display);
268        data.writeInt32(id);
269        remote()->transact(BnSurfaceComposer::SET_ACTIVE_CONFIG, data, &reply);
270        return reply.readInt32();
271    }
272
273    virtual status_t clearAnimationFrameStats() {
274        Parcel data, reply;
275        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
276        remote()->transact(BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS, data, &reply);
277        return reply.readInt32();
278    }
279
280    virtual status_t getAnimationFrameStats(FrameStats* outStats) const {
281        Parcel data, reply;
282        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
283        remote()->transact(BnSurfaceComposer::GET_ANIMATION_FRAME_STATS, data, &reply);
284        reply.read(*outStats);
285        return reply.readInt32();
286    }
287};
288
289// Out-of-line virtual method definition to trigger vtable emission in this
290// translation unit (see clang warning -Wweak-vtables)
291BpSurfaceComposer::~BpSurfaceComposer() {}
292
293IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
294
295// ----------------------------------------------------------------------
296
297status_t BnSurfaceComposer::onTransact(
298    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
299{
300    switch(code) {
301        case CREATE_CONNECTION: {
302            CHECK_INTERFACE(ISurfaceComposer, data, reply);
303            sp<IBinder> b = IInterface::asBinder(createConnection());
304            reply->writeStrongBinder(b);
305            return NO_ERROR;
306        }
307        case CREATE_GRAPHIC_BUFFER_ALLOC: {
308            CHECK_INTERFACE(ISurfaceComposer, data, reply);
309            sp<IBinder> b = IInterface::asBinder(createGraphicBufferAlloc());
310            reply->writeStrongBinder(b);
311            return NO_ERROR;
312        }
313        case SET_TRANSACTION_STATE: {
314            CHECK_INTERFACE(ISurfaceComposer, data, reply);
315
316            size_t count = data.readUint32();
317            if (count > data.dataSize()) {
318                return BAD_VALUE;
319            }
320            ComposerState s;
321            Vector<ComposerState> state;
322            state.setCapacity(count);
323            for (size_t i = 0; i < count; i++) {
324                if (s.read(data) == BAD_VALUE) {
325                    return BAD_VALUE;
326                }
327                state.add(s);
328            }
329
330            count = data.readUint32();
331            if (count > data.dataSize()) {
332                return BAD_VALUE;
333            }
334            DisplayState d;
335            Vector<DisplayState> displays;
336            displays.setCapacity(count);
337            for (size_t i = 0; i < count; i++) {
338                if (d.read(data) == BAD_VALUE) {
339                    return BAD_VALUE;
340                }
341                displays.add(d);
342            }
343
344            uint32_t stateFlags = data.readUint32();
345            setTransactionState(state, displays, stateFlags);
346            return NO_ERROR;
347        }
348        case BOOT_FINISHED: {
349            CHECK_INTERFACE(ISurfaceComposer, data, reply);
350            bootFinished();
351            return NO_ERROR;
352        }
353        case CAPTURE_SCREEN: {
354            CHECK_INTERFACE(ISurfaceComposer, data, reply);
355            sp<IBinder> display = data.readStrongBinder();
356            sp<IGraphicBufferProducer> producer =
357                    interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
358            Rect sourceCrop;
359            data.read(sourceCrop);
360            uint32_t reqWidth = data.readUint32();
361            uint32_t reqHeight = data.readUint32();
362            uint32_t minLayerZ = data.readUint32();
363            uint32_t maxLayerZ = data.readUint32();
364            bool useIdentityTransform = static_cast<bool>(data.readInt32());
365            int32_t rotation = data.readInt32();
366
367            status_t res = captureScreen(display, producer,
368                    sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
369                    useIdentityTransform,
370                    static_cast<ISurfaceComposer::Rotation>(rotation));
371            reply->writeInt32(res);
372            return NO_ERROR;
373        }
374        case AUTHENTICATE_SURFACE: {
375            CHECK_INTERFACE(ISurfaceComposer, data, reply);
376            sp<IGraphicBufferProducer> bufferProducer =
377                    interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
378            int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
379            reply->writeInt32(result);
380            return NO_ERROR;
381        }
382        case CREATE_DISPLAY_EVENT_CONNECTION: {
383            CHECK_INTERFACE(ISurfaceComposer, data, reply);
384            sp<IDisplayEventConnection> connection(createDisplayEventConnection());
385            reply->writeStrongBinder(IInterface::asBinder(connection));
386            return NO_ERROR;
387        }
388        case CREATE_DISPLAY: {
389            CHECK_INTERFACE(ISurfaceComposer, data, reply);
390            String8 displayName = data.readString8();
391            bool secure = bool(data.readInt32());
392            sp<IBinder> display(createDisplay(displayName, secure));
393            reply->writeStrongBinder(display);
394            return NO_ERROR;
395        }
396        case DESTROY_DISPLAY: {
397            CHECK_INTERFACE(ISurfaceComposer, data, reply);
398            sp<IBinder> display = data.readStrongBinder();
399            destroyDisplay(display);
400            return NO_ERROR;
401        }
402        case GET_BUILT_IN_DISPLAY: {
403            CHECK_INTERFACE(ISurfaceComposer, data, reply);
404            int32_t id = data.readInt32();
405            sp<IBinder> display(getBuiltInDisplay(id));
406            reply->writeStrongBinder(display);
407            return NO_ERROR;
408        }
409        case GET_DISPLAY_CONFIGS: {
410            CHECK_INTERFACE(ISurfaceComposer, data, reply);
411            Vector<DisplayInfo> configs;
412            sp<IBinder> display = data.readStrongBinder();
413            status_t result = getDisplayConfigs(display, &configs);
414            reply->writeInt32(result);
415            if (result == NO_ERROR) {
416                reply->writeUint32(static_cast<uint32_t>(configs.size()));
417                for (size_t c = 0; c < configs.size(); ++c) {
418                    memcpy(reply->writeInplace(sizeof(DisplayInfo)),
419                            &configs[c], sizeof(DisplayInfo));
420                }
421            }
422            return NO_ERROR;
423        }
424        case GET_DISPLAY_STATS: {
425            CHECK_INTERFACE(ISurfaceComposer, data, reply);
426            DisplayStatInfo stats;
427            sp<IBinder> display = data.readStrongBinder();
428            status_t result = getDisplayStats(display, &stats);
429            reply->writeInt32(result);
430            if (result == NO_ERROR) {
431                memcpy(reply->writeInplace(sizeof(DisplayStatInfo)),
432                        &stats, sizeof(DisplayStatInfo));
433            }
434            return NO_ERROR;
435        }
436        case GET_ACTIVE_CONFIG: {
437            CHECK_INTERFACE(ISurfaceComposer, data, reply);
438            sp<IBinder> display = data.readStrongBinder();
439            int id = getActiveConfig(display);
440            reply->writeInt32(id);
441            return NO_ERROR;
442        }
443        case SET_ACTIVE_CONFIG: {
444            CHECK_INTERFACE(ISurfaceComposer, data, reply);
445            sp<IBinder> display = data.readStrongBinder();
446            int id = data.readInt32();
447            status_t result = setActiveConfig(display, id);
448            reply->writeInt32(result);
449            return NO_ERROR;
450        }
451        case CLEAR_ANIMATION_FRAME_STATS: {
452            CHECK_INTERFACE(ISurfaceComposer, data, reply);
453            status_t result = clearAnimationFrameStats();
454            reply->writeInt32(result);
455            return NO_ERROR;
456        }
457        case GET_ANIMATION_FRAME_STATS: {
458            CHECK_INTERFACE(ISurfaceComposer, data, reply);
459            FrameStats stats;
460            status_t result = getAnimationFrameStats(&stats);
461            reply->write(stats);
462            reply->writeInt32(result);
463            return NO_ERROR;
464        }
465        case SET_POWER_MODE: {
466            CHECK_INTERFACE(ISurfaceComposer, data, reply);
467            sp<IBinder> display = data.readStrongBinder();
468            int32_t mode = data.readInt32();
469            setPowerMode(display, mode);
470            return NO_ERROR;
471        }
472        default: {
473            return BBinder::onTransact(code, data, reply, flags);
474        }
475    }
476}
477
478// ----------------------------------------------------------------------------
479
480};
481