1 /* 2 * Copyright (C) 2010 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 #define LOG_TAG "Surface" 18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS 19 //#define LOG_NDEBUG 0 20 21 #include <android/native_window.h> 22 23 #include <binder/Parcel.h> 24 25 #include <utils/Log.h> 26 #include <utils/Trace.h> 27 #include <utils/NativeHandle.h> 28 29 #include <ui/Fence.h> 30 #include <ui/Region.h> 31 32 #include <gui/IProducerListener.h> 33 #include <gui/ISurfaceComposer.h> 34 #include <gui/SurfaceComposerClient.h> 35 #include <gui/GLConsumer.h> 36 #include <gui/Surface.h> 37 38 #include <private/gui/ComposerService.h> 39 40 namespace android { 41 42 Surface::Surface( 43 const sp<IGraphicBufferProducer>& bufferProducer, 44 bool controlledByApp) 45 : mGraphicBufferProducer(bufferProducer), 46 mGenerationNumber(0) 47 { 48 // Initialize the ANativeWindow function pointers. 49 ANativeWindow::setSwapInterval = hook_setSwapInterval; 50 ANativeWindow::dequeueBuffer = hook_dequeueBuffer; 51 ANativeWindow::cancelBuffer = hook_cancelBuffer; 52 ANativeWindow::queueBuffer = hook_queueBuffer; 53 ANativeWindow::query = hook_query; 54 ANativeWindow::perform = hook_perform; 55 56 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; 57 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED; 58 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED; 59 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED; 60 61 const_cast<int&>(ANativeWindow::minSwapInterval) = 0; 62 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; 63 64 mReqWidth = 0; 65 mReqHeight = 0; 66 mReqFormat = 0; 67 mReqUsage = 0; 68 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; 69 mDataSpace = HAL_DATASPACE_UNKNOWN; 70 mCrop.clear(); 71 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; 72 mTransform = 0; 73 mStickyTransform = 0; 74 mDefaultWidth = 0; 75 mDefaultHeight = 0; 76 mUserWidth = 0; 77 mUserHeight = 0; 78 mTransformHint = 0; 79 mConsumerRunningBehind = false; 80 mConnectedToCpu = false; 81 mProducerControlledByApp = controlledByApp; 82 mSwapIntervalZero = false; 83 } 84 85 Surface::~Surface() { 86 if (mConnectedToCpu) { 87 Surface::disconnect(NATIVE_WINDOW_API_CPU); 88 } 89 } 90 91 sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { 92 return mGraphicBufferProducer; 93 } 94 95 void Surface::setSidebandStream(const sp<NativeHandle>& stream) { 96 mGraphicBufferProducer->setSidebandStream(stream); 97 } 98 99 void Surface::allocateBuffers() { 100 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; 101 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; 102 mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, reqWidth, 103 reqHeight, mReqFormat, mReqUsage); 104 } 105 106 status_t Surface::setGenerationNumber(uint32_t generation) { 107 status_t result = mGraphicBufferProducer->setGenerationNumber(generation); 108 if (result == NO_ERROR) { 109 mGenerationNumber = generation; 110 } 111 return result; 112 } 113 114 String8 Surface::getConsumerName() const { 115 return mGraphicBufferProducer->getConsumerName(); 116 } 117 118 int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { 119 Surface* c = getSelf(window); 120 return c->setSwapInterval(interval); 121 } 122 123 int Surface::hook_dequeueBuffer(ANativeWindow* window, 124 ANativeWindowBuffer** buffer, int* fenceFd) { 125 Surface* c = getSelf(window); 126 return c->dequeueBuffer(buffer, fenceFd); 127 } 128 129 int Surface::hook_cancelBuffer(ANativeWindow* window, 130 ANativeWindowBuffer* buffer, int fenceFd) { 131 Surface* c = getSelf(window); 132 return c->cancelBuffer(buffer, fenceFd); 133 } 134 135 int Surface::hook_queueBuffer(ANativeWindow* window, 136 ANativeWindowBuffer* buffer, int fenceFd) { 137 Surface* c = getSelf(window); 138 return c->queueBuffer(buffer, fenceFd); 139 } 140 141 int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, 142 ANativeWindowBuffer** buffer) { 143 Surface* c = getSelf(window); 144 ANativeWindowBuffer* buf; 145 int fenceFd = -1; 146 int result = c->dequeueBuffer(&buf, &fenceFd); 147 sp<Fence> fence(new Fence(fenceFd)); 148 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); 149 if (waitResult != OK) { 150 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", 151 waitResult); 152 c->cancelBuffer(buf, -1); 153 return waitResult; 154 } 155 *buffer = buf; 156 return result; 157 } 158 159 int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, 160 ANativeWindowBuffer* buffer) { 161 Surface* c = getSelf(window); 162 return c->cancelBuffer(buffer, -1); 163 } 164 165 int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, 166 ANativeWindowBuffer* buffer) { 167 Surface* c = getSelf(window); 168 return c->lockBuffer_DEPRECATED(buffer); 169 } 170 171 int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, 172 ANativeWindowBuffer* buffer) { 173 Surface* c = getSelf(window); 174 return c->queueBuffer(buffer, -1); 175 } 176 177 int Surface::hook_query(const ANativeWindow* window, 178 int what, int* value) { 179 const Surface* c = getSelf(window); 180 return c->query(what, value); 181 } 182 183 int Surface::hook_perform(ANativeWindow* window, int operation, ...) { 184 va_list args; 185 va_start(args, operation); 186 Surface* c = getSelf(window); 187 return c->perform(operation, args); 188 } 189 190 int Surface::setSwapInterval(int interval) { 191 ATRACE_CALL(); 192 // EGL specification states: 193 // interval is silently clamped to minimum and maximum implementation 194 // dependent values before being stored. 195 196 if (interval < minSwapInterval) 197 interval = minSwapInterval; 198 199 if (interval > maxSwapInterval) 200 interval = maxSwapInterval; 201 202 mSwapIntervalZero = (interval == 0); 203 204 return NO_ERROR; 205 } 206 207 int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { 208 ATRACE_CALL(); 209 ALOGV("Surface::dequeueBuffer"); 210 211 uint32_t reqWidth; 212 uint32_t reqHeight; 213 bool swapIntervalZero; 214 PixelFormat reqFormat; 215 uint32_t reqUsage; 216 217 { 218 Mutex::Autolock lock(mMutex); 219 220 reqWidth = mReqWidth ? mReqWidth : mUserWidth; 221 reqHeight = mReqHeight ? mReqHeight : mUserHeight; 222 223 swapIntervalZero = mSwapIntervalZero; 224 reqFormat = mReqFormat; 225 reqUsage = mReqUsage; 226 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer 227 228 int buf = -1; 229 sp<Fence> fence; 230 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero, 231 reqWidth, reqHeight, reqFormat, reqUsage); 232 233 if (result < 0) { 234 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)" 235 "failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat, 236 reqUsage, result); 237 return result; 238 } 239 240 Mutex::Autolock lock(mMutex); 241 242 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); 243 244 // this should never happen 245 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); 246 247 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { 248 freeAllBuffers(); 249 } 250 251 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { 252 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); 253 if (result != NO_ERROR) { 254 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); 255 mGraphicBufferProducer->cancelBuffer(buf, fence); 256 return result; 257 } 258 } 259 260 if (fence->isValid()) { 261 *fenceFd = fence->dup(); 262 if (*fenceFd == -1) { 263 ALOGE("dequeueBuffer: error duping fence: %d", errno); 264 // dup() should never fail; something is badly wrong. Soldier on 265 // and hope for the best; the worst that should happen is some 266 // visible corruption that lasts until the next frame. 267 } 268 } else { 269 *fenceFd = -1; 270 } 271 272 *buffer = gbuf.get(); 273 return OK; 274 } 275 276 int Surface::cancelBuffer(android_native_buffer_t* buffer, 277 int fenceFd) { 278 ATRACE_CALL(); 279 ALOGV("Surface::cancelBuffer"); 280 Mutex::Autolock lock(mMutex); 281 int i = getSlotFromBufferLocked(buffer); 282 if (i < 0) { 283 if (fenceFd >= 0) { 284 close(fenceFd); 285 } 286 return i; 287 } 288 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); 289 mGraphicBufferProducer->cancelBuffer(i, fence); 290 return OK; 291 } 292 293 int Surface::getSlotFromBufferLocked( 294 android_native_buffer_t* buffer) const { 295 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { 296 if (mSlots[i].buffer != NULL && 297 mSlots[i].buffer->handle == buffer->handle) { 298 return i; 299 } 300 } 301 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); 302 return BAD_VALUE; 303 } 304 305 int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { 306 ALOGV("Surface::lockBuffer"); 307 Mutex::Autolock lock(mMutex); 308 return OK; 309 } 310 311 int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { 312 ATRACE_CALL(); 313 ALOGV("Surface::queueBuffer"); 314 Mutex::Autolock lock(mMutex); 315 int64_t timestamp; 316 bool isAutoTimestamp = false; 317 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { 318 timestamp = systemTime(SYSTEM_TIME_MONOTONIC); 319 isAutoTimestamp = true; 320 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", 321 timestamp / 1000000.f); 322 } else { 323 timestamp = mTimestamp; 324 } 325 int i = getSlotFromBufferLocked(buffer); 326 if (i < 0) { 327 if (fenceFd >= 0) { 328 close(fenceFd); 329 } 330 return i; 331 } 332 333 334 // Make sure the crop rectangle is entirely inside the buffer. 335 Rect crop; 336 mCrop.intersect(Rect(buffer->width, buffer->height), &crop); 337 338 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); 339 IGraphicBufferProducer::QueueBufferOutput output; 340 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, 341 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform, 342 mSwapIntervalZero, fence, mStickyTransform); 343 344 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { 345 input.setSurfaceDamage(Region::INVALID_REGION); 346 } else { 347 // Here we do two things: 348 // 1) The surface damage was specified using the OpenGL ES convention of 349 // the origin being in the bottom-left corner. Here we flip to the 350 // convention that the rest of the system uses (top-left corner) by 351 // subtracting all top/bottom coordinates from the buffer height. 352 // 2) If the buffer is coming in rotated (for example, because the EGL 353 // implementation is reacting to the transform hint coming back from 354 // SurfaceFlinger), the surface damage needs to be rotated the 355 // opposite direction, since it was generated assuming an unrotated 356 // buffer (the app doesn't know that the EGL implementation is 357 // reacting to the transform hint behind its back). The 358 // transformations in the switch statement below apply those 359 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees). 360 361 int width = buffer->width; 362 int height = buffer->height; 363 bool rotated90 = (mTransform ^ mStickyTransform) & 364 NATIVE_WINDOW_TRANSFORM_ROT_90; 365 if (rotated90) { 366 std::swap(width, height); 367 } 368 369 Region flippedRegion; 370 for (auto rect : mDirtyRegion) { 371 int left = rect.left; 372 int right = rect.right; 373 int top = height - rect.bottom; // Flip from OpenGL convention 374 int bottom = height - rect.top; // Flip from OpenGL convention 375 switch (mTransform ^ mStickyTransform) { 376 case NATIVE_WINDOW_TRANSFORM_ROT_90: { 377 // Rotate 270 degrees 378 Rect flippedRect{top, width - right, bottom, width - left}; 379 flippedRegion.orSelf(flippedRect); 380 break; 381 } 382 case NATIVE_WINDOW_TRANSFORM_ROT_180: { 383 // Rotate 180 degrees 384 Rect flippedRect{width - right, height - bottom, 385 width - left, height - top}; 386 flippedRegion.orSelf(flippedRect); 387 break; 388 } 389 case NATIVE_WINDOW_TRANSFORM_ROT_270: { 390 // Rotate 90 degrees 391 Rect flippedRect{height - bottom, left, 392 height - top, right}; 393 flippedRegion.orSelf(flippedRect); 394 break; 395 } 396 default: { 397 Rect flippedRect{left, top, right, bottom}; 398 flippedRegion.orSelf(flippedRect); 399 break; 400 } 401 } 402 } 403 404 input.setSurfaceDamage(flippedRegion); 405 } 406 407 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); 408 if (err != OK) { 409 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); 410 } 411 uint32_t numPendingBuffers = 0; 412 uint32_t hint = 0; 413 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, 414 &numPendingBuffers); 415 416 // Disable transform hint if sticky transform is set. 417 if (mStickyTransform == 0) { 418 mTransformHint = hint; 419 } 420 421 mConsumerRunningBehind = (numPendingBuffers >= 2); 422 423 if (!mConnectedToCpu) { 424 // Clear surface damage back to full-buffer 425 mDirtyRegion = Region::INVALID_REGION; 426 } 427 428 return err; 429 } 430 431 int Surface::query(int what, int* value) const { 432 ATRACE_CALL(); 433 ALOGV("Surface::query"); 434 { // scope for the lock 435 Mutex::Autolock lock(mMutex); 436 switch (what) { 437 case NATIVE_WINDOW_FORMAT: 438 if (mReqFormat) { 439 *value = static_cast<int>(mReqFormat); 440 return NO_ERROR; 441 } 442 break; 443 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { 444 sp<ISurfaceComposer> composer( 445 ComposerService::getComposerService()); 446 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) { 447 *value = 1; 448 } else { 449 *value = 0; 450 } 451 return NO_ERROR; 452 } 453 case NATIVE_WINDOW_CONCRETE_TYPE: 454 *value = NATIVE_WINDOW_SURFACE; 455 return NO_ERROR; 456 case NATIVE_WINDOW_DEFAULT_WIDTH: 457 *value = static_cast<int>( 458 mUserWidth ? mUserWidth : mDefaultWidth); 459 return NO_ERROR; 460 case NATIVE_WINDOW_DEFAULT_HEIGHT: 461 *value = static_cast<int>( 462 mUserHeight ? mUserHeight : mDefaultHeight); 463 return NO_ERROR; 464 case NATIVE_WINDOW_TRANSFORM_HINT: 465 *value = static_cast<int>(mTransformHint); 466 return NO_ERROR; 467 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { 468 status_t err = NO_ERROR; 469 if (!mConsumerRunningBehind) { 470 *value = 0; 471 } else { 472 err = mGraphicBufferProducer->query(what, value); 473 if (err == NO_ERROR) { 474 mConsumerRunningBehind = *value; 475 } 476 } 477 return err; 478 } 479 } 480 } 481 return mGraphicBufferProducer->query(what, value); 482 } 483 484 int Surface::perform(int operation, va_list args) 485 { 486 int res = NO_ERROR; 487 switch (operation) { 488 case NATIVE_WINDOW_CONNECT: 489 // deprecated. must return NO_ERROR. 490 break; 491 case NATIVE_WINDOW_DISCONNECT: 492 // deprecated. must return NO_ERROR. 493 break; 494 case NATIVE_WINDOW_SET_USAGE: 495 res = dispatchSetUsage(args); 496 break; 497 case NATIVE_WINDOW_SET_CROP: 498 res = dispatchSetCrop(args); 499 break; 500 case NATIVE_WINDOW_SET_BUFFER_COUNT: 501 res = dispatchSetBufferCount(args); 502 break; 503 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: 504 res = dispatchSetBuffersGeometry(args); 505 break; 506 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: 507 res = dispatchSetBuffersTransform(args); 508 break; 509 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: 510 res = dispatchSetBuffersStickyTransform(args); 511 break; 512 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: 513 res = dispatchSetBuffersTimestamp(args); 514 break; 515 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: 516 res = dispatchSetBuffersDimensions(args); 517 break; 518 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: 519 res = dispatchSetBuffersUserDimensions(args); 520 break; 521 case NATIVE_WINDOW_SET_BUFFERS_FORMAT: 522 res = dispatchSetBuffersFormat(args); 523 break; 524 case NATIVE_WINDOW_LOCK: 525 res = dispatchLock(args); 526 break; 527 case NATIVE_WINDOW_UNLOCK_AND_POST: 528 res = dispatchUnlockAndPost(args); 529 break; 530 case NATIVE_WINDOW_SET_SCALING_MODE: 531 res = dispatchSetScalingMode(args); 532 break; 533 case NATIVE_WINDOW_API_CONNECT: 534 res = dispatchConnect(args); 535 break; 536 case NATIVE_WINDOW_API_DISCONNECT: 537 res = dispatchDisconnect(args); 538 break; 539 case NATIVE_WINDOW_SET_SIDEBAND_STREAM: 540 res = dispatchSetSidebandStream(args); 541 break; 542 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: 543 res = dispatchSetBuffersDataSpace(args); 544 break; 545 case NATIVE_WINDOW_SET_SURFACE_DAMAGE: 546 res = dispatchSetSurfaceDamage(args); 547 break; 548 default: 549 res = NAME_NOT_FOUND; 550 break; 551 } 552 return res; 553 } 554 555 int Surface::dispatchConnect(va_list args) { 556 int api = va_arg(args, int); 557 return connect(api); 558 } 559 560 int Surface::dispatchDisconnect(va_list args) { 561 int api = va_arg(args, int); 562 return disconnect(api); 563 } 564 565 int Surface::dispatchSetUsage(va_list args) { 566 int usage = va_arg(args, int); 567 return setUsage(static_cast<uint32_t>(usage)); 568 } 569 570 int Surface::dispatchSetCrop(va_list args) { 571 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); 572 return setCrop(reinterpret_cast<Rect const*>(rect)); 573 } 574 575 int Surface::dispatchSetBufferCount(va_list args) { 576 size_t bufferCount = va_arg(args, size_t); 577 return setBufferCount(static_cast<int32_t>(bufferCount)); 578 } 579 580 int Surface::dispatchSetBuffersGeometry(va_list args) { 581 uint32_t width = va_arg(args, uint32_t); 582 uint32_t height = va_arg(args, uint32_t); 583 PixelFormat format = va_arg(args, PixelFormat); 584 int err = setBuffersDimensions(width, height); 585 if (err != 0) { 586 return err; 587 } 588 return setBuffersFormat(format); 589 } 590 591 int Surface::dispatchSetBuffersDimensions(va_list args) { 592 uint32_t width = va_arg(args, uint32_t); 593 uint32_t height = va_arg(args, uint32_t); 594 return setBuffersDimensions(width, height); 595 } 596 597 int Surface::dispatchSetBuffersUserDimensions(va_list args) { 598 uint32_t width = va_arg(args, uint32_t); 599 uint32_t height = va_arg(args, uint32_t); 600 return setBuffersUserDimensions(width, height); 601 } 602 603 int Surface::dispatchSetBuffersFormat(va_list args) { 604 PixelFormat format = va_arg(args, PixelFormat); 605 return setBuffersFormat(format); 606 } 607 608 int Surface::dispatchSetScalingMode(va_list args) { 609 int mode = va_arg(args, int); 610 return setScalingMode(mode); 611 } 612 613 int Surface::dispatchSetBuffersTransform(va_list args) { 614 uint32_t transform = va_arg(args, uint32_t); 615 return setBuffersTransform(transform); 616 } 617 618 int Surface::dispatchSetBuffersStickyTransform(va_list args) { 619 uint32_t transform = va_arg(args, uint32_t); 620 return setBuffersStickyTransform(transform); 621 } 622 623 int Surface::dispatchSetBuffersTimestamp(va_list args) { 624 int64_t timestamp = va_arg(args, int64_t); 625 return setBuffersTimestamp(timestamp); 626 } 627 628 int Surface::dispatchLock(va_list args) { 629 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); 630 ARect* inOutDirtyBounds = va_arg(args, ARect*); 631 return lock(outBuffer, inOutDirtyBounds); 632 } 633 634 int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { 635 return unlockAndPost(); 636 } 637 638 int Surface::dispatchSetSidebandStream(va_list args) { 639 native_handle_t* sH = va_arg(args, native_handle_t*); 640 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); 641 setSidebandStream(sidebandHandle); 642 return OK; 643 } 644 645 int Surface::dispatchSetBuffersDataSpace(va_list args) { 646 android_dataspace dataspace = 647 static_cast<android_dataspace>(va_arg(args, int)); 648 return setBuffersDataSpace(dataspace); 649 } 650 651 int Surface::dispatchSetSurfaceDamage(va_list args) { 652 android_native_rect_t* rects = va_arg(args, android_native_rect_t*); 653 size_t numRects = va_arg(args, size_t); 654 setSurfaceDamage(rects, numRects); 655 return NO_ERROR; 656 } 657 658 int Surface::connect(int api) { 659 static sp<IProducerListener> listener = new DummyProducerListener(); 660 return connect(api, listener); 661 } 662 663 int Surface::connect(int api, const sp<IProducerListener>& listener) { 664 ATRACE_CALL(); 665 ALOGV("Surface::connect"); 666 Mutex::Autolock lock(mMutex); 667 IGraphicBufferProducer::QueueBufferOutput output; 668 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); 669 if (err == NO_ERROR) { 670 uint32_t numPendingBuffers = 0; 671 uint32_t hint = 0; 672 output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, 673 &numPendingBuffers); 674 675 // Disable transform hint if sticky transform is set. 676 if (mStickyTransform == 0) { 677 mTransformHint = hint; 678 } 679 680 mConsumerRunningBehind = (numPendingBuffers >= 2); 681 } 682 if (!err && api == NATIVE_WINDOW_API_CPU) { 683 mConnectedToCpu = true; 684 // Clear the dirty region in case we're switching from a non-CPU API 685 mDirtyRegion.clear(); 686 } else if (!err) { 687 // Initialize the dirty region for tracking surface damage 688 mDirtyRegion = Region::INVALID_REGION; 689 } 690 691 return err; 692 } 693 694 695 int Surface::disconnect(int api) { 696 ATRACE_CALL(); 697 ALOGV("Surface::disconnect"); 698 Mutex::Autolock lock(mMutex); 699 freeAllBuffers(); 700 int err = mGraphicBufferProducer->disconnect(api); 701 if (!err) { 702 mReqFormat = 0; 703 mReqWidth = 0; 704 mReqHeight = 0; 705 mReqUsage = 0; 706 mCrop.clear(); 707 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; 708 mTransform = 0; 709 mStickyTransform = 0; 710 711 if (api == NATIVE_WINDOW_API_CPU) { 712 mConnectedToCpu = false; 713 } 714 } 715 return err; 716 } 717 718 int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, 719 sp<Fence>* outFence) { 720 ATRACE_CALL(); 721 ALOGV("Surface::detachNextBuffer"); 722 723 if (outBuffer == NULL || outFence == NULL) { 724 return BAD_VALUE; 725 } 726 727 Mutex::Autolock lock(mMutex); 728 729 sp<GraphicBuffer> buffer(NULL); 730 sp<Fence> fence(NULL); 731 status_t result = mGraphicBufferProducer->detachNextBuffer( 732 &buffer, &fence); 733 if (result != NO_ERROR) { 734 return result; 735 } 736 737 *outBuffer = buffer; 738 if (fence != NULL && fence->isValid()) { 739 *outFence = fence; 740 } else { 741 *outFence = Fence::NO_FENCE; 742 } 743 744 return NO_ERROR; 745 } 746 747 int Surface::attachBuffer(ANativeWindowBuffer* buffer) 748 { 749 ATRACE_CALL(); 750 ALOGV("Surface::attachBuffer"); 751 752 Mutex::Autolock lock(mMutex); 753 754 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); 755 uint32_t priorGeneration = graphicBuffer->mGenerationNumber; 756 graphicBuffer->mGenerationNumber = mGenerationNumber; 757 int32_t attachedSlot = -1; 758 status_t result = mGraphicBufferProducer->attachBuffer( 759 &attachedSlot, graphicBuffer); 760 if (result != NO_ERROR) { 761 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); 762 graphicBuffer->mGenerationNumber = priorGeneration; 763 return result; 764 } 765 mSlots[attachedSlot].buffer = graphicBuffer; 766 767 return NO_ERROR; 768 } 769 770 int Surface::setUsage(uint32_t reqUsage) 771 { 772 ALOGV("Surface::setUsage"); 773 Mutex::Autolock lock(mMutex); 774 mReqUsage = reqUsage; 775 return OK; 776 } 777 778 int Surface::setCrop(Rect const* rect) 779 { 780 ATRACE_CALL(); 781 782 Rect realRect; 783 if (rect == NULL || rect->isEmpty()) { 784 realRect.clear(); 785 } else { 786 realRect = *rect; 787 } 788 789 ALOGV("Surface::setCrop rect=[%d %d %d %d]", 790 realRect.left, realRect.top, realRect.right, realRect.bottom); 791 792 Mutex::Autolock lock(mMutex); 793 mCrop = realRect; 794 return NO_ERROR; 795 } 796 797 int Surface::setBufferCount(int bufferCount) 798 { 799 ATRACE_CALL(); 800 ALOGV("Surface::setBufferCount"); 801 Mutex::Autolock lock(mMutex); 802 803 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount); 804 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", 805 bufferCount, strerror(-err)); 806 807 if (err == NO_ERROR) { 808 freeAllBuffers(); 809 } 810 811 return err; 812 } 813 814 int Surface::setBuffersDimensions(uint32_t width, uint32_t height) 815 { 816 ATRACE_CALL(); 817 ALOGV("Surface::setBuffersDimensions"); 818 819 if ((width && !height) || (!width && height)) 820 return BAD_VALUE; 821 822 Mutex::Autolock lock(mMutex); 823 mReqWidth = width; 824 mReqHeight = height; 825 return NO_ERROR; 826 } 827 828 int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) 829 { 830 ATRACE_CALL(); 831 ALOGV("Surface::setBuffersUserDimensions"); 832 833 if ((width && !height) || (!width && height)) 834 return BAD_VALUE; 835 836 Mutex::Autolock lock(mMutex); 837 mUserWidth = width; 838 mUserHeight = height; 839 return NO_ERROR; 840 } 841 842 int Surface::setBuffersFormat(PixelFormat format) 843 { 844 ALOGV("Surface::setBuffersFormat"); 845 846 Mutex::Autolock lock(mMutex); 847 mReqFormat = format; 848 return NO_ERROR; 849 } 850 851 int Surface::setScalingMode(int mode) 852 { 853 ATRACE_CALL(); 854 ALOGV("Surface::setScalingMode(%d)", mode); 855 856 switch (mode) { 857 case NATIVE_WINDOW_SCALING_MODE_FREEZE: 858 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: 859 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: 860 break; 861 default: 862 ALOGE("unknown scaling mode: %d", mode); 863 return BAD_VALUE; 864 } 865 866 Mutex::Autolock lock(mMutex); 867 mScalingMode = mode; 868 return NO_ERROR; 869 } 870 871 int Surface::setBuffersTransform(uint32_t transform) 872 { 873 ATRACE_CALL(); 874 ALOGV("Surface::setBuffersTransform"); 875 Mutex::Autolock lock(mMutex); 876 mTransform = transform; 877 return NO_ERROR; 878 } 879 880 int Surface::setBuffersStickyTransform(uint32_t transform) 881 { 882 ATRACE_CALL(); 883 ALOGV("Surface::setBuffersStickyTransform"); 884 Mutex::Autolock lock(mMutex); 885 mStickyTransform = transform; 886 return NO_ERROR; 887 } 888 889 int Surface::setBuffersTimestamp(int64_t timestamp) 890 { 891 ALOGV("Surface::setBuffersTimestamp"); 892 Mutex::Autolock lock(mMutex); 893 mTimestamp = timestamp; 894 return NO_ERROR; 895 } 896 897 int Surface::setBuffersDataSpace(android_dataspace dataSpace) 898 { 899 ALOGV("Surface::setBuffersDataSpace"); 900 Mutex::Autolock lock(mMutex); 901 mDataSpace = dataSpace; 902 return NO_ERROR; 903 } 904 905 void Surface::freeAllBuffers() { 906 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { 907 mSlots[i].buffer = 0; 908 } 909 } 910 911 void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) { 912 ATRACE_CALL(); 913 ALOGV("Surface::setSurfaceDamage"); 914 Mutex::Autolock lock(mMutex); 915 916 if (mConnectedToCpu || numRects == 0) { 917 mDirtyRegion = Region::INVALID_REGION; 918 return; 919 } 920 921 mDirtyRegion.clear(); 922 for (size_t r = 0; r < numRects; ++r) { 923 // We intentionally flip top and bottom here, since because they're 924 // specified with a bottom-left origin, top > bottom, which fails 925 // validation in the Region class. We will fix this up when we flip to a 926 // top-left origin in queueBuffer. 927 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top); 928 mDirtyRegion.orSelf(rect); 929 } 930 } 931 932 // ---------------------------------------------------------------------- 933 // the lock/unlock APIs must be used from the same thread 934 935 static status_t copyBlt( 936 const sp<GraphicBuffer>& dst, 937 const sp<GraphicBuffer>& src, 938 const Region& reg) 939 { 940 // src and dst with, height and format must be identical. no verification 941 // is done here. 942 status_t err; 943 uint8_t* src_bits = NULL; 944 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), 945 reinterpret_cast<void**>(&src_bits)); 946 ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); 947 948 uint8_t* dst_bits = NULL; 949 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), 950 reinterpret_cast<void**>(&dst_bits)); 951 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); 952 953 Region::const_iterator head(reg.begin()); 954 Region::const_iterator tail(reg.end()); 955 if (head != tail && src_bits && dst_bits) { 956 const size_t bpp = bytesPerPixel(src->format); 957 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; 958 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; 959 960 while (head != tail) { 961 const Rect& r(*head++); 962 int32_t h = r.height(); 963 if (h <= 0) continue; 964 size_t size = static_cast<uint32_t>(r.width()) * bpp; 965 uint8_t const * s = src_bits + 966 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; 967 uint8_t * d = dst_bits + 968 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; 969 if (dbpr==sbpr && size==sbpr) { 970 size *= static_cast<size_t>(h); 971 h = 1; 972 } 973 do { 974 memcpy(d, s, size); 975 d += dbpr; 976 s += sbpr; 977 } while (--h > 0); 978 } 979 } 980 981 if (src_bits) 982 src->unlock(); 983 984 if (dst_bits) 985 dst->unlock(); 986 987 return err; 988 } 989 990 // ---------------------------------------------------------------------------- 991 992 status_t Surface::lock( 993 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) 994 { 995 if (mLockedBuffer != 0) { 996 ALOGE("Surface::lock failed, already locked"); 997 return INVALID_OPERATION; 998 } 999 1000 if (!mConnectedToCpu) { 1001 int err = Surface::connect(NATIVE_WINDOW_API_CPU); 1002 if (err) { 1003 return err; 1004 } 1005 // we're intending to do software rendering from this point 1006 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); 1007 } 1008 1009 ANativeWindowBuffer* out; 1010 int fenceFd = -1; 1011 status_t err = dequeueBuffer(&out, &fenceFd); 1012 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); 1013 if (err == NO_ERROR) { 1014 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); 1015 const Rect bounds(backBuffer->width, backBuffer->height); 1016 1017 Region newDirtyRegion; 1018 if (inOutDirtyBounds) { 1019 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); 1020 newDirtyRegion.andSelf(bounds); 1021 } else { 1022 newDirtyRegion.set(bounds); 1023 } 1024 1025 // figure out if we can copy the frontbuffer back 1026 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); 1027 const bool canCopyBack = (frontBuffer != 0 && 1028 backBuffer->width == frontBuffer->width && 1029 backBuffer->height == frontBuffer->height && 1030 backBuffer->format == frontBuffer->format); 1031 1032 if (canCopyBack) { 1033 // copy the area that is invalid and not repainted this round 1034 const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); 1035 if (!copyback.isEmpty()) 1036 copyBlt(backBuffer, frontBuffer, copyback); 1037 } else { 1038 // if we can't copy-back anything, modify the user's dirty 1039 // region to make sure they redraw the whole buffer 1040 newDirtyRegion.set(bounds); 1041 mDirtyRegion.clear(); 1042 Mutex::Autolock lock(mMutex); 1043 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { 1044 mSlots[i].dirtyRegion.clear(); 1045 } 1046 } 1047 1048 1049 { // scope for the lock 1050 Mutex::Autolock lock(mMutex); 1051 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); 1052 if (backBufferSlot >= 0) { 1053 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); 1054 mDirtyRegion.subtract(dirtyRegion); 1055 dirtyRegion = newDirtyRegion; 1056 } 1057 } 1058 1059 mDirtyRegion.orSelf(newDirtyRegion); 1060 if (inOutDirtyBounds) { 1061 *inOutDirtyBounds = newDirtyRegion.getBounds(); 1062 } 1063 1064 void* vaddr; 1065 status_t res = backBuffer->lockAsync( 1066 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, 1067 newDirtyRegion.bounds(), &vaddr, fenceFd); 1068 1069 ALOGW_IF(res, "failed locking buffer (handle = %p)", 1070 backBuffer->handle); 1071 1072 if (res != 0) { 1073 err = INVALID_OPERATION; 1074 } else { 1075 mLockedBuffer = backBuffer; 1076 outBuffer->width = backBuffer->width; 1077 outBuffer->height = backBuffer->height; 1078 outBuffer->stride = backBuffer->stride; 1079 outBuffer->format = backBuffer->format; 1080 outBuffer->bits = vaddr; 1081 } 1082 } 1083 return err; 1084 } 1085 1086 status_t Surface::unlockAndPost() 1087 { 1088 if (mLockedBuffer == 0) { 1089 ALOGE("Surface::unlockAndPost failed, no locked buffer"); 1090 return INVALID_OPERATION; 1091 } 1092 1093 int fd = -1; 1094 status_t err = mLockedBuffer->unlockAsync(&fd); 1095 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); 1096 1097 err = queueBuffer(mLockedBuffer.get(), fd); 1098 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", 1099 mLockedBuffer->handle, strerror(-err)); 1100 1101 mPostedBuffer = mLockedBuffer; 1102 mLockedBuffer = 0; 1103 return err; 1104 } 1105 1106 }; // namespace android 1107