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 sp<IMemoryHeap> getCblk() const
72    {
73        Parcel data, reply;
74        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
75        remote()->transact(BnSurfaceComposer::GET_CBLK, data, &reply);
76        return interface_cast<IMemoryHeap>(reply.readStrongBinder());
77    }
78
79    virtual void setTransactionState(const Vector<ComposerState>& state,
80            int orientation, uint32_t flags)
81    {
82        Parcel data, reply;
83        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
84        Vector<ComposerState>::const_iterator b(state.begin());
85        Vector<ComposerState>::const_iterator e(state.end());
86        data.writeInt32(state.size());
87        for ( ; b != e ; ++b ) {
88            b->write(data);
89        }
90        data.writeInt32(orientation);
91        data.writeInt32(flags);
92        remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
93    }
94
95    virtual void bootFinished()
96    {
97        Parcel data, reply;
98        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
99        remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
100    }
101
102    virtual status_t captureScreen(DisplayID dpy,
103            sp<IMemoryHeap>* heap,
104            uint32_t* width, uint32_t* height, PixelFormat* format,
105            uint32_t reqWidth, uint32_t reqHeight,
106            uint32_t minLayerZ, uint32_t maxLayerZ)
107    {
108        Parcel data, reply;
109        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
110        data.writeInt32(dpy);
111        data.writeInt32(reqWidth);
112        data.writeInt32(reqHeight);
113        data.writeInt32(minLayerZ);
114        data.writeInt32(maxLayerZ);
115        remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
116        *heap = interface_cast<IMemoryHeap>(reply.readStrongBinder());
117        *width = reply.readInt32();
118        *height = reply.readInt32();
119        *format = reply.readInt32();
120        return reply.readInt32();
121    }
122
123    virtual status_t turnElectronBeamOff(int32_t mode)
124    {
125        Parcel data, reply;
126        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
127        data.writeInt32(mode);
128        remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_OFF, data, &reply);
129        return reply.readInt32();
130    }
131
132    virtual status_t turnElectronBeamOn(int32_t mode)
133    {
134        Parcel data, reply;
135        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
136        data.writeInt32(mode);
137        remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_ON, data, &reply);
138        return reply.readInt32();
139    }
140
141    virtual bool authenticateSurfaceTexture(
142            const sp<ISurfaceTexture>& surfaceTexture) const
143    {
144        Parcel data, reply;
145        int err = NO_ERROR;
146        err = data.writeInterfaceToken(
147                ISurfaceComposer::getInterfaceDescriptor());
148        if (err != NO_ERROR) {
149            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
150                    "interface descriptor: %s (%d)", strerror(-err), -err);
151            return false;
152        }
153        err = data.writeStrongBinder(surfaceTexture->asBinder());
154        if (err != NO_ERROR) {
155            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
156                    "strong binder to parcel: %s (%d)", strerror(-err), -err);
157            return false;
158        }
159        err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
160                &reply);
161        if (err != NO_ERROR) {
162            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
163                    "performing transaction: %s (%d)", strerror(-err), -err);
164            return false;
165        }
166        int32_t result = 0;
167        err = reply.readInt32(&result);
168        if (err != NO_ERROR) {
169            ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
170                    "retrieving result: %s (%d)", strerror(-err), -err);
171            return false;
172        }
173        return result != 0;
174    }
175
176    virtual sp<IDisplayEventConnection> createDisplayEventConnection()
177    {
178        Parcel data, reply;
179        sp<IDisplayEventConnection> result;
180        int err = data.writeInterfaceToken(
181                ISurfaceComposer::getInterfaceDescriptor());
182        if (err != NO_ERROR) {
183            return result;
184        }
185        err = remote()->transact(
186                BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
187                data, &reply);
188        if (err != NO_ERROR) {
189            ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
190                    "transaction: %s (%d)", strerror(-err), -err);
191            return result;
192        }
193        result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
194        return result;
195    }
196};
197
198IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
199
200// ----------------------------------------------------------------------
201
202status_t BnSurfaceComposer::onTransact(
203    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
204{
205    switch(code) {
206        case CREATE_CONNECTION: {
207            CHECK_INTERFACE(ISurfaceComposer, data, reply);
208            sp<IBinder> b = createConnection()->asBinder();
209            reply->writeStrongBinder(b);
210        } break;
211        case CREATE_GRAPHIC_BUFFER_ALLOC: {
212            CHECK_INTERFACE(ISurfaceComposer, data, reply);
213            sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
214            reply->writeStrongBinder(b);
215        } break;
216        case SET_TRANSACTION_STATE: {
217            CHECK_INTERFACE(ISurfaceComposer, data, reply);
218            size_t count = data.readInt32();
219            ComposerState s;
220            Vector<ComposerState> state;
221            state.setCapacity(count);
222            for (size_t i=0 ; i<count ; i++) {
223                s.read(data);
224                state.add(s);
225            }
226            int orientation = data.readInt32();
227            uint32_t flags = data.readInt32();
228            setTransactionState(state, orientation, flags);
229        } break;
230        case BOOT_FINISHED: {
231            CHECK_INTERFACE(ISurfaceComposer, data, reply);
232            bootFinished();
233        } break;
234        case GET_CBLK: {
235            CHECK_INTERFACE(ISurfaceComposer, data, reply);
236            sp<IBinder> b = getCblk()->asBinder();
237            reply->writeStrongBinder(b);
238        } break;
239        case CAPTURE_SCREEN: {
240            CHECK_INTERFACE(ISurfaceComposer, data, reply);
241            DisplayID dpy = data.readInt32();
242            uint32_t reqWidth = data.readInt32();
243            uint32_t reqHeight = data.readInt32();
244            uint32_t minLayerZ = data.readInt32();
245            uint32_t maxLayerZ = data.readInt32();
246            sp<IMemoryHeap> heap;
247            uint32_t w, h;
248            PixelFormat f;
249            status_t res = captureScreen(dpy, &heap, &w, &h, &f,
250                    reqWidth, reqHeight, minLayerZ, maxLayerZ);
251            reply->writeStrongBinder(heap->asBinder());
252            reply->writeInt32(w);
253            reply->writeInt32(h);
254            reply->writeInt32(f);
255            reply->writeInt32(res);
256        } break;
257        case TURN_ELECTRON_BEAM_OFF: {
258            CHECK_INTERFACE(ISurfaceComposer, data, reply);
259            int32_t mode = data.readInt32();
260            status_t res = turnElectronBeamOff(mode);
261            reply->writeInt32(res);
262        } break;
263        case TURN_ELECTRON_BEAM_ON: {
264            CHECK_INTERFACE(ISurfaceComposer, data, reply);
265            int32_t mode = data.readInt32();
266            status_t res = turnElectronBeamOn(mode);
267            reply->writeInt32(res);
268        } break;
269        case AUTHENTICATE_SURFACE: {
270            CHECK_INTERFACE(ISurfaceComposer, data, reply);
271            sp<ISurfaceTexture> surfaceTexture =
272                    interface_cast<ISurfaceTexture>(data.readStrongBinder());
273            int32_t result = authenticateSurfaceTexture(surfaceTexture) ? 1 : 0;
274            reply->writeInt32(result);
275        } break;
276        case CREATE_DISPLAY_EVENT_CONNECTION: {
277            CHECK_INTERFACE(ISurfaceComposer, data, reply);
278            sp<IDisplayEventConnection> connection(createDisplayEventConnection());
279            reply->writeStrongBinder(connection->asBinder());
280            return NO_ERROR;
281        } break;
282        default:
283            return BBinder::onTransact(code, data, reply, flags);
284    }
285    return NO_ERROR;
286}
287
288// ----------------------------------------------------------------------------
289
290};
291