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