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