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