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