| 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> | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 30 |  | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 31 | #include <gui/IProducerListener.h> | 
| Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 32 | #include <gui/ISurfaceComposer.h> | 
| Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 33 | #include <gui/SurfaceComposerClient.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 34 | #include <gui/GLConsumer.h> | 
|  | 35 | #include <gui/Surface.h> | 
|  | 36 |  | 
|  | 37 | #include <private/gui/ComposerService.h> | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 38 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | namespace android { | 
|  | 40 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 41 | Surface::Surface( | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 42 | const sp<IGraphicBufferProducer>& bufferProducer, | 
|  | 43 | bool controlledByApp) | 
| Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 44 | : mGraphicBufferProducer(bufferProducer) | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 45 | { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 46 | // Initialize the ANativeWindow function pointers. | 
|  | 47 | ANativeWindow::setSwapInterval  = hook_setSwapInterval; | 
|  | 48 | ANativeWindow::dequeueBuffer    = hook_dequeueBuffer; | 
|  | 49 | ANativeWindow::cancelBuffer     = hook_cancelBuffer; | 
|  | 50 | ANativeWindow::queueBuffer      = hook_queueBuffer; | 
|  | 51 | ANativeWindow::query            = hook_query; | 
|  | 52 | ANativeWindow::perform          = hook_perform; | 
|  | 53 |  | 
|  | 54 | ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED; | 
|  | 55 | ANativeWindow::cancelBuffer_DEPRECATED  = hook_cancelBuffer_DEPRECATED; | 
|  | 56 | ANativeWindow::lockBuffer_DEPRECATED    = hook_lockBuffer_DEPRECATED; | 
|  | 57 | ANativeWindow::queueBuffer_DEPRECATED   = hook_queueBuffer_DEPRECATED; | 
|  | 58 |  | 
|  | 59 | const_cast<int&>(ANativeWindow::minSwapInterval) = 0; | 
|  | 60 | const_cast<int&>(ANativeWindow::maxSwapInterval) = 1; | 
|  | 61 |  | 
|  | 62 | mReqWidth = 0; | 
|  | 63 | mReqHeight = 0; | 
|  | 64 | mReqFormat = 0; | 
|  | 65 | mReqUsage = 0; | 
|  | 66 | mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO; | 
|  | 67 | mCrop.clear(); | 
|  | 68 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; | 
|  | 69 | mTransform = 0; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 70 | mStickyTransform = 0; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 71 | mDefaultWidth = 0; | 
|  | 72 | mDefaultHeight = 0; | 
|  | 73 | mUserWidth = 0; | 
|  | 74 | mUserHeight = 0; | 
|  | 75 | mTransformHint = 0; | 
|  | 76 | mConsumerRunningBehind = false; | 
|  | 77 | mConnectedToCpu = false; | 
| Eino-Ville Talvala | 7895e90 | 2013-08-21 11:53:37 -0700 | [diff] [blame] | 78 | mProducerControlledByApp = controlledByApp; | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 79 | mSwapIntervalZero = false; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Mathias Agopian | 35ffa6a | 2013-03-12 18:45:09 -0700 | [diff] [blame] | 82 | Surface::~Surface() { | 
|  | 83 | if (mConnectedToCpu) { | 
|  | 84 | Surface::disconnect(NATIVE_WINDOW_API_CPU); | 
|  | 85 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const { | 
|  | 89 | return mGraphicBufferProducer; | 
|  | 90 | } | 
|  | 91 |  | 
| Wonsik Kim | 0ee14ca | 2014-03-17 17:46:53 +0900 | [diff] [blame] | 92 | void Surface::setSidebandStream(const sp<NativeHandle>& stream) { | 
|  | 93 | mGraphicBufferProducer->setSidebandStream(stream); | 
|  | 94 | } | 
|  | 95 |  | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 96 | void Surface::allocateBuffers() { | 
|  | 97 | uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth; | 
|  | 98 | uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight; | 
| Andreas Gampe | 7398a5a | 2014-12-08 20:42:40 -0800 | [diff] [blame] | 99 | mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, reqWidth, | 
|  | 100 | reqHeight, mReqFormat, mReqUsage); | 
| Dan Stoza | 29a3e90 | 2014-06-20 13:13:57 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 103 | int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) { | 
|  | 104 | Surface* c = getSelf(window); | 
|  | 105 | return c->setSwapInterval(interval); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | int Surface::hook_dequeueBuffer(ANativeWindow* window, | 
|  | 109 | ANativeWindowBuffer** buffer, int* fenceFd) { | 
|  | 110 | Surface* c = getSelf(window); | 
|  | 111 | return c->dequeueBuffer(buffer, fenceFd); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | int Surface::hook_cancelBuffer(ANativeWindow* window, | 
|  | 115 | ANativeWindowBuffer* buffer, int fenceFd) { | 
|  | 116 | Surface* c = getSelf(window); | 
|  | 117 | return c->cancelBuffer(buffer, fenceFd); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | int Surface::hook_queueBuffer(ANativeWindow* window, | 
|  | 121 | ANativeWindowBuffer* buffer, int fenceFd) { | 
|  | 122 | Surface* c = getSelf(window); | 
|  | 123 | return c->queueBuffer(buffer, fenceFd); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 127 | ANativeWindowBuffer** buffer) { | 
|  | 128 | Surface* c = getSelf(window); | 
|  | 129 | ANativeWindowBuffer* buf; | 
|  | 130 | int fenceFd = -1; | 
|  | 131 | int result = c->dequeueBuffer(&buf, &fenceFd); | 
|  | 132 | sp<Fence> fence(new Fence(fenceFd)); | 
| Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 133 | int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED"); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 134 | if (waitResult != OK) { | 
|  | 135 | ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d", | 
|  | 136 | waitResult); | 
|  | 137 | c->cancelBuffer(buf, -1); | 
|  | 138 | return waitResult; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 139 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 140 | *buffer = buf; | 
|  | 141 | return result; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 144 | int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 145 | ANativeWindowBuffer* buffer) { | 
|  | 146 | Surface* c = getSelf(window); | 
|  | 147 | return c->cancelBuffer(buffer, -1); | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 150 | int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 151 | ANativeWindowBuffer* buffer) { | 
|  | 152 | Surface* c = getSelf(window); | 
|  | 153 | return c->lockBuffer_DEPRECATED(buffer); | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 156 | int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window, | 
|  | 157 | ANativeWindowBuffer* buffer) { | 
|  | 158 | Surface* c = getSelf(window); | 
|  | 159 | return c->queueBuffer(buffer, -1); | 
| Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 160 | } | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 161 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 162 | int Surface::hook_query(const ANativeWindow* window, | 
|  | 163 | int what, int* value) { | 
|  | 164 | const Surface* c = getSelf(window); | 
|  | 165 | return c->query(what, value); | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | int Surface::hook_perform(ANativeWindow* window, int operation, ...) { | 
|  | 169 | va_list args; | 
|  | 170 | va_start(args, operation); | 
|  | 171 | Surface* c = getSelf(window); | 
|  | 172 | return c->perform(operation, args); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | int Surface::setSwapInterval(int interval) { | 
|  | 176 | ATRACE_CALL(); | 
|  | 177 | // EGL specification states: | 
|  | 178 | //  interval is silently clamped to minimum and maximum implementation | 
|  | 179 | //  dependent values before being stored. | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 180 |  | 
|  | 181 | if (interval < minSwapInterval) | 
|  | 182 | interval = minSwapInterval; | 
|  | 183 |  | 
|  | 184 | if (interval > maxSwapInterval) | 
|  | 185 | interval = maxSwapInterval; | 
|  | 186 |  | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 187 | mSwapIntervalZero = (interval == 0); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 188 |  | 
| Mathias Agopian | 7cdd786 | 2013-07-18 22:10:56 -0700 | [diff] [blame] | 189 | return NO_ERROR; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 192 | int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 193 | ATRACE_CALL(); | 
|  | 194 | ALOGV("Surface::dequeueBuffer"); | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 195 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 196 | uint32_t reqWidth; | 
|  | 197 | uint32_t reqHeight; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 198 | bool swapIntervalZero; | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 199 | PixelFormat reqFormat; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 200 | uint32_t reqUsage; | 
|  | 201 |  | 
|  | 202 | { | 
|  | 203 | Mutex::Autolock lock(mMutex); | 
|  | 204 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 205 | reqWidth = mReqWidth ? mReqWidth : mUserWidth; | 
|  | 206 | reqHeight = mReqHeight ? mReqHeight : mUserHeight; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 207 |  | 
|  | 208 | swapIntervalZero = mSwapIntervalZero; | 
|  | 209 | reqFormat = mReqFormat; | 
|  | 210 | reqUsage = mReqUsage; | 
|  | 211 | } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer | 
|  | 212 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 213 | int buf = -1; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 214 | sp<Fence> fence; | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 215 | status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero, | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 216 | reqWidth, reqHeight, reqFormat, reqUsage); | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 217 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 218 | if (result < 0) { | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 219 | ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)" | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 220 | "failed: %d", swapIntervalZero, reqWidth, reqHeight, reqFormat, | 
|  | 221 | reqUsage, result); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 222 | return result; | 
| Mathias Agopian | 62185b7 | 2009-04-16 16:19:50 -0700 | [diff] [blame] | 223 | } | 
| Igor Murashkin | 0f1889e | 2014-02-28 18:44:21 -0800 | [diff] [blame] | 224 |  | 
|  | 225 | Mutex::Autolock lock(mMutex); | 
|  | 226 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 227 | sp<GraphicBuffer>& gbuf(mSlots[buf].buffer); | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 228 |  | 
|  | 229 | // this should never happen | 
|  | 230 | ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf); | 
|  | 231 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 232 | if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) { | 
|  | 233 | freeAllBuffers(); | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 234 | } | 
| Ted Bonkenburg | bd050ab | 2011-07-15 15:10:10 -0700 | [diff] [blame] | 235 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 236 | if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) { | 
|  | 237 | result = mGraphicBufferProducer->requestBuffer(buf, &gbuf); | 
|  | 238 | if (result != NO_ERROR) { | 
| Mathias Agopian | ba93b3f | 2013-08-01 15:48:40 -0700 | [diff] [blame] | 239 | ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result); | 
| Jesse Hall | 9f5a1b6 | 2014-10-02 11:09:03 -0700 | [diff] [blame] | 240 | mGraphicBufferProducer->cancelBuffer(buf, fence); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 241 | return result; | 
|  | 242 | } | 
|  | 243 | } | 
| Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 244 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 245 | if (fence->isValid()) { | 
|  | 246 | *fenceFd = fence->dup(); | 
|  | 247 | if (*fenceFd == -1) { | 
|  | 248 | ALOGE("dequeueBuffer: error duping fence: %d", errno); | 
|  | 249 | // dup() should never fail; something is badly wrong. Soldier on | 
|  | 250 | // and hope for the best; the worst that should happen is some | 
|  | 251 | // visible corruption that lasts until the next frame. | 
|  | 252 | } | 
| Ted Bonkenburg | e5d6eb8 | 2011-08-09 22:38:41 -0700 | [diff] [blame] | 253 | } else { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 254 | *fenceFd = -1; | 
| Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 255 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 256 |  | 
|  | 257 | *buffer = gbuf.get(); | 
|  | 258 | return OK; | 
| Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 261 | int Surface::cancelBuffer(android_native_buffer_t* buffer, | 
|  | 262 | int fenceFd) { | 
|  | 263 | ATRACE_CALL(); | 
|  | 264 | ALOGV("Surface::cancelBuffer"); | 
|  | 265 | Mutex::Autolock lock(mMutex); | 
|  | 266 | int i = getSlotFromBufferLocked(buffer); | 
|  | 267 | if (i < 0) { | 
|  | 268 | return i; | 
|  | 269 | } | 
|  | 270 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); | 
|  | 271 | mGraphicBufferProducer->cancelBuffer(i, fence); | 
|  | 272 | return OK; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | int Surface::getSlotFromBufferLocked( | 
|  | 276 | android_native_buffer_t* buffer) const { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 277 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
|  | 278 | if (mSlots[i].buffer != NULL && | 
|  | 279 | mSlots[i].buffer->handle == buffer->handle) { | 
|  | 280 | return i; | 
| Jamie Gennis | aca4e22 | 2010-07-15 17:29:15 -0700 | [diff] [blame] | 281 | } | 
|  | 282 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 283 | ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); | 
|  | 284 | return BAD_VALUE; | 
| Mathias Agopian | a0c30e9 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 287 | int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 288 | ALOGV("Surface::lockBuffer"); | 
|  | 289 | Mutex::Autolock lock(mMutex); | 
|  | 290 | return OK; | 
|  | 291 | } | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 292 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 293 | int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) { | 
|  | 294 | ATRACE_CALL(); | 
|  | 295 | ALOGV("Surface::queueBuffer"); | 
|  | 296 | Mutex::Autolock lock(mMutex); | 
|  | 297 | int64_t timestamp; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 298 | bool isAutoTimestamp = false; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 299 | if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) { | 
| Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 300 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 301 | isAutoTimestamp = true; | 
| Andy McFadden | 4b49e08 | 2013-08-02 15:31:45 -0700 | [diff] [blame] | 302 | ALOGV("Surface::queueBuffer making up timestamp: %.2f ms", | 
|  | 303 | timestamp / 1000000.f); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 304 | } else { | 
|  | 305 | timestamp = mTimestamp; | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 306 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 307 | int i = getSlotFromBufferLocked(buffer); | 
|  | 308 | if (i < 0) { | 
|  | 309 | return i; | 
|  | 310 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 313 | // Make sure the crop rectangle is entirely inside the buffer. | 
|  | 314 | Rect crop; | 
|  | 315 | mCrop.intersect(Rect(buffer->width, buffer->height), &crop); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 317 | sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE); | 
|  | 318 | IGraphicBufferProducer::QueueBufferOutput output; | 
| Andy McFadden | 3c25621 | 2013-08-16 14:55:39 -0700 | [diff] [blame] | 319 | IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp, | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 320 | crop, mScalingMode, mTransform ^ mStickyTransform, mSwapIntervalZero, | 
|  | 321 | fence, mStickyTransform); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 322 | status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output); | 
|  | 323 | if (err != OK)  { | 
|  | 324 | ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err); | 
|  | 325 | } | 
|  | 326 | uint32_t numPendingBuffers = 0; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 327 | uint32_t hint = 0; | 
|  | 328 | output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 329 | &numPendingBuffers); | 
| tedbo | 1e7fa9e | 2011-06-22 15:52:53 -0700 | [diff] [blame] | 330 |  | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 331 | // Disable transform hint if sticky transform is set. | 
|  | 332 | if (mStickyTransform == 0) { | 
|  | 333 | mTransformHint = hint; | 
|  | 334 | } | 
|  | 335 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 336 | mConsumerRunningBehind = (numPendingBuffers >= 2); | 
| Mathias Agopian | 631f358 | 2010-05-25 17:51:34 -0700 | [diff] [blame] | 337 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 338 | return err; | 
|  | 339 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 340 |  | 
| Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 341 | int Surface::query(int what, int* value) const { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 342 | ATRACE_CALL(); | 
|  | 343 | ALOGV("Surface::query"); | 
|  | 344 | { // scope for the lock | 
|  | 345 | Mutex::Autolock lock(mMutex); | 
|  | 346 | switch (what) { | 
|  | 347 | case NATIVE_WINDOW_FORMAT: | 
|  | 348 | if (mReqFormat) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 349 | *value = static_cast<int>(mReqFormat); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 350 | return NO_ERROR; | 
|  | 351 | } | 
|  | 352 | break; | 
|  | 353 | case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: { | 
|  | 354 | sp<ISurfaceComposer> composer( | 
|  | 355 | ComposerService::getComposerService()); | 
|  | 356 | if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) { | 
|  | 357 | *value = 1; | 
|  | 358 | } else { | 
|  | 359 | *value = 0; | 
|  | 360 | } | 
|  | 361 | return NO_ERROR; | 
|  | 362 | } | 
|  | 363 | case NATIVE_WINDOW_CONCRETE_TYPE: | 
|  | 364 | *value = NATIVE_WINDOW_SURFACE; | 
|  | 365 | return NO_ERROR; | 
|  | 366 | case NATIVE_WINDOW_DEFAULT_WIDTH: | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 367 | *value = static_cast<int>( | 
|  | 368 | mUserWidth ? mUserWidth : mDefaultWidth); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 369 | return NO_ERROR; | 
|  | 370 | case NATIVE_WINDOW_DEFAULT_HEIGHT: | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 371 | *value = static_cast<int>( | 
|  | 372 | mUserHeight ? mUserHeight : mDefaultHeight); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 373 | return NO_ERROR; | 
|  | 374 | case NATIVE_WINDOW_TRANSFORM_HINT: | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 375 | *value = static_cast<int>(mTransformHint); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 376 | return NO_ERROR; | 
|  | 377 | case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: { | 
|  | 378 | status_t err = NO_ERROR; | 
|  | 379 | if (!mConsumerRunningBehind) { | 
|  | 380 | *value = 0; | 
|  | 381 | } else { | 
|  | 382 | err = mGraphicBufferProducer->query(what, value); | 
|  | 383 | if (err == NO_ERROR) { | 
|  | 384 | mConsumerRunningBehind = *value; | 
|  | 385 | } | 
|  | 386 | } | 
|  | 387 | return err; | 
|  | 388 | } | 
|  | 389 | } | 
| Jamie Gennis | 391bbe2 | 2011-03-14 15:00:06 -0700 | [diff] [blame] | 390 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 391 | return mGraphicBufferProducer->query(what, value); | 
| Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 394 | int Surface::perform(int operation, va_list args) | 
|  | 395 | { | 
|  | 396 | int res = NO_ERROR; | 
|  | 397 | switch (operation) { | 
|  | 398 | case NATIVE_WINDOW_CONNECT: | 
|  | 399 | // deprecated. must return NO_ERROR. | 
|  | 400 | break; | 
|  | 401 | case NATIVE_WINDOW_DISCONNECT: | 
|  | 402 | // deprecated. must return NO_ERROR. | 
|  | 403 | break; | 
|  | 404 | case NATIVE_WINDOW_SET_USAGE: | 
|  | 405 | res = dispatchSetUsage(args); | 
|  | 406 | break; | 
|  | 407 | case NATIVE_WINDOW_SET_CROP: | 
|  | 408 | res = dispatchSetCrop(args); | 
|  | 409 | break; | 
|  | 410 | case NATIVE_WINDOW_SET_BUFFER_COUNT: | 
|  | 411 | res = dispatchSetBufferCount(args); | 
|  | 412 | break; | 
|  | 413 | case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY: | 
|  | 414 | res = dispatchSetBuffersGeometry(args); | 
|  | 415 | break; | 
|  | 416 | case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM: | 
|  | 417 | res = dispatchSetBuffersTransform(args); | 
|  | 418 | break; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 419 | case NATIVE_WINDOW_SET_BUFFERS_STICKY_TRANSFORM: | 
|  | 420 | res = dispatchSetBuffersStickyTransform(args); | 
|  | 421 | break; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 422 | case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP: | 
|  | 423 | res = dispatchSetBuffersTimestamp(args); | 
|  | 424 | break; | 
|  | 425 | case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS: | 
|  | 426 | res = dispatchSetBuffersDimensions(args); | 
|  | 427 | break; | 
|  | 428 | case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS: | 
|  | 429 | res = dispatchSetBuffersUserDimensions(args); | 
|  | 430 | break; | 
|  | 431 | case NATIVE_WINDOW_SET_BUFFERS_FORMAT: | 
|  | 432 | res = dispatchSetBuffersFormat(args); | 
|  | 433 | break; | 
|  | 434 | case NATIVE_WINDOW_LOCK: | 
|  | 435 | res = dispatchLock(args); | 
|  | 436 | break; | 
|  | 437 | case NATIVE_WINDOW_UNLOCK_AND_POST: | 
|  | 438 | res = dispatchUnlockAndPost(args); | 
|  | 439 | break; | 
|  | 440 | case NATIVE_WINDOW_SET_SCALING_MODE: | 
|  | 441 | res = dispatchSetScalingMode(args); | 
|  | 442 | break; | 
|  | 443 | case NATIVE_WINDOW_API_CONNECT: | 
|  | 444 | res = dispatchConnect(args); | 
|  | 445 | break; | 
|  | 446 | case NATIVE_WINDOW_API_DISCONNECT: | 
|  | 447 | res = dispatchDisconnect(args); | 
|  | 448 | break; | 
| Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 449 | case NATIVE_WINDOW_SET_SIDEBAND_STREAM: | 
|  | 450 | res = dispatchSetSidebandStream(args); | 
|  | 451 | break; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 452 | default: | 
|  | 453 | res = NAME_NOT_FOUND; | 
|  | 454 | break; | 
|  | 455 | } | 
|  | 456 | return res; | 
|  | 457 | } | 
| Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 458 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 459 | int Surface::dispatchConnect(va_list args) { | 
|  | 460 | int api = va_arg(args, int); | 
|  | 461 | return connect(api); | 
|  | 462 | } | 
| Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 463 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 464 | int Surface::dispatchDisconnect(va_list args) { | 
|  | 465 | int api = va_arg(args, int); | 
|  | 466 | return disconnect(api); | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | int Surface::dispatchSetUsage(va_list args) { | 
|  | 470 | int usage = va_arg(args, int); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 471 | return setUsage(static_cast<uint32_t>(usage)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 472 | } | 
|  | 473 |  | 
|  | 474 | int Surface::dispatchSetCrop(va_list args) { | 
|  | 475 | android_native_rect_t const* rect = va_arg(args, android_native_rect_t*); | 
|  | 476 | return setCrop(reinterpret_cast<Rect const*>(rect)); | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | int Surface::dispatchSetBufferCount(va_list args) { | 
|  | 480 | size_t bufferCount = va_arg(args, size_t); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 481 | return setBufferCount(static_cast<int32_t>(bufferCount)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
|  | 484 | int Surface::dispatchSetBuffersGeometry(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 485 | uint32_t width = va_arg(args, uint32_t); | 
|  | 486 | uint32_t height = va_arg(args, uint32_t); | 
|  | 487 | PixelFormat format = va_arg(args, PixelFormat); | 
|  | 488 | int err = setBuffersDimensions(width, height); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 489 | if (err != 0) { | 
|  | 490 | return err; | 
|  | 491 | } | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 492 | return setBuffersFormat(format); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 493 | } | 
|  | 494 |  | 
|  | 495 | int Surface::dispatchSetBuffersDimensions(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 496 | uint32_t width = va_arg(args, uint32_t); | 
|  | 497 | uint32_t height = va_arg(args, uint32_t); | 
|  | 498 | return setBuffersDimensions(width, height); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 499 | } | 
|  | 500 |  | 
|  | 501 | int Surface::dispatchSetBuffersUserDimensions(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 502 | uint32_t width = va_arg(args, uint32_t); | 
|  | 503 | uint32_t height = va_arg(args, uint32_t); | 
|  | 504 | return setBuffersUserDimensions(width, height); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 505 | } | 
|  | 506 |  | 
|  | 507 | int Surface::dispatchSetBuffersFormat(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 508 | PixelFormat format = va_arg(args, PixelFormat); | 
|  | 509 | return setBuffersFormat(format); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 510 | } | 
|  | 511 |  | 
|  | 512 | int Surface::dispatchSetScalingMode(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 513 | int mode = va_arg(args, int); | 
|  | 514 | return setScalingMode(mode); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 515 | } | 
|  | 516 |  | 
|  | 517 | int Surface::dispatchSetBuffersTransform(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 518 | uint32_t transform = va_arg(args, uint32_t); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 519 | return setBuffersTransform(transform); | 
|  | 520 | } | 
|  | 521 |  | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 522 | int Surface::dispatchSetBuffersStickyTransform(va_list args) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 523 | uint32_t transform = va_arg(args, uint32_t); | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 524 | return setBuffersStickyTransform(transform); | 
|  | 525 | } | 
|  | 526 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 527 | int Surface::dispatchSetBuffersTimestamp(va_list args) { | 
|  | 528 | int64_t timestamp = va_arg(args, int64_t); | 
|  | 529 | return setBuffersTimestamp(timestamp); | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | int Surface::dispatchLock(va_list args) { | 
|  | 533 | ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*); | 
|  | 534 | ARect* inOutDirtyBounds = va_arg(args, ARect*); | 
|  | 535 | return lock(outBuffer, inOutDirtyBounds); | 
|  | 536 | } | 
|  | 537 |  | 
| Igor Murashkin | 7d2d160 | 2013-11-12 18:02:20 -0800 | [diff] [blame] | 538 | int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 539 | return unlockAndPost(); | 
|  | 540 | } | 
|  | 541 |  | 
| Rachad | 7cb0d39 | 2014-07-29 17:53:53 -0700 | [diff] [blame] | 542 | int Surface::dispatchSetSidebandStream(va_list args) { | 
|  | 543 | native_handle_t* sH = va_arg(args, native_handle_t*); | 
|  | 544 | sp<NativeHandle> sidebandHandle = NativeHandle::create(sH, false); | 
|  | 545 | setSidebandStream(sidebandHandle); | 
|  | 546 | return OK; | 
|  | 547 | } | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 548 |  | 
|  | 549 | int Surface::connect(int api) { | 
|  | 550 | ATRACE_CALL(); | 
|  | 551 | ALOGV("Surface::connect"); | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 552 | static sp<IProducerListener> listener = new DummyProducerListener(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 553 | Mutex::Autolock lock(mMutex); | 
|  | 554 | IGraphicBufferProducer::QueueBufferOutput output; | 
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 555 | int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 556 | if (err == NO_ERROR) { | 
|  | 557 | uint32_t numPendingBuffers = 0; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 558 | uint32_t hint = 0; | 
|  | 559 | output.deflate(&mDefaultWidth, &mDefaultHeight, &hint, | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 560 | &numPendingBuffers); | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 561 |  | 
|  | 562 | // Disable transform hint if sticky transform is set. | 
|  | 563 | if (mStickyTransform == 0) { | 
|  | 564 | mTransformHint = hint; | 
|  | 565 | } | 
|  | 566 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 567 | mConsumerRunningBehind = (numPendingBuffers >= 2); | 
|  | 568 | } | 
|  | 569 | if (!err && api == NATIVE_WINDOW_API_CPU) { | 
|  | 570 | mConnectedToCpu = true; | 
|  | 571 | } | 
|  | 572 | return err; | 
|  | 573 | } | 
|  | 574 |  | 
| Mathias Agopian | 365857d | 2013-09-11 19:35:45 -0700 | [diff] [blame] | 575 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 576 | int Surface::disconnect(int api) { | 
|  | 577 | ATRACE_CALL(); | 
|  | 578 | ALOGV("Surface::disconnect"); | 
|  | 579 | Mutex::Autolock lock(mMutex); | 
|  | 580 | freeAllBuffers(); | 
|  | 581 | int err = mGraphicBufferProducer->disconnect(api); | 
|  | 582 | if (!err) { | 
|  | 583 | mReqFormat = 0; | 
|  | 584 | mReqWidth = 0; | 
|  | 585 | mReqHeight = 0; | 
|  | 586 | mReqUsage = 0; | 
|  | 587 | mCrop.clear(); | 
|  | 588 | mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; | 
|  | 589 | mTransform = 0; | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 590 | mStickyTransform = 0; | 
|  | 591 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 592 | if (api == NATIVE_WINDOW_API_CPU) { | 
|  | 593 | mConnectedToCpu = false; | 
|  | 594 | } | 
|  | 595 | } | 
|  | 596 | return err; | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | int Surface::setUsage(uint32_t reqUsage) | 
|  | 600 | { | 
|  | 601 | ALOGV("Surface::setUsage"); | 
|  | 602 | Mutex::Autolock lock(mMutex); | 
|  | 603 | mReqUsage = reqUsage; | 
|  | 604 | return OK; | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 | int Surface::setCrop(Rect const* rect) | 
|  | 608 | { | 
|  | 609 | ATRACE_CALL(); | 
|  | 610 |  | 
|  | 611 | Rect realRect; | 
|  | 612 | if (rect == NULL || rect->isEmpty()) { | 
|  | 613 | realRect.clear(); | 
|  | 614 | } else { | 
|  | 615 | realRect = *rect; | 
| Mathias Agopian | 55fa251 | 2010-03-11 15:06:54 -0800 | [diff] [blame] | 616 | } | 
|  | 617 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 618 | ALOGV("Surface::setCrop rect=[%d %d %d %d]", | 
|  | 619 | realRect.left, realRect.top, realRect.right, realRect.bottom); | 
|  | 620 |  | 
|  | 621 | Mutex::Autolock lock(mMutex); | 
|  | 622 | mCrop = realRect; | 
|  | 623 | return NO_ERROR; | 
|  | 624 | } | 
|  | 625 |  | 
|  | 626 | int Surface::setBufferCount(int bufferCount) | 
|  | 627 | { | 
|  | 628 | ATRACE_CALL(); | 
|  | 629 | ALOGV("Surface::setBufferCount"); | 
|  | 630 | Mutex::Autolock lock(mMutex); | 
|  | 631 |  | 
|  | 632 | status_t err = mGraphicBufferProducer->setBufferCount(bufferCount); | 
|  | 633 | ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s", | 
|  | 634 | bufferCount, strerror(-err)); | 
| Mathias Agopian | 9014726 | 2010-01-22 11:47:55 -0800 | [diff] [blame] | 635 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 636 | if (err == NO_ERROR) { | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 637 | freeAllBuffers(); | 
| Mathias Agopian | 87a96ea | 2011-08-23 21:09:41 -0700 | [diff] [blame] | 638 | } | 
|  | 639 |  | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 640 | return err; | 
|  | 641 | } | 
| Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 642 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 643 | int Surface::setBuffersDimensions(uint32_t width, uint32_t height) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 644 | { | 
|  | 645 | ATRACE_CALL(); | 
|  | 646 | ALOGV("Surface::setBuffersDimensions"); | 
|  | 647 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 648 | if ((width && !height) || (!width && height)) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 649 | return BAD_VALUE; | 
|  | 650 |  | 
|  | 651 | Mutex::Autolock lock(mMutex); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 652 | mReqWidth = width; | 
|  | 653 | mReqHeight = height; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 654 | return NO_ERROR; | 
|  | 655 | } | 
|  | 656 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 657 | int Surface::setBuffersUserDimensions(uint32_t width, uint32_t height) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 658 | { | 
|  | 659 | ATRACE_CALL(); | 
|  | 660 | ALOGV("Surface::setBuffersUserDimensions"); | 
|  | 661 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 662 | if ((width && !height) || (!width && height)) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 663 | return BAD_VALUE; | 
|  | 664 |  | 
|  | 665 | Mutex::Autolock lock(mMutex); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 666 | mUserWidth = width; | 
|  | 667 | mUserHeight = height; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 668 | return NO_ERROR; | 
|  | 669 | } | 
|  | 670 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 671 | int Surface::setBuffersFormat(PixelFormat format) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 672 | { | 
|  | 673 | ALOGV("Surface::setBuffersFormat"); | 
|  | 674 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 675 | Mutex::Autolock lock(mMutex); | 
|  | 676 | mReqFormat = format; | 
|  | 677 | return NO_ERROR; | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | int Surface::setScalingMode(int mode) | 
|  | 681 | { | 
|  | 682 | ATRACE_CALL(); | 
|  | 683 | ALOGV("Surface::setScalingMode(%d)", mode); | 
|  | 684 |  | 
|  | 685 | switch (mode) { | 
|  | 686 | case NATIVE_WINDOW_SCALING_MODE_FREEZE: | 
|  | 687 | case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: | 
|  | 688 | case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: | 
|  | 689 | break; | 
|  | 690 | default: | 
|  | 691 | ALOGE("unknown scaling mode: %d", mode); | 
|  | 692 | return BAD_VALUE; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | Mutex::Autolock lock(mMutex); | 
|  | 696 | mScalingMode = mode; | 
|  | 697 | return NO_ERROR; | 
|  | 698 | } | 
|  | 699 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 700 | int Surface::setBuffersTransform(uint32_t transform) | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 701 | { | 
|  | 702 | ATRACE_CALL(); | 
|  | 703 | ALOGV("Surface::setBuffersTransform"); | 
|  | 704 | Mutex::Autolock lock(mMutex); | 
|  | 705 | mTransform = transform; | 
|  | 706 | return NO_ERROR; | 
|  | 707 | } | 
|  | 708 |  | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 709 | int Surface::setBuffersStickyTransform(uint32_t transform) | 
| Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 710 | { | 
|  | 711 | ATRACE_CALL(); | 
|  | 712 | ALOGV("Surface::setBuffersStickyTransform"); | 
|  | 713 | Mutex::Autolock lock(mMutex); | 
|  | 714 | mStickyTransform = transform; | 
|  | 715 | return NO_ERROR; | 
|  | 716 | } | 
|  | 717 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 718 | int Surface::setBuffersTimestamp(int64_t timestamp) | 
|  | 719 | { | 
|  | 720 | ALOGV("Surface::setBuffersTimestamp"); | 
|  | 721 | Mutex::Autolock lock(mMutex); | 
|  | 722 | mTimestamp = timestamp; | 
|  | 723 | return NO_ERROR; | 
|  | 724 | } | 
|  | 725 |  | 
|  | 726 | void Surface::freeAllBuffers() { | 
|  | 727 | for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { | 
|  | 728 | mSlots[i].buffer = 0; | 
|  | 729 | } | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | // ---------------------------------------------------------------------- | 
|  | 733 | // the lock/unlock APIs must be used from the same thread | 
|  | 734 |  | 
|  | 735 | static status_t copyBlt( | 
|  | 736 | const sp<GraphicBuffer>& dst, | 
|  | 737 | const sp<GraphicBuffer>& src, | 
|  | 738 | const Region& reg) | 
|  | 739 | { | 
|  | 740 | // src and dst with, height and format must be identical. no verification | 
|  | 741 | // is done here. | 
|  | 742 | status_t err; | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 743 | uint8_t* src_bits = NULL; | 
|  | 744 | err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), | 
|  | 745 | reinterpret_cast<void**>(&src_bits)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 746 | ALOGE_IF(err, "error locking src buffer %s", strerror(-err)); | 
|  | 747 |  | 
|  | 748 | uint8_t* dst_bits = NULL; | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 749 | err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), | 
|  | 750 | reinterpret_cast<void**>(&dst_bits)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 751 | ALOGE_IF(err, "error locking dst buffer %s", strerror(-err)); | 
|  | 752 |  | 
|  | 753 | Region::const_iterator head(reg.begin()); | 
|  | 754 | Region::const_iterator tail(reg.end()); | 
|  | 755 | if (head != tail && src_bits && dst_bits) { | 
|  | 756 | const size_t bpp = bytesPerPixel(src->format); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 757 | const size_t dbpr = static_cast<uint32_t>(dst->stride) * bpp; | 
|  | 758 | const size_t sbpr = static_cast<uint32_t>(src->stride) * bpp; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 759 |  | 
|  | 760 | while (head != tail) { | 
|  | 761 | const Rect& r(*head++); | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 762 | int32_t h = r.height(); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 763 | if (h <= 0) continue; | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 764 | size_t size = static_cast<uint32_t>(r.width()) * bpp; | 
|  | 765 | uint8_t const * s = src_bits + | 
|  | 766 | static_cast<uint32_t>(r.left + src->stride * r.top) * bpp; | 
|  | 767 | uint8_t       * d = dst_bits + | 
|  | 768 | static_cast<uint32_t>(r.left + dst->stride * r.top) * bpp; | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 769 | if (dbpr==sbpr && size==sbpr) { | 
| Dan Stoza | dd883c0 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 770 | size *= static_cast<size_t>(h); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 771 | h = 1; | 
|  | 772 | } | 
|  | 773 | do { | 
|  | 774 | memcpy(d, s, size); | 
|  | 775 | d += dbpr; | 
|  | 776 | s += sbpr; | 
|  | 777 | } while (--h > 0); | 
|  | 778 | } | 
|  | 779 | } | 
|  | 780 |  | 
|  | 781 | if (src_bits) | 
|  | 782 | src->unlock(); | 
|  | 783 |  | 
|  | 784 | if (dst_bits) | 
|  | 785 | dst->unlock(); | 
|  | 786 |  | 
|  | 787 | return err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } | 
|  | 789 |  | 
| Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 790 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 791 |  | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 792 | status_t Surface::lock( | 
|  | 793 | ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) | 
|  | 794 | { | 
|  | 795 | if (mLockedBuffer != 0) { | 
|  | 796 | ALOGE("Surface::lock failed, already locked"); | 
|  | 797 | return INVALID_OPERATION; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | if (!mConnectedToCpu) { | 
|  | 801 | int err = Surface::connect(NATIVE_WINDOW_API_CPU); | 
|  | 802 | if (err) { | 
|  | 803 | return err; | 
|  | 804 | } | 
|  | 805 | // we're intending to do software rendering from this point | 
|  | 806 | setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | ANativeWindowBuffer* out; | 
|  | 810 | int fenceFd = -1; | 
|  | 811 | status_t err = dequeueBuffer(&out, &fenceFd); | 
|  | 812 | ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err)); | 
|  | 813 | if (err == NO_ERROR) { | 
|  | 814 | sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out)); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 815 | const Rect bounds(backBuffer->width, backBuffer->height); | 
|  | 816 |  | 
|  | 817 | Region newDirtyRegion; | 
|  | 818 | if (inOutDirtyBounds) { | 
|  | 819 | newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds)); | 
|  | 820 | newDirtyRegion.andSelf(bounds); | 
|  | 821 | } else { | 
|  | 822 | newDirtyRegion.set(bounds); | 
|  | 823 | } | 
|  | 824 |  | 
|  | 825 | // figure out if we can copy the frontbuffer back | 
|  | 826 | const sp<GraphicBuffer>& frontBuffer(mPostedBuffer); | 
|  | 827 | const bool canCopyBack = (frontBuffer != 0 && | 
|  | 828 | backBuffer->width  == frontBuffer->width && | 
|  | 829 | backBuffer->height == frontBuffer->height && | 
|  | 830 | backBuffer->format == frontBuffer->format); | 
|  | 831 |  | 
|  | 832 | if (canCopyBack) { | 
|  | 833 | // copy the area that is invalid and not repainted this round | 
|  | 834 | const Region copyback(mDirtyRegion.subtract(newDirtyRegion)); | 
|  | 835 | if (!copyback.isEmpty()) | 
|  | 836 | copyBlt(backBuffer, frontBuffer, copyback); | 
|  | 837 | } else { | 
|  | 838 | // if we can't copy-back anything, modify the user's dirty | 
|  | 839 | // region to make sure they redraw the whole buffer | 
|  | 840 | newDirtyRegion.set(bounds); | 
|  | 841 | mDirtyRegion.clear(); | 
|  | 842 | Mutex::Autolock lock(mMutex); | 
|  | 843 | for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) { | 
|  | 844 | mSlots[i].dirtyRegion.clear(); | 
|  | 845 | } | 
|  | 846 | } | 
|  | 847 |  | 
|  | 848 |  | 
|  | 849 | { // scope for the lock | 
|  | 850 | Mutex::Autolock lock(mMutex); | 
|  | 851 | int backBufferSlot(getSlotFromBufferLocked(backBuffer.get())); | 
|  | 852 | if (backBufferSlot >= 0) { | 
|  | 853 | Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion); | 
|  | 854 | mDirtyRegion.subtract(dirtyRegion); | 
|  | 855 | dirtyRegion = newDirtyRegion; | 
|  | 856 | } | 
|  | 857 | } | 
|  | 858 |  | 
|  | 859 | mDirtyRegion.orSelf(newDirtyRegion); | 
|  | 860 | if (inOutDirtyBounds) { | 
|  | 861 | *inOutDirtyBounds = newDirtyRegion.getBounds(); | 
|  | 862 | } | 
|  | 863 |  | 
|  | 864 | void* vaddr; | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 865 | status_t res = backBuffer->lockAsync( | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 866 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 867 | newDirtyRegion.bounds(), &vaddr, fenceFd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 868 |  | 
|  | 869 | ALOGW_IF(res, "failed locking buffer (handle = %p)", | 
|  | 870 | backBuffer->handle); | 
|  | 871 |  | 
|  | 872 | if (res != 0) { | 
|  | 873 | err = INVALID_OPERATION; | 
|  | 874 | } else { | 
|  | 875 | mLockedBuffer = backBuffer; | 
|  | 876 | outBuffer->width  = backBuffer->width; | 
|  | 877 | outBuffer->height = backBuffer->height; | 
|  | 878 | outBuffer->stride = backBuffer->stride; | 
|  | 879 | outBuffer->format = backBuffer->format; | 
|  | 880 | outBuffer->bits   = vaddr; | 
|  | 881 | } | 
|  | 882 | } | 
|  | 883 | return err; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | status_t Surface::unlockAndPost() | 
|  | 887 | { | 
|  | 888 | if (mLockedBuffer == 0) { | 
|  | 889 | ALOGE("Surface::unlockAndPost failed, no locked buffer"); | 
|  | 890 | return INVALID_OPERATION; | 
|  | 891 | } | 
|  | 892 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 893 | int fd = -1; | 
|  | 894 | status_t err = mLockedBuffer->unlockAsync(&fd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 895 | ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle); | 
|  | 896 |  | 
| Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 897 | err = queueBuffer(mLockedBuffer.get(), fd); | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 898 | ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)", | 
|  | 899 | mLockedBuffer->handle, strerror(-err)); | 
|  | 900 |  | 
|  | 901 | mPostedBuffer = mLockedBuffer; | 
|  | 902 | mLockedBuffer = 0; | 
|  | 903 | return err; | 
|  | 904 | } | 
|  | 905 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | }; // namespace android |