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