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