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