| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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" | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 19 | //#define LOG_NDEBUG 0 | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 |  | 
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 21 | #include <gui/Surface.h> | 
| Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 22 |  | 
| Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 23 | #include <condition_variable> | 
|  | 24 | #include <deque> | 
|  | 25 | #include <mutex> | 
|  | 26 | #include <thread> | 
|  | 27 |  | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 28 | #include <inttypes.h> | 
|  | 29 |  | 
| Mathias Agopian | 05debe1 | 2017-02-08 17:04:18 -0800 | [diff] [blame] | 30 | #include <android/native_window.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 31 |  | 
| Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> | 
| Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 34 | #include <utils/NativeHandle.h> | 
| Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 35 |  | 
| Ian Elliott | 62c48c9 | 2017-01-20 13:13:20 -0700 | [diff] [blame] | 36 | #include <ui/DisplayStatInfo.h> | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 37 | #include <ui/Fence.h> | 
| Shuzhen Wang | 8a9d62f | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 38 | #include <ui/GraphicBuffer.h> | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 39 | #include <ui/HdrCapabilities.h> | 
|  | 40 | #include <ui/Region.h> | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 41 |  | 
| Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 42 | #include <gui/BufferItem.h> | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 43 | #include <gui/IProducerListener.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 44 |  | 
| Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 45 | #include <gui/ISurfaceComposer.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 46 | #include <private/gui/ComposerService.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 47 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | namespace android { | 
|  | 49 |  | 
| Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 50 | using ui::ColorMode; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 51 | using ui::Dataspace; | 
| Peiyong Lin | fd997e0 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 52 |  | 
| Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 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>()) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 87 | mDataSpace = Dataspace::UNKNOWN; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 88 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; | 
|  | 89 | mTransform = 0; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 90 | mStickyTransform = 0; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 91 | mDefaultWidth = 0; | 
|  | 92 | mDefaultHeight = 0; | 
|  | 93 | mUserWidth = 0; | 
|  | 94 | mUserHeight = 0; | 
|  | 95 | mTransformHint = 0; | 
|  | 96 | mConsumerRunningBehind = false; | 
|  | 97 | mConnectedToCpu = false; | 
| Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 98 | mProducerControlledByApp = controlledByApp; | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 99 | mSwapIntervalZero = false; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 102 | Surface::~Surface() { | 
|  | 103 | if (mConnectedToCpu) { | 
|  | 104 | Surface::disconnect(NATIVE_WINDOW_API_CPU); | 
|  | 105 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 108 | sp<ISurfaceComposer> Surface::composerService() const { | 
|  | 109 | return ComposerService::getComposerService(); | 
|  | 110 | } | 
|  | 111 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 112 | nsecs_t Surface::now() const { | 
|  | 113 | return systemTime(); | 
|  | 114 | } | 
|  | 115 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 116 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { | 
|  | 117 | return mGraphicBufferProducer; | 
|  | 118 | } | 
|  | 119 |  | 
| Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 120 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { | 
|  | 121 | mGraphicBufferProducer->setSidebandStream(stream); | 
|  | 122 | } | 
|  | 123 |  | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 124 | void Surface::allocateBuffers() { | 
|  | 125 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; | 
|  | 126 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 127 | mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight, | 
|  | 128 | mReqFormat, mReqUsage); | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 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 |  | 
| Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 139 | uint64_t Surface::getNextFrameNumber() const { | 
| Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 140 | Mutex::Autolock lock(mMutex); | 
|  | 141 | return mNextFrameNumber; | 
| Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Dan Stoza | c6f30bd | 2015-06-08 09:32:50 -0700 | [diff] [blame] | 144 | String8 Surface::getConsumerName() const { | 
|  | 145 | return mGraphicBufferProducer->getConsumerName(); | 
|  | 146 | } | 
|  | 147 |  | 
| Dan Stoza | 127fc63 | 2015-06-30 13:43:32 -0700 | [diff] [blame] | 148 | status_t Surface::setDequeueTimeout(nsecs_t timeout) { | 
|  | 149 | return mGraphicBufferProducer->setDequeueTimeout(timeout); | 
|  | 150 | } | 
|  | 151 |  | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 152 | status_t Surface::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, | 
| John Reck | 1a61da5 | 2016-04-28 13:18:15 -0700 | [diff] [blame] | 153 | sp<Fence>* outFence, float outTransformMatrix[16]) { | 
|  | 154 | return mGraphicBufferProducer->getLastQueuedBuffer(outBuffer, outFence, | 
|  | 155 | outTransformMatrix); | 
| Dan Stoza | 50101d0 | 2016-04-07 16:53:23 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 158 | status_t Surface::getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration) { | 
|  | 159 | ATRACE_CALL(); | 
|  | 160 |  | 
|  | 161 | DisplayStatInfo stats; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 162 | status_t result = composerService()->getDisplayStats(nullptr, &stats); | 
| Chih-Hung Hsieh | d66be0a | 2017-10-05 13:51:32 -0700 | [diff] [blame] | 163 | if (result != NO_ERROR) { | 
|  | 164 | return result; | 
|  | 165 | } | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 166 |  | 
|  | 167 | *outRefreshDuration = stats.vsyncPeriod; | 
|  | 168 |  | 
|  | 169 | return NO_ERROR; | 
|  | 170 | } | 
|  | 171 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 172 | void Surface::enableFrameTimestamps(bool enable) { | 
|  | 173 | Mutex::Autolock lock(mMutex); | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 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 | } | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 181 | mEnableFrameTimestamps = enable; | 
|  | 182 | } | 
|  | 183 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 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 |  | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 206 | static bool checkConsumerForUpdates( | 
|  | 207 | const FrameEvents* e, const uint64_t lastFrameNumber, | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 208 | const nsecs_t* outLatchTime, | 
|  | 209 | const nsecs_t* outFirstRefreshStartTime, | 
|  | 210 | const nsecs_t* outLastRefreshStartTime, | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 211 | const nsecs_t* outGpuCompositionDoneTime, | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 212 | const nsecs_t* outDisplayPresentTime, | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 213 | const nsecs_t* outDequeueReadyTime, | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 214 | const nsecs_t* outReleaseTime) { | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 215 | bool checkForLatch = (outLatchTime != nullptr) && !e->hasLatchInfo(); | 
|  | 216 | bool checkForFirstRefreshStart = (outFirstRefreshStartTime != nullptr) && | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 217 | !e->hasFirstRefreshStartInfo(); | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 218 | bool checkForGpuCompositionDone = (outGpuCompositionDoneTime != nullptr) && | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 219 | !e->hasGpuCompositionDoneInfo(); | 
|  | 220 | bool checkForDisplayPresent = (outDisplayPresentTime != nullptr) && | 
|  | 221 | !e->hasDisplayPresentInfo(); | 
|  | 222 |  | 
| Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 223 | // LastRefreshStart, DequeueReady, and Release are never available for the | 
|  | 224 | // last frame. | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 225 | bool checkForLastRefreshStart = (outLastRefreshStartTime != nullptr) && | 
|  | 226 | !e->hasLastRefreshStartInfo() && | 
|  | 227 | (e->frameNumber != lastFrameNumber); | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 228 | bool checkForDequeueReady = (outDequeueReadyTime != nullptr) && | 
|  | 229 | !e->hasDequeueReadyInfo() && (e->frameNumber != lastFrameNumber); | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 230 | bool checkForRelease = (outReleaseTime != nullptr) && | 
|  | 231 | !e->hasReleaseInfo() && (e->frameNumber != lastFrameNumber); | 
|  | 232 |  | 
|  | 233 | // RequestedPresent and Acquire info are always available producer-side. | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 234 | return checkForLatch || checkForFirstRefreshStart || | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 235 | checkForLastRefreshStart || checkForGpuCompositionDone || | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 236 | checkForDisplayPresent || checkForDequeueReady || checkForRelease; | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 239 | static void getFrameTimestamp(nsecs_t *dst, const nsecs_t& src) { | 
|  | 240 | if (dst != nullptr) { | 
| Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 241 | // We always get valid timestamps for these eventually. | 
|  | 242 | *dst = (src == FrameEvents::TIMESTAMP_PENDING) ? | 
|  | 243 | NATIVE_WINDOW_TIMESTAMP_PENDING : src; | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 244 | } | 
|  | 245 | } | 
|  | 246 |  | 
| Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 247 | static void getFrameTimestampFence(nsecs_t *dst, | 
|  | 248 | const std::shared_ptr<FenceTime>& src, bool fenceShouldBeKnown) { | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 249 | if (dst != nullptr) { | 
| Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 250 | if (!fenceShouldBeKnown) { | 
|  | 251 | *dst = NATIVE_WINDOW_TIMESTAMP_PENDING; | 
|  | 252 | return; | 
|  | 253 | } | 
|  | 254 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 255 | nsecs_t signalTime = src->getSignalTime(); | 
| Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 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; | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 261 | } | 
|  | 262 | } | 
|  | 263 |  | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 264 | status_t Surface::getFrameTimestamps(uint64_t frameNumber, | 
| Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 265 | nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime, | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 266 | nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime, | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 267 | nsecs_t* outLastRefreshStartTime, nsecs_t* outGpuCompositionDoneTime, | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 268 | nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime, | 
|  | 269 | nsecs_t* outReleaseTime) { | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 270 | ATRACE_CALL(); | 
|  | 271 |  | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 272 | Mutex::Autolock lock(mMutex); | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 273 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 274 | if (!mEnableFrameTimestamps) { | 
|  | 275 | return INVALID_OPERATION; | 
|  | 276 | } | 
|  | 277 |  | 
| Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 278 | // Verify the requested timestamps are supported. | 
|  | 279 | querySupportedTimestampsLocked(); | 
|  | 280 | if (outDisplayPresentTime != nullptr && !mFrameTimestampsSupportsPresent) { | 
|  | 281 | return BAD_VALUE; | 
|  | 282 | } | 
|  | 283 |  | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 284 | FrameEvents* events = mFrameEventHistory->getFrame(frameNumber); | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 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; | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 289 | } | 
|  | 290 |  | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 291 | // Update our cache of events if the requested events are not available. | 
|  | 292 | if (checkConsumerForUpdates(events, mLastFrameNumber, | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 293 | outLatchTime, outFirstRefreshStartTime, outLastRefreshStartTime, | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 294 | outGpuCompositionDoneTime, outDisplayPresentTime, | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 295 | outDequeueReadyTime, outReleaseTime)) { | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 296 | FrameEventHistoryDelta delta; | 
|  | 297 | mGraphicBufferProducer->getFrameTimestamps(&delta); | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 298 | mFrameEventHistory->applyDelta(delta); | 
|  | 299 | events = mFrameEventHistory->getFrame(frameNumber); | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 302 | if (events == nullptr) { | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 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. | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 305 | return NAME_NOT_FOUND; | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 306 | } | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 307 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 308 | getFrameTimestamp(outRequestedPresentTime, events->requestedPresentTime); | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 309 | getFrameTimestamp(outLatchTime, events->latchTime); | 
|  | 310 | getFrameTimestamp(outFirstRefreshStartTime, events->firstRefreshStartTime); | 
|  | 311 | getFrameTimestamp(outLastRefreshStartTime, events->lastRefreshStartTime); | 
|  | 312 | getFrameTimestamp(outDequeueReadyTime, events->dequeueReadyTime); | 
| Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 313 |  | 
| Brian Anderson | dc96fdf | 2017-03-20 16:54:25 -0700 | [diff] [blame] | 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()); | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 323 |  | 
|  | 324 | return NO_ERROR; | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
| Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 327 | status_t Surface::getWideColorSupport(bool* supported) { | 
|  | 328 | ATRACE_CALL(); | 
|  | 329 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 330 | const sp<IBinder> display = composerService()->getInternalDisplayToken(); | 
|  | 331 | if (display == nullptr) { | 
|  | 332 | return NAME_NOT_FOUND; | 
|  | 333 | } | 
|  | 334 |  | 
| Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 335 | *supported = false; | 
| Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 336 | status_t error = composerService()->isWideColorDisplay(display, supported); | 
|  | 337 | return error; | 
| Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 340 | status_t Surface::getHdrSupport(bool* supported) { | 
|  | 341 | ATRACE_CALL(); | 
|  | 342 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 343 | const sp<IBinder> display = composerService()->getInternalDisplayToken(); | 
|  | 344 | if (display == nullptr) { | 
|  | 345 | return NAME_NOT_FOUND; | 
|  | 346 | } | 
|  | 347 |  | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 360 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { | 
|  | 361 | Surface* c = getSelf(window); | 
|  | 362 | return c->setSwapInterval(interval); | 
|  | 363 | } | 
|  | 364 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 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); | 
| Mike Stroyan | 87709c9 | 2016-06-03 12:43:26 -0600 | [diff] [blame] | 389 | if (result != OK) { | 
|  | 390 | return result; | 
|  | 391 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 392 | sp<Fence> fence(new Fence(fenceFd)); | 
| Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 393 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 394 | if (waitResult != OK) { | 
|  | 395 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", | 
|  | 396 | waitResult); | 
|  | 397 | c->cancelBuffer(buf, -1); | 
|  | 398 | return waitResult; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 399 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 400 | *buffer = buf; | 
|  | 401 | return result; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 402 | } | 
|  | 403 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 404 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 405 | ANativeWindowBuffer* buffer) { | 
|  | 406 | Surface* c = getSelf(window); | 
|  | 407 | return c->cancelBuffer(buffer, -1); | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 408 | } | 
|  | 409 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 410 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 411 | ANativeWindowBuffer* buffer) { | 
|  | 412 | Surface* c = getSelf(window); | 
|  | 413 | return c->lockBuffer_DEPRECATED(buffer); | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 416 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 417 | ANativeWindowBuffer* buffer) { | 
|  | 418 | Surface* c = getSelf(window); | 
|  | 419 | return c->queueBuffer(buffer, -1); | 
| Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 420 | } | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 421 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
|  | 428 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { | 
|  | 429 | va_list args; | 
|  | 430 | va_start(args, operation); | 
|  | 431 | Surface* c = getSelf(window); | 
| Haixia Shi | d89c2bb | 2015-09-14 11:02:18 -0700 | [diff] [blame] | 432 | int result = c->perform(operation, args); | 
|  | 433 | va_end(args); | 
|  | 434 | return result; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 435 | } | 
|  | 436 |  | 
|  | 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. | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 442 |  | 
|  | 443 | if (interval < minSwapInterval) | 
|  | 444 | interval = minSwapInterval; | 
|  | 445 |  | 
|  | 446 | if (interval > maxSwapInterval) | 
|  | 447 | interval = maxSwapInterval; | 
|  | 448 |  | 
| Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 449 | const bool wasSwapIntervalZero = mSwapIntervalZero; | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 450 | mSwapIntervalZero = (interval == 0); | 
| Jorim Jaggi | 0a3e784 | 2018-07-17 13:48:33 +0200 | [diff] [blame] | 451 |  | 
|  | 452 | if (mSwapIntervalZero != wasSwapIntervalZero) { | 
|  | 453 | mGraphicBufferProducer->setAsyncMode(mSwapIntervalZero); | 
|  | 454 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 455 |  | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 456 | return NO_ERROR; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 457 | } | 
|  | 458 |  | 
| Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 459 | class FenceMonitor { | 
|  | 460 | public: | 
|  | 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 |  | 
|  | 467 | void queueFence(const sp<Fence>& fence) { | 
|  | 468 | char message[64]; | 
|  | 469 |  | 
|  | 470 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 2817813 | 2019-03-08 17:14:53 -0800 | [diff] [blame] | 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); | 
| Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 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" | 
|  | 491 | void loop() { | 
|  | 492 | while (true) { | 
|  | 493 | threadLoop(); | 
|  | 494 | } | 
|  | 495 | } | 
|  | 496 | #pragma clang diagnostic pop | 
|  | 497 |  | 
|  | 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 |  | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 535 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 536 | ATRACE_CALL(); | 
|  | 537 | ALOGV("Surface::dequeueBuffer"); | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 538 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 539 | uint32_t reqWidth; | 
|  | 540 | uint32_t reqHeight; | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 541 | PixelFormat reqFormat; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 542 | uint64_t reqUsage; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 543 | bool enableFrameTimestamps; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 544 |  | 
|  | 545 | { | 
|  | 546 | Mutex::Autolock lock(mMutex); | 
| Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 547 | if (mReportRemovedBuffers) { | 
|  | 548 | mRemovedBuffers.clear(); | 
|  | 549 | } | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 550 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 551 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; | 
|  | 552 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 553 |  | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 554 | reqFormat = mReqFormat; | 
|  | 555 | reqUsage = mReqUsage; | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 556 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 557 | enableFrameTimestamps = mEnableFrameTimestamps; | 
|  | 558 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 559 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot != | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 560 | BufferItem::INVALID_BUFFER_SLOT) { | 
|  | 561 | sp<GraphicBuffer>& gbuf(mSlots[mSharedBufferSlot].buffer); | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 562 | if (gbuf != nullptr) { | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 563 | *buffer = gbuf.get(); | 
|  | 564 | *fenceFd = -1; | 
|  | 565 | return OK; | 
|  | 566 | } | 
|  | 567 | } | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 568 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer | 
|  | 569 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 570 | int buf = -1; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 571 | sp<Fence> fence; | 
| Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 572 | nsecs_t startTime = systemTime(); | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 573 |  | 
|  | 574 | FrameEventHistoryDelta frameTimestamps; | 
| Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 575 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, reqWidth, reqHeight, | 
|  | 576 | reqFormat, reqUsage, &mBufferAge, | 
|  | 577 | enableFrameTimestamps ? &frameTimestamps | 
|  | 578 | : nullptr); | 
| Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 579 | mLastDequeueDuration = systemTime() - startTime; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 580 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 581 | if (result < 0) { | 
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame] | 582 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer" | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 583 | "(%d, %d, %d, %#" PRIx64 ") failed: %d", | 
|  | 584 | reqWidth, reqHeight, reqFormat, reqUsage, result); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 585 | return result; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 586 | } | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 587 |  | 
| Dan Stoza | 90ce2a9 | 2017-05-01 16:31:53 -0700 | [diff] [blame] | 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 |  | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 594 | Mutex::Autolock lock(mMutex); | 
|  | 595 |  | 
| Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 596 | // Write this while holding the mutex | 
|  | 597 | mLastDequeueStartTime = startTime; | 
|  | 598 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 599 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 600 |  | 
|  | 601 | // this should never happen | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 602 | ALOGE_IF(fence == nullptr, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 603 |  | 
| Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 604 | if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) { | 
|  | 605 | static FenceMonitor hwcReleaseThread("HWC release"); | 
|  | 606 | hwcReleaseThread.queueFence(fence); | 
|  | 607 | } | 
|  | 608 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 609 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { | 
|  | 610 | freeAllBuffers(); | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 611 | } | 
| Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 612 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 613 | if (enableFrameTimestamps) { | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 614 | mFrameEventHistory->applyDelta(frameTimestamps); | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 615 | } | 
|  | 616 |  | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 617 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == nullptr) { | 
|  | 618 | if (mReportRemovedBuffers && (gbuf != nullptr)) { | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 619 | mRemovedBuffers.push_back(gbuf); | 
|  | 620 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 621 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); | 
|  | 622 | if (result != NO_ERROR) { | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 623 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); | 
| Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 624 | mGraphicBufferProducer->cancelBuffer(buf, fence); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 625 | return result; | 
|  | 626 | } | 
|  | 627 | } | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 628 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 | } | 
| Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 637 | } else { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 638 | *fenceFd = -1; | 
| Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 639 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 640 |  | 
|  | 641 | *buffer = gbuf.get(); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 642 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 643 | if (mSharedBufferMode && mAutoRefresh) { | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 644 | mSharedBufferSlot = buf; | 
|  | 645 | mSharedBufferHasBeenQueued = false; | 
|  | 646 | } else if (mSharedBufferSlot == buf) { | 
|  | 647 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 648 | mSharedBufferHasBeenQueued = false; | 
|  | 649 | } | 
|  | 650 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 651 | return OK; | 
| Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 652 | } | 
|  | 653 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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) { | 
| Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 661 | if (fenceFd >= 0) { | 
|  | 662 | close(fenceFd); | 
|  | 663 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 664 | return i; | 
|  | 665 | } | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 666 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { | 
|  | 667 | if (fenceFd >= 0) { | 
|  | 668 | close(fenceFd); | 
|  | 669 | } | 
|  | 670 | return OK; | 
|  | 671 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 672 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); | 
|  | 673 | mGraphicBufferProducer->cancelBuffer(i, fence); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 674 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 675 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 676 | mSharedBufferHasBeenQueued = true; | 
|  | 677 | } | 
|  | 678 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 679 | return OK; | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | int Surface::getSlotFromBufferLocked( | 
|  | 683 | android_native_buffer_t* buffer) const { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 684 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 685 | if (mSlots[i].buffer != nullptr && | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 686 | mSlots[i].buffer->handle == buffer->handle) { | 
|  | 687 | return i; | 
| Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 688 | } | 
|  | 689 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 690 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); | 
|  | 691 | return BAD_VALUE; | 
| Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 692 | } | 
|  | 693 |  | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 694 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 695 | ALOGV("Surface::lockBuffer"); | 
|  | 696 | Mutex::Autolock lock(mMutex); | 
|  | 697 | return OK; | 
|  | 698 | } | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 699 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 705 | bool isAutoTimestamp = false; | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 706 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 707 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { | 
| Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 708 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 709 | isAutoTimestamp = true; | 
| Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 710 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", | 
| Colin Cross | 9a80d50 | 2016-09-27 14:12:48 -0700 | [diff] [blame] | 711 | timestamp / 1000000.0); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 712 | } else { | 
|  | 713 | timestamp = mTimestamp; | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 714 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 715 | int i = getSlotFromBufferLocked(buffer); | 
|  | 716 | if (i < 0) { | 
| Taiju Tsuiki | 4d0cd3f | 2015-04-30 22:15:33 +0900 | [diff] [blame] | 717 | if (fenceFd >= 0) { | 
|  | 718 | close(fenceFd); | 
|  | 719 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 720 | return i; | 
|  | 721 | } | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 722 | if (mSharedBufferSlot == i && mSharedBufferHasBeenQueued) { | 
|  | 723 | if (fenceFd >= 0) { | 
|  | 724 | close(fenceFd); | 
|  | 725 | } | 
|  | 726 | return OK; | 
|  | 727 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 730 | // Make sure the crop rectangle is entirely inside the buffer. | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 731 | Rect crop(Rect::EMPTY_RECT); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 732 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 734 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); | 
|  | 735 | IGraphicBufferProducer::QueueBufferOutput output; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 736 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 737 | static_cast<android_dataspace>(mDataSpace), crop, mScalingMode, | 
|  | 738 | mTransform ^ mStickyTransform, fence, mStickyTransform, | 
|  | 739 | mEnableFrameTimestamps); | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 740 |  | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 741 | // we should send HDR metadata as needed if this becomes a bottleneck | 
|  | 742 | input.setHdrMetadata(mHdrMetadata); | 
|  | 743 |  | 
| Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 744 | if (mConnectedToCpu || mDirtyRegion.bounds() == Rect::INVALID_RECT) { | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 745 | input.setSurfaceDamage(Region::INVALID_REGION); | 
|  | 746 | } else { | 
| Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 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; | 
| Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 762 | int height = buffer->height; | 
| Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 763 | bool rotated90 = (mTransform ^ mStickyTransform) & | 
|  | 764 | NATIVE_WINDOW_TRANSFORM_ROT_90; | 
|  | 765 | if (rotated90) { | 
|  | 766 | std::swap(width, height); | 
| Dan Stoza | 0e65e6c | 2015-05-26 13:22:27 -0700 | [diff] [blame] | 767 | } | 
| Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 768 |  | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 769 | Region flippedRegion; | 
|  | 770 | for (auto rect : mDirtyRegion) { | 
| Dan Stoza | db4850c | 2015-06-25 16:10:18 -0700 | [diff] [blame] | 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 | } | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
|  | 804 | input.setSurfaceDamage(flippedRegion); | 
|  | 805 | } | 
|  | 806 |  | 
| Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 807 | nsecs_t now = systemTime(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 808 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); | 
| Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 809 | mLastQueueDuration = systemTime() - now; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 810 | if (err != OK)  { | 
|  | 811 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); | 
|  | 812 | } | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 813 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 814 | if (mEnableFrameTimestamps) { | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 815 | mFrameEventHistory->applyDelta(output.frameTimestamps); | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 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. | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 819 | mFrameEventHistory->updateAcquireFence(mNextFrameNumber, | 
| Yiwei Zhang | f0c1250 | 2019-03-13 16:09:05 -0700 | [diff] [blame] | 820 | std::make_shared<FenceTime>(fence)); | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 821 |  | 
|  | 822 | // Cache timestamps of signaled fences so we can close their file | 
|  | 823 | // descriptors. | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 824 | mFrameEventHistory->updateSignalTimes(); | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 825 | } | 
|  | 826 |  | 
| Brian Anderson | 50143b3 | 2016-09-30 14:01:24 -0700 | [diff] [blame] | 827 | mLastFrameNumber = mNextFrameNumber; | 
|  | 828 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 829 | mDefaultWidth = output.width; | 
|  | 830 | mDefaultHeight = output.height; | 
|  | 831 | mNextFrameNumber = output.nextFrameNumber; | 
| tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 832 |  | 
| Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 833 | // Ignore transform hint if sticky transform is set or transform to display inverse flag is | 
|  | 834 | // set. | 
|  | 835 | if (mStickyTransform == 0 && !transformToDisplayInverse()) { | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 836 | mTransformHint = output.transformHint; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 837 | } | 
|  | 838 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 839 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 840 |  | 
| Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 841 | if (!mConnectedToCpu) { | 
|  | 842 | // Clear surface damage back to full-buffer | 
|  | 843 | mDirtyRegion = Region::INVALID_REGION; | 
|  | 844 | } | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 845 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 846 | if (mSharedBufferMode && mAutoRefresh && mSharedBufferSlot == i) { | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 847 | mSharedBufferHasBeenQueued = true; | 
|  | 848 | } | 
|  | 849 |  | 
| Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 850 | mQueueBufferCondition.broadcast(); | 
|  | 851 |  | 
| Yiwei Zhang | 9e919eb | 2019-03-07 23:21:40 -0800 | [diff] [blame] | 852 | if (CC_UNLIKELY(atrace_is_tag_enabled(ATRACE_TAG_GRAPHICS))) { | 
|  | 853 | static FenceMonitor gpuCompletionThread("GPU completion"); | 
|  | 854 | gpuCompletionThread.queueFence(fence); | 
|  | 855 | } | 
|  | 856 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 857 | return err; | 
|  | 858 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 859 |  | 
| Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 860 | 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 |  | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 883 | int Surface::query(int what, int* value) const { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 891 | *value = static_cast<int>(mReqFormat); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 892 | return NO_ERROR; | 
|  | 893 | } | 
|  | 894 | break; | 
|  | 895 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { | 
| Brian Anderson | 3da8d27 | 2016-07-28 16:20:47 -0700 | [diff] [blame] | 896 | if (composerService()->authenticateSurfaceTexture( | 
|  | 897 | mGraphicBufferProducer)) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 908 | *value = static_cast<int>( | 
|  | 909 | mUserWidth ? mUserWidth : mDefaultWidth); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 910 | return NO_ERROR; | 
|  | 911 | case NATIVE_WINDOW_DEFAULT_HEIGHT: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 912 | *value = static_cast<int>( | 
|  | 913 | mUserHeight ? mUserHeight : mDefaultHeight); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 914 | return NO_ERROR; | 
|  | 915 | case NATIVE_WINDOW_TRANSFORM_HINT: | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 916 | *value = static_cast<int>(mTransformHint); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 | } | 
| Ian Elliott | a2eb34c | 2017-07-18 11:05:49 -0600 | [diff] [blame] | 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 | } | 
| Dan Stoza | 70ccba5 | 2016-07-01 14:00:40 -0700 | [diff] [blame] | 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 | } | 
| Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 952 | case NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT: { | 
|  | 953 | querySupportedTimestampsLocked(); | 
|  | 954 | *value = mFrameTimestampsSupportsPresent ? 1 : 0; | 
|  | 955 | return NO_ERROR; | 
|  | 956 | } | 
| Mathias Agopian | 10e9ab5 | 2017-03-08 15:02:55 -0800 | [diff] [blame] | 957 | case NATIVE_WINDOW_IS_VALID: { | 
|  | 958 | *value = mGraphicBufferProducer != nullptr ? 1 : 0; | 
|  | 959 | return NO_ERROR; | 
|  | 960 | } | 
| Peiyong Lin | 654f87b | 2018-01-30 14:21:33 -0800 | [diff] [blame] | 961 | case NATIVE_WINDOW_DATASPACE: { | 
|  | 962 | *value = static_cast<int>(mDataSpace); | 
|  | 963 | return NO_ERROR; | 
|  | 964 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 965 | } | 
| Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 966 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 967 | return mGraphicBufferProducer->query(what, value); | 
| Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 968 | } | 
|  | 969 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 995 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: | 
|  | 996 | res = dispatchSetBuffersStickyTransform(args); | 
|  | 997 | break; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 1025 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: | 
|  | 1026 | res = dispatchSetSidebandStream(args); | 
|  | 1027 | break; | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1028 | case NATIVE_WINDOW_SET_BUFFERS_DATASPACE: | 
|  | 1029 | res = dispatchSetBuffersDataSpace(args); | 
|  | 1030 | break; | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 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; | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 1037 | case NATIVE_WINDOW_SET_BUFFERS_HDR10_PLUS_METADATA: | 
|  | 1038 | res = dispatchSetBuffersHdr10PlusMetadata(args); | 
|  | 1039 | break; | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1040 | case NATIVE_WINDOW_SET_SURFACE_DAMAGE: | 
|  | 1041 | res = dispatchSetSurfaceDamage(args); | 
|  | 1042 | break; | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1043 | case NATIVE_WINDOW_SET_SHARED_BUFFER_MODE: | 
|  | 1044 | res = dispatchSetSharedBufferMode(args); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1045 | break; | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1046 | case NATIVE_WINDOW_SET_AUTO_REFRESH: | 
|  | 1047 | res = dispatchSetAutoRefresh(args); | 
|  | 1048 | break; | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1049 | case NATIVE_WINDOW_GET_REFRESH_CYCLE_DURATION: | 
|  | 1050 | res = dispatchGetDisplayRefreshCycleDuration(args); | 
|  | 1051 | break; | 
| Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1052 | case NATIVE_WINDOW_GET_NEXT_FRAME_ID: | 
|  | 1053 | res = dispatchGetNextFrameId(args); | 
|  | 1054 | break; | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1055 | case NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS: | 
|  | 1056 | res = dispatchEnableFrameTimestamps(args); | 
|  | 1057 | break; | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1058 | case NATIVE_WINDOW_GET_COMPOSITOR_TIMING: | 
|  | 1059 | res = dispatchGetCompositorTiming(args); | 
|  | 1060 | break; | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1061 | case NATIVE_WINDOW_GET_FRAME_TIMESTAMPS: | 
|  | 1062 | res = dispatchGetFrameTimestamps(args); | 
|  | 1063 | break; | 
| Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1064 | case NATIVE_WINDOW_GET_WIDE_COLOR_SUPPORT: | 
|  | 1065 | res = dispatchGetWideColorSupport(args); | 
|  | 1066 | break; | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1067 | case NATIVE_WINDOW_GET_HDR_SUPPORT: | 
|  | 1068 | res = dispatchGetHdrSupport(args); | 
|  | 1069 | break; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1070 | case NATIVE_WINDOW_SET_USAGE64: | 
|  | 1071 | res = dispatchSetUsage64(args); | 
|  | 1072 | break; | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1073 | case NATIVE_WINDOW_GET_CONSUMER_USAGE64: | 
|  | 1074 | res = dispatchGetConsumerUsage64(args); | 
|  | 1075 | break; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1076 | default: | 
|  | 1077 | res = NAME_NOT_FOUND; | 
|  | 1078 | break; | 
|  | 1079 | } | 
|  | 1080 | return res; | 
|  | 1081 | } | 
| Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1082 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1083 | int Surface::dispatchConnect(va_list args) { | 
|  | 1084 | int api = va_arg(args, int); | 
|  | 1085 | return connect(api); | 
|  | 1086 | } | 
| Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1087 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1088 | int Surface::dispatchDisconnect(va_list args) { | 
|  | 1089 | int api = va_arg(args, int); | 
|  | 1090 | return disconnect(api); | 
|  | 1091 | } | 
|  | 1092 |  | 
|  | 1093 | int Surface::dispatchSetUsage(va_list args) { | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1094 | uint64_t usage = va_arg(args, uint32_t); | 
|  | 1095 | return setUsage(usage); | 
|  | 1096 | } | 
|  | 1097 |  | 
|  | 1098 | int Surface::dispatchSetUsage64(va_list args) { | 
|  | 1099 | uint64_t usage = va_arg(args, uint64_t); | 
|  | 1100 | return setUsage(usage); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1101 | } | 
|  | 1102 |  | 
|  | 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 |  | 
|  | 1108 | int Surface::dispatchSetBufferCount(va_list args) { | 
|  | 1109 | size_t bufferCount = va_arg(args, size_t); | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1110 | return setBufferCount(static_cast<int32_t>(bufferCount)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1111 | } | 
|  | 1112 |  | 
|  | 1113 | int Surface::dispatchSetBuffersGeometry(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 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); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1118 | if (err != 0) { | 
|  | 1119 | return err; | 
|  | 1120 | } | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1121 | return setBuffersFormat(format); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1122 | } | 
|  | 1123 |  | 
|  | 1124 | int Surface::dispatchSetBuffersDimensions(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1125 | uint32_t width = va_arg(args, uint32_t); | 
|  | 1126 | uint32_t height = va_arg(args, uint32_t); | 
|  | 1127 | return setBuffersDimensions(width, height); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1128 | } | 
|  | 1129 |  | 
|  | 1130 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1131 | uint32_t width = va_arg(args, uint32_t); | 
|  | 1132 | uint32_t height = va_arg(args, uint32_t); | 
|  | 1133 | return setBuffersUserDimensions(width, height); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1134 | } | 
|  | 1135 |  | 
|  | 1136 | int Surface::dispatchSetBuffersFormat(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1137 | PixelFormat format = va_arg(args, PixelFormat); | 
|  | 1138 | return setBuffersFormat(format); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | int Surface::dispatchSetScalingMode(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1142 | int mode = va_arg(args, int); | 
|  | 1143 | return setScalingMode(mode); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | int Surface::dispatchSetBuffersTransform(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1147 | uint32_t transform = va_arg(args, uint32_t); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1148 | return setBuffersTransform(transform); | 
|  | 1149 | } | 
|  | 1150 |  | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1151 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1152 | uint32_t transform = va_arg(args, uint32_t); | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1153 | return setBuffersStickyTransform(transform); | 
|  | 1154 | } | 
|  | 1155 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1156 | int Surface::dispatchSetBuffersTimestamp(va_list args) { | 
|  | 1157 | int64_t timestamp = va_arg(args, int64_t); | 
|  | 1158 | return setBuffersTimestamp(timestamp); | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 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 |  | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 1167 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1168 | return unlockAndPost(); | 
|  | 1169 | } | 
|  | 1170 |  | 
| Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 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 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1177 |  | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1178 | int Surface::dispatchSetBuffersDataSpace(va_list args) { | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 1179 | Dataspace dataspace = static_cast<Dataspace>(va_arg(args, int)); | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1180 | return setBuffersDataSpace(dataspace); | 
|  | 1181 | } | 
|  | 1182 |  | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 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 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1208 | int Surface::dispatchSetSharedBufferMode(va_list args) { | 
|  | 1209 | bool sharedBufferMode = va_arg(args, int); | 
|  | 1210 | return setSharedBufferMode(sharedBufferMode); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1211 | } | 
|  | 1212 |  | 
|  | 1213 | int Surface::dispatchSetAutoRefresh(va_list args) { | 
|  | 1214 | bool autoRefresh = va_arg(args, int); | 
|  | 1215 | return setAutoRefresh(autoRefresh); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1216 | } | 
|  | 1217 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1218 | int Surface::dispatchGetDisplayRefreshCycleDuration(va_list args) { | 
|  | 1219 | nsecs_t* outRefreshDuration = va_arg(args, int64_t*); | 
|  | 1220 | return getDisplayRefreshCycleDuration(outRefreshDuration); | 
|  | 1221 | } | 
|  | 1222 |  | 
| Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 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 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1229 | int Surface::dispatchEnableFrameTimestamps(va_list args) { | 
|  | 1230 | bool enable = va_arg(args, int); | 
|  | 1231 | enableFrameTimestamps(enable); | 
|  | 1232 | return NO_ERROR; | 
|  | 1233 | } | 
|  | 1234 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 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 |  | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1243 | int Surface::dispatchGetFrameTimestamps(va_list args) { | 
| Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1244 | uint64_t frameId = va_arg(args, uint64_t); | 
| Brian Anderson | dbd0ea8 | 2016-07-22 09:38:59 -0700 | [diff] [blame] | 1245 | nsecs_t* outRequestedPresentTime = va_arg(args, int64_t*); | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1246 | nsecs_t* outAcquireTime = va_arg(args, int64_t*); | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 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*); | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1250 | nsecs_t* outGpuCompositionDoneTime = va_arg(args, int64_t*); | 
| Brian Anderson | 069b365 | 2016-07-22 10:32:47 -0700 | [diff] [blame] | 1251 | nsecs_t* outDisplayPresentTime = va_arg(args, int64_t*); | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1252 | nsecs_t* outDequeueReadyTime = va_arg(args, int64_t*); | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1253 | nsecs_t* outReleaseTime = va_arg(args, int64_t*); | 
| Brian Anderson | 1049d1d | 2016-12-16 17:25:57 -0800 | [diff] [blame] | 1254 | return getFrameTimestamps(frameId, | 
| Brian Anderson | f7fd56a | 2016-09-02 10:10:04 -0700 | [diff] [blame] | 1255 | outRequestedPresentTime, outAcquireTime, outLatchTime, | 
|  | 1256 | outFirstRefreshStartTime, outLastRefreshStartTime, | 
| Brian Anderson | b04c6f0 | 2016-10-21 12:57:46 -0700 | [diff] [blame] | 1257 | outGpuCompositionDoneTime, outDisplayPresentTime, | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1258 | outDequeueReadyTime, outReleaseTime); | 
| Pablo Ceballos | ce796e7 | 2016-02-04 19:10:51 -0800 | [diff] [blame] | 1259 | } | 
|  | 1260 |  | 
| Courtney Goeltzenleuchter | 1eb1b27 | 2017-02-02 16:51:06 -0700 | [diff] [blame] | 1261 | int Surface::dispatchGetWideColorSupport(va_list args) { | 
|  | 1262 | bool* outSupport = va_arg(args, bool*); | 
|  | 1263 | return getWideColorSupport(outSupport); | 
|  | 1264 | } | 
|  | 1265 |  | 
| Courtney Goeltzenleuchter | c5b97c5 | 2017-02-26 14:47:13 -0700 | [diff] [blame] | 1266 | int Surface::dispatchGetHdrSupport(va_list args) { | 
|  | 1267 | bool* outSupport = va_arg(args, bool*); | 
|  | 1268 | return getHdrSupport(outSupport); | 
|  | 1269 | } | 
|  | 1270 |  | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1271 | int Surface::dispatchGetConsumerUsage64(va_list args) { | 
|  | 1272 | uint64_t* usage = va_arg(args, uint64_t*); | 
|  | 1273 | return getConsumerUsage(usage); | 
|  | 1274 | } | 
|  | 1275 |  | 
| Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 1276 | bool Surface::transformToDisplayInverse() { | 
|  | 1277 | return (mTransform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY) == | 
|  | 1278 | NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; | 
|  | 1279 | } | 
|  | 1280 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1281 | int Surface::connect(int api) { | 
| Dan Stoza | 966b98b | 2015-03-02 22:12:37 -0800 | [diff] [blame] | 1282 | static sp<IProducerListener> listener = new DummyProducerListener(); | 
|  | 1283 | return connect(api, listener); | 
|  | 1284 | } | 
|  | 1285 |  | 
|  | 1286 | int Surface::connect(int api, const sp<IProducerListener>& listener) { | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1287 | return connect(api, listener, false); | 
|  | 1288 | } | 
|  | 1289 |  | 
|  | 1290 | int Surface::connect( | 
| Shuzhen Wang | 8a9d62f | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 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 |  | 
|  | 1298 | int Surface::connect( | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1299 | int api, const sp<IProducerListener>& listener, bool reportBufferRemoval) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1300 | ATRACE_CALL(); | 
|  | 1301 | ALOGV("Surface::connect"); | 
|  | 1302 | Mutex::Autolock lock(mMutex); | 
|  | 1303 | IGraphicBufferProducer::QueueBufferOutput output; | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1304 | mReportRemovedBuffers = reportBufferRemoval; | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 1305 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1306 | if (err == NO_ERROR) { | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1307 | mDefaultWidth = output.width; | 
|  | 1308 | mDefaultHeight = output.height; | 
|  | 1309 | mNextFrameNumber = output.nextFrameNumber; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1310 |  | 
| Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 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()) { | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1315 | mTransformHint = output.transformHint; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1316 | } | 
|  | 1317 |  | 
| Brian Anderson | 7c3ba8a | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 1318 | mConsumerRunningBehind = (output.numPendingBuffers >= 2); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1319 | } | 
|  | 1320 | if (!err && api == NATIVE_WINDOW_API_CPU) { | 
|  | 1321 | mConnectedToCpu = true; | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 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; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1327 | } | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 1328 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1329 | return err; | 
|  | 1330 | } | 
|  | 1331 |  | 
| Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 1332 |  | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1333 | int Surface::disconnect(int api, IGraphicBufferProducer::DisconnectMode mode) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1334 | ATRACE_CALL(); | 
|  | 1335 | ALOGV("Surface::disconnect"); | 
|  | 1336 | Mutex::Autolock lock(mMutex); | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1337 | mRemovedBuffers.clear(); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1338 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 1339 | mSharedBufferHasBeenQueued = false; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1340 | freeAllBuffers(); | 
| Robert Carr | 97b9c86 | 2016-09-08 13:54:35 -0700 | [diff] [blame] | 1341 | int err = mGraphicBufferProducer->disconnect(api, mode); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1350 | mStickyTransform = 0; | 
|  | 1351 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1352 | if (api == NATIVE_WINDOW_API_CPU) { | 
|  | 1353 | mConnectedToCpu = false; | 
|  | 1354 | } | 
|  | 1355 | } | 
|  | 1356 | return err; | 
|  | 1357 | } | 
|  | 1358 |  | 
| Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1359 | int Surface::detachNextBuffer(sp<GraphicBuffer>* outBuffer, | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1360 | sp<Fence>* outFence) { | 
|  | 1361 | ATRACE_CALL(); | 
|  | 1362 | ALOGV("Surface::detachNextBuffer"); | 
|  | 1363 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1364 | if (outBuffer == nullptr || outFence == nullptr) { | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1365 | return BAD_VALUE; | 
|  | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | Mutex::Autolock lock(mMutex); | 
| Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1369 | if (mReportRemovedBuffers) { | 
|  | 1370 | mRemovedBuffers.clear(); | 
|  | 1371 | } | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1372 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1373 | sp<GraphicBuffer> buffer(nullptr); | 
|  | 1374 | sp<Fence> fence(nullptr); | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1375 | status_t result = mGraphicBufferProducer->detachNextBuffer( | 
|  | 1376 | &buffer, &fence); | 
|  | 1377 | if (result != NO_ERROR) { | 
|  | 1378 | return result; | 
|  | 1379 | } | 
|  | 1380 |  | 
| Dan Stoza | d9c4971 | 2015-04-27 11:06:01 -0700 | [diff] [blame] | 1381 | *outBuffer = buffer; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1382 | if (fence != nullptr && fence->isValid()) { | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1383 | *outFence = fence; | 
|  | 1384 | } else { | 
|  | 1385 | *outFence = Fence::NO_FENCE; | 
|  | 1386 | } | 
|  | 1387 |  | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1388 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1389 | if (mSlots[i].buffer != nullptr && | 
| Shuzhen Wang | 55be505 | 2017-05-23 09:41:54 -0700 | [diff] [blame] | 1390 | mSlots[i].buffer->getId() == buffer->getId()) { | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1391 | if (mReportRemovedBuffers) { | 
|  | 1392 | mRemovedBuffers.push_back(mSlots[i].buffer); | 
|  | 1393 | } | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1394 | mSlots[i].buffer = nullptr; | 
| Pablo Ceballos | 23b4abe | 2016-01-08 12:15:22 -0800 | [diff] [blame] | 1395 | } | 
|  | 1396 | } | 
|  | 1397 |  | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1398 | return NO_ERROR; | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | int Surface::attachBuffer(ANativeWindowBuffer* buffer) | 
|  | 1402 | { | 
|  | 1403 | ATRACE_CALL(); | 
|  | 1404 | ALOGV("Surface::attachBuffer"); | 
|  | 1405 |  | 
|  | 1406 | Mutex::Autolock lock(mMutex); | 
| Yin-Chia Yeh | 1f2af5c | 2017-05-11 16:54:04 -0700 | [diff] [blame] | 1407 | if (mReportRemovedBuffers) { | 
|  | 1408 | mRemovedBuffers.clear(); | 
|  | 1409 | } | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1410 |  | 
|  | 1411 | sp<GraphicBuffer> graphicBuffer(static_cast<GraphicBuffer*>(buffer)); | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1412 | uint32_t priorGeneration = graphicBuffer->mGenerationNumber; | 
|  | 1413 | graphicBuffer->mGenerationNumber = mGenerationNumber; | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1414 | int32_t attachedSlot = -1; | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1415 | status_t result = mGraphicBufferProducer->attachBuffer(&attachedSlot, graphicBuffer); | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1416 | if (result != NO_ERROR) { | 
|  | 1417 | ALOGE("attachBuffer: IGraphicBufferProducer call failed (%d)", result); | 
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 1418 | graphicBuffer->mGenerationNumber = priorGeneration; | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1419 | return result; | 
|  | 1420 | } | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1421 | if (mReportRemovedBuffers && (mSlots[attachedSlot].buffer != nullptr)) { | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 1422 | mRemovedBuffers.push_back(mSlots[attachedSlot].buffer); | 
|  | 1423 | } | 
| Dan Stoza | 231832e | 2015-03-11 11:55:01 -0700 | [diff] [blame] | 1424 | mSlots[attachedSlot].buffer = graphicBuffer; | 
|  | 1425 |  | 
|  | 1426 | return NO_ERROR; | 
|  | 1427 | } | 
|  | 1428 |  | 
| Mathias Agopian | cb496ac | 2017-05-22 14:21:00 -0700 | [diff] [blame] | 1429 | int Surface::setUsage(uint64_t reqUsage) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1430 | { | 
|  | 1431 | ALOGV("Surface::setUsage"); | 
|  | 1432 | Mutex::Autolock lock(mMutex); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1433 | if (reqUsage != mReqUsage) { | 
|  | 1434 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 1435 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1436 | mReqUsage = reqUsage; | 
|  | 1437 | return OK; | 
|  | 1438 | } | 
|  | 1439 |  | 
|  | 1440 | int Surface::setCrop(Rect const* rect) | 
|  | 1441 | { | 
|  | 1442 | ATRACE_CALL(); | 
|  | 1443 |  | 
| Pablo Ceballos | 60d6922 | 2015-08-07 14:47:20 -0700 | [diff] [blame] | 1444 | Rect realRect(Rect::EMPTY_RECT); | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1445 | if (rect == nullptr || rect->isEmpty()) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1446 | realRect.clear(); | 
|  | 1447 | } else { | 
|  | 1448 | realRect = *rect; | 
| Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 1449 | } | 
|  | 1450 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
|  | 1459 | int Surface::setBufferCount(int bufferCount) | 
|  | 1460 | { | 
|  | 1461 | ATRACE_CALL(); | 
|  | 1462 | ALOGV("Surface::setBufferCount"); | 
|  | 1463 | Mutex::Autolock lock(mMutex); | 
|  | 1464 |  | 
| Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 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 | } | 
| Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 1477 |  | 
| Pablo Ceballos | e5b755a | 2015-08-13 16:18:19 -0700 | [diff] [blame] | 1478 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", | 
|  | 1479 | bufferCount, strerror(-err)); | 
|  | 1480 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1481 | return err; | 
|  | 1482 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1483 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 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 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1494 | return err; | 
|  | 1495 | } | 
|  | 1496 |  | 
|  | 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 |  | 
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 1506 | return err; | 
|  | 1507 | } | 
|  | 1508 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1509 | int Surface::setSharedBufferMode(bool sharedBufferMode) { | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1510 | ATRACE_CALL(); | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1511 | ALOGV("Surface::setSharedBufferMode (%d)", sharedBufferMode); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1512 | Mutex::Autolock lock(mMutex); | 
|  | 1513 |  | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1514 | status_t err = mGraphicBufferProducer->setSharedBufferMode( | 
|  | 1515 | sharedBufferMode); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1516 | if (err == NO_ERROR) { | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1517 | mSharedBufferMode = sharedBufferMode; | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1518 | } | 
| Pablo Ceballos | 3559fbf | 2016-03-17 15:50:23 -0700 | [diff] [blame] | 1519 | ALOGE_IF(err, "IGraphicBufferProducer::setSharedBufferMode(%d) returned" | 
|  | 1520 | "%s", sharedBufferMode, strerror(-err)); | 
| Pablo Ceballos | ccdfd60 | 2015-10-07 15:05:45 -0700 | [diff] [blame] | 1521 |  | 
|  | 1522 | return err; | 
|  | 1523 | } | 
|  | 1524 |  | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1539 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1540 | { | 
|  | 1541 | ATRACE_CALL(); | 
|  | 1542 | ALOGV("Surface::setBuffersDimensions"); | 
|  | 1543 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1544 | if ((width && !height) || (!width && height)) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1545 | return BAD_VALUE; | 
|  | 1546 |  | 
|  | 1547 | Mutex::Autolock lock(mMutex); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1548 | if (width != mReqWidth || height != mReqHeight) { | 
|  | 1549 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 1550 | } | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1551 | mReqWidth = width; | 
|  | 1552 | mReqHeight = height; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1553 | return NO_ERROR; | 
|  | 1554 | } | 
|  | 1555 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1556 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1557 | { | 
|  | 1558 | ATRACE_CALL(); | 
|  | 1559 | ALOGV("Surface::setBuffersUserDimensions"); | 
|  | 1560 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1561 | if ((width && !height) || (!width && height)) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1562 | return BAD_VALUE; | 
|  | 1563 |  | 
|  | 1564 | Mutex::Autolock lock(mMutex); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1565 | if (width != mUserWidth || height != mUserHeight) { | 
|  | 1566 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 1567 | } | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1568 | mUserWidth = width; | 
|  | 1569 | mUserHeight = height; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1570 | return NO_ERROR; | 
|  | 1571 | } | 
|  | 1572 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1573 | int Surface::setBuffersFormat(PixelFormat format) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1574 | { | 
|  | 1575 | ALOGV("Surface::setBuffersFormat"); | 
|  | 1576 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1577 | Mutex::Autolock lock(mMutex); | 
| Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1578 | if (format != mReqFormat) { | 
|  | 1579 | mSharedBufferSlot = BufferItem::INVALID_BUFFER_SLOT; | 
|  | 1580 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1581 | mReqFormat = format; | 
|  | 1582 | return NO_ERROR; | 
|  | 1583 | } | 
|  | 1584 |  | 
|  | 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: | 
| Robert Carr | c2e7788 | 2015-12-16 18:14:03 -0800 | [diff] [blame] | 1594 | case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1606 | int Surface::setBuffersTransform(uint32_t transform) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1607 | { | 
|  | 1608 | ATRACE_CALL(); | 
|  | 1609 | ALOGV("Surface::setBuffersTransform"); | 
|  | 1610 | Mutex::Autolock lock(mMutex); | 
| Vishnu Nair | 5eb3f06 | 2019-04-08 08:21:03 -0700 | [diff] [blame] | 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 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1618 | mTransform = transform; | 
|  | 1619 | return NO_ERROR; | 
|  | 1620 | } | 
|  | 1621 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1622 | int Surface::setBuffersStickyTransform(uint32_t transform) | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1623 | { | 
|  | 1624 | ATRACE_CALL(); | 
|  | 1625 | ALOGV("Surface::setBuffersStickyTransform"); | 
|  | 1626 | Mutex::Autolock lock(mMutex); | 
|  | 1627 | mStickyTransform = transform; | 
|  | 1628 | return NO_ERROR; | 
|  | 1629 | } | 
|  | 1630 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 1639 | int Surface::setBuffersDataSpace(Dataspace dataSpace) | 
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 1640 | { | 
|  | 1641 | ALOGV("Surface::setBuffersDataSpace"); | 
|  | 1642 | Mutex::Autolock lock(mMutex); | 
|  | 1643 | mDataSpace = dataSpace; | 
|  | 1644 | return NO_ERROR; | 
|  | 1645 | } | 
|  | 1646 |  | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 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 |  | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 1684 | Dataspace Surface::getBuffersDataSpace() { | 
| Courtney Goeltzenleuchter | 152279d | 2017-08-14 18:18:30 -0600 | [diff] [blame] | 1685 | ALOGV("Surface::getBuffersDataSpace"); | 
|  | 1686 | Mutex::Autolock lock(mMutex); | 
|  | 1687 | return mDataSpace; | 
|  | 1688 | } | 
|  | 1689 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1690 | void Surface::freeAllBuffers() { | 
|  | 1691 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1692 | mSlots[i].buffer = nullptr; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1693 | } | 
|  | 1694 | } | 
|  | 1695 |  | 
| Shuzhen Wang | 8a9d62f | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 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 |  | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 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 |  | 
| Dan Stoza | c62acbd | 2015-04-21 16:42:49 -0700 | [diff] [blame] | 1723 | if (mConnectedToCpu || numRects == 0) { | 
| Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 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 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1739 | // ---------------------------------------------------------------------- | 
|  | 1740 | // the lock/unlock APIs must be used from the same thread | 
|  | 1741 |  | 
|  | 1742 | static status_t copyBlt( | 
|  | 1743 | const sp<GraphicBuffer>& dst, | 
|  | 1744 | const sp<GraphicBuffer>& src, | 
| Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1745 | const Region& reg, | 
|  | 1746 | int *dstFenceFd) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1747 | { | 
| Dennis Kempin | add458f | 2017-05-17 19:16:31 -0700 | [diff] [blame] | 1748 | if (dst->getId() == src->getId()) | 
|  | 1749 | return OK; | 
|  | 1750 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1751 | // src and dst with, height and format must be identical. no verification | 
|  | 1752 | // is done here. | 
|  | 1753 | status_t err; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1754 | uint8_t* src_bits = nullptr; | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1755 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), | 
|  | 1756 | reinterpret_cast<void**>(&src_bits)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1757 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); | 
|  | 1758 |  | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1759 | uint8_t* dst_bits = nullptr; | 
| Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1760 | err = dst->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), | 
|  | 1761 | reinterpret_cast<void**>(&dst_bits), *dstFenceFd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1762 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); | 
| Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1763 | *dstFenceFd = -1; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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); | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1769 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; | 
|  | 1770 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1771 |  | 
|  | 1772 | while (head != tail) { | 
|  | 1773 | const Rect& r(*head++); | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1774 | int32_t h = r.height(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1775 | if (h <= 0) continue; | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 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; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1781 | if (dbpr==sbpr && size==sbpr) { | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 1782 | size *= static_cast<size_t>(h); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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) | 
| Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1797 | dst->unlockAsync(dstFenceFd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1798 |  | 
|  | 1799 | return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1800 | } | 
|  | 1801 |  | 
| Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1802 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1803 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1804 | status_t Surface::lock( | 
|  | 1805 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) | 
|  | 1806 | { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1807 | if (mLockedBuffer != nullptr) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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); | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1839 | const bool canCopyBack = (frontBuffer != nullptr && | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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)); | 
| Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1847 | if (!copyback.isEmpty()) { | 
| Francis Hart | 7b09e79 | 2015-01-09 11:10:54 +0200 | [diff] [blame] | 1848 | copyBlt(backBuffer, frontBuffer, copyback, &fenceFd); | 
| Francis Hart | dc10f84 | 2014-12-01 16:04:49 +0200 | [diff] [blame] | 1849 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1878 | status_t res = backBuffer->lockAsync( | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1879 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1880 | newDirtyRegion.bounds(), &vaddr, fenceFd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 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 |  | 
|  | 1899 | status_t Surface::unlockAndPost() | 
|  | 1900 | { | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1901 | if (mLockedBuffer == nullptr) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1902 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); | 
|  | 1903 | return INVALID_OPERATION; | 
|  | 1904 | } | 
|  | 1905 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1906 | int fd = -1; | 
|  | 1907 | status_t err = mLockedBuffer->unlockAsync(&fd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1908 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); | 
|  | 1909 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 1910 | err = queueBuffer(mLockedBuffer.get(), fd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1911 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", | 
|  | 1912 | mLockedBuffer->handle, strerror(-err)); | 
|  | 1913 |  | 
|  | 1914 | mPostedBuffer = mLockedBuffer; | 
| Yi Kong | 48a619f | 2018-06-05 16:34:59 -0700 | [diff] [blame] | 1915 | mLockedBuffer = nullptr; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 1916 | return err; | 
|  | 1917 | } | 
|  | 1918 |  | 
| Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1919 | bool Surface::waitForNextFrame(uint64_t lastFrame, nsecs_t timeout) { | 
|  | 1920 | Mutex::Autolock lock(mMutex); | 
| Pablo Ceballos | bc8c192 | 2016-07-01 14:15:41 -0700 | [diff] [blame] | 1921 | if (mNextFrameNumber > lastFrame) { | 
| Robert Carr | 9f31e29 | 2016-04-11 11:15:32 -0700 | [diff] [blame] | 1922 | return true; | 
|  | 1923 | } | 
|  | 1924 | return mQueueBufferCondition.waitRelative(mMutex, timeout) == OK; | 
|  | 1925 | } | 
|  | 1926 |  | 
| Pablo Ceballos | 8e3e92b | 2016-06-27 17:56:53 -0700 | [diff] [blame] | 1927 | status_t Surface::getUniqueId(uint64_t* outId) const { | 
|  | 1928 | Mutex::Autolock lock(mMutex); | 
|  | 1929 | return mGraphicBufferProducer->getUniqueId(outId); | 
|  | 1930 | } | 
|  | 1931 |  | 
| Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 1932 | int Surface::getConsumerUsage(uint64_t* outUsage) const { | 
|  | 1933 | Mutex::Autolock lock(mMutex); | 
|  | 1934 | return mGraphicBufferProducer->getConsumerUsage(outUsage); | 
|  | 1935 | } | 
|  | 1936 |  | 
| Dan Stoza | 932f008 | 2017-05-31 13:50:16 -0700 | [diff] [blame] | 1937 | nsecs_t Surface::getLastDequeueStartTime() const { | 
|  | 1938 | Mutex::Autolock lock(mMutex); | 
|  | 1939 | return mLastDequeueStartTime; | 
|  | 1940 | } | 
|  | 1941 |  | 
| Yin-Chia Yeh | e572fd7 | 2017-03-28 19:07:39 -0700 | [diff] [blame] | 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 |  | 
| Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 1954 | status_t Surface::attachAndQueueBufferWithDataspace(Surface* surface, sp<GraphicBuffer> buffer, | 
|  | 1955 | Dataspace dataspace) { | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 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 | } | 
| Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 1964 | ui::Dataspace tmpDataspace = surface->getBuffersDataSpace(); | 
|  | 1965 | err = surface->setBuffersDataSpace(dataspace); | 
|  | 1966 | if (err != OK) { | 
|  | 1967 | return err; | 
|  | 1968 | } | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 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 | } | 
| Peiyong Lin | 0f6a412 | 2019-06-25 17:25:02 -0700 | [diff] [blame] | 1977 | err = surface->setBuffersDataSpace(tmpDataspace); | 
|  | 1978 | if (err != OK) { | 
|  | 1979 | return err; | 
|  | 1980 | } | 
| Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 1981 | err = surface->disconnect(NATIVE_WINDOW_API_CPU); | 
|  | 1982 | return err; | 
|  | 1983 | } | 
|  | 1984 |  | 
| Shuzhen Wang | 8a9d62f | 2019-08-14 10:41:12 -0700 | [diff] [blame] | 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 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | }; // namespace android |