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