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 <gui/Surface.h>
22
23 #include <inttypes.h>
24
25 #include <android/native_window.h>
26
27 #include <utils/Log.h>
28 #include <utils/Trace.h>
29 #include <utils/NativeHandle.h>
30
31 #include <ui/DisplayStatInfo.h>
32 #include <ui/Fence.h>
33 #include <ui/HdrCapabilities.h>
34 #include <ui/Region.h>
35
36 #include <gui/BufferItem.h>
37 #include <gui/IProducerListener.h>
38
39 #include <gui/ISurfaceComposer.h>
40 #include <private/gui/ComposerService.h>
41
42 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
43 #include <configstore/Utils.h>
44
45 namespace android {
46
47 using ui::ColorMode;
48 using ui::Dataspace;
49
Surface(const sp<IGraphicBufferProducer> & bufferProducer,bool controlledByApp)50 Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp)
51 : mGraphicBufferProducer(bufferProducer),
52 mCrop(Rect::EMPTY_RECT),
53 mBufferAge(0),
54 mGenerationNumber(0),
55 mSharedBufferMode(false),
56 mAutoRefresh(false),
57 mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
58 mSharedBufferHasBeenQueued(false),
59 mQueriedSupportedTimestamps(false),
60 mFrameTimestampsSupportsPresent(false),
61 mEnableFrameTimestamps(false),
62 mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
63 // Initialize the ANativeWindow function pointers.
64 ANativeWindow::setSwapInterval = hook_setSwapInterval;
65 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
66 ANativeWindow::cancelBuffer = hook_cancelBuffer;
67 ANativeWindow::queueBuffer = hook_queueBuffer;
68 ANativeWindow::query = hook_query;
69 ANativeWindow::perform = hook_perform;
70
71 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
72 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
73 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
74 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
75
76 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
77 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
78
79 mReqWidth = 0;
80 mReqHeight = 0;
81 mReqFormat = 0;
82 mReqUsage = 0;
83 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
84 mDataSpace = Dataspace::UNKNOWN;
85 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
86 mTransform = 0;
87 mStickyTransform = 0;
88 mDefaultWidth = 0;
89 mDefaultHeight = 0;
90 mUserWidth = 0;
91 mUserHeight = 0;
92 mTransformHint = 0;
93 mConsumerRunningBehind = false;
94 mConnectedToCpu = false;
95 mProducerControlledByApp = controlledByApp;
96 mSwapIntervalZero = false;
97 }
98
~Surface()99 Surface::~Surface() {
100 if (mConnectedToCpu) {
101 Surface::disconnect(NATIVE_WINDOW_API_CPU);
102 }
103 }
104
composerService() const105 sp<ISurfaceComposer> Surface::composerService() const {
106 return ComposerService::getComposerService();
107 }
108
now() const109 nsecs_t Surface::now() const {
110 return systemTime();
111 }
112
getIGraphicBufferProducer() const113 sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
114 return mGraphicBufferProducer;
115 }
116
setSidebandStream(const sp<NativeHandle> & stream)117 void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
118 mGraphicBufferProducer->setSidebandStream(stream);
119 }
120
allocateBuffers()121 void Surface::allocateBuffers() {
122 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
123 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
124 mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
125 mReqFormat, mReqUsage);
126 }
127
setGenerationNumber(uint32_t generation)128 status_t Surface::setGenerationNumber(uint32_t generation) {
129 status_t result = mGraphicBufferProducer->setGenerationNumber(generation);
130 if (result == NO_ERROR) {
131 mGenerationNumber = generation;
132 }
133 return result;
134 }
135
getNextFrameNumber() const136 uint64_t Surface::getNextFrameNumber() const {
137 Mutex::Autolock lock(mMutex);
138 return mNextFrameNumber;
139 }
140
getConsumerName() const141 String8 Surface::getConsumerName() const {
142 return mGraphicBufferProducer->getConsumerName();
143 }
144
setDequeueTimeout(nsecs_t timeout)145 status_t Surface::setDequeueTimeout(nsecs_t timeout) {
146 return mGraphicBufferProducer->setDequeueTimeout(timeout);
147 }
148
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])149 status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
150 sp<Fence>* outFence, float outTransformMatrix[16]) {
151 return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence,
152 outTransformMatrix);
153 }
154
getDisplayRefreshCycleDuration(nsecs_t * outRefreshDuration)155 status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) {
156 ATRACE_CALL();
157
158 DisplayStatInfo stats;
159 status_t result = composerService()->getDisplayStats(NULL, &stats);
160 if (result != NO_ERROR) {
161 return result;
162 }
163
164 *outRefreshDuration = stats.vsyncPeriod;
165
166 return NO_ERROR;
167 }
168
enableFrameTimestamps(bool enable)169 void Surface::enableFrameTimestamps(bool enable) {
170 Mutex::Autolock lock(mMutex);
171 // If going from disabled to enabled, get the initial values for
172 // compositor and display timing.
173 if (!mEnableFrameTimestamps && enable) {
174 FrameEventHistoryDelta delta;
175 mGraphicBufferProducer->getFrameTimestamps(&delta);
176 mFrameEventHistory->applyDelta(delta);
177 }
178 mEnableFrameTimestamps = enable;
179 }
180
getCompositorTiming(nsecs_t * compositeDeadline,nsecs_t * compositeInterval,nsecs_t * compositeToPresentLatency)181 status_t Surface::getCompositorTiming(
182 nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
183 nsecs_t* compositeToPresentLatency) {
184 Mutex::Autolock lock(mMutex);
185 if (!mEnableFrameTimestamps) {
186 return INVALID_OPERATION;
187 }
188
189 if (compositeDeadline != nullptr) {
190 *compositeDeadline =
191 mFrameEventHistory->getNextCompositeDeadline(now());
192 }
193 if (compositeInterval != nullptr) {
194 *compositeInterval = mFrameEventHistory->getCompositeInterval();
195 }
196 if (compositeToPresentLatency != nullptr) {
197 *compositeToPresentLatency =
198 mFrameEventHistory->getCompositeToPresentLatency();
199 }
200 return NO_ERROR;
201 }
202
checkConsumerForUpdates(const FrameEvents * e,const uint64_t lastFrameNumber,const nsecs_t * outLatchTime,const nsecs_t * outFirstRefreshStartTime,const nsecs_t * outLastRefreshStartTime,const nsecs_t * outGpuCompositionDoneTime,const nsecs_t * outDisplayPresentTime,const nsecs_t * outDequeueReadyTime,const nsecs_t * outReleaseTime)203 static bool checkConsumerForUpdates(
204 const FrameEvents* e, const uint64_t lastFrameNumber,
205 const nsecs_t* outLatchTime,
206 const nsecs_t* outFirstRefreshStartTime,
207 const nsecs_t* outLastRefreshStartTime,
208 const nsecs_t* outGpuCompositionDoneTime,
209 const nsecs_t* outDisplayPresentTime,
210 const nsecs_t* outDequeueReadyTime,
211 const nsecs_t* outReleaseTime) {
212 bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo();
213 bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) &&
214 !e->hasFirstRefreshStartInfo();
215 bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) &&
216 !e->hasGpuCompositionDoneInfo();
217 bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) &&
218 !e->hasDisplayPresentInfo();
219
220 // LastRefreshStart, DequeueReady, and Release are never available for the
221 // last frame.
222 bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) &&
223 !e->hasLastRefreshStartInfo() &&
224 (e->frameNumber != lastFrameNumber);
225 bool checkForDequeueReady = (outDequeueReadyTime != nullptr) &&
226 !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber);
227 bool checkForRelease = (outReleaseTime != nullptr) &&
228 !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber);
229
230 // RequestedPresent and Acquire info are always available producer-side.
231 return checkForLatch || checkForFirstRefreshStart ||
232 checkForLastRefreshStart || checkForGpuCompositionDone ||
233 checkForDisplayPresent || checkForDequeueReady || checkForRelease;
234 }
235
getFrameTimestamp(nsecs_t * dst,const nsecs_t & src)236 static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) {
237 if (dst != nullptr) {
238 // We always get valid timestamps for these eventually.
239 *dst = (src == FrameEvents::TIMESTAMP_PENDING) ?
240 NATIVE_WINDOW_TIMESTAMP_PENDING : src;
241 }
242 }
243
getFrameTimestampFence(nsecs_t * dst,const std::shared_ptr<FenceTime> & src,bool fenceShouldBeKnown)244 static void getFrameTimestampFence(nsecs_t *dst,
245 const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) {
246 if (dst != nullptr) {
247 if (!fenceShouldBeKnown) {
248 *dst = NATIVE_WINDOW_TIMESTAMP_PENDING;
249 return;
250 }
251
252 nsecs_t signalTime = src->getSignalTime();
253 *dst = (signalTime == Fence::SIGNAL_TIME_PENDING) ?
254 NATIVE_WINDOW_TIMESTAMP_PENDING :
255 (signalTime == Fence::SIGNAL_TIME_INVALID) ?
256 NATIVE_WINDOW_TIMESTAMP_INVALID :
257 signalTime;
258 }
259 }
260
getFrameTimestamps(uint64_t frameNumber,nsecs_t * outRequestedPresentTime,nsecs_t * outAcquireTime,nsecs_t * outLatchTime,nsecs_t * outFirstRefreshStartTime,nsecs_t * outLastRefreshStartTime,nsecs_t * outGpuCompositionDoneTime,nsecs_t * outDisplayPresentTime,nsecs_t * outDequeueReadyTime,nsecs_t * outReleaseTime)261 status_t Surface::getFrameTimestamps(uint64_t frameNumber,
262 nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
263 nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
264 nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime,
265 nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
266 nsecs_t* outReleaseTime) {
267 ATRACE_CALL();
268
269 Mutex::Autolock lock(mMutex);
270
271 if (!mEnableFrameTimestamps) {
272 return INVALID_OPERATION;
273 }
274
275 // Verify the requested timestamps are supported.
276 querySupportedTimestampsLocked();
277 if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) {
278 return BAD_VALUE;
279 }
280
281 FrameEvents* events = mFrameEventHistory->getFrame(frameNumber);
282 if (events == nullptr) {
283 // If the entry isn't available in the producer, it's definitely not
284 // available in the consumer.
285 return NAME_NOT_FOUND;
286 }
287
288 // Update our cache of events if the requested events are not available.
289 if (checkConsumerForUpdates(events, mLastFrameNumber,
290 outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime,
291 outGpuCompositionDoneTime, outDisplayPresentTime,
292 outDequeueReadyTime, outReleaseTime)) {
293 FrameEventHistoryDelta delta;
294 mGraphicBufferProducer->getFrameTimestamps(&delta);
295 mFrameEventHistory->applyDelta(delta);
296 events = mFrameEventHistory->getFrame(frameNumber);
297 }
298
299 if (events == nullptr) {
300 // The entry was available before the update, but was overwritten
301 // after the update. Make sure not to send the wrong frame's data.
302 return NAME_NOT_FOUND;
303 }
304
305 getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime);
306 getFrameTimestamp(outLatchTime, events->latchTime);
307 getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime);
308 getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime);
309 getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime);
310
311 getFrameTimestampFence(outAcquireTime, events->acquireFence,
312 events->hasAcquireInfo());
313 getFrameTimestampFence(outGpuCompositionDoneTime,
314 events->gpuCompositionDoneFence,
315 events->hasGpuCompositionDoneInfo());
316 getFrameTimestampFence(outDisplayPresentTime, events->displayPresentFence,
317 events->hasDisplayPresentInfo());
318 getFrameTimestampFence(outReleaseTime, events->releaseFence,
319 events->hasReleaseInfo());
320
321 return NO_ERROR;
322 }
323
324 using namespace android::hardware::configstore;
325 using namespace android::hardware::configstore::V1_0;
326
getWideColorSupport(bool * supported)327 status_t Surface::getWideColorSupport(bool* supported) {
328 ATRACE_CALL();
329
330 sp<IBinder> display(
331 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
332 Vector<ColorMode> colorModes;
333 status_t err =
334 composerService()->getDisplayColorModes(display, &colorModes);
335
336 if (err)
337 return err;
338
339 bool wideColorBoardConfig =
340 getBool<ISurfaceFlingerConfigs,
341 &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
342
343 *supported = false;
344 for (ColorMode colorMode : colorModes) {
345 switch (colorMode) {
346 case ColorMode::DISPLAY_P3:
347 case ColorMode::ADOBE_RGB:
348 case ColorMode::DCI_P3:
349 if (wideColorBoardConfig) {
350 *supported = true;
351 }
352 break;
353 default:
354 break;
355 }
356 }
357
358 return NO_ERROR;
359 }
360
getHdrSupport(bool * supported)361 status_t Surface::getHdrSupport(bool* supported) {
362 ATRACE_CALL();
363
364 sp<IBinder> display(
365 composerService()->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
366 HdrCapabilities hdrCapabilities;
367 status_t err =
368 composerService()->getHdrCapabilities(display, &hdrCapabilities);
369
370 if (err)
371 return err;
372
373 *supported = !hdrCapabilities.getSupportedHdrTypes().empty();
374
375 return NO_ERROR;
376 }
377
hook_setSwapInterval(ANativeWindow * window,int interval)378 int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
379 Surface* c = getSelf(window);
380 return c->setSwapInterval(interval);
381 }
382
hook_dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer,int * fenceFd)383 int Surface::hook_dequeueBuffer(ANativeWindow* window,
384 ANativeWindowBuffer** buffer, int* fenceFd) {
385 Surface* c = getSelf(window);
386 return c->dequeueBuffer(buffer, fenceFd);
387 }
388
hook_cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)389 int Surface::hook_cancelBuffer(ANativeWindow* window,
390 ANativeWindowBuffer* buffer, int fenceFd) {
391 Surface* c = getSelf(window);
392 return c->cancelBuffer(buffer, fenceFd);
393 }
394
hook_queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)395 int Surface::hook_queueBuffer(ANativeWindow* window,
396 ANativeWindowBuffer* buffer, int fenceFd) {
397 Surface* c = getSelf(window);
398 return c->queueBuffer(buffer, fenceFd);
399 }
400
hook_dequeueBuffer_DEPRECATED(ANativeWindow * window,ANativeWindowBuffer ** buffer)401 int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
402 ANativeWindowBuffer** buffer) {
403 Surface* c = getSelf(window);
404 ANativeWindowBuffer* buf;
405 int fenceFd = -1;
406 int result = c->dequeueBuffer(&buf, &fenceFd);
407 if (result != OK) {
408 return result;
409 }
410 sp<Fence> fence(new Fence(fenceFd));
411 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
412 if (waitResult != OK) {
413 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
414 waitResult);
415 c->cancelBuffer(buf, -1);
416 return waitResult;
417 }
418 *buffer = buf;
419 return result;
420 }
421
hook_cancelBuffer_DEPRECATED(ANativeWindow * window,ANativeWindowBuffer * buffer)422 int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
423 ANativeWindowBuffer* buffer) {
424 Surface* c = getSelf(window);
425 return c->cancelBuffer(buffer, -1);
426 }
427
hook_lockBuffer_DEPRECATED(ANativeWindow * window,ANativeWindowBuffer * buffer)428 int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
429 ANativeWindowBuffer* buffer) {
430 Surface* c = getSelf(window);
431 return c->lockBuffer_DEPRECATED(buffer);
432 }
433
hook_queueBuffer_DEPRECATED(ANativeWindow * window,ANativeWindowBuffer * buffer)434 int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
435 ANativeWindowBuffer* buffer) {
436 Surface* c = getSelf(window);
437 return c->queueBuffer(buffer, -1);
438 }
439
hook_query(const ANativeWindow * window,int what,int * value)440 int Surface::hook_query(const ANativeWindow* window,
441 int what, int* value) {
442 const Surface* c = getSelf(window);
443 return c->query(what, value);
444 }
445
hook_perform(ANativeWindow * window,int operation,...)446 int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
447 va_list args;
448 va_start(args, operation);
449 Surface* c = getSelf(window);
450 int result = c->perform(operation, args);
451 va_end(args);
452 return result;
453 }
454
setSwapInterval(int interval)455 int Surface::setSwapInterval(int interval) {
456 ATRACE_CALL();
457 // EGL specification states:
458 // interval is silently clamped to minimum and maximum implementation
459 // dependent values before being stored.
460
461 if (interval < minSwapInterval)
462 interval = minSwapInterval;
463
464 if (interval > maxSwapInterval)
465 interval = maxSwapInterval;
466
467 mSwapIntervalZero = (interval == 0);
468 mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero);
469
470 return NO_ERROR;
471 }
472
dequeueBuffer(android_native_buffer_t ** buffer,int * fenceFd)473 int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
474 ATRACE_CALL();
475 ALOGV("Surface::dequeueBuffer");
476
477 uint32_t reqWidth;
478 uint32_t reqHeight;
479 PixelFormat reqFormat;
480 uint64_t reqUsage;
481 bool enableFrameTimestamps;
482
483 {
484 Mutex::Autolock lock(mMutex);
485 if (mReportRemovedBuffers) {
486 mRemovedBuffers.clear();
487 }
488
489 reqWidth = mReqWidth ? mReqWidth : mUserWidth;
490 reqHeight = mReqHeight ? mReqHeight : mUserHeight;
491
492 reqFormat = mReqFormat;
493 reqUsage = mReqUsage;
494
495 enableFrameTimestamps = mEnableFrameTimestamps;
496
497 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot !=
498 BufferItem::INVALID_BUFFER_SLOT) {
499 sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer);
500 if (gbuf != NULL) {
501 *buffer = gbuf.get();
502 *fenceFd = -1;
503 return OK;
504 }
505 }
506 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
507
508 int buf = -1;
509 sp<Fence> fence;
510 nsecs_t startTime = systemTime();
511
512 FrameEventHistoryDelta frameTimestamps;
513 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight,
514 reqFormat, reqUsage, &mBufferAge,
515 enableFrameTimestamps ? &frameTimestamps
516 : nullptr);
517 mLastDequeueDuration = systemTime() - startTime;
518
519 if (result < 0) {
520 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer"
521 "(%d, %d, %d, %#" PRIx64 ") failed: %d",
522 reqWidth, reqHeight, reqFormat, reqUsage, result);
523 return result;
524 }
525
526 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
527 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
528 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
529 return FAILED_TRANSACTION;
530 }
531
532 Mutex::Autolock lock(mMutex);
533
534 // Write this while holding the mutex
535 mLastDequeueStartTime = startTime;
536
537 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
538
539 // this should never happen
540 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
541
542 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
543 freeAllBuffers();
544 }
545
546 if (enableFrameTimestamps) {
547 mFrameEventHistory->applyDelta(frameTimestamps);
548 }
549
550 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) {
551 if (mReportRemovedBuffers && (gbuf != nullptr)) {
552 mRemovedBuffers.push_back(gbuf);
553 }
554 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
555 if (result != NO_ERROR) {
556 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
557 mGraphicBufferProducer->cancelBuffer(buf, fence);
558 return result;
559 }
560 }
561
562 if (fence->isValid()) {
563 *fenceFd = fence->dup();
564 if (*fenceFd == -1) {
565 ALOGE("dequeueBuffer: error duping fence: %d", errno);
566 // dup() should never fail; something is badly wrong. Soldier on
567 // and hope for the best; the worst that should happen is some
568 // visible corruption that lasts until the next frame.
569 }
570 } else {
571 *fenceFd = -1;
572 }
573
574 *buffer = gbuf.get();
575
576 if (mSharedBufferMode && mAutoRefresh) {
577 mSharedBufferSlot = buf;
578 mSharedBufferHasBeenQueued = false;
579 } else if (mSharedBufferSlot == buf) {
580 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
581 mSharedBufferHasBeenQueued = false;
582 }
583
584 return OK;
585 }
586
cancelBuffer(android_native_buffer_t * buffer,int fenceFd)587 int Surface::cancelBuffer(android_native_buffer_t* buffer,
588 int fenceFd) {
589 ATRACE_CALL();
590 ALOGV("Surface::cancelBuffer");
591 Mutex::Autolock lock(mMutex);
592 int i = getSlotFromBufferLocked(buffer);
593 if (i < 0) {
594 if (fenceFd >= 0) {
595 close(fenceFd);
596 }
597 return i;
598 }
599 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
600 if (fenceFd >= 0) {
601 close(fenceFd);
602 }
603 return OK;
604 }
605 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
606 mGraphicBufferProducer->cancelBuffer(i, fence);
607
608 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
609 mSharedBufferHasBeenQueued = true;
610 }
611
612 return OK;
613 }
614
getSlotFromBufferLocked(android_native_buffer_t * buffer) const615 int Surface::getSlotFromBufferLocked(
616 android_native_buffer_t* buffer) const {
617 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
618 if (mSlots[i].buffer != NULL &&
619 mSlots[i].buffer->handle == buffer->handle) {
620 return i;
621 }
622 }
623 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
624 return BAD_VALUE;
625 }
626
lockBuffer_DEPRECATED(android_native_buffer_t * buffer)627 int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
628 ALOGV("Surface::lockBuffer");
629 Mutex::Autolock lock(mMutex);
630 return OK;
631 }
632
queueBuffer(android_native_buffer_t * buffer,int fenceFd)633 int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
634 ATRACE_CALL();
635 ALOGV("Surface::queueBuffer");
636 Mutex::Autolock lock(mMutex);
637 int64_t timestamp;
638 bool isAutoTimestamp = false;
639
640 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
641 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
642 isAutoTimestamp = true;
643 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
644 timestamp / 1000000.0);
645 } else {
646 timestamp = mTimestamp;
647 }
648 int i = getSlotFromBufferLocked(buffer);
649 if (i < 0) {
650 if (fenceFd >= 0) {
651 close(fenceFd);
652 }
653 return i;
654 }
655 if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) {
656 if (fenceFd >= 0) {
657 close(fenceFd);
658 }
659 return OK;
660 }
661
662
663 // Make sure the crop rectangle is entirely inside the buffer.
664 Rect crop(Rect::EMPTY_RECT);
665 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
666
667 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
668 IGraphicBufferProducer::QueueBufferOutput output;
669 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
670 static_cast<android_dataspace>(mDataSpace), crop, mScalingMode,
671 mTransform ^ mStickyTransform, fence, mStickyTransform,
672 mEnableFrameTimestamps);
673
674 // we should send HDR metadata as needed if this becomes a bottleneck
675 input.setHdrMetadata(mHdrMetadata);
676
677 if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) {
678 input.setSurfaceDamage(Region::INVALID_REGION);
679 } else {
680 // Here we do two things:
681 // 1) The surface damage was specified using the OpenGL ES convention of
682 // the origin being in the bottom-left corner. Here we flip to the
683 // convention that the rest of the system uses (top-left corner) by
684 // subtracting all top/bottom coordinates from the buffer height.
685 // 2) If the buffer is coming in rotated (for example, because the EGL
686 // implementation is reacting to the transform hint coming back from
687 // SurfaceFlinger), the surface damage needs to be rotated the
688 // opposite direction, since it was generated assuming an unrotated
689 // buffer (the app doesn't know that the EGL implementation is
690 // reacting to the transform hint behind its back). The
691 // transformations in the switch statement below apply those
692 // complementary rotations (e.g., if 90 degrees, rotate 270 degrees).
693
694 int width = buffer->width;
695 int height = buffer->height;
696 bool rotated90 = (mTransform ^ mStickyTransform) &
697 NATIVE_WINDOW_TRANSFORM_ROT_90;
698 if (rotated90) {
699 std::swap(width, height);
700 }
701
702 Region flippedRegion;
703 for (auto rect : mDirtyRegion) {
704 int left = rect.left;
705 int right = rect.right;
706 int top = height - rect.bottom; // Flip from OpenGL convention
707 int bottom = height - rect.top; // Flip from OpenGL convention
708 switch (mTransform ^ mStickyTransform) {
709 case NATIVE_WINDOW_TRANSFORM_ROT_90: {
710 // Rotate 270 degrees
711 Rect flippedRect{top, width - right, bottom, width - left};
712 flippedRegion.orSelf(flippedRect);
713 break;
714 }
715 case NATIVE_WINDOW_TRANSFORM_ROT_180: {
716 // Rotate 180 degrees
717 Rect flippedRect{width - right, height - bottom,
718 width - left, height - top};
719 flippedRegion.orSelf(flippedRect);
720 break;
721 }
722 case NATIVE_WINDOW_TRANSFORM_ROT_270: {
723 // Rotate 90 degrees
724 Rect flippedRect{height - bottom, left,
725 height - top, right};
726 flippedRegion.orSelf(flippedRect);
727 break;
728 }
729 default: {
730 Rect flippedRect{left, top, right, bottom};
731 flippedRegion.orSelf(flippedRect);
732 break;
733 }
734 }
735 }
736
737 input.setSurfaceDamage(flippedRegion);
738 }
739
740 nsecs_t now = systemTime();
741 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
742 mLastQueueDuration = systemTime() - now;
743 if (err != OK) {
744 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
745 }
746
747 if (mEnableFrameTimestamps) {
748 mFrameEventHistory->applyDelta(output.frameTimestamps);
749 // Update timestamps with the local acquire fence.
750 // The consumer doesn't send it back to prevent us from having two
751 // file descriptors of the same fence.
752 mFrameEventHistory->updateAcquireFence(mNextFrameNumber,
753 std::make_shared<FenceTime>(std::move(fence)));
754
755 // Cache timestamps of signaled fences so we can close their file
756 // descriptors.
757 mFrameEventHistory->updateSignalTimes();
758 }
759
760 mLastFrameNumber = mNextFrameNumber;
761
762 mDefaultWidth = output.width;
763 mDefaultHeight = output.height;
764 mNextFrameNumber = output.nextFrameNumber;
765
766 // Disable transform hint if sticky transform is set.
767 if (mStickyTransform == 0) {
768 mTransformHint = output.transformHint;
769 }
770
771 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
772
773 if (!mConnectedToCpu) {
774 // Clear surface damage back to full-buffer
775 mDirtyRegion = Region::INVALID_REGION;
776 }
777
778 if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) {
779 mSharedBufferHasBeenQueued = true;
780 }
781
782 mQueueBufferCondition.broadcast();
783
784 return err;
785 }
786
querySupportedTimestampsLocked() const787 void Surface::querySupportedTimestampsLocked() const {
788 // mMutex must be locked when calling this method.
789
790 if (mQueriedSupportedTimestamps) {
791 return;
792 }
793 mQueriedSupportedTimestamps = true;
794
795 std::vector<FrameEvent> supportedFrameTimestamps;
796 status_t err = composerService()->getSupportedFrameTimestamps(
797 &supportedFrameTimestamps);
798
799 if (err != NO_ERROR) {
800 return;
801 }
802
803 for (auto sft : supportedFrameTimestamps) {
804 if (sft == FrameEvent::DISPLAY_PRESENT) {
805 mFrameTimestampsSupportsPresent = true;
806 }
807 }
808 }
809
query(int what,int * value) const810 int Surface::query(int what, int* value) const {
811 ATRACE_CALL();
812 ALOGV("Surface::query");
813 { // scope for the lock
814 Mutex::Autolock lock(mMutex);
815 switch (what) {
816 case NATIVE_WINDOW_FORMAT:
817 if (mReqFormat) {
818 *value = static_cast<int>(mReqFormat);
819 return NO_ERROR;
820 }
821 break;
822 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
823 if (composerService()->authenticateSurfaceTexture(
824 mGraphicBufferProducer)) {
825 *value = 1;
826 } else {
827 *value = 0;
828 }
829 return NO_ERROR;
830 }
831 case NATIVE_WINDOW_CONCRETE_TYPE:
832 *value = NATIVE_WINDOW_SURFACE;
833 return NO_ERROR;
834 case NATIVE_WINDOW_DEFAULT_WIDTH:
835 *value = static_cast<int>(
836 mUserWidth ? mUserWidth : mDefaultWidth);
837 return NO_ERROR;
838 case NATIVE_WINDOW_DEFAULT_HEIGHT:
839 *value = static_cast<int>(
840 mUserHeight ? mUserHeight : mDefaultHeight);
841 return NO_ERROR;
842 case NATIVE_WINDOW_TRANSFORM_HINT:
843 *value = static_cast<int>(mTransformHint);
844 return NO_ERROR;
845 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
846 status_t err = NO_ERROR;
847 if (!mConsumerRunningBehind) {
848 *value = 0;
849 } else {
850 err = mGraphicBufferProducer->query(what, value);
851 if (err == NO_ERROR) {
852 mConsumerRunningBehind = *value;
853 }
854 }
855 return err;
856 }
857 case NATIVE_WINDOW_BUFFER_AGE: {
858 if (mBufferAge > INT32_MAX) {
859 *value = 0;
860 } else {
861 *value = static_cast<int32_t>(mBufferAge);
862 }
863 return NO_ERROR;
864 }
865 case NATIVE_WINDOW_LAST_DEQUEUE_DURATION: {
866 int64_t durationUs = mLastDequeueDuration / 1000;
867 *value = durationUs > std::numeric_limits<int>::max() ?
868 std::numeric_limits<int>::max() :
869 static_cast<int>(durationUs);
870 return NO_ERROR;
871 }
872 case NATIVE_WINDOW_LAST_QUEUE_DURATION: {
873 int64_t durationUs = mLastQueueDuration / 1000;
874 *value = durationUs > std::numeric_limits<int>::max() ?
875 std::numeric_limits<int>::max() :
876 static_cast<int>(durationUs);
877 return NO_ERROR;
878 }
879 case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: {
880 querySupportedTimestampsLocked();
881 *value = mFrameTimestampsSupportsPresent ? 1 : 0;
882 return NO_ERROR;
883 }
884 case NATIVE_WINDOW_IS_VALID: {
885 *value = mGraphicBufferProducer != nullptr ? 1 : 0;
886 return NO_ERROR;
887 }
888 case NATIVE_WINDOW_DATASPACE: {
889 *value = static_cast<int>(mDataSpace);
890 return NO_ERROR;
891 }
892 }
893 }
894 return mGraphicBufferProducer->query(what, value);
895 }
896
perform(int operation,va_list args)897 int Surface::perform(int operation, va_list args)
898 {
899 int res = NO_ERROR;
900 switch (operation) {
901 case NATIVE_WINDOW_CONNECT:
902 // deprecated. must return NO_ERROR.
903 break;
904 case NATIVE_WINDOW_DISCONNECT:
905 // deprecated. must return NO_ERROR.
906 break;
907 case NATIVE_WINDOW_SET_USAGE:
908 res = dispatchSetUsage(args);
909 break;
910 case NATIVE_WINDOW_SET_CROP:
911 res = dispatchSetCrop(args);
912 break;
913 case NATIVE_WINDOW_SET_BUFFER_COUNT:
914 res = dispatchSetBufferCount(args);
915 break;
916 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
917 res = dispatchSetBuffersGeometry(args);
918 break;
919 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
920 res = dispatchSetBuffersTransform(args);
921 break;
922 case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM:
923 res = dispatchSetBuffersStickyTransform(args);
924 break;
925 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
926 res = dispatchSetBuffersTimestamp(args);
927 break;
928 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
929 res = dispatchSetBuffersDimensions(args);
930 break;
931 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
932 res = dispatchSetBuffersUserDimensions(args);
933 break;
934 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
935 res = dispatchSetBuffersFormat(args);
936 break;
937 case NATIVE_WINDOW_LOCK:
938 res = dispatchLock(args);
939 break;
940 case NATIVE_WINDOW_UNLOCK_AND_POST:
941 res = dispatchUnlockAndPost(args);
942 break;
943 case NATIVE_WINDOW_SET_SCALING_MODE:
944 res = dispatchSetScalingMode(args);
945 break;
946 case NATIVE_WINDOW_API_CONNECT:
947 res = dispatchConnect(args);
948 break;
949 case NATIVE_WINDOW_API_DISCONNECT:
950 res = dispatchDisconnect(args);
951 break;
952 case NATIVE_WINDOW_SET_SIDEBAND_STREAM:
953 res = dispatchSetSidebandStream(args);
954 break;
955 case NATIVE_WINDOW_SET_BUFFERS_DATASPACE:
956 res = dispatchSetBuffersDataSpace(args);
957 break;
958 case NATIVE_WINDOW_SET_BUFFERS_SMPTE2086_METADATA:
959 res = dispatchSetBuffersSmpte2086Metadata(args);
960 break;
961 case NATIVE_WINDOW_SET_BUFFERS_CTA861_3_METADATA:
962 res = dispatchSetBuffersCta8613Metadata(args);
963 break;
964 case NATIVE_WINDOW_SET_SURFACE_DAMAGE:
965 res = dispatchSetSurfaceDamage(args);
966 break;
967 case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE:
968 res = dispatchSetSharedBufferMode(args);
969 break;
970 case NATIVE_WINDOW_SET_AUTO_REFRESH:
971 res = dispatchSetAutoRefresh(args);
972 break;
973 case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION:
974 res = dispatchGetDisplayRefreshCycleDuration(args);
975 break;
976 case NATIVE_WINDOW_GET_NEXT_FRAME_ID:
977 res = dispatchGetNextFrameId(args);
978 break;
979 case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS:
980 res = dispatchEnableFrameTimestamps(args);
981 break;
982 case NATIVE_WINDOW_GET_COMPOSITOR_TIMING:
983 res = dispatchGetCompositorTiming(args);
984 break;
985 case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS:
986 res = dispatchGetFrameTimestamps(args);
987 break;
988 case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT:
989 res = dispatchGetWideColorSupport(args);
990 break;
991 case NATIVE_WINDOW_GET_HDR_SUPPORT:
992 res = dispatchGetHdrSupport(args);
993 break;
994 case NATIVE_WINDOW_SET_USAGE64:
995 res = dispatchSetUsage64(args);
996 break;
997 case NATIVE_WINDOW_GET_CONSUMER_USAGE64:
998 res = dispatchGetConsumerUsage64(args);
999 break;
1000 default:
1001 res = NAME_NOT_FOUND;
1002 break;
1003 }
1004 return res;
1005 }
1006
dispatchConnect(va_list args)1007 int Surface::dispatchConnect(va_list args) {
1008 int api = va_arg(args, int);
1009 return connect(api);
1010 }
1011
dispatchDisconnect(va_list args)1012 int Surface::dispatchDisconnect(va_list args) {
1013 int api = va_arg(args, int);
1014 return disconnect(api);
1015 }
1016
dispatchSetUsage(va_list args)1017 int Surface::dispatchSetUsage(va_list args) {
1018 uint64_t usage = va_arg(args, uint32_t);
1019 return setUsage(usage);
1020 }
1021
dispatchSetUsage64(va_list args)1022 int Surface::dispatchSetUsage64(va_list args) {
1023 uint64_t usage = va_arg(args, uint64_t);
1024 return setUsage(usage);
1025 }
1026
dispatchSetCrop(va_list args)1027 int Surface::dispatchSetCrop(va_list args) {
1028 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
1029 return setCrop(reinterpret_cast<Rect const*>(rect));
1030 }
1031
dispatchSetBufferCount(va_list args)1032 int Surface::dispatchSetBufferCount(va_list args) {
1033 size_t bufferCount = va_arg(args, size_t);
1034 return setBufferCount(static_cast<int32_t>(bufferCount));
1035 }
1036
dispatchSetBuffersGeometry(va_list args)1037 int Surface::dispatchSetBuffersGeometry(va_list args) {
1038 uint32_t width = va_arg(args, uint32_t);
1039 uint32_t height = va_arg(args, uint32_t);
1040 PixelFormat format = va_arg(args, PixelFormat);
1041 int err = setBuffersDimensions(width, height);
1042 if (err != 0) {
1043 return err;
1044 }
1045 return setBuffersFormat(format);
1046 }
1047
dispatchSetBuffersDimensions(va_list args)1048 int Surface::dispatchSetBuffersDimensions(va_list args) {
1049 uint32_t width = va_arg(args, uint32_t);
1050 uint32_t height = va_arg(args, uint32_t);
1051 return setBuffersDimensions(width, height);
1052 }
1053
dispatchSetBuffersUserDimensions(va_list args)1054 int Surface::dispatchSetBuffersUserDimensions(va_list args) {
1055 uint32_t width = va_arg(args, uint32_t);
1056 uint32_t height = va_arg(args, uint32_t);
1057 return setBuffersUserDimensions(width, height);
1058 }
1059
dispatchSetBuffersFormat(va_list args)1060 int Surface::dispatchSetBuffersFormat(va_list args) {
1061 PixelFormat format = va_arg(args, PixelFormat);
1062 return setBuffersFormat(format);
1063 }
1064
dispatchSetScalingMode(va_list args)1065 int Surface::dispatchSetScalingMode(va_list args) {
1066 int mode = va_arg(args, int);
1067 return setScalingMode(mode);
1068 }
1069
dispatchSetBuffersTransform(va_list args)1070 int Surface::dispatchSetBuffersTransform(va_list args) {
1071 uint32_t transform = va_arg(args, uint32_t);
1072 return setBuffersTransform(transform);
1073 }
1074
dispatchSetBuffersStickyTransform(va_list args)1075 int Surface::dispatchSetBuffersStickyTransform(va_list args) {
1076 uint32_t transform = va_arg(args, uint32_t);
1077 return setBuffersStickyTransform(transform);
1078 }
1079
dispatchSetBuffersTimestamp(va_list args)1080 int Surface::dispatchSetBuffersTimestamp(va_list args) {
1081 int64_t timestamp = va_arg(args, int64_t);
1082 return setBuffersTimestamp(timestamp);
1083 }
1084
dispatchLock(va_list args)1085 int Surface::dispatchLock(va_list args) {
1086 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
1087 ARect* inOutDirtyBounds = va_arg(args, ARect*);
1088 return lock(outBuffer, inOutDirtyBounds);
1089 }
1090
dispatchUnlockAndPost(va_list args)1091 int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
1092 return unlockAndPost();
1093 }
1094
dispatchSetSidebandStream(va_list args)1095 int Surface::dispatchSetSidebandStream(va_list args) {
1096 native_handle_t* sH = va_arg(args, native_handle_t*);
1097 sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false);
1098 setSidebandStream(sidebandHandle);
1099 return OK;
1100 }
1101
dispatchSetBuffersDataSpace(va_list args)1102 int Surface::dispatchSetBuffersDataSpace(va_list args) {
1103 Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int));
1104 return setBuffersDataSpace(dataspace);
1105 }
1106
dispatchSetBuffersSmpte2086Metadata(va_list args)1107 int Surface::dispatchSetBuffersSmpte2086Metadata(va_list args) {
1108 const android_smpte2086_metadata* metadata =
1109 va_arg(args, const android_smpte2086_metadata*);
1110 return setBuffersSmpte2086Metadata(metadata);
1111 }
1112
dispatchSetBuffersCta8613Metadata(va_list args)1113 int Surface::dispatchSetBuffersCta8613Metadata(va_list args) {
1114 const android_cta861_3_metadata* metadata =
1115 va_arg(args, const android_cta861_3_metadata*);
1116 return setBuffersCta8613Metadata(metadata);
1117 }
1118
dispatchSetSurfaceDamage(va_list args)1119 int Surface::dispatchSetSurfaceDamage(va_list args) {
1120 android_native_rect_t* rects = va_arg(args, android_native_rect_t*);
1121 size_t numRects = va_arg(args, size_t);
1122 setSurfaceDamage(rects, numRects);
1123 return NO_ERROR;
1124 }
1125
dispatchSetSharedBufferMode(va_list args)1126 int Surface::dispatchSetSharedBufferMode(va_list args) {
1127 bool sharedBufferMode = va_arg(args, int);
1128 return setSharedBufferMode(sharedBufferMode);
1129 }
1130
dispatchSetAutoRefresh(va_list args)1131 int Surface::dispatchSetAutoRefresh(va_list args) {
1132 bool autoRefresh = va_arg(args, int);
1133 return setAutoRefresh(autoRefresh);
1134 }
1135
dispatchGetDisplayRefreshCycleDuration(va_list args)1136 int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) {
1137 nsecs_t* outRefreshDuration = va_arg(args, int64_t*);
1138 return getDisplayRefreshCycleDuration(outRefreshDuration);
1139 }
1140
dispatchGetNextFrameId(va_list args)1141 int Surface::dispatchGetNextFrameId(va_list args) {
1142 uint64_t* nextFrameId = va_arg(args, uint64_t*);
1143 *nextFrameId = getNextFrameNumber();
1144 return NO_ERROR;
1145 }
1146
dispatchEnableFrameTimestamps(va_list args)1147 int Surface::dispatchEnableFrameTimestamps(va_list args) {
1148 bool enable = va_arg(args, int);
1149 enableFrameTimestamps(enable);
1150 return NO_ERROR;
1151 }
1152
dispatchGetCompositorTiming(va_list args)1153 int Surface::dispatchGetCompositorTiming(va_list args) {
1154 nsecs_t* compositeDeadline = va_arg(args, int64_t*);
1155 nsecs_t* compositeInterval = va_arg(args, int64_t*);
1156 nsecs_t* compositeToPresentLatency = va_arg(args, int64_t*);
1157 return getCompositorTiming(compositeDeadline, compositeInterval,
1158 compositeToPresentLatency);
1159 }
1160
dispatchGetFrameTimestamps(va_list args)1161 int Surface::dispatchGetFrameTimestamps(va_list args) {
1162 uint64_t frameId = va_arg(args, uint64_t);
1163 nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*);
1164 nsecs_t* outAcquireTime = va_arg(args, int64_t*);
1165 nsecs_t* outLatchTime = va_arg(args, int64_t*);
1166 nsecs_t* outFirstRefreshStartTime = va_arg(args, int64_t*);
1167 nsecs_t* outLastRefreshStartTime = va_arg(args, int64_t*);
1168 nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*);
1169 nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*);
1170 nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*);
1171 nsecs_t* outReleaseTime = va_arg(args, int64_t*);
1172 return getFrameTimestamps(frameId,
1173 outRequestedPresentTime, outAcquireTime, outLatchTime,
1174 outFirstRefreshStartTime, outLastRefreshStartTime,
1175 outGpuCompositionDoneTime, outDisplayPresentTime,
1176 outDequeueReadyTime, outReleaseTime);
1177 }
1178
dispatchGetWideColorSupport(va_list args)1179 int Surface::dispatchGetWideColorSupport(va_list args) {
1180 bool* outSupport = va_arg(args, bool*);
1181 return getWideColorSupport(outSupport);
1182 }
1183
dispatchGetHdrSupport(va_list args)1184 int Surface::dispatchGetHdrSupport(va_list args) {
1185 bool* outSupport = va_arg(args, bool*);
1186 return getHdrSupport(outSupport);
1187 }
1188
dispatchGetConsumerUsage64(va_list args)1189 int Surface::dispatchGetConsumerUsage64(va_list args) {
1190 uint64_t* usage = va_arg(args, uint64_t*);
1191 return getConsumerUsage(usage);
1192 }
1193
connect(int api)1194 int Surface::connect(int api) {
1195 static sp<IProducerListener> listener = new DummyProducerListener();
1196 return connect(api, listener);
1197 }
1198
connect(int api,const sp<IProducerListener> & listener)1199 int Surface::connect(int api, const sp<IProducerListener>& listener) {
1200 return connect(api, listener, false);
1201 }
1202
connect(int api,const sp<IProducerListener> & listener,bool reportBufferRemoval)1203 int Surface::connect(
1204 int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) {
1205 ATRACE_CALL();
1206 ALOGV("Surface::connect");
1207 Mutex::Autolock lock(mMutex);
1208 IGraphicBufferProducer::QueueBufferOutput output;
1209 mReportRemovedBuffers = reportBufferRemoval;
1210 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
1211 if (err == NO_ERROR) {
1212 mDefaultWidth = output.width;
1213 mDefaultHeight = output.height;
1214 mNextFrameNumber = output.nextFrameNumber;
1215
1216 // Disable transform hint if sticky transform is set.
1217 if (mStickyTransform == 0) {
1218 mTransformHint = output.transformHint;
1219 }
1220
1221 mConsumerRunningBehind = (output.numPendingBuffers >= 2);
1222 }
1223 if (!err && api == NATIVE_WINDOW_API_CPU) {
1224 mConnectedToCpu = true;
1225 // Clear the dirty region in case we're switching from a non-CPU API
1226 mDirtyRegion.clear();
1227 } else if (!err) {
1228 // Initialize the dirty region for tracking surface damage
1229 mDirtyRegion = Region::INVALID_REGION;
1230 }
1231
1232 return err;
1233 }
1234
1235
disconnect(int api,IGraphicBufferProducer::DisconnectMode mode)1236 int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) {
1237 ATRACE_CALL();
1238 ALOGV("Surface::disconnect");
1239 Mutex::Autolock lock(mMutex);
1240 mRemovedBuffers.clear();
1241 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1242 mSharedBufferHasBeenQueued = false;
1243 freeAllBuffers();
1244 int err = mGraphicBufferProducer->disconnect(api, mode);
1245 if (!err) {
1246 mReqFormat = 0;
1247 mReqWidth = 0;
1248 mReqHeight = 0;
1249 mReqUsage = 0;
1250 mCrop.clear();
1251 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
1252 mTransform = 0;
1253 mStickyTransform = 0;
1254
1255 if (api == NATIVE_WINDOW_API_CPU) {
1256 mConnectedToCpu = false;
1257 }
1258 }
1259 return err;
1260 }
1261
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)1262 int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
1263 sp<Fence>* outFence) {
1264 ATRACE_CALL();
1265 ALOGV("Surface::detachNextBuffer");
1266
1267 if (outBuffer == NULL || outFence == NULL) {
1268 return BAD_VALUE;
1269 }
1270
1271 Mutex::Autolock lock(mMutex);
1272 if (mReportRemovedBuffers) {
1273 mRemovedBuffers.clear();
1274 }
1275
1276 sp<GraphicBuffer> buffer(NULL);
1277 sp<Fence> fence(NULL);
1278 status_t result = mGraphicBufferProducer->detachNextBuffer(
1279 &buffer, &fence);
1280 if (result != NO_ERROR) {
1281 return result;
1282 }
1283
1284 *outBuffer = buffer;
1285 if (fence != NULL && fence->isValid()) {
1286 *outFence = fence;
1287 } else {
1288 *outFence = Fence::NO_FENCE;
1289 }
1290
1291 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1292 if (mSlots[i].buffer != NULL &&
1293 mSlots[i].buffer->getId() == buffer->getId()) {
1294 if (mReportRemovedBuffers) {
1295 mRemovedBuffers.push_back(mSlots[i].buffer);
1296 }
1297 mSlots[i].buffer = NULL;
1298 }
1299 }
1300
1301 return NO_ERROR;
1302 }
1303
attachBuffer(ANativeWindowBuffer * buffer)1304 int Surface::attachBuffer(ANativeWindowBuffer* buffer)
1305 {
1306 ATRACE_CALL();
1307 ALOGV("Surface::attachBuffer");
1308
1309 Mutex::Autolock lock(mMutex);
1310 if (mReportRemovedBuffers) {
1311 mRemovedBuffers.clear();
1312 }
1313
1314 sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer));
1315 uint32_t priorGeneration = graphicBuffer->mGenerationNumber;
1316 graphicBuffer->mGenerationNumber = mGenerationNumber;
1317 int32_t attachedSlot = -1;
1318 status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer);
1319 if (result != NO_ERROR) {
1320 ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result);
1321 graphicBuffer->mGenerationNumber = priorGeneration;
1322 return result;
1323 }
1324 if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) {
1325 mRemovedBuffers.push_back(mSlots[attachedSlot].buffer);
1326 }
1327 mSlots[attachedSlot].buffer = graphicBuffer;
1328
1329 return NO_ERROR;
1330 }
1331
setUsage(uint64_t reqUsage)1332 int Surface::setUsage(uint64_t reqUsage)
1333 {
1334 ALOGV("Surface::setUsage");
1335 Mutex::Autolock lock(mMutex);
1336 if (reqUsage != mReqUsage) {
1337 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1338 }
1339 mReqUsage = reqUsage;
1340 return OK;
1341 }
1342
setCrop(Rect const * rect)1343 int Surface::setCrop(Rect const* rect)
1344 {
1345 ATRACE_CALL();
1346
1347 Rect realRect(Rect::EMPTY_RECT);
1348 if (rect == NULL || rect->isEmpty()) {
1349 realRect.clear();
1350 } else {
1351 realRect = *rect;
1352 }
1353
1354 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
1355 realRect.left, realRect.top, realRect.right, realRect.bottom);
1356
1357 Mutex::Autolock lock(mMutex);
1358 mCrop = realRect;
1359 return NO_ERROR;
1360 }
1361
setBufferCount(int bufferCount)1362 int Surface::setBufferCount(int bufferCount)
1363 {
1364 ATRACE_CALL();
1365 ALOGV("Surface::setBufferCount");
1366 Mutex::Autolock lock(mMutex);
1367
1368 status_t err = NO_ERROR;
1369 if (bufferCount == 0) {
1370 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(1);
1371 } else {
1372 int minUndequeuedBuffers = 0;
1373 err = mGraphicBufferProducer->query(
1374 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBuffers);
1375 if (err == NO_ERROR) {
1376 err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1377 bufferCount - minUndequeuedBuffers);
1378 }
1379 }
1380
1381 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
1382 bufferCount, strerror(-err));
1383
1384 return err;
1385 }
1386
setMaxDequeuedBufferCount(int maxDequeuedBuffers)1387 int Surface::setMaxDequeuedBufferCount(int maxDequeuedBuffers) {
1388 ATRACE_CALL();
1389 ALOGV("Surface::setMaxDequeuedBufferCount");
1390 Mutex::Autolock lock(mMutex);
1391
1392 status_t err = mGraphicBufferProducer->setMaxDequeuedBufferCount(
1393 maxDequeuedBuffers);
1394 ALOGE_IF(err, "IGraphicBufferProducer::setMaxDequeuedBufferCount(%d) "
1395 "returned %s", maxDequeuedBuffers, strerror(-err));
1396
1397 return err;
1398 }
1399
setAsyncMode(bool async)1400 int Surface::setAsyncMode(bool async) {
1401 ATRACE_CALL();
1402 ALOGV("Surface::setAsyncMode");
1403 Mutex::Autolock lock(mMutex);
1404
1405 status_t err = mGraphicBufferProducer->setAsyncMode(async);
1406 ALOGE_IF(err, "IGraphicBufferProducer::setAsyncMode(%d) returned %s",
1407 async, strerror(-err));
1408
1409 return err;
1410 }
1411
setSharedBufferMode(bool sharedBufferMode)1412 int Surface::setSharedBufferMode(bool sharedBufferMode) {
1413 ATRACE_CALL();
1414 ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode);
1415 Mutex::Autolock lock(mMutex);
1416
1417 status_t err = mGraphicBufferProducer->setSharedBufferMode(
1418 sharedBufferMode);
1419 if (err == NO_ERROR) {
1420 mSharedBufferMode = sharedBufferMode;
1421 }
1422 ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned"
1423 "%s", sharedBufferMode, strerror(-err));
1424
1425 return err;
1426 }
1427
setAutoRefresh(bool autoRefresh)1428 int Surface::setAutoRefresh(bool autoRefresh) {
1429 ATRACE_CALL();
1430 ALOGV("Surface::setAutoRefresh (%d)", autoRefresh);
1431 Mutex::Autolock lock(mMutex);
1432
1433 status_t err = mGraphicBufferProducer->setAutoRefresh(autoRefresh);
1434 if (err == NO_ERROR) {
1435 mAutoRefresh = autoRefresh;
1436 }
1437 ALOGE_IF(err, "IGraphicBufferProducer::setAutoRefresh(%d) returned %s",
1438 autoRefresh, strerror(-err));
1439 return err;
1440 }
1441
setBuffersDimensions(uint32_t width,uint32_t height)1442 int Surface::setBuffersDimensions(uint32_t width, uint32_t height)
1443 {
1444 ATRACE_CALL();
1445 ALOGV("Surface::setBuffersDimensions");
1446
1447 if ((width && !height) || (!width && height))
1448 return BAD_VALUE;
1449
1450 Mutex::Autolock lock(mMutex);
1451 if (width != mReqWidth || height != mReqHeight) {
1452 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1453 }
1454 mReqWidth = width;
1455 mReqHeight = height;
1456 return NO_ERROR;
1457 }
1458
setBuffersUserDimensions(uint32_t width,uint32_t height)1459 int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height)
1460 {
1461 ATRACE_CALL();
1462 ALOGV("Surface::setBuffersUserDimensions");
1463
1464 if ((width && !height) || (!width && height))
1465 return BAD_VALUE;
1466
1467 Mutex::Autolock lock(mMutex);
1468 if (width != mUserWidth || height != mUserHeight) {
1469 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1470 }
1471 mUserWidth = width;
1472 mUserHeight = height;
1473 return NO_ERROR;
1474 }
1475
setBuffersFormat(PixelFormat format)1476 int Surface::setBuffersFormat(PixelFormat format)
1477 {
1478 ALOGV("Surface::setBuffersFormat");
1479
1480 Mutex::Autolock lock(mMutex);
1481 if (format != mReqFormat) {
1482 mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT;
1483 }
1484 mReqFormat = format;
1485 return NO_ERROR;
1486 }
1487
setScalingMode(int mode)1488 int Surface::setScalingMode(int mode)
1489 {
1490 ATRACE_CALL();
1491 ALOGV("Surface::setScalingMode(%d)", mode);
1492
1493 switch (mode) {
1494 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
1495 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
1496 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
1497 case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP:
1498 break;
1499 default:
1500 ALOGE("unknown scaling mode: %d", mode);
1501 return BAD_VALUE;
1502 }
1503
1504 Mutex::Autolock lock(mMutex);
1505 mScalingMode = mode;
1506 return NO_ERROR;
1507 }
1508
setBuffersTransform(uint32_t transform)1509 int Surface::setBuffersTransform(uint32_t transform)
1510 {
1511 ATRACE_CALL();
1512 ALOGV("Surface::setBuffersTransform");
1513 Mutex::Autolock lock(mMutex);
1514 mTransform = transform;
1515 return NO_ERROR;
1516 }
1517
setBuffersStickyTransform(uint32_t transform)1518 int Surface::setBuffersStickyTransform(uint32_t transform)
1519 {
1520 ATRACE_CALL();
1521 ALOGV("Surface::setBuffersStickyTransform");
1522 Mutex::Autolock lock(mMutex);
1523 mStickyTransform = transform;
1524 return NO_ERROR;
1525 }
1526
setBuffersTimestamp(int64_t timestamp)1527 int Surface::setBuffersTimestamp(int64_t timestamp)
1528 {
1529 ALOGV("Surface::setBuffersTimestamp");
1530 Mutex::Autolock lock(mMutex);
1531 mTimestamp = timestamp;
1532 return NO_ERROR;
1533 }
1534
setBuffersDataSpace(Dataspace dataSpace)1535 int Surface::setBuffersDataSpace(Dataspace dataSpace)
1536 {
1537 ALOGV("Surface::setBuffersDataSpace");
1538 Mutex::Autolock lock(mMutex);
1539 mDataSpace = dataSpace;
1540 return NO_ERROR;
1541 }
1542
setBuffersSmpte2086Metadata(const android_smpte2086_metadata * metadata)1543 int Surface::setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata) {
1544 ALOGV("Surface::setBuffersSmpte2086Metadata");
1545 Mutex::Autolock lock(mMutex);
1546 if (metadata) {
1547 mHdrMetadata.smpte2086 = *metadata;
1548 mHdrMetadata.validTypes |= HdrMetadata::SMPTE2086;
1549 } else {
1550 mHdrMetadata.validTypes &= ~HdrMetadata::SMPTE2086;
1551 }
1552 return NO_ERROR;
1553 }
1554
setBuffersCta8613Metadata(const android_cta861_3_metadata * metadata)1555 int Surface::setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata) {
1556 ALOGV("Surface::setBuffersCta8613Metadata");
1557 Mutex::Autolock lock(mMutex);
1558 if (metadata) {
1559 mHdrMetadata.cta8613 = *metadata;
1560 mHdrMetadata.validTypes |= HdrMetadata::CTA861_3;
1561 } else {
1562 mHdrMetadata.validTypes &= ~HdrMetadata::CTA861_3;
1563 }
1564 return NO_ERROR;
1565 }
1566
getBuffersDataSpace()1567 Dataspace Surface::getBuffersDataSpace() {
1568 ALOGV("Surface::getBuffersDataSpace");
1569 Mutex::Autolock lock(mMutex);
1570 return mDataSpace;
1571 }
1572
freeAllBuffers()1573 void Surface::freeAllBuffers() {
1574 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
1575 mSlots[i].buffer = 0;
1576 }
1577 }
1578
setSurfaceDamage(android_native_rect_t * rects,size_t numRects)1579 void Surface::setSurfaceDamage(android_native_rect_t* rects, size_t numRects) {
1580 ATRACE_CALL();
1581 ALOGV("Surface::setSurfaceDamage");
1582 Mutex::Autolock lock(mMutex);
1583
1584 if (mConnectedToCpu || numRects == 0) {
1585 mDirtyRegion = Region::INVALID_REGION;
1586 return;
1587 }
1588
1589 mDirtyRegion.clear();
1590 for (size_t r = 0; r < numRects; ++r) {
1591 // We intentionally flip top and bottom here, since because they're
1592 // specified with a bottom-left origin, top > bottom, which fails
1593 // validation in the Region class. We will fix this up when we flip to a
1594 // top-left origin in queueBuffer.
1595 Rect rect(rects[r].left, rects[r].bottom, rects[r].right, rects[r].top);
1596 mDirtyRegion.orSelf(rect);
1597 }
1598 }
1599
1600 // ----------------------------------------------------------------------
1601 // the lock/unlock APIs must be used from the same thread
1602
copyBlt(const sp<GraphicBuffer> & dst,const sp<GraphicBuffer> & src,const Region & reg,int * dstFenceFd)1603 static status_t copyBlt(
1604 const sp<GraphicBuffer>& dst,
1605 const sp<GraphicBuffer>& src,
1606 const Region& reg,
1607 int *dstFenceFd)
1608 {
1609 if (dst->getId() == src->getId())
1610 return OK;
1611
1612 // src and dst with, height and format must be identical. no verification
1613 // is done here.
1614 status_t err;
1615 uint8_t* src_bits = NULL;
1616 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(),
1617 reinterpret_cast<void**>(&src_bits));
1618 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
1619
1620 uint8_t* dst_bits = NULL;
1621 err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(),
1622 reinterpret_cast<void**>(&dst_bits), *dstFenceFd);
1623 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
1624 *dstFenceFd = -1;
1625
1626 Region::const_iterator head(reg.begin());
1627 Region::const_iterator tail(reg.end());
1628 if (head != tail && src_bits && dst_bits) {
1629 const size_t bpp = bytesPerPixel(src->format);
1630 const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp;
1631 const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp;
1632
1633 while (head != tail) {
1634 const Rect& r(*head++);
1635 int32_t h = r.height();
1636 if (h <= 0) continue;
1637 size_t size = static_cast<uint32_t>(r.width()) * bpp;
1638 uint8_t const * s = src_bits +
1639 static_cast<uint32_t>(r.left + src->stride * r.top) * bpp;
1640 uint8_t * d = dst_bits +
1641 static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp;
1642 if (dbpr==sbpr && size==sbpr) {
1643 size *= static_cast<size_t>(h);
1644 h = 1;
1645 }
1646 do {
1647 memcpy(d, s, size);
1648 d += dbpr;
1649 s += sbpr;
1650 } while (--h > 0);
1651 }
1652 }
1653
1654 if (src_bits)
1655 src->unlock();
1656
1657 if (dst_bits)
1658 dst->unlockAsync(dstFenceFd);
1659
1660 return err;
1661 }
1662
1663 // ----------------------------------------------------------------------------
1664
lock(ANativeWindow_Buffer * outBuffer,ARect * inOutDirtyBounds)1665 status_t Surface::lock(
1666 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
1667 {
1668 if (mLockedBuffer != 0) {
1669 ALOGE("Surface::lock failed, already locked");
1670 return INVALID_OPERATION;
1671 }
1672
1673 if (!mConnectedToCpu) {
1674 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
1675 if (err) {
1676 return err;
1677 }
1678 // we're intending to do software rendering from this point
1679 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
1680 }
1681
1682 ANativeWindowBuffer* out;
1683 int fenceFd = -1;
1684 status_t err = dequeueBuffer(&out, &fenceFd);
1685 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
1686 if (err == NO_ERROR) {
1687 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
1688 const Rect bounds(backBuffer->width, backBuffer->height);
1689
1690 Region newDirtyRegion;
1691 if (inOutDirtyBounds) {
1692 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
1693 newDirtyRegion.andSelf(bounds);
1694 } else {
1695 newDirtyRegion.set(bounds);
1696 }
1697
1698 // figure out if we can copy the frontbuffer back
1699 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
1700 const bool canCopyBack = (frontBuffer != 0 &&
1701 backBuffer->width == frontBuffer->width &&
1702 backBuffer->height == frontBuffer->height &&
1703 backBuffer->format == frontBuffer->format);
1704
1705 if (canCopyBack) {
1706 // copy the area that is invalid and not repainted this round
1707 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
1708 if (!copyback.isEmpty()) {
1709 copyBlt(backBuffer, frontBuffer, copyback, &fenceFd);
1710 }
1711 } else {
1712 // if we can't copy-back anything, modify the user's dirty
1713 // region to make sure they redraw the whole buffer
1714 newDirtyRegion.set(bounds);
1715 mDirtyRegion.clear();
1716 Mutex::Autolock lock(mMutex);
1717 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
1718 mSlots[i].dirtyRegion.clear();
1719 }
1720 }
1721
1722
1723 { // scope for the lock
1724 Mutex::Autolock lock(mMutex);
1725 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
1726 if (backBufferSlot >= 0) {
1727 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
1728 mDirtyRegion.subtract(dirtyRegion);
1729 dirtyRegion = newDirtyRegion;
1730 }
1731 }
1732
1733 mDirtyRegion.orSelf(newDirtyRegion);
1734 if (inOutDirtyBounds) {
1735 *inOutDirtyBounds = newDirtyRegion.getBounds();
1736 }
1737
1738 void* vaddr;
1739 status_t res = backBuffer->lockAsync(
1740 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
1741 newDirtyRegion.bounds(), &vaddr, fenceFd);
1742
1743 ALOGW_IF(res, "failed locking buffer (handle = %p)",
1744 backBuffer->handle);
1745
1746 if (res != 0) {
1747 err = INVALID_OPERATION;
1748 } else {
1749 mLockedBuffer = backBuffer;
1750 outBuffer->width = backBuffer->width;
1751 outBuffer->height = backBuffer->height;
1752 outBuffer->stride = backBuffer->stride;
1753 outBuffer->format = backBuffer->format;
1754 outBuffer->bits = vaddr;
1755 }
1756 }
1757 return err;
1758 }
1759
unlockAndPost()1760 status_t Surface::unlockAndPost()
1761 {
1762 if (mLockedBuffer == 0) {
1763 ALOGE("Surface::unlockAndPost failed, no locked buffer");
1764 return INVALID_OPERATION;
1765 }
1766
1767 int fd = -1;
1768 status_t err = mLockedBuffer->unlockAsync(&fd);
1769 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
1770
1771 err = queueBuffer(mLockedBuffer.get(), fd);
1772 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
1773 mLockedBuffer->handle, strerror(-err));
1774
1775 mPostedBuffer = mLockedBuffer;
1776 mLockedBuffer = 0;
1777 return err;
1778 }
1779
waitForNextFrame(uint64_t lastFrame,nsecs_t timeout)1780 bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) {
1781 Mutex::Autolock lock(mMutex);
1782 if (mNextFrameNumber > lastFrame) {
1783 return true;
1784 }
1785 return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK;
1786 }
1787
getUniqueId(uint64_t * outId) const1788 status_t Surface::getUniqueId(uint64_t* outId) const {
1789 Mutex::Autolock lock(mMutex);
1790 return mGraphicBufferProducer->getUniqueId(outId);
1791 }
1792
getConsumerUsage(uint64_t * outUsage) const1793 int Surface::getConsumerUsage(uint64_t* outUsage) const {
1794 Mutex::Autolock lock(mMutex);
1795 return mGraphicBufferProducer->getConsumerUsage(outUsage);
1796 }
1797
getLastDequeueStartTime() const1798 nsecs_t Surface::getLastDequeueStartTime() const {
1799 Mutex::Autolock lock(mMutex);
1800 return mLastDequeueStartTime;
1801 }
1802
getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>> * out)1803 status_t Surface::getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out) {
1804 if (out == nullptr) {
1805 ALOGE("%s: out must not be null!", __FUNCTION__);
1806 return BAD_VALUE;
1807 }
1808
1809 Mutex::Autolock lock(mMutex);
1810 *out = mRemovedBuffers;
1811 mRemovedBuffers.clear();
1812 return OK;
1813 }
1814
attachAndQueueBuffer(Surface * surface,sp<GraphicBuffer> buffer)1815 status_t Surface::attachAndQueueBuffer(Surface* surface, sp<GraphicBuffer> buffer) {
1816 if (buffer == nullptr) {
1817 return BAD_VALUE;
1818 }
1819 int err = static_cast<ANativeWindow*>(surface)->perform(surface, NATIVE_WINDOW_API_CONNECT,
1820 NATIVE_WINDOW_API_CPU);
1821 if (err != OK) {
1822 return err;
1823 }
1824 err = surface->attachBuffer(buffer->getNativeBuffer());
1825 if (err != OK) {
1826 return err;
1827 }
1828 err = static_cast<ANativeWindow*>(surface)->queueBuffer(surface, buffer->getNativeBuffer(), -1);
1829 if (err != OK) {
1830 return err;
1831 }
1832 err = surface->disconnect(NATIVE_WINDOW_API_CPU);
1833 return err;
1834 }
1835
1836 }; // namespace android
1837