2 * Copyright (C) 2010 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 #define LOG_TAG "Surface"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
21 #include <gui/Surface.h>
23 #include <android/native_window.h>
25 #include <utils/Log.h>
26 #include <utils/Trace.h>
27 #include <utils/NativeHandle.h>
29 #include <ui/DisplayStatInfo.h>
31 #include <ui/HdrCapabilities.h>
32 #include <ui/Region.h>
34 #include <gui/BufferItem.h>
35 #include <gui/IProducerListener.h>
37 #include <gui/ISurfaceComposer.h>
38 #include <private/gui/ComposerService.h>
40 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
41 #include <configstore/Utils.h>
46 const sp<IGraphicBufferProducer>& bufferProducer,
48 : mGraphicBufferProducer(bufferProducer),
49 mCrop(Rect::EMPTY_RECT),
51 mSharedBufferMode(false),
53 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
54 mSharedBufferHasBeenQueued(false),
55 mQueriedSupportedTimestamps(false),
56 mFrameTimestampsSupportsPresent(false),
57 mEnableFrameTimestamps(false),
58 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>())
60 // Initialize the ANativeWindow function pointers.
61 ANativeWindow::setSwapInterval = hook_setSwapInterval;
62 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
63 ANativeWindow::cancelBuffer = hook_cancelBuffer;
64 ANativeWindow::queueBuffer = hook_queueBuffer;
65 ANativeWindow::query = hook_query;
66 ANativeWindow::perform = hook_perform;
68 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
69 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
70 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
71 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
73 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
74 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
80 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
81 mDataSpace = HAL_DATASPACE_UNKNOWN;
82 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
90 mConsumerRunningBehind = false;
91 mConnectedToCpu = false;
92 mProducerControlledByApp = controlledByApp;
93 mSwapIntervalZero = false;
97 if (mConnectedToCpu) {
98 Surface::disconnect(NATIVE_WINDOW_API_CPU);
102 sp<ISurfaceComposer> Surface::composerService() const {
103 return ComposerService::getComposerService();
106 nsecs_t Surface::now() const {
110 sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
111 return mGraphicBufferProducer;
114 void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
115 mGraphicBufferProducer->setSidebandStream(stream);
118 void Surface::allocateBuffers() {
119 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
120 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
121 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
122 mReqFormat, mReqUsage);
125 status_t Surface::setGenerationNumber(uint32_t generation) {
126 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
127 if (result == NO_ERROR) {
128 mGenerationNumber = generation;
133 uint64_t Surface::getNextFrameNumber() const {
134 Mutex::Autolock lock(mMutex);
135 return mNextFrameNumber;
138 String8 Surface::getConsumerName() const {
139 return mGraphicBufferProducer->getConsumerName();
142 status_t Surface::setDequeueTimeout(nsecs_t timeout) {
143 return mGraphicBufferProducer->setDequeueTimeout(timeout);
146 status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
147 sp<Fence>* outFence, float outTransformMatrix[16]) {
148 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
152 status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
155 DisplayStatInfo stats;
156 status_t err = composerService()->getDisplayStats(NULL, &stats);
158 *outRefreshDuration = stats.vsyncPeriod;
163 void Surface::enableFrameTimestamps(bool enable) {
164 Mutex::Autolock lock(mMutex);
165 // If going from disabled to enabled, get the initial values for
166 // compositor and display timing.
167 if (!mEnableFrameTimestamps && enable) {
168 FrameEventHistoryDelta delta;
169 mGraphicBufferProducer->getFrameTimestamps(&delta);
170 mFrameEventHistory->applyDelta(delta);
172 mEnableFrameTimestamps = enable;
175 status_t Surface::getCompositorTiming(
176 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
177 nsecs_t* compositeToPresentLatency) {
178 Mutex::Autolock lock(mMutex);
179 if (!mEnableFrameTimestamps) {
180 return INVALID_OPERATION;
183 if (compositeDeadline != nullptr) {
185 mFrameEventHistory->getNextCompositeDeadline(now());
187 if (compositeInterval != nullptr) {
188 *compositeInterval = mFrameEventHistory->getCompositeInterval();
190 if (compositeToPresentLatency != nullptr) {
191 *compositeToPresentLatency =
192 mFrameEventHistory->getCompositeToPresentLatency();
197 static bool checkConsumerForUpdates(
198 const FrameEvents* e, const uint64_t lastFrameNumber,
199 const nsecs_t* outLatchTime,
200 const nsecs_t* outFirstRefreshStartTime,
201 const nsecs_t* outLastRefreshStartTime,
202 const nsecs_t* outGpuCompositionDoneTime,
203 const nsecs_t* outDisplayPresentTime,
204 const nsecs_t* outDequeueReadyTime,
205 const nsecs_t* outReleaseTime) {
206 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
207 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
208 !e->hasFirstRefreshStartInfo();
209 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
210 !e->hasGpuCompositionDoneInfo();
211 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
212 !e->hasDisplayPresentInfo();
214 // LastRefreshStart, DequeueReady, and Release are never available for the
216 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
217 !e->hasLastRefreshStartInfo() &&
218 (e->frameNumber != lastFrameNumber);
219 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
220 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
221 bool checkForRelease = (outReleaseTime != nullptr) &&
222 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
224 // RequestedPresent and Acquire info are always available producer-side.
225 return checkForLatch || checkForFirstRefreshStart ||
226 checkForLastRefreshStart || checkForGpuCompositionDone ||
227 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
230 static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
231 if (dst != nullptr) {
232 // We always get valid timestamps for these eventually.
233 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
234 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
238 static void getFrameTimestampFence(nsecs_t *dst,
239 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
240 if (dst != nullptr) {
241 if (!fenceShouldBeKnown) {
242 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
246 nsecs_t signalTime = src->getSignalTime();
247 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
248 NATIVE_WINDOW_TIMESTAMP_PENDING :
249 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
250 NATIVE_WINDOW_TIMESTAMP_INVALID :
255 status_t Surface::getFrameTimestamps(uint64_t frameNumber,
256 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
257 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
258 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
259 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
260 nsecs_t* outReleaseTime) {
263 Mutex::Autolock lock(mMutex);
265 if (!mEnableFrameTimestamps) {
266 return INVALID_OPERATION;
269 // Verify the requested timestamps are supported.
270 querySupportedTimestampsLocked();
271 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
275 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
276 if (events == nullptr) {
277 // If the entry isn't available in the producer, it's definitely not
278 // available in the consumer.
279 return NAME_NOT_FOUND;
282 // Update our cache of events if the requested events are not available.
283 if (checkConsumerForUpdates(events, mLastFrameNumber,
284 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
285 outGpuCompositionDoneTime, outDisplayPresentTime,
286 outDequeueReadyTime, outReleaseTime)) {
287 FrameEventHistoryDelta delta;
288 mGraphicBufferProducer->getFrameTimestamps(&delta);
289 mFrameEventHistory->applyDelta(delta);
290 events = mFrameEventHistory->getFrame(frameNumber);
293 if (events == nullptr) {
294 // The entry was available before the update, but was overwritten
295 // after the update. Make sure not to send the wrong frame's data.
296 return NAME_NOT_FOUND;
299 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
300 getFrameTimestamp(outLatchTime, events->latchTime);
301 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
302 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
303 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
305 getFrameTimestampFence(outAcquireTime, events->acquireFence,
306 events->hasAcquireInfo());
307 getFrameTimestampFence(outGpuCompositionDoneTime,
308 events->gpuCompositionDoneFence,
309 events->hasGpuCompositionDoneInfo());
310 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
311 events->hasDisplayPresentInfo());
312 getFrameTimestampFence(outReleaseTime, events->releaseFence,
313 events->hasReleaseInfo());
318 using namespace android::hardware::configstore;
319 using namespace android::hardware::configstore::V1_0;
321 status_t Surface::getWideColorSupport(bool* supported) {
325 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
326 Vector<android_color_mode_t> colorModes;
328 composerService()->getDisplayColorModes(display, &colorModes);
333 bool wideColorBoardConfig =
334 getBool<ISurfaceFlingerConfigs,
335 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
338 for (android_color_mode_t colorMode : colorModes) {
340 case HAL_COLOR_MODE_DISPLAY_P3:
341 case HAL_COLOR_MODE_ADOBE_RGB:
342 case HAL_COLOR_MODE_DCI_P3:
343 if (wideColorBoardConfig) {
355 status_t Surface::getHdrSupport(bool* supported) {
359 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
360 HdrCapabilities hdrCapabilities;
362 composerService()->getHdrCapabilities(display, &hdrCapabilities);
367 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
372 int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
373 Surface* c = getSelf(window);
374 return c->setSwapInterval(interval);
377 int Surface::hook_dequeueBuffer(ANativeWindow* window,
378 ANativeWindowBuffer** buffer, int* fenceFd) {
379 Surface* c = getSelf(window);
380 return c->dequeueBuffer(buffer, fenceFd);
383 int Surface::hook_cancelBuffer(ANativeWindow* window,
384 ANativeWindowBuffer* buffer, int fenceFd) {
385 Surface* c = getSelf(window);
386 return c->cancelBuffer(buffer, fenceFd);
389 int Surface::hook_queueBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->queueBuffer(buffer, fenceFd);
395 int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
396 ANativeWindowBuffer** buffer) {
397 Surface* c = getSelf(window);
398 ANativeWindowBuffer* buf;
400 int result = c->dequeueBuffer(&buf, &fenceFd);
404 sp<Fence> fence(new Fence(fenceFd));
405 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
406 if (waitResult != OK) {
407 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
409 c->cancelBuffer(buf, -1);
416 int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
417 ANativeWindowBuffer* buffer) {
418 Surface* c = getSelf(window);
419 return c->cancelBuffer(buffer, -1);
422 int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->lockBuffer_DEPRECATED(buffer);
428 int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->queueBuffer(buffer, -1);
434 int Surface::hook_query(const ANativeWindow* window,
435 int what, int* value) {
436 const Surface* c = getSelf(window);
437 return c->query(what, value);
440 int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
442 va_start(args, operation);
443 Surface* c = getSelf(window);
444 int result = c->perform(operation, args);
449 int Surface::setSwapInterval(int interval) {
451 // EGL specification states:
452 // interval is silently clamped to minimum and maximum implementation
453 // dependent values before being stored.
455 if (interval < minSwapInterval)
456 interval = minSwapInterval;
458 if (interval > maxSwapInterval)
459 interval = maxSwapInterval;
461 mSwapIntervalZero = (interval == 0);
462 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
467 int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
469 ALOGV("Surface::dequeueBuffer");
473 PixelFormat reqFormat;
475 bool enableFrameTimestamps;
478 Mutex::Autolock lock(mMutex);
479 if (mReportRemovedBuffers) {
480 mRemovedBuffers.clear();
483 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
484 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
486 reqFormat = mReqFormat;
487 reqUsage = mReqUsage;
489 enableFrameTimestamps = mEnableFrameTimestamps;
491 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
492 BufferItem::INVALID_BUFFER_SLOT) {
493 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
495 *buffer = gbuf.get();
500 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
504 nsecs_t now = systemTime();
506 FrameEventHistoryDelta frameTimestamps;
507 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence,
508 reqWidth, reqHeight, reqFormat, reqUsage,
509 enableFrameTimestamps ? &frameTimestamps : nullptr);
510 mLastDequeueDuration = systemTime() - now;
513 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
514 "(%d, %d, %d, %d) failed: %d", reqWidth, reqHeight, reqFormat,
519 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
520 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
521 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
522 return FAILED_TRANSACTION;
525 Mutex::Autolock lock(mMutex);
527 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
529 // this should never happen
530 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
532 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
536 if (enableFrameTimestamps) {
537 mFrameEventHistory->applyDelta(frameTimestamps);
540 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
541 if (mReportRemovedBuffers && (gbuf != nullptr)) {
542 mRemovedBuffers.push_back(gbuf);
544 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
545 if (result != NO_ERROR) {
546 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
547 mGraphicBufferProducer->cancelBuffer(buf, fence);
552 if (fence->isValid()) {
553 *fenceFd = fence->dup();
554 if (*fenceFd == -1) {
555 ALOGE("dequeueBuffer: error duping fence: %d", errno);
556 // dup() should never fail; something is badly wrong. Soldier on
557 // and hope for the best; the worst that should happen is some
558 // visible corruption that lasts until the next frame.
564 *buffer = gbuf.get();
566 if (mSharedBufferMode && mAutoRefresh) {
567 mSharedBufferSlot = buf;
568 mSharedBufferHasBeenQueued = false;
569 } else if (mSharedBufferSlot == buf) {
570 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
571 mSharedBufferHasBeenQueued = false;
577 int Surface::cancelBuffer(android_native_buffer_t* buffer,
580 ALOGV("Surface::cancelBuffer");
581 Mutex::Autolock lock(mMutex);
582 int i = getSlotFromBufferLocked(buffer);
589 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
595 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
596 mGraphicBufferProducer->cancelBuffer(i, fence);
598 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
599 mSharedBufferHasBeenQueued = true;
605 int Surface::getSlotFromBufferLocked(
606 android_native_buffer_t* buffer) const {
607 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
608 if (mSlots[i].buffer != NULL &&
609 mSlots[i].buffer->handle == buffer->handle) {
613 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
617 int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
618 ALOGV("Surface::lockBuffer");
619 Mutex::Autolock lock(mMutex);
623 int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
625 ALOGV("Surface::queueBuffer");
626 Mutex::Autolock lock(mMutex);
628 bool isAutoTimestamp = false;
630 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
631 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
632 isAutoTimestamp = true;
633 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
634 timestamp / 1000000.0);
636 timestamp = mTimestamp;
638 int i = getSlotFromBufferLocked(buffer);
645 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
653 // Make sure the crop rectangle is entirely inside the buffer.
654 Rect crop(Rect::EMPTY_RECT);
655 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
657 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
658 IGraphicBufferProducer::QueueBufferOutput output;
659 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
660 mDataSpace, crop, mScalingMode, mTransform ^ mStickyTransform,
661 fence, mStickyTransform, mEnableFrameTimestamps);
663 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
664 input.setSurfaceDamage(Region::INVALID_REGION);
666 // Here we do two things:
667 // 1) The surface damage was specified using the OpenGL ES convention of
668 // the origin being in the bottom-left corner. Here we flip to the
669 // convention that the rest of the system uses (top-left corner) by
670 // subtracting all top/bottom coordinates from the buffer height.
671 // 2) If the buffer is coming in rotated (for example, because the EGL
672 // implementation is reacting to the transform hint coming back from
673 // SurfaceFlinger), the surface damage needs to be rotated the
674 // opposite direction, since it was generated assuming an unrotated
675 // buffer (the app doesn't know that the EGL implementation is
676 // reacting to the transform hint behind its back). The
677 // transformations in the switch statement below apply those
678 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
680 int width = buffer->width;
681 int height = buffer->height;
682 bool rotated90 = (mTransform ^ mStickyTransform) &
683 NATIVE_WINDOW_TRANSFORM_ROT_90;
685 std::swap(width, height);
688 Region flippedRegion;
689 for (auto rect : mDirtyRegion) {
690 int left = rect.left;
691 int right = rect.right;
692 int top = height - rect.bottom; // Flip from OpenGL convention
693 int bottom = height - rect.top; // Flip from OpenGL convention
694 switch (mTransform ^ mStickyTransform) {
695 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
696 // Rotate 270 degrees
697 Rect flippedRect{top, width - right, bottom, width - left};
698 flippedRegion.orSelf(flippedRect);
701 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
702 // Rotate 180 degrees
703 Rect flippedRect{width - right, height - bottom,
704 width - left, height - top};
705 flippedRegion.orSelf(flippedRect);
708 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
710 Rect flippedRect{height - bottom, left,
711 height - top, right};
712 flippedRegion.orSelf(flippedRect);
716 Rect flippedRect{left, top, right, bottom};
717 flippedRegion.orSelf(flippedRect);
723 input.setSurfaceDamage(flippedRegion);
726 nsecs_t now = systemTime();
727 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
728 mLastQueueDuration = systemTime() - now;
730 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
733 if (mEnableFrameTimestamps) {
734 mFrameEventHistory->applyDelta(output.frameTimestamps);
735 // Update timestamps with the local acquire fence.
736 // The consumer doesn't send it back to prevent us from having two
737 // file descriptors of the same fence.
738 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
739 std::make_shared<FenceTime>(std::move(fence)));
741 // Cache timestamps of signaled fences so we can close their file
743 mFrameEventHistory->updateSignalTimes();
746 mLastFrameNumber = mNextFrameNumber;
748 mDefaultWidth = output.width;
749 mDefaultHeight = output.height;
750 mNextFrameNumber = output.nextFrameNumber;
752 // Disable transform hint if sticky transform is set.
753 if (mStickyTransform == 0) {
754 mTransformHint = output.transformHint;
757 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
759 if (!mConnectedToCpu) {
760 // Clear surface damage back to full-buffer
761 mDirtyRegion = Region::INVALID_REGION;
764 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
765 mSharedBufferHasBeenQueued = true;
768 mQueueBufferCondition.broadcast();
773 void Surface::querySupportedTimestampsLocked() const {
774 // mMutex must be locked when calling this method.
776 if (mQueriedSupportedTimestamps) {
779 mQueriedSupportedTimestamps = true;
781 std::vector<FrameEvent> supportedFrameTimestamps;
782 status_t err = composerService()->getSupportedFrameTimestamps(
783 &supportedFrameTimestamps);
785 if (err != NO_ERROR) {
789 for (auto sft : supportedFrameTimestamps) {
790 if (sft == FrameEvent::DISPLAY_PRESENT) {
791 mFrameTimestampsSupportsPresent = true;
796 int Surface::query(int what, int* value) const {
798 ALOGV("Surface::query");
799 { // scope for the lock
800 Mutex::Autolock lock(mMutex);
802 case NATIVE_WINDOW_FORMAT:
804 *value = static_cast<int>(mReqFormat);
808 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
809 if (composerService()->authenticateSurfaceTexture(
810 mGraphicBufferProducer)) {
817 case NATIVE_WINDOW_CONCRETE_TYPE:
818 *value = NATIVE_WINDOW_SURFACE;
820 case NATIVE_WINDOW_DEFAULT_WIDTH:
821 *value = static_cast<int>(
822 mUserWidth ? mUserWidth : mDefaultWidth);
824 case NATIVE_WINDOW_DEFAULT_HEIGHT:
825 *value = static_cast<int>(
826 mUserHeight ? mUserHeight : mDefaultHeight);
828 case NATIVE_WINDOW_TRANSFORM_HINT:
829 *value = static_cast<int>(mTransformHint);
831 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
832 status_t err = NO_ERROR;
833 if (!mConsumerRunningBehind) {
836 err = mGraphicBufferProducer->query(what, value);
837 if (err == NO_ERROR) {
838 mConsumerRunningBehind = *value;
843 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
844 int64_t durationUs = mLastDequeueDuration / 1000;
845 *value = durationUs > std::numeric_limits<int>::max() ?
846 std::numeric_limits<int>::max() :
847 static_cast<int>(durationUs);
850 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
851 int64_t durationUs = mLastQueueDuration / 1000;
852 *value = durationUs > std::numeric_limits<int>::max() ?
853 std::numeric_limits<int>::max() :
854 static_cast<int>(durationUs);
857 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
858 querySupportedTimestampsLocked();
859 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
862 case NATIVE_WINDOW_IS_VALID: {
863 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
868 return mGraphicBufferProducer->query(what, value);
871 int Surface::perform(int operation, va_list args)
875 case NATIVE_WINDOW_CONNECT:
876 // deprecated. must return NO_ERROR.
878 case NATIVE_WINDOW_DISCONNECT:
879 // deprecated. must return NO_ERROR.
881 case NATIVE_WINDOW_SET_USAGE:
882 res = dispatchSetUsage(args);
884 case NATIVE_WINDOW_SET_CROP:
885 res = dispatchSetCrop(args);
887 case NATIVE_WINDOW_SET_BUFFER_COUNT:
888 res = dispatchSetBufferCount(args);
890 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
891 res = dispatchSetBuffersGeometry(args);
893 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
894 res = dispatchSetBuffersTransform(args);
896 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
897 res = dispatchSetBuffersStickyTransform(args);
899 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
900 res = dispatchSetBuffersTimestamp(args);
902 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
903 res = dispatchSetBuffersDimensions(args);
905 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
906 res = dispatchSetBuffersUserDimensions(args);
908 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
909 res = dispatchSetBuffersFormat(args);
911 case NATIVE_WINDOW_LOCK:
912 res = dispatchLock(args);
914 case NATIVE_WINDOW_UNLOCK_AND_POST:
915 res = dispatchUnlockAndPost(args);
917 case NATIVE_WINDOW_SET_SCALING_MODE:
918 res = dispatchSetScalingMode(args);
920 case NATIVE_WINDOW_API_CONNECT:
921 res = dispatchConnect(args);
923 case NATIVE_WINDOW_API_DISCONNECT:
924 res = dispatchDisconnect(args);
926 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
927 res = dispatchSetSidebandStream(args);
929 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
930 res = dispatchSetBuffersDataSpace(args);
932 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
933 res = dispatchSetSurfaceDamage(args);
935 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
936 res = dispatchSetSharedBufferMode(args);
938 case NATIVE_WINDOW_SET_AUTO_REFRESH:
939 res = dispatchSetAutoRefresh(args);
941 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
942 res = dispatchGetDisplayRefreshCycleDuration(args);
944 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
945 res = dispatchGetNextFrameId(args);
947 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
948 res = dispatchEnableFrameTimestamps(args);
950 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
951 res = dispatchGetCompositorTiming(args);
953 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
954 res = dispatchGetFrameTimestamps(args);
956 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
957 res = dispatchGetWideColorSupport(args);
959 case NATIVE_WINDOW_GET_HDR_SUPPORT:
960 res = dispatchGetHdrSupport(args);
963 res = NAME_NOT_FOUND;
969 int Surface::dispatchConnect(va_list args) {
970 int api = va_arg(args, int);
974 int Surface::dispatchDisconnect(va_list args) {
975 int api = va_arg(args, int);
976 return disconnect(api);
979 int Surface::dispatchSetUsage(va_list args) {
980 int usage = va_arg(args, int);
981 return setUsage(static_cast<uint32_t>(usage));
984 int Surface::dispatchSetCrop(va_list args) {
985 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
986 return setCrop(reinterpret_cast<Rect const*>(rect));
989 int Surface::dispatchSetBufferCount(va_list args) {
990 size_t bufferCount = va_arg(args, size_t);
991 return setBufferCount(static_cast<int32_t>(bufferCount));
994 int Surface::dispatchSetBuffersGeometry(va_list args) {
995 uint32_t width = va_arg(args, uint32_t);
996 uint32_t height = va_arg(args, uint32_t);
997 PixelFormat format = va_arg(args, PixelFormat);
998 int err = setBuffersDimensions(width, height);
1002 return setBuffersFormat(format);
1005 int Surface::dispatchSetBuffersDimensions(va_list args) {
1006 uint32_t width = va_arg(args, uint32_t);
1007 uint32_t height = va_arg(args, uint32_t);
1008 return setBuffersDimensions(width, height);
1011 int Surface::dispatchSetBuffersUserDimensions(va_list args) {
1012 uint32_t width = va_arg(args, uint32_t);
1013 uint32_t height = va_arg(args, uint32_t);
1014 return setBuffersUserDimensions(width, height);
1017 int Surface::dispatchSetBuffersFormat(va_list args) {
1018 PixelFormat format = va_arg(args, PixelFormat);
1019 return setBuffersFormat(format);
1022 int Surface::dispatchSetScalingMode(va_list args) {
1023 int mode = va_arg(args, int);
1024 return setScalingMode(mode);
1027 int Surface::dispatchSetBuffersTransform(va_list args) {
1028 uint32_t transform = va_arg(args, uint32_t);
1029 return setBuffersTransform(transform);
1032 int Surface::dispatchSetBuffersStickyTransform(va_list args) {
1033 uint32_t transform = va_arg(args, uint32_t);
1034 return setBuffersStickyTransform(transform);
1037 int Surface::dispatchSetBuffersTimestamp(va_list args) {
1038 int64_t timestamp = va_arg(args, int64_t);
1039 return setBuffersTimestamp(timestamp);
1042 int Surface::dispatchLock(va_list args) {
1043 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1044 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1045 return lock(outBuffer, inOutDirtyBounds);
1048 int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
1049 return unlockAndPost();
1052 int Surface::dispatchSetSidebandStream(va_list args) {
1053 native_handle_t* sH = va_arg(args, native_handle_t*);
1054 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1055 setSidebandStream(sidebandHandle);
1059 int Surface::dispatchSetBuffersDataSpace(va_list args) {
1060 android_dataspace dataspace =
1061 static_cast<android_dataspace>(va_arg(args, int));
1062 return setBuffersDataSpace(dataspace);
1065 int Surface::dispatchSetSurfaceDamage(va_list args) {
1066 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1067 size_t numRects = va_arg(args, size_t);
1068 setSurfaceDamage(rects, numRects);
1072 int Surface::dispatchSetSharedBufferMode(va_list args) {
1073 bool sharedBufferMode = va_arg(args, int);
1074 return setSharedBufferMode(sharedBufferMode);
1077 int Surface::dispatchSetAutoRefresh(va_list args) {
1078 bool autoRefresh = va_arg(args, int);
1079 return setAutoRefresh(autoRefresh);
1082 int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1083 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1084 return getDisplayRefreshCycleDuration(outRefreshDuration);
1087 int Surface::dispatchGetNextFrameId(va_list args) {
1088 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1089 *nextFrameId = getNextFrameNumber();
1093 int Surface::dispatchEnableFrameTimestamps(va_list args) {
1094 bool enable = va_arg(args, int);
1095 enableFrameTimestamps(enable);
1099 int Surface::dispatchGetCompositorTiming(va_list args) {
1100 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1101 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1102 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1103 return getCompositorTiming(compositeDeadline, compositeInterval,
1104 compositeToPresentLatency);
1107 int Surface::dispatchGetFrameTimestamps(va_list args) {
1108 uint64_t frameId = va_arg(args, uint64_t);
1109 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
1110 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
1111 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1112 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1113 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
1114 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
1115 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
1116 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
1117 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
1118 return getFrameTimestamps(frameId,
1119 outRequestedPresentTime, outAcquireTime, outLatchTime,
1120 outFirstRefreshStartTime, outLastRefreshStartTime,
1121 outGpuCompositionDoneTime, outDisplayPresentTime,
1122 outDequeueReadyTime, outReleaseTime);
1125 int Surface::dispatchGetWideColorSupport(va_list args) {
1126 bool* outSupport = va_arg(args, bool*);
1127 return getWideColorSupport(outSupport);
1130 int Surface::dispatchGetHdrSupport(va_list args) {
1131 bool* outSupport = va_arg(args, bool*);
1132 return getHdrSupport(outSupport);
1135 int Surface::connect(int api) {
1136 static sp<IProducerListener> listener = new DummyProducerListener();
1137 return connect(api, listener);
1140 int Surface::connect(int api, const sp<IProducerListener>& listener) {
1141 return connect(api, listener, false);
1144 int Surface::connect(
1145 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
1147 ALOGV("Surface::connect");
1148 Mutex::Autolock lock(mMutex);
1149 IGraphicBufferProducer::QueueBufferOutput output;
1150 mReportRemovedBuffers = reportBufferRemoval;
1151 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
1152 if (err == NO_ERROR) {
1153 mDefaultWidth = output.width;
1154 mDefaultHeight = output.height;
1155 mNextFrameNumber = output.nextFrameNumber;
1157 // Disable transform hint if sticky transform is set.
1158 if (mStickyTransform == 0) {
1159 mTransformHint = output.transformHint;
1162 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
1164 if (!err && api == NATIVE_WINDOW_API_CPU) {
1165 mConnectedToCpu = true;
1166 // Clear the dirty region in case we're switching from a non-CPU API
1167 mDirtyRegion.clear();
1169 // Initialize the dirty region for tracking surface damage
1170 mDirtyRegion = Region::INVALID_REGION;
1177 int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
1179 ALOGV("Surface::disconnect");
1180 Mutex::Autolock lock(mMutex);
1181 mRemovedBuffers.clear();
1182 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1183 mSharedBufferHasBeenQueued = false;
1185 int err = mGraphicBufferProducer->disconnect(api, mode);
1192 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1194 mStickyTransform = 0;
1196 if (api == NATIVE_WINDOW_API_CPU) {
1197 mConnectedToCpu = false;
1203 int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
1204 sp<Fence>* outFence) {
1206 ALOGV("Surface::detachNextBuffer");
1208 if (outBuffer == NULL || outFence == NULL) {
1212 Mutex::Autolock lock(mMutex);
1213 if (mReportRemovedBuffers) {
1214 mRemovedBuffers.clear();
1217 sp<GraphicBuffer> buffer(NULL);
1218 sp<Fence> fence(NULL);
1219 status_t result = mGraphicBufferProducer->detachNextBuffer(
1221 if (result != NO_ERROR) {
1225 *outBuffer = buffer;
1226 if (fence != NULL && fence->isValid()) {
1229 *outFence = Fence::NO_FENCE;
1232 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1233 if (mSlots[i].buffer != NULL &&
1234 mSlots[i].buffer->handle == buffer->handle) {
1235 if (mReportRemovedBuffers) {
1236 mRemovedBuffers.push_back(mSlots[i].buffer);
1238 mSlots[i].buffer = NULL;
1245 int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1248 ALOGV("Surface::attachBuffer");
1250 Mutex::Autolock lock(mMutex);
1251 if (mReportRemovedBuffers) {
1252 mRemovedBuffers.clear();
1255 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
1256 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1257 graphicBuffer->mGenerationNumber = mGenerationNumber;
1258 int32_t attachedSlot = -1;
1259 status_t result = mGraphicBufferProducer->attachBuffer(
1260 &attachedSlot, graphicBuffer);
1261 if (result != NO_ERROR) {
1262 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
1263 graphicBuffer->mGenerationNumber = priorGeneration;
1266 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
1267 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1269 mSlots[attachedSlot].buffer = graphicBuffer;
1274 int Surface::setUsage(uint32_t reqUsage)
1276 ALOGV("Surface::setUsage");
1277 Mutex::Autolock lock(mMutex);
1278 if (reqUsage != mReqUsage) {
1279 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1281 mReqUsage = reqUsage;
1285 int Surface::setCrop(Rect const* rect)
1289 Rect realRect(Rect::EMPTY_RECT);
1290 if (rect == NULL || rect->isEmpty()) {
1296 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1297 realRect.left, realRect.top, realRect.right, realRect.bottom);
1299 Mutex::Autolock lock(mMutex);
1304 int Surface::setBufferCount(int bufferCount)
1307 ALOGV("Surface::setBufferCount");
1308 Mutex::Autolock lock(mMutex);
1310 status_t err = NO_ERROR;
1311 if (bufferCount == 0) {
1312 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1314 int minUndequeuedBuffers = 0;
1315 err = mGraphicBufferProducer->query(
1316 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1317 if (err == NO_ERROR) {
1318 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1319 bufferCount - minUndequeuedBuffers);
1323 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1324 bufferCount, strerror(-err));
1329 int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1331 ALOGV("Surface::setMaxDequeuedBufferCount");
1332 Mutex::Autolock lock(mMutex);
1334 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1335 maxDequeuedBuffers);
1336 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1337 "returned %s", maxDequeuedBuffers, strerror(-err));
1342 int Surface::setAsyncMode(bool async) {
1344 ALOGV("Surface::setAsyncMode");
1345 Mutex::Autolock lock(mMutex);
1347 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1348 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1349 async, strerror(-err));
1354 int Surface::setSharedBufferMode(bool sharedBufferMode) {
1356 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
1357 Mutex::Autolock lock(mMutex);
1359 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1361 if (err == NO_ERROR) {
1362 mSharedBufferMode = sharedBufferMode;
1364 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1365 "%s", sharedBufferMode, strerror(-err));
1370 int Surface::setAutoRefresh(bool autoRefresh) {
1372 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1373 Mutex::Autolock lock(mMutex);
1375 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1376 if (err == NO_ERROR) {
1377 mAutoRefresh = autoRefresh;
1379 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1380 autoRefresh, strerror(-err));
1384 int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
1387 ALOGV("Surface::setBuffersDimensions");
1389 if ((width && !height) || (!width && height))
1392 Mutex::Autolock lock(mMutex);
1393 if (width != mReqWidth || height != mReqHeight) {
1394 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1397 mReqHeight = height;
1401 int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
1404 ALOGV("Surface::setBuffersUserDimensions");
1406 if ((width && !height) || (!width && height))
1409 Mutex::Autolock lock(mMutex);
1410 if (width != mUserWidth || height != mUserHeight) {
1411 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1414 mUserHeight = height;
1418 int Surface::setBuffersFormat(PixelFormat format)
1420 ALOGV("Surface::setBuffersFormat");
1422 Mutex::Autolock lock(mMutex);
1423 if (format != mReqFormat) {
1424 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1426 mReqFormat = format;
1430 int Surface::setScalingMode(int mode)
1433 ALOGV("Surface::setScalingMode(%d)", mode);
1436 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1437 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1438 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1439 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1442 ALOGE("unknown scaling mode: %d", mode);
1446 Mutex::Autolock lock(mMutex);
1447 mScalingMode = mode;
1451 int Surface::setBuffersTransform(uint32_t transform)
1454 ALOGV("Surface::setBuffersTransform");
1455 Mutex::Autolock lock(mMutex);
1456 mTransform = transform;
1460 int Surface::setBuffersStickyTransform(uint32_t transform)
1463 ALOGV("Surface::setBuffersStickyTransform");
1464 Mutex::Autolock lock(mMutex);
1465 mStickyTransform = transform;
1469 int Surface::setBuffersTimestamp(int64_t timestamp)
1471 ALOGV("Surface::setBuffersTimestamp");
1472 Mutex::Autolock lock(mMutex);
1473 mTimestamp = timestamp;
1477 int Surface::setBuffersDataSpace(android_dataspace dataSpace)
1479 ALOGV("Surface::setBuffersDataSpace");
1480 Mutex::Autolock lock(mMutex);
1481 mDataSpace = dataSpace;
1485 void Surface::freeAllBuffers() {
1486 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1487 mSlots[i].buffer = 0;
1491 void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1493 ALOGV("Surface::setSurfaceDamage");
1494 Mutex::Autolock lock(mMutex);
1496 if (mConnectedToCpu || numRects == 0) {
1497 mDirtyRegion = Region::INVALID_REGION;
1501 mDirtyRegion.clear();
1502 for (size_t r = 0; r < numRects; ++r) {
1503 // We intentionally flip top and bottom here, since because they're
1504 // specified with a bottom-left origin, top > bottom, which fails
1505 // validation in the Region class. We will fix this up when we flip to a
1506 // top-left origin in queueBuffer.
1507 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1508 mDirtyRegion.orSelf(rect);
1512 // ----------------------------------------------------------------------
1513 // the lock/unlock APIs must be used from the same thread
1515 static status_t copyBlt(
1516 const sp<GraphicBuffer>& dst,
1517 const sp<GraphicBuffer>& src,
1521 // src and dst with, height and format must be identical. no verification
1524 uint8_t* src_bits = NULL;
1525 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1526 reinterpret_cast<void**>(&src_bits));
1527 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1529 uint8_t* dst_bits = NULL;
1530 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1531 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
1532 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1535 Region::const_iterator head(reg.begin());
1536 Region::const_iterator tail(reg.end());
1537 if (head != tail && src_bits && dst_bits) {
1538 const size_t bpp = bytesPerPixel(src->format);
1539 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1540 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
1542 while (head != tail) {
1543 const Rect& r(*head++);
1544 int32_t h = r.height();
1545 if (h <= 0) continue;
1546 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1547 uint8_t const * s = src_bits +
1548 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1549 uint8_t * d = dst_bits +
1550 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
1551 if (dbpr==sbpr && size==sbpr) {
1552 size *= static_cast<size_t>(h);
1567 dst->unlockAsync(dstFenceFd);
1572 // ----------------------------------------------------------------------------
1574 status_t Surface::lock(
1575 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1577 if (mLockedBuffer != 0) {
1578 ALOGE("Surface::lock failed, already locked");
1579 return INVALID_OPERATION;
1582 if (!mConnectedToCpu) {
1583 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1587 // we're intending to do software rendering from this point
1588 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1591 ANativeWindowBuffer* out;
1593 status_t err = dequeueBuffer(&out, &fenceFd);
1594 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1595 if (err == NO_ERROR) {
1596 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
1597 const Rect bounds(backBuffer->width, backBuffer->height);
1599 Region newDirtyRegion;
1600 if (inOutDirtyBounds) {
1601 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1602 newDirtyRegion.andSelf(bounds);
1604 newDirtyRegion.set(bounds);
1607 // figure out if we can copy the frontbuffer back
1608 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1609 const bool canCopyBack = (frontBuffer != 0 &&
1610 backBuffer->width == frontBuffer->width &&
1611 backBuffer->height == frontBuffer->height &&
1612 backBuffer->format == frontBuffer->format);
1615 // copy the area that is invalid and not repainted this round
1616 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
1617 if (!copyback.isEmpty()) {
1618 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
1621 // if we can't copy-back anything, modify the user's dirty
1622 // region to make sure they redraw the whole buffer
1623 newDirtyRegion.set(bounds);
1624 mDirtyRegion.clear();
1625 Mutex::Autolock lock(mMutex);
1626 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1627 mSlots[i].dirtyRegion.clear();
1632 { // scope for the lock
1633 Mutex::Autolock lock(mMutex);
1634 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1635 if (backBufferSlot >= 0) {
1636 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1637 mDirtyRegion.subtract(dirtyRegion);
1638 dirtyRegion = newDirtyRegion;
1642 mDirtyRegion.orSelf(newDirtyRegion);
1643 if (inOutDirtyBounds) {
1644 *inOutDirtyBounds = newDirtyRegion.getBounds();
1648 status_t res = backBuffer->lockAsync(
1649 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
1650 newDirtyRegion.bounds(), &vaddr, fenceFd);
1652 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1653 backBuffer->handle);
1656 err = INVALID_OPERATION;
1658 mLockedBuffer = backBuffer;
1659 outBuffer->width = backBuffer->width;
1660 outBuffer->height = backBuffer->height;
1661 outBuffer->stride = backBuffer->stride;
1662 outBuffer->format = backBuffer->format;
1663 outBuffer->bits = vaddr;
1669 status_t Surface::unlockAndPost()
1671 if (mLockedBuffer == 0) {
1672 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1673 return INVALID_OPERATION;
1677 status_t err = mLockedBuffer->unlockAsync(&fd);
1678 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1680 err = queueBuffer(mLockedBuffer.get(), fd);
1681 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1682 mLockedBuffer->handle, strerror(-err));
1684 mPostedBuffer = mLockedBuffer;
1689 bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1690 Mutex::Autolock lock(mMutex);
1691 if (mNextFrameNumber > lastFrame) {
1694 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1697 status_t Surface::getUniqueId(uint64_t* outId) const {
1698 Mutex::Autolock lock(mMutex);
1699 return mGraphicBufferProducer->getUniqueId(outId);
1702 status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1703 if (out == nullptr) {
1704 ALOGE("%s: out must not be null!", __FUNCTION__);
1708 Mutex::Autolock lock(mMutex);
1709 *out = mRemovedBuffers;
1710 mRemovedBuffers.clear();
1714 }; // namespace android