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