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