blob: 85a94882ee28e4f11894a5ad4db15eafdbc3293b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Parcel.h>
24#include <binder/IMemory.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiand0566bc2011-11-17 17:49:17 -080028#include <gui/BitTube.h>
29#include <gui/IDisplayEventConnection.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080030#include <gui/ISurfaceComposer.h>
31#include <gui/ISurfaceTexture.h>
32
33#include <private/gui/LayerState.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080034
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <ui/DisplayInfo.h>
36
Jamie Gennis134f0422011-03-08 12:18:54 -080037#include <utils/Log.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039// ---------------------------------------------------------------------------
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041namespace android {
42
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043class IDisplayEventConnection;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
46{
47public:
48 BpSurfaceComposer(const sp<IBinder>& impl)
49 : BpInterface<ISurfaceComposer>(impl)
50 {
51 }
52
Mathias Agopian7e27f052010-05-28 14:22:23 -070053 virtual sp<ISurfaceComposerClient> createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 {
55 uint32_t n;
56 Parcel data, reply;
57 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
58 remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
Mathias Agopian7e27f052010-05-28 14:22:23 -070059 return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 }
61
Jamie Gennis9a78c902011-01-12 18:30:40 -080062 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
Mathias Agopian8b33f032012-07-24 20:43:54 -070071 virtual void setTransactionState(
72 const Vector<ComposerState>& state,
73 const Vector<DisplayState>& displays,
74 uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 {
76 Parcel data, reply;
77 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Mathias Agopian8b33f032012-07-24 20:43:54 -070078 {
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 }
Mathias Agopian698c0872011-06-28 19:09:31 -070085 }
Mathias Agopian8b33f032012-07-24 20:43:54 -070086 {
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 }
Jamie Gennis28378392011-10-12 17:39:00 -070094 data.writeInt32(flags);
Jamie Gennisb8d69a52011-10-10 15:48:06 -070095 remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 }
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
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700105 virtual status_t captureScreen(
106 const sp<IBinder>& display, sp<IMemoryHeap>* heap,
Mathias Agopiandf85c452010-09-29 13:02:36 -0700107 uint32_t* width, uint32_t* height, PixelFormat* format,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800108 uint32_t reqWidth, uint32_t reqHeight,
109 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian1b0b30d2010-09-24 11:26:58 -0700110 {
111 Parcel data, reply;
112 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700113 data.writeStrongBinder(display);
Mathias Agopiandf85c452010-09-29 13:02:36 -0700114 data.writeInt32(reqWidth);
115 data.writeInt32(reqHeight);
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800116 data.writeInt32(minLayerZ);
117 data.writeInt32(maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -0700118 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
Jamie Gennis582270d2011-08-17 18:19:00 -0700126 virtual bool authenticateSurfaceTexture(
127 const sp<ISurfaceTexture>& surfaceTexture) const
Jamie Gennis134f0422011-03-08 12:18:54 -0800128 {
129 Parcel data, reply;
130 int err = NO_ERROR;
131 err = data.writeInterfaceToken(
132 ISurfaceComposer::getInterfaceDescriptor());
133 if (err != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000134 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
Jamie Gennis134f0422011-03-08 12:18:54 -0800135 "interface descriptor: %s (%d)", strerror(-err), -err);
136 return false;
137 }
Jamie Gennis582270d2011-08-17 18:19:00 -0700138 err = data.writeStrongBinder(surfaceTexture->asBinder());
Jamie Gennis134f0422011-03-08 12:18:54 -0800139 if (err != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000140 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
Jamie Gennis582270d2011-08-17 18:19:00 -0700141 "strong binder to parcel: %s (%d)", strerror(-err), -err);
Jamie Gennis134f0422011-03-08 12:18:54 -0800142 return false;
143 }
144 err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
145 &reply);
146 if (err != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000147 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
Jamie Gennis582270d2011-08-17 18:19:00 -0700148 "performing transaction: %s (%d)", strerror(-err), -err);
Jamie Gennis134f0422011-03-08 12:18:54 -0800149 return false;
150 }
151 int32_t result = 0;
152 err = reply.readInt32(&result);
153 if (err != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000154 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
Jamie Gennis582270d2011-08-17 18:19:00 -0700155 "retrieving result: %s (%d)", strerror(-err), -err);
Jamie Gennis134f0422011-03-08 12:18:54 -0800156 return false;
157 }
158 return result != 0;
159 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800160
161 virtual sp<IDisplayEventConnection> createDisplayEventConnection()
162 {
163 Parcel data, reply;
164 sp<IDisplayEventConnection> result;
165 int err = data.writeInterfaceToken(
166 ISurfaceComposer::getInterfaceDescriptor());
167 if (err != NO_ERROR) {
168 return result;
169 }
170 err = remote()->transact(
171 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
172 data, &reply);
173 if (err != NO_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000174 ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800175 "transaction: %s (%d)", strerror(-err), -err);
176 return result;
177 }
178 result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
179 return result;
180 }
Colin Cross8e533062012-06-07 13:17:52 -0700181
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700182 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700183 {
184 Parcel data, reply;
185 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700186 data.writeString8(displayName);
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700187 data.writeInt32(secure ? 1 : 0);
Mathias Agopiane57f2922012-08-09 16:29:12 -0700188 remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
189 return reply.readStrongBinder();
190 }
191
192 virtual sp<IBinder> getBuiltInDisplay(int32_t id)
193 {
194 Parcel data, reply;
195 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
196 data.writeInt32(id);
197 remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
198 return reply.readStrongBinder();
199 }
200
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700201 virtual void blank(const sp<IBinder>& display)
Colin Cross8e533062012-06-07 13:17:52 -0700202 {
203 Parcel data, reply;
204 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700205 data.writeStrongBinder(display);
Colin Cross8e533062012-06-07 13:17:52 -0700206 remote()->transact(BnSurfaceComposer::BLANK, data, &reply);
207 }
208
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700209 virtual void unblank(const sp<IBinder>& display)
Colin Cross8e533062012-06-07 13:17:52 -0700210 {
211 Parcel data, reply;
212 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700213 data.writeStrongBinder(display);
Colin Cross8e533062012-06-07 13:17:52 -0700214 remote()->transact(BnSurfaceComposer::UNBLANK, data, &reply);
215 }
Mathias Agopian3094df32012-06-18 18:06:45 -0700216
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700217 virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info)
Mathias Agopianc666cae2012-07-25 18:56:13 -0700218 {
219 Parcel data, reply;
220 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700221 data.writeStrongBinder(display);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700222 remote()->transact(BnSurfaceComposer::GET_DISPLAY_INFO, data, &reply);
223 memcpy(info, reply.readInplace(sizeof(DisplayInfo)), sizeof(DisplayInfo));
224 return reply.readInt32();
225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226};
227
228IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
229
230// ----------------------------------------------------------------------
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232status_t BnSurfaceComposer::onTransact(
233 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
234{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 switch(code) {
236 case CREATE_CONNECTION: {
Mathias Agopian83c04462009-05-22 19:00:22 -0700237 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 sp<IBinder> b = createConnection()->asBinder();
239 reply->writeStrongBinder(b);
240 } break;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800241 case CREATE_GRAPHIC_BUFFER_ALLOC: {
242 CHECK_INTERFACE(ISurfaceComposer, data, reply);
243 sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
244 reply->writeStrongBinder(b);
245 } break;
Mathias Agopian698c0872011-06-28 19:09:31 -0700246 case SET_TRANSACTION_STATE: {
Mathias Agopian83c04462009-05-22 19:00:22 -0700247 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Mathias Agopian698c0872011-06-28 19:09:31 -0700248 size_t count = data.readInt32();
249 ComposerState s;
250 Vector<ComposerState> state;
251 state.setCapacity(count);
252 for (size_t i=0 ; i<count ; i++) {
253 s.read(data);
254 state.add(s);
255 }
Mathias Agopian8b33f032012-07-24 20:43:54 -0700256 count = data.readInt32();
257 DisplayState d;
258 Vector<DisplayState> displays;
259 displays.setCapacity(count);
260 for (size_t i=0 ; i<count ; i++) {
261 d.read(data);
262 displays.add(d);
263 }
Jamie Gennis28378392011-10-12 17:39:00 -0700264 uint32_t flags = data.readInt32();
Mathias Agopian8b33f032012-07-24 20:43:54 -0700265 setTransactionState(state, displays, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 } break;
267 case BOOT_FINISHED: {
Mathias Agopian83c04462009-05-22 19:00:22 -0700268 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 bootFinished();
270 } break;
Mathias Agopian1b0b30d2010-09-24 11:26:58 -0700271 case CAPTURE_SCREEN: {
272 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700273 sp<IBinder> display = data.readStrongBinder();
Mathias Agopiandf85c452010-09-29 13:02:36 -0700274 uint32_t reqWidth = data.readInt32();
275 uint32_t reqHeight = data.readInt32();
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800276 uint32_t minLayerZ = data.readInt32();
277 uint32_t maxLayerZ = data.readInt32();
Mathias Agopian1b0b30d2010-09-24 11:26:58 -0700278 sp<IMemoryHeap> heap;
279 uint32_t w, h;
280 PixelFormat f;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700281 status_t res = captureScreen(display, &heap, &w, &h, &f,
Mathias Agopianbf2c6a62010-12-10 16:22:31 -0800282 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian1b0b30d2010-09-24 11:26:58 -0700283 reply->writeStrongBinder(heap->asBinder());
284 reply->writeInt32(w);
285 reply->writeInt32(h);
286 reply->writeInt32(f);
287 reply->writeInt32(res);
288 } break;
Jamie Gennis134f0422011-03-08 12:18:54 -0800289 case AUTHENTICATE_SURFACE: {
290 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Jamie Gennis582270d2011-08-17 18:19:00 -0700291 sp<ISurfaceTexture> surfaceTexture =
292 interface_cast<ISurfaceTexture>(data.readStrongBinder());
293 int32_t result = authenticateSurfaceTexture(surfaceTexture) ? 1 : 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800294 reply->writeInt32(result);
295 } break;
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800296 case CREATE_DISPLAY_EVENT_CONNECTION: {
297 CHECK_INTERFACE(ISurfaceComposer, data, reply);
298 sp<IDisplayEventConnection> connection(createDisplayEventConnection());
299 reply->writeStrongBinder(connection->asBinder());
300 return NO_ERROR;
301 } break;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700302 case CREATE_DISPLAY: {
303 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700304 String8 displayName = data.readString8();
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700305 bool secure = bool(data.readInt32());
306 sp<IBinder> display(createDisplay(displayName, secure));
Mathias Agopiane57f2922012-08-09 16:29:12 -0700307 reply->writeStrongBinder(display);
308 return NO_ERROR;
309 } break;
310 case GET_BUILT_IN_DISPLAY: {
311 CHECK_INTERFACE(ISurfaceComposer, data, reply);
312 int32_t id = data.readInt32();
313 sp<IBinder> display(getBuiltInDisplay(id));
314 reply->writeStrongBinder(display);
315 return NO_ERROR;
316 } break;
Colin Cross8e533062012-06-07 13:17:52 -0700317 case BLANK: {
318 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700319 sp<IBinder> display = data.readStrongBinder();
320 blank(display);
Colin Cross8e533062012-06-07 13:17:52 -0700321 } break;
322 case UNBLANK: {
323 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700324 sp<IBinder> display = data.readStrongBinder();
325 unblank(display);
Colin Cross8e533062012-06-07 13:17:52 -0700326 } break;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700327 case GET_DISPLAY_INFO: {
328 CHECK_INTERFACE(ISurfaceComposer, data, reply);
329 DisplayInfo info;
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700330 sp<IBinder> display = data.readStrongBinder();
331 status_t result = getDisplayInfo(display, &info);
Mathias Agopianc666cae2012-07-25 18:56:13 -0700332 memcpy(reply->writeInplace(sizeof(DisplayInfo)), &info, sizeof(DisplayInfo));
333 reply->writeInt32(result);
334 } break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 default:
Mathias Agopian83c04462009-05-22 19:00:22 -0700336 return BBinder::onTransact(code, data, reply, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 }
338 return NO_ERROR;
339}
340
341// ----------------------------------------------------------------------------
342
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343};