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