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