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