2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // tag as surfaceflinger
18 #define LOG_TAG "SurfaceFlinger"
21 #include <sys/types.h>
23 #include <binder/Parcel.h>
24 #include <binder/IMemory.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
28 #include <gui/BitTube.h>
29 #include <gui/IDisplayEventConnection.h>
30 #include <gui/ISurfaceComposer.h>
31 #include <gui/IGraphicBufferProducer.h>
33 #include <private/gui/LayerState.h>
35 #include <ui/DisplayInfo.h>
37 #include <utils/Log.h>
39 // ---------------------------------------------------------------------------
43 class IDisplayEventConnection;
45 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
48 BpSurfaceComposer(const sp<IBinder>& impl)
49 : BpInterface<ISurfaceComposer>(impl)
53 virtual sp<ISurfaceComposerClient> createConnection()
57 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
58 remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
59 return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
62 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc()
66 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
67 remote()->transact(BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, data, &reply);
68 return interface_cast<IGraphicBufferAlloc>(reply.readStrongBinder());
71 virtual void setTransactionState(
72 const Vector<ComposerState>& state,
73 const Vector<DisplayState>& displays,
77 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
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 ) {
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 ) {
94 data.writeInt32(flags);
95 remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
98 virtual void bootFinished()
101 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
102 remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
105 virtual status_t captureScreen(const sp<IBinder>& display,
106 const sp<IGraphicBufferProducer>& producer,
107 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
108 uint32_t minLayerZ, uint32_t maxLayerZ,
109 bool useIdentityTransform)
112 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
113 data.writeStrongBinder(display);
114 data.writeStrongBinder(producer->asBinder());
115 data.write(sourceCrop);
116 data.writeInt32(reqWidth);
117 data.writeInt32(reqHeight);
118 data.writeInt32(minLayerZ);
119 data.writeInt32(maxLayerZ);
120 data.writeInt32(static_cast<int32_t>(useIdentityTransform));
121 remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
122 return reply.readInt32();
125 virtual bool authenticateSurfaceTexture(
126 const sp<IGraphicBufferProducer>& bufferProducer) const
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);
137 err = data.writeStrongBinder(bufferProducer->asBinder());
138 if (err != NO_ERROR) {
139 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
140 "strong binder to parcel: %s (%d)", strerror(-err), -err);
143 err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
145 if (err != NO_ERROR) {
146 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
147 "performing transaction: %s (%d)", strerror(-err), -err);
151 err = reply.readInt32(&result);
152 if (err != NO_ERROR) {
153 ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
154 "retrieving result: %s (%d)", strerror(-err), -err);
160 virtual sp<IDisplayEventConnection> createDisplayEventConnection()
163 sp<IDisplayEventConnection> result;
164 int err = data.writeInterfaceToken(
165 ISurfaceComposer::getInterfaceDescriptor());
166 if (err != NO_ERROR) {
169 err = remote()->transact(
170 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
172 if (err != NO_ERROR) {
173 ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
174 "transaction: %s (%d)", strerror(-err), -err);
177 result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
181 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
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();
191 virtual void destroyDisplay(const sp<IBinder>& display)
194 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
195 data.writeStrongBinder(display);
196 remote()->transact(BnSurfaceComposer::DESTROY_DISPLAY, data, &reply);
199 virtual sp<IBinder> getBuiltInDisplay(int32_t id)
202 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
204 remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
205 return reply.readStrongBinder();
208 virtual void setPowerMode(const sp<IBinder>& display, int mode)
211 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
212 data.writeStrongBinder(display);
213 data.writeInt32(mode);
214 remote()->transact(BnSurfaceComposer::SET_POWER_MODE, data, &reply);
217 virtual status_t getDisplayConfigs(const sp<IBinder>& display,
218 Vector<DisplayInfo>* configs)
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 = static_cast<size_t>(reply.readInt32());
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));
238 virtual int getActiveConfig(const sp<IBinder>& display)
241 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
242 data.writeStrongBinder(display);
243 remote()->transact(BnSurfaceComposer::GET_ACTIVE_CONFIG, data, &reply);
244 return reply.readInt32();
247 virtual status_t setActiveConfig(const sp<IBinder>& display, int id)
250 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
251 data.writeStrongBinder(display);
253 remote()->transact(BnSurfaceComposer::SET_ACTIVE_CONFIG, data, &reply);
254 return reply.readInt32();
257 virtual status_t clearAnimationFrameStats() {
259 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
260 remote()->transact(BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS, data, &reply);
261 return reply.readInt32();
264 virtual status_t getAnimationFrameStats(FrameStats* outStats) const {
266 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
267 remote()->transact(BnSurfaceComposer::GET_ANIMATION_FRAME_STATS, data, &reply);
268 reply.read(*outStats);
269 return reply.readInt32();
273 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
275 // ----------------------------------------------------------------------
277 status_t BnSurfaceComposer::onTransact(
278 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
281 case CREATE_CONNECTION: {
282 CHECK_INTERFACE(ISurfaceComposer, data, reply);
283 sp<IBinder> b = createConnection()->asBinder();
284 reply->writeStrongBinder(b);
287 case CREATE_GRAPHIC_BUFFER_ALLOC: {
288 CHECK_INTERFACE(ISurfaceComposer, data, reply);
289 sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
290 reply->writeStrongBinder(b);
293 case SET_TRANSACTION_STATE: {
294 CHECK_INTERFACE(ISurfaceComposer, data, reply);
295 size_t count = data.readInt32();
297 Vector<ComposerState> state;
298 state.setCapacity(count);
299 for (size_t i=0 ; i<count ; i++) {
303 count = data.readInt32();
305 Vector<DisplayState> displays;
306 displays.setCapacity(count);
307 for (size_t i=0 ; i<count ; i++) {
311 uint32_t flags = data.readInt32();
312 setTransactionState(state, displays, flags);
315 case BOOT_FINISHED: {
316 CHECK_INTERFACE(ISurfaceComposer, data, reply);
320 case CAPTURE_SCREEN: {
321 CHECK_INTERFACE(ISurfaceComposer, data, reply);
322 sp<IBinder> display = data.readStrongBinder();
323 sp<IGraphicBufferProducer> producer =
324 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
326 data.read(sourceCrop);
327 uint32_t reqWidth = data.readInt32();
328 uint32_t reqHeight = data.readInt32();
329 uint32_t minLayerZ = data.readInt32();
330 uint32_t maxLayerZ = data.readInt32();
331 bool useIdentityTransform = static_cast<bool>(data.readInt32());
333 status_t res = captureScreen(display, producer,
334 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
335 useIdentityTransform);
336 reply->writeInt32(res);
339 case AUTHENTICATE_SURFACE: {
340 CHECK_INTERFACE(ISurfaceComposer, data, reply);
341 sp<IGraphicBufferProducer> bufferProducer =
342 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
343 int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
344 reply->writeInt32(result);
347 case CREATE_DISPLAY_EVENT_CONNECTION: {
348 CHECK_INTERFACE(ISurfaceComposer, data, reply);
349 sp<IDisplayEventConnection> connection(createDisplayEventConnection());
350 reply->writeStrongBinder(connection->asBinder());
353 case CREATE_DISPLAY: {
354 CHECK_INTERFACE(ISurfaceComposer, data, reply);
355 String8 displayName = data.readString8();
356 bool secure = bool(data.readInt32());
357 sp<IBinder> display(createDisplay(displayName, secure));
358 reply->writeStrongBinder(display);
361 case DESTROY_DISPLAY: {
362 CHECK_INTERFACE(ISurfaceComposer, data, reply);
363 sp<IBinder> display = data.readStrongBinder();
364 destroyDisplay(display);
367 case GET_BUILT_IN_DISPLAY: {
368 CHECK_INTERFACE(ISurfaceComposer, data, reply);
369 int32_t id = data.readInt32();
370 sp<IBinder> display(getBuiltInDisplay(id));
371 reply->writeStrongBinder(display);
374 case GET_DISPLAY_CONFIGS: {
375 CHECK_INTERFACE(ISurfaceComposer, data, reply);
376 Vector<DisplayInfo> configs;
377 sp<IBinder> display = data.readStrongBinder();
378 status_t result = getDisplayConfigs(display, &configs);
379 reply->writeInt32(result);
380 if (result == NO_ERROR) {
381 reply->writeInt32(static_cast<int32_t>(configs.size()));
382 for (size_t c = 0; c < configs.size(); ++c) {
383 memcpy(reply->writeInplace(sizeof(DisplayInfo)),
384 &configs[c], sizeof(DisplayInfo));
389 case GET_ACTIVE_CONFIG: {
390 CHECK_INTERFACE(ISurfaceComposer, data, reply);
391 sp<IBinder> display = data.readStrongBinder();
392 int id = getActiveConfig(display);
393 reply->writeInt32(id);
396 case SET_ACTIVE_CONFIG: {
397 CHECK_INTERFACE(ISurfaceComposer, data, reply);
398 sp<IBinder> display = data.readStrongBinder();
399 int id = data.readInt32();
400 status_t result = setActiveConfig(display, id);
401 reply->writeInt32(result);
404 case CLEAR_ANIMATION_FRAME_STATS: {
405 CHECK_INTERFACE(ISurfaceComposer, data, reply);
406 status_t result = clearAnimationFrameStats();
407 reply->writeInt32(result);
410 case GET_ANIMATION_FRAME_STATS: {
411 CHECK_INTERFACE(ISurfaceComposer, data, reply);
413 status_t result = getAnimationFrameStats(&stats);
415 reply->writeInt32(result);
418 case SET_POWER_MODE: {
419 CHECK_INTERFACE(ISurfaceComposer, data, reply);
420 sp<IBinder> display = data.readStrongBinder();
421 int32_t mode = data.readInt32();
422 setPowerMode(display, mode);
426 return BBinder::onTransact(code, data, reply, flags);
429 // should be unreachable
433 // ----------------------------------------------------------------------------