blob: b628a15f8eca0274e50b76d92a85b8aa30e50e00 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopiane3c697f2013-02-14 17:11:02 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
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 "Surface"
Mathias Agopiane3c697f2013-02-14 17:11:02 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Mathias Agopianb0e76f42012-03-23 14:15:44 -070021#include <android/native_window.h>
22
Mathias Agopiane3c697f2013-02-14 17:11:02 -080023#include <binder/Parcel.h>
24
Mathias Agopian9cce3252010-02-09 17:46:37 -080025#include <utils/Log.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080026#include <utils/Trace.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080027
Mathias Agopiane3c697f2013-02-14 17:11:02 -080028#include <ui/Fence.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029
Mathias Agopian90ac7992012-02-25 18:48:35 -080030#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080032#include <gui/GLConsumer.h>
33#include <gui/Surface.h>
34
35#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037namespace android {
38
Mathias Agopiane3c697f2013-02-14 17:11:02 -080039Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070040 const sp<IGraphicBufferProducer>& bufferProducer,
41 bool controlledByApp)
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070042 : mGraphicBufferProducer(bufferProducer)
Mathias Agopian62185b72009-04-16 16:19:50 -070043{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044 // Initialize the ANativeWindow function pointers.
45 ANativeWindow::setSwapInterval = hook_setSwapInterval;
46 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
47 ANativeWindow::cancelBuffer = hook_cancelBuffer;
48 ANativeWindow::queueBuffer = hook_queueBuffer;
49 ANativeWindow::query = hook_query;
50 ANativeWindow::perform = hook_perform;
51
52 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
53 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
54 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
55 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
56
57 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
58 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
59
60 mReqWidth = 0;
61 mReqHeight = 0;
62 mReqFormat = 0;
63 mReqUsage = 0;
64 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
65 mCrop.clear();
66 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
67 mTransform = 0;
68 mDefaultWidth = 0;
69 mDefaultHeight = 0;
70 mUserWidth = 0;
71 mUserHeight = 0;
72 mTransformHint = 0;
73 mConsumerRunningBehind = false;
74 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070075 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070076 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070077}
78
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070079Surface::~Surface() {
80 if (mConnectedToCpu) {
81 Surface::disconnect(NATIVE_WINDOW_API_CPU);
82 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080083}
84
85sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
86 return mGraphicBufferProducer;
87}
88
89int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
90 Surface* c = getSelf(window);
91 return c->setSwapInterval(interval);
92}
93
94int Surface::hook_dequeueBuffer(ANativeWindow* window,
95 ANativeWindowBuffer** buffer, int* fenceFd) {
96 Surface* c = getSelf(window);
97 return c->dequeueBuffer(buffer, fenceFd);
98}
99
100int Surface::hook_cancelBuffer(ANativeWindow* window,
101 ANativeWindowBuffer* buffer, int fenceFd) {
102 Surface* c = getSelf(window);
103 return c->cancelBuffer(buffer, fenceFd);
104}
105
106int Surface::hook_queueBuffer(ANativeWindow* window,
107 ANativeWindowBuffer* buffer, int fenceFd) {
108 Surface* c = getSelf(window);
109 return c->queueBuffer(buffer, fenceFd);
110}
111
112int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
113 ANativeWindowBuffer** buffer) {
114 Surface* c = getSelf(window);
115 ANativeWindowBuffer* buf;
116 int fenceFd = -1;
117 int result = c->dequeueBuffer(&buf, &fenceFd);
118 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700119 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800120 if (waitResult != OK) {
121 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
122 waitResult);
123 c->cancelBuffer(buf, -1);
124 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700125 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800126 *buffer = buf;
127 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700128}
129
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800130int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
131 ANativeWindowBuffer* buffer) {
132 Surface* c = getSelf(window);
133 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700134}
135
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800136int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
137 ANativeWindowBuffer* buffer) {
138 Surface* c = getSelf(window);
139 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700140}
141
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800142int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
143 ANativeWindowBuffer* buffer) {
144 Surface* c = getSelf(window);
145 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700146}
Mathias Agopian62185b72009-04-16 16:19:50 -0700147
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148int Surface::hook_query(const ANativeWindow* window,
149 int what, int* value) {
150 const Surface* c = getSelf(window);
151 return c->query(what, value);
152}
153
154int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
155 va_list args;
156 va_start(args, operation);
157 Surface* c = getSelf(window);
158 return c->perform(operation, args);
159}
160
161int Surface::setSwapInterval(int interval) {
162 ATRACE_CALL();
163 // EGL specification states:
164 // interval is silently clamped to minimum and maximum implementation
165 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800166
167 if (interval < minSwapInterval)
168 interval = minSwapInterval;
169
170 if (interval > maxSwapInterval)
171 interval = maxSwapInterval;
172
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700173 mSwapIntervalZero = (interval == 0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800174
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700175 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800176}
177
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700178int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800179 ATRACE_CALL();
180 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800181
182 int reqW;
183 int reqH;
184 bool swapIntervalZero;
185 uint32_t reqFormat;
186 uint32_t reqUsage;
187
188 {
189 Mutex::Autolock lock(mMutex);
190
191 reqW = mReqWidth ? mReqWidth : mUserWidth;
192 reqH = mReqHeight ? mReqHeight : mUserHeight;
193
194 swapIntervalZero = mSwapIntervalZero;
195 reqFormat = mReqFormat;
196 reqUsage = mReqUsage;
197 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
198
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800199 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800200 sp<Fence> fence;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800201 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
202 reqW, reqH, reqFormat, reqUsage);
203
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800204 if (result < 0) {
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800205 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
206 "failed: %d", swapIntervalZero, reqW, reqH, reqFormat, reqUsage,
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800207 result);
208 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700209 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800210
211 Mutex::Autolock lock(mMutex);
212
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800213 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700214
215 // this should never happen
216 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
217
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
219 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700220 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700221
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800222 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
223 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
224 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700225 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800226 return result;
227 }
228 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700229
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800230 if (fence->isValid()) {
231 *fenceFd = fence->dup();
232 if (*fenceFd == -1) {
233 ALOGE("dequeueBuffer: error duping fence: %d", errno);
234 // dup() should never fail; something is badly wrong. Soldier on
235 // and hope for the best; the worst that should happen is some
236 // visible corruption that lasts until the next frame.
237 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700238 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800239 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700240 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800241
242 *buffer = gbuf.get();
243 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700244}
245
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800246int Surface::cancelBuffer(android_native_buffer_t* buffer,
247 int fenceFd) {
248 ATRACE_CALL();
249 ALOGV("Surface::cancelBuffer");
250 Mutex::Autolock lock(mMutex);
251 int i = getSlotFromBufferLocked(buffer);
252 if (i < 0) {
253 return i;
254 }
255 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
256 mGraphicBufferProducer->cancelBuffer(i, fence);
257 return OK;
258}
259
260int Surface::getSlotFromBufferLocked(
261 android_native_buffer_t* buffer) const {
262 bool dumpedState = false;
263 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
264 if (mSlots[i].buffer != NULL &&
265 mSlots[i].buffer->handle == buffer->handle) {
266 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700267 }
268 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800269 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
270 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700271}
272
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800273int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274 ALOGV("Surface::lockBuffer");
275 Mutex::Autolock lock(mMutex);
276 return OK;
277}
Mathias Agopian631f3582010-05-25 17:51:34 -0700278
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800279int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
280 ATRACE_CALL();
281 ALOGV("Surface::queueBuffer");
282 Mutex::Autolock lock(mMutex);
283 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700284 bool isAutoTimestamp = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800285 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700286 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700287 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700288 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
289 timestamp / 1000000.f);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800290 } else {
291 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700292 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800293 int i = getSlotFromBufferLocked(buffer);
294 if (i < 0) {
295 return i;
296 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800299 // Make sure the crop rectangle is entirely inside the buffer.
300 Rect crop;
301 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800303 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
304 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700305 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
306 crop, mScalingMode, mTransform, mSwapIntervalZero, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800307 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
308 if (err != OK) {
309 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
310 }
311 uint32_t numPendingBuffers = 0;
312 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
313 &numPendingBuffers);
tedbo1e7fa9e2011-06-22 15:52:53 -0700314
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800315 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700316
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800317 return err;
318}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700319
Mathias Agopiana67932f2011-04-20 14:20:59 -0700320int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800321 ATRACE_CALL();
322 ALOGV("Surface::query");
323 { // scope for the lock
324 Mutex::Autolock lock(mMutex);
325 switch (what) {
326 case NATIVE_WINDOW_FORMAT:
327 if (mReqFormat) {
328 *value = mReqFormat;
329 return NO_ERROR;
330 }
331 break;
332 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
333 sp<ISurfaceComposer> composer(
334 ComposerService::getComposerService());
335 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
336 *value = 1;
337 } else {
338 *value = 0;
339 }
340 return NO_ERROR;
341 }
342 case NATIVE_WINDOW_CONCRETE_TYPE:
343 *value = NATIVE_WINDOW_SURFACE;
344 return NO_ERROR;
345 case NATIVE_WINDOW_DEFAULT_WIDTH:
346 *value = mUserWidth ? mUserWidth : mDefaultWidth;
347 return NO_ERROR;
348 case NATIVE_WINDOW_DEFAULT_HEIGHT:
349 *value = mUserHeight ? mUserHeight : mDefaultHeight;
350 return NO_ERROR;
351 case NATIVE_WINDOW_TRANSFORM_HINT:
352 *value = mTransformHint;
353 return NO_ERROR;
354 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
355 status_t err = NO_ERROR;
356 if (!mConsumerRunningBehind) {
357 *value = 0;
358 } else {
359 err = mGraphicBufferProducer->query(what, value);
360 if (err == NO_ERROR) {
361 mConsumerRunningBehind = *value;
362 }
363 }
364 return err;
365 }
366 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700367 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800368 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800369}
370
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800371int Surface::perform(int operation, va_list args)
372{
373 int res = NO_ERROR;
374 switch (operation) {
375 case NATIVE_WINDOW_CONNECT:
376 // deprecated. must return NO_ERROR.
377 break;
378 case NATIVE_WINDOW_DISCONNECT:
379 // deprecated. must return NO_ERROR.
380 break;
381 case NATIVE_WINDOW_SET_USAGE:
382 res = dispatchSetUsage(args);
383 break;
384 case NATIVE_WINDOW_SET_CROP:
385 res = dispatchSetCrop(args);
386 break;
387 case NATIVE_WINDOW_SET_BUFFER_COUNT:
388 res = dispatchSetBufferCount(args);
389 break;
390 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
391 res = dispatchSetBuffersGeometry(args);
392 break;
393 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
394 res = dispatchSetBuffersTransform(args);
395 break;
396 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
397 res = dispatchSetBuffersTimestamp(args);
398 break;
399 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
400 res = dispatchSetBuffersDimensions(args);
401 break;
402 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
403 res = dispatchSetBuffersUserDimensions(args);
404 break;
405 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
406 res = dispatchSetBuffersFormat(args);
407 break;
408 case NATIVE_WINDOW_LOCK:
409 res = dispatchLock(args);
410 break;
411 case NATIVE_WINDOW_UNLOCK_AND_POST:
412 res = dispatchUnlockAndPost(args);
413 break;
414 case NATIVE_WINDOW_SET_SCALING_MODE:
415 res = dispatchSetScalingMode(args);
416 break;
417 case NATIVE_WINDOW_API_CONNECT:
418 res = dispatchConnect(args);
419 break;
420 case NATIVE_WINDOW_API_DISCONNECT:
421 res = dispatchDisconnect(args);
422 break;
423 default:
424 res = NAME_NOT_FOUND;
425 break;
426 }
427 return res;
428}
Mathias Agopiana138f892010-05-21 17:24:35 -0700429
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800430int Surface::dispatchConnect(va_list args) {
431 int api = va_arg(args, int);
432 return connect(api);
433}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800434
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800435int Surface::dispatchDisconnect(va_list args) {
436 int api = va_arg(args, int);
437 return disconnect(api);
438}
439
440int Surface::dispatchSetUsage(va_list args) {
441 int usage = va_arg(args, int);
442 return setUsage(usage);
443}
444
445int Surface::dispatchSetCrop(va_list args) {
446 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
447 return setCrop(reinterpret_cast<Rect const*>(rect));
448}
449
450int Surface::dispatchSetBufferCount(va_list args) {
451 size_t bufferCount = va_arg(args, size_t);
452 return setBufferCount(bufferCount);
453}
454
455int Surface::dispatchSetBuffersGeometry(va_list args) {
456 int w = va_arg(args, int);
457 int h = va_arg(args, int);
458 int f = va_arg(args, int);
459 int err = setBuffersDimensions(w, h);
460 if (err != 0) {
461 return err;
462 }
463 return setBuffersFormat(f);
464}
465
466int Surface::dispatchSetBuffersDimensions(va_list args) {
467 int w = va_arg(args, int);
468 int h = va_arg(args, int);
469 return setBuffersDimensions(w, h);
470}
471
472int Surface::dispatchSetBuffersUserDimensions(va_list args) {
473 int w = va_arg(args, int);
474 int h = va_arg(args, int);
475 return setBuffersUserDimensions(w, h);
476}
477
478int Surface::dispatchSetBuffersFormat(va_list args) {
479 int f = va_arg(args, int);
480 return setBuffersFormat(f);
481}
482
483int Surface::dispatchSetScalingMode(va_list args) {
484 int m = va_arg(args, int);
485 return setScalingMode(m);
486}
487
488int Surface::dispatchSetBuffersTransform(va_list args) {
489 int transform = va_arg(args, int);
490 return setBuffersTransform(transform);
491}
492
493int Surface::dispatchSetBuffersTimestamp(va_list args) {
494 int64_t timestamp = va_arg(args, int64_t);
495 return setBuffersTimestamp(timestamp);
496}
497
498int Surface::dispatchLock(va_list args) {
499 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
500 ARect* inOutDirtyBounds = va_arg(args, ARect*);
501 return lock(outBuffer, inOutDirtyBounds);
502}
503
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800504int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800505 return unlockAndPost();
506}
507
508
509int Surface::connect(int api) {
510 ATRACE_CALL();
511 ALOGV("Surface::connect");
Mathias Agopian365857d2013-09-11 19:35:45 -0700512 static sp<BBinder> sLife = new BBinder();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800513 Mutex::Autolock lock(mMutex);
514 IGraphicBufferProducer::QueueBufferOutput output;
Mathias Agopian365857d2013-09-11 19:35:45 -0700515 int err = mGraphicBufferProducer->connect(sLife, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800516 if (err == NO_ERROR) {
517 uint32_t numPendingBuffers = 0;
518 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
519 &numPendingBuffers);
520 mConsumerRunningBehind = (numPendingBuffers >= 2);
521 }
522 if (!err && api == NATIVE_WINDOW_API_CPU) {
523 mConnectedToCpu = true;
524 }
525 return err;
526}
527
Mathias Agopian365857d2013-09-11 19:35:45 -0700528
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800529int Surface::disconnect(int api) {
530 ATRACE_CALL();
531 ALOGV("Surface::disconnect");
532 Mutex::Autolock lock(mMutex);
533 freeAllBuffers();
534 int err = mGraphicBufferProducer->disconnect(api);
535 if (!err) {
536 mReqFormat = 0;
537 mReqWidth = 0;
538 mReqHeight = 0;
539 mReqUsage = 0;
540 mCrop.clear();
541 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
542 mTransform = 0;
543 if (api == NATIVE_WINDOW_API_CPU) {
544 mConnectedToCpu = false;
545 }
546 }
547 return err;
548}
549
550int Surface::setUsage(uint32_t reqUsage)
551{
552 ALOGV("Surface::setUsage");
553 Mutex::Autolock lock(mMutex);
554 mReqUsage = reqUsage;
555 return OK;
556}
557
558int Surface::setCrop(Rect const* rect)
559{
560 ATRACE_CALL();
561
562 Rect realRect;
563 if (rect == NULL || rect->isEmpty()) {
564 realRect.clear();
565 } else {
566 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800567 }
568
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800569 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
570 realRect.left, realRect.top, realRect.right, realRect.bottom);
571
572 Mutex::Autolock lock(mMutex);
573 mCrop = realRect;
574 return NO_ERROR;
575}
576
577int Surface::setBufferCount(int bufferCount)
578{
579 ATRACE_CALL();
580 ALOGV("Surface::setBufferCount");
581 Mutex::Autolock lock(mMutex);
582
583 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
584 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
585 bufferCount, strerror(-err));
Mathias Agopian90147262010-01-22 11:47:55 -0800586
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700587 if (err == NO_ERROR) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800588 freeAllBuffers();
Mathias Agopian87a96ea2011-08-23 21:09:41 -0700589 }
590
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 return err;
592}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700593
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800594int Surface::setBuffersDimensions(int w, int h)
595{
596 ATRACE_CALL();
597 ALOGV("Surface::setBuffersDimensions");
598
599 if (w<0 || h<0)
600 return BAD_VALUE;
601
602 if ((w && !h) || (!w && h))
603 return BAD_VALUE;
604
605 Mutex::Autolock lock(mMutex);
606 mReqWidth = w;
607 mReqHeight = h;
608 return NO_ERROR;
609}
610
611int Surface::setBuffersUserDimensions(int w, int h)
612{
613 ATRACE_CALL();
614 ALOGV("Surface::setBuffersUserDimensions");
615
616 if (w<0 || h<0)
617 return BAD_VALUE;
618
619 if ((w && !h) || (!w && h))
620 return BAD_VALUE;
621
622 Mutex::Autolock lock(mMutex);
623 mUserWidth = w;
624 mUserHeight = h;
625 return NO_ERROR;
626}
627
628int Surface::setBuffersFormat(int format)
629{
630 ALOGV("Surface::setBuffersFormat");
631
632 if (format<0)
633 return BAD_VALUE;
634
635 Mutex::Autolock lock(mMutex);
636 mReqFormat = format;
637 return NO_ERROR;
638}
639
640int Surface::setScalingMode(int mode)
641{
642 ATRACE_CALL();
643 ALOGV("Surface::setScalingMode(%d)", mode);
644
645 switch (mode) {
646 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
647 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
648 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
649 break;
650 default:
651 ALOGE("unknown scaling mode: %d", mode);
652 return BAD_VALUE;
653 }
654
655 Mutex::Autolock lock(mMutex);
656 mScalingMode = mode;
657 return NO_ERROR;
658}
659
660int Surface::setBuffersTransform(int transform)
661{
662 ATRACE_CALL();
663 ALOGV("Surface::setBuffersTransform");
664 Mutex::Autolock lock(mMutex);
665 mTransform = transform;
666 return NO_ERROR;
667}
668
669int Surface::setBuffersTimestamp(int64_t timestamp)
670{
671 ALOGV("Surface::setBuffersTimestamp");
672 Mutex::Autolock lock(mMutex);
673 mTimestamp = timestamp;
674 return NO_ERROR;
675}
676
677void Surface::freeAllBuffers() {
678 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
679 mSlots[i].buffer = 0;
680 }
681}
682
683// ----------------------------------------------------------------------
684// the lock/unlock APIs must be used from the same thread
685
686static status_t copyBlt(
687 const sp<GraphicBuffer>& dst,
688 const sp<GraphicBuffer>& src,
689 const Region& reg)
690{
691 // src and dst with, height and format must be identical. no verification
692 // is done here.
693 status_t err;
694 uint8_t const * src_bits = NULL;
695 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
696 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
697
698 uint8_t* dst_bits = NULL;
699 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
700 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
701
702 Region::const_iterator head(reg.begin());
703 Region::const_iterator tail(reg.end());
704 if (head != tail && src_bits && dst_bits) {
705 const size_t bpp = bytesPerPixel(src->format);
706 const size_t dbpr = dst->stride * bpp;
707 const size_t sbpr = src->stride * bpp;
708
709 while (head != tail) {
710 const Rect& r(*head++);
711 ssize_t h = r.height();
712 if (h <= 0) continue;
713 size_t size = r.width() * bpp;
714 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
715 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
716 if (dbpr==sbpr && size==sbpr) {
717 size *= h;
718 h = 1;
719 }
720 do {
721 memcpy(d, s, size);
722 d += dbpr;
723 s += sbpr;
724 } while (--h > 0);
725 }
726 }
727
728 if (src_bits)
729 src->unlock();
730
731 if (dst_bits)
732 dst->unlock();
733
734 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735}
736
Mathias Agopiana138f892010-05-21 17:24:35 -0700737// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800738
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800739status_t Surface::lock(
740 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
741{
742 if (mLockedBuffer != 0) {
743 ALOGE("Surface::lock failed, already locked");
744 return INVALID_OPERATION;
745 }
746
747 if (!mConnectedToCpu) {
748 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
749 if (err) {
750 return err;
751 }
752 // we're intending to do software rendering from this point
753 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
754 }
755
756 ANativeWindowBuffer* out;
757 int fenceFd = -1;
758 status_t err = dequeueBuffer(&out, &fenceFd);
759 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
760 if (err == NO_ERROR) {
761 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
762 sp<Fence> fence(new Fence(fenceFd));
763
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700764 err = fence->waitForever("Surface::lock");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800765 if (err != OK) {
766 ALOGE("Fence::wait failed (%s)", strerror(-err));
767 cancelBuffer(out, fenceFd);
768 return err;
769 }
770
771 const Rect bounds(backBuffer->width, backBuffer->height);
772
773 Region newDirtyRegion;
774 if (inOutDirtyBounds) {
775 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
776 newDirtyRegion.andSelf(bounds);
777 } else {
778 newDirtyRegion.set(bounds);
779 }
780
781 // figure out if we can copy the frontbuffer back
782 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
783 const bool canCopyBack = (frontBuffer != 0 &&
784 backBuffer->width == frontBuffer->width &&
785 backBuffer->height == frontBuffer->height &&
786 backBuffer->format == frontBuffer->format);
787
788 if (canCopyBack) {
789 // copy the area that is invalid and not repainted this round
790 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
791 if (!copyback.isEmpty())
792 copyBlt(backBuffer, frontBuffer, copyback);
793 } else {
794 // if we can't copy-back anything, modify the user's dirty
795 // region to make sure they redraw the whole buffer
796 newDirtyRegion.set(bounds);
797 mDirtyRegion.clear();
798 Mutex::Autolock lock(mMutex);
799 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
800 mSlots[i].dirtyRegion.clear();
801 }
802 }
803
804
805 { // scope for the lock
806 Mutex::Autolock lock(mMutex);
807 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
808 if (backBufferSlot >= 0) {
809 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
810 mDirtyRegion.subtract(dirtyRegion);
811 dirtyRegion = newDirtyRegion;
812 }
813 }
814
815 mDirtyRegion.orSelf(newDirtyRegion);
816 if (inOutDirtyBounds) {
817 *inOutDirtyBounds = newDirtyRegion.getBounds();
818 }
819
820 void* vaddr;
821 status_t res = backBuffer->lock(
822 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
823 newDirtyRegion.bounds(), &vaddr);
824
825 ALOGW_IF(res, "failed locking buffer (handle = %p)",
826 backBuffer->handle);
827
828 if (res != 0) {
829 err = INVALID_OPERATION;
830 } else {
831 mLockedBuffer = backBuffer;
832 outBuffer->width = backBuffer->width;
833 outBuffer->height = backBuffer->height;
834 outBuffer->stride = backBuffer->stride;
835 outBuffer->format = backBuffer->format;
836 outBuffer->bits = vaddr;
837 }
838 }
839 return err;
840}
841
842status_t Surface::unlockAndPost()
843{
844 if (mLockedBuffer == 0) {
845 ALOGE("Surface::unlockAndPost failed, no locked buffer");
846 return INVALID_OPERATION;
847 }
848
849 status_t err = mLockedBuffer->unlock();
850 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
851
852 err = queueBuffer(mLockedBuffer.get(), -1);
853 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
854 mLockedBuffer->handle, strerror(-err));
855
856 mPostedBuffer = mLockedBuffer;
857 mLockedBuffer = 0;
858 return err;
859}
860
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800861}; // namespace android