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