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