blob: 95f4084260ad4983cbd1cb252976365992e3267b [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
Wonsik Kim0ee14ca2014-03-17 17:46:53 +090089void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
90 mGraphicBufferProducer->setSidebandStream(stream);
91}
92
Mathias Agopiane3c697f2013-02-14 17:11:02 -080093int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
94 Surface* c = getSelf(window);
95 return c->setSwapInterval(interval);
96}
97
98int Surface::hook_dequeueBuffer(ANativeWindow* window,
99 ANativeWindowBuffer** buffer, int* fenceFd) {
100 Surface* c = getSelf(window);
101 return c->dequeueBuffer(buffer, fenceFd);
102}
103
104int Surface::hook_cancelBuffer(ANativeWindow* window,
105 ANativeWindowBuffer* buffer, int fenceFd) {
106 Surface* c = getSelf(window);
107 return c->cancelBuffer(buffer, fenceFd);
108}
109
110int Surface::hook_queueBuffer(ANativeWindow* window,
111 ANativeWindowBuffer* buffer, int fenceFd) {
112 Surface* c = getSelf(window);
113 return c->queueBuffer(buffer, fenceFd);
114}
115
116int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
117 ANativeWindowBuffer** buffer) {
118 Surface* c = getSelf(window);
119 ANativeWindowBuffer* buf;
120 int fenceFd = -1;
121 int result = c->dequeueBuffer(&buf, &fenceFd);
122 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700123 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800124 if (waitResult != OK) {
125 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
126 waitResult);
127 c->cancelBuffer(buf, -1);
128 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700129 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800130 *buffer = buf;
131 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700132}
133
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800134int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
135 ANativeWindowBuffer* buffer) {
136 Surface* c = getSelf(window);
137 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700138}
139
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800140int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
141 ANativeWindowBuffer* buffer) {
142 Surface* c = getSelf(window);
143 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700144}
145
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800146int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
147 ANativeWindowBuffer* buffer) {
148 Surface* c = getSelf(window);
149 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700150}
Mathias Agopian62185b72009-04-16 16:19:50 -0700151
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800152int Surface::hook_query(const ANativeWindow* window,
153 int what, int* value) {
154 const Surface* c = getSelf(window);
155 return c->query(what, value);
156}
157
158int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
159 va_list args;
160 va_start(args, operation);
161 Surface* c = getSelf(window);
162 return c->perform(operation, args);
163}
164
165int Surface::setSwapInterval(int interval) {
166 ATRACE_CALL();
167 // EGL specification states:
168 // interval is silently clamped to minimum and maximum implementation
169 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800170
171 if (interval < minSwapInterval)
172 interval = minSwapInterval;
173
174 if (interval > maxSwapInterval)
175 interval = maxSwapInterval;
176
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700177 mSwapIntervalZero = (interval == 0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700179 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800180}
181
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700182int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800183 ATRACE_CALL();
184 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800185
186 int reqW;
187 int reqH;
188 bool swapIntervalZero;
189 uint32_t reqFormat;
190 uint32_t reqUsage;
191
192 {
193 Mutex::Autolock lock(mMutex);
194
195 reqW = mReqWidth ? mReqWidth : mUserWidth;
196 reqH = mReqHeight ? mReqHeight : mUserHeight;
197
198 swapIntervalZero = mSwapIntervalZero;
199 reqFormat = mReqFormat;
200 reqUsage = mReqUsage;
201 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
202
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800203 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800204 sp<Fence> fence;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800205 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
206 reqW, reqH, reqFormat, reqUsage);
207
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800208 if (result < 0) {
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800209 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
210 "failed: %d", swapIntervalZero, reqW, reqH, reqFormat, reqUsage,
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800211 result);
212 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700213 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800214
215 Mutex::Autolock lock(mMutex);
216
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800217 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700218
219 // this should never happen
220 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
221
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800222 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
223 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700224 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700225
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800226 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
227 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
228 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700229 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800230 return result;
231 }
232 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700233
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800234 if (fence->isValid()) {
235 *fenceFd = fence->dup();
236 if (*fenceFd == -1) {
237 ALOGE("dequeueBuffer: error duping fence: %d", errno);
238 // dup() should never fail; something is badly wrong. Soldier on
239 // and hope for the best; the worst that should happen is some
240 // visible corruption that lasts until the next frame.
241 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700242 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800243 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700244 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800245
246 *buffer = gbuf.get();
247 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700248}
249
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800250int Surface::cancelBuffer(android_native_buffer_t* buffer,
251 int fenceFd) {
252 ATRACE_CALL();
253 ALOGV("Surface::cancelBuffer");
254 Mutex::Autolock lock(mMutex);
255 int i = getSlotFromBufferLocked(buffer);
256 if (i < 0) {
257 return i;
258 }
259 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
260 mGraphicBufferProducer->cancelBuffer(i, fence);
261 return OK;
262}
263
264int Surface::getSlotFromBufferLocked(
265 android_native_buffer_t* buffer) const {
266 bool dumpedState = false;
267 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
268 if (mSlots[i].buffer != NULL &&
269 mSlots[i].buffer->handle == buffer->handle) {
270 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700271 }
272 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800273 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
274 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700275}
276
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800277int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800278 ALOGV("Surface::lockBuffer");
279 Mutex::Autolock lock(mMutex);
280 return OK;
281}
Mathias Agopian631f3582010-05-25 17:51:34 -0700282
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800283int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
284 ATRACE_CALL();
285 ALOGV("Surface::queueBuffer");
286 Mutex::Autolock lock(mMutex);
287 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700288 bool isAutoTimestamp = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800289 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700290 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700291 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700292 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
293 timestamp / 1000000.f);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800294 } else {
295 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700296 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800297 int i = getSlotFromBufferLocked(buffer);
298 if (i < 0) {
299 return i;
300 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800303 // Make sure the crop rectangle is entirely inside the buffer.
304 Rect crop;
305 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800307 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
308 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700309 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
310 crop, mScalingMode, mTransform, mSwapIntervalZero, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800311 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
312 if (err != OK) {
313 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
314 }
315 uint32_t numPendingBuffers = 0;
316 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
317 &numPendingBuffers);
tedbo1e7fa9e2011-06-22 15:52:53 -0700318
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800319 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700320
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800321 return err;
322}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700323
Mathias Agopiana67932f2011-04-20 14:20:59 -0700324int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800325 ATRACE_CALL();
326 ALOGV("Surface::query");
327 { // scope for the lock
328 Mutex::Autolock lock(mMutex);
329 switch (what) {
330 case NATIVE_WINDOW_FORMAT:
331 if (mReqFormat) {
332 *value = mReqFormat;
333 return NO_ERROR;
334 }
335 break;
336 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
337 sp<ISurfaceComposer> composer(
338 ComposerService::getComposerService());
339 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
340 *value = 1;
341 } else {
342 *value = 0;
343 }
344 return NO_ERROR;
345 }
346 case NATIVE_WINDOW_CONCRETE_TYPE:
347 *value = NATIVE_WINDOW_SURFACE;
348 return NO_ERROR;
349 case NATIVE_WINDOW_DEFAULT_WIDTH:
350 *value = mUserWidth ? mUserWidth : mDefaultWidth;
351 return NO_ERROR;
352 case NATIVE_WINDOW_DEFAULT_HEIGHT:
353 *value = mUserHeight ? mUserHeight : mDefaultHeight;
354 return NO_ERROR;
355 case NATIVE_WINDOW_TRANSFORM_HINT:
356 *value = mTransformHint;
357 return NO_ERROR;
358 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
359 status_t err = NO_ERROR;
360 if (!mConsumerRunningBehind) {
361 *value = 0;
362 } else {
363 err = mGraphicBufferProducer->query(what, value);
364 if (err == NO_ERROR) {
365 mConsumerRunningBehind = *value;
366 }
367 }
368 return err;
369 }
370 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700371 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800372 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800373}
374
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800375int Surface::perform(int operation, va_list args)
376{
377 int res = NO_ERROR;
378 switch (operation) {
379 case NATIVE_WINDOW_CONNECT:
380 // deprecated. must return NO_ERROR.
381 break;
382 case NATIVE_WINDOW_DISCONNECT:
383 // deprecated. must return NO_ERROR.
384 break;
385 case NATIVE_WINDOW_SET_USAGE:
386 res = dispatchSetUsage(args);
387 break;
388 case NATIVE_WINDOW_SET_CROP:
389 res = dispatchSetCrop(args);
390 break;
391 case NATIVE_WINDOW_SET_BUFFER_COUNT:
392 res = dispatchSetBufferCount(args);
393 break;
394 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
395 res = dispatchSetBuffersGeometry(args);
396 break;
397 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
398 res = dispatchSetBuffersTransform(args);
399 break;
400 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
401 res = dispatchSetBuffersTimestamp(args);
402 break;
403 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
404 res = dispatchSetBuffersDimensions(args);
405 break;
406 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
407 res = dispatchSetBuffersUserDimensions(args);
408 break;
409 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
410 res = dispatchSetBuffersFormat(args);
411 break;
412 case NATIVE_WINDOW_LOCK:
413 res = dispatchLock(args);
414 break;
415 case NATIVE_WINDOW_UNLOCK_AND_POST:
416 res = dispatchUnlockAndPost(args);
417 break;
418 case NATIVE_WINDOW_SET_SCALING_MODE:
419 res = dispatchSetScalingMode(args);
420 break;
421 case NATIVE_WINDOW_API_CONNECT:
422 res = dispatchConnect(args);
423 break;
424 case NATIVE_WINDOW_API_DISCONNECT:
425 res = dispatchDisconnect(args);
426 break;
427 default:
428 res = NAME_NOT_FOUND;
429 break;
430 }
431 return res;
432}
Mathias Agopiana138f892010-05-21 17:24:35 -0700433
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800434int Surface::dispatchConnect(va_list args) {
435 int api = va_arg(args, int);
436 return connect(api);
437}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800438
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800439int Surface::dispatchDisconnect(va_list args) {
440 int api = va_arg(args, int);
441 return disconnect(api);
442}
443
444int Surface::dispatchSetUsage(va_list args) {
445 int usage = va_arg(args, int);
446 return setUsage(usage);
447}
448
449int Surface::dispatchSetCrop(va_list args) {
450 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
451 return setCrop(reinterpret_cast<Rect const*>(rect));
452}
453
454int Surface::dispatchSetBufferCount(va_list args) {
455 size_t bufferCount = va_arg(args, size_t);
456 return setBufferCount(bufferCount);
457}
458
459int Surface::dispatchSetBuffersGeometry(va_list args) {
460 int w = va_arg(args, int);
461 int h = va_arg(args, int);
462 int f = va_arg(args, int);
463 int err = setBuffersDimensions(w, h);
464 if (err != 0) {
465 return err;
466 }
467 return setBuffersFormat(f);
468}
469
470int Surface::dispatchSetBuffersDimensions(va_list args) {
471 int w = va_arg(args, int);
472 int h = va_arg(args, int);
473 return setBuffersDimensions(w, h);
474}
475
476int Surface::dispatchSetBuffersUserDimensions(va_list args) {
477 int w = va_arg(args, int);
478 int h = va_arg(args, int);
479 return setBuffersUserDimensions(w, h);
480}
481
482int Surface::dispatchSetBuffersFormat(va_list args) {
483 int f = va_arg(args, int);
484 return setBuffersFormat(f);
485}
486
487int Surface::dispatchSetScalingMode(va_list args) {
488 int m = va_arg(args, int);
489 return setScalingMode(m);
490}
491
492int Surface::dispatchSetBuffersTransform(va_list args) {
493 int transform = va_arg(args, int);
494 return setBuffersTransform(transform);
495}
496
497int Surface::dispatchSetBuffersTimestamp(va_list args) {
498 int64_t timestamp = va_arg(args, int64_t);
499 return setBuffersTimestamp(timestamp);
500}
501
502int Surface::dispatchLock(va_list args) {
503 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
504 ARect* inOutDirtyBounds = va_arg(args, ARect*);
505 return lock(outBuffer, inOutDirtyBounds);
506}
507
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800508int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800509 return unlockAndPost();
510}
511
512
513int Surface::connect(int api) {
514 ATRACE_CALL();
515 ALOGV("Surface::connect");
Mathias Agopian365857d2013-09-11 19:35:45 -0700516 static sp<BBinder> sLife = new BBinder();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 Mutex::Autolock lock(mMutex);
518 IGraphicBufferProducer::QueueBufferOutput output;
Mathias Agopian365857d2013-09-11 19:35:45 -0700519 int err = mGraphicBufferProducer->connect(sLife, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800520 if (err == NO_ERROR) {
521 uint32_t numPendingBuffers = 0;
522 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
523 &numPendingBuffers);
524 mConsumerRunningBehind = (numPendingBuffers >= 2);
525 }
526 if (!err && api == NATIVE_WINDOW_API_CPU) {
527 mConnectedToCpu = true;
528 }
529 return err;
530}
531
Mathias Agopian365857d2013-09-11 19:35:45 -0700532
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800533int Surface::disconnect(int api) {
534 ATRACE_CALL();
535 ALOGV("Surface::disconnect");
536 Mutex::Autolock lock(mMutex);
537 freeAllBuffers();
538 int err = mGraphicBufferProducer->disconnect(api);
539 if (!err) {
540 mReqFormat = 0;
541 mReqWidth = 0;
542 mReqHeight = 0;
543 mReqUsage = 0;
544 mCrop.clear();
545 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
546 mTransform = 0;
547 if (api == NATIVE_WINDOW_API_CPU) {
548 mConnectedToCpu = false;
549 }
550 }
551 return err;
552}
553
554int Surface::setUsage(uint32_t reqUsage)
555{
556 ALOGV("Surface::setUsage");
557 Mutex::Autolock lock(mMutex);
558 mReqUsage = reqUsage;
559 return OK;
560}
561
562int Surface::setCrop(Rect const* rect)
563{
564 ATRACE_CALL();
565
566 Rect realRect;
567 if (rect == NULL || rect->isEmpty()) {
568 realRect.clear();
569 } else {
570 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800571 }
572
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800573 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
574 realRect.left, realRect.top, realRect.right, realRect.bottom);
575
576 Mutex::Autolock lock(mMutex);
577 mCrop = realRect;
578 return NO_ERROR;
579}
580
581int Surface::setBufferCount(int bufferCount)
582{
583 ATRACE_CALL();
584 ALOGV("Surface::setBufferCount");
585 Mutex::Autolock lock(mMutex);
586
587 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
588 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
589 bufferCount, strerror(-err));
Mathias Agopian90147262010-01-22 11:47:55 -0800590
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700591 if (err == NO_ERROR) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800592 freeAllBuffers();
Mathias Agopian87a96ea2011-08-23 21:09:41 -0700593 }
594
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700595 return err;
596}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700597
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800598int Surface::setBuffersDimensions(int w, int h)
599{
600 ATRACE_CALL();
601 ALOGV("Surface::setBuffersDimensions");
602
603 if (w<0 || h<0)
604 return BAD_VALUE;
605
606 if ((w && !h) || (!w && h))
607 return BAD_VALUE;
608
609 Mutex::Autolock lock(mMutex);
610 mReqWidth = w;
611 mReqHeight = h;
612 return NO_ERROR;
613}
614
615int Surface::setBuffersUserDimensions(int w, int h)
616{
617 ATRACE_CALL();
618 ALOGV("Surface::setBuffersUserDimensions");
619
620 if (w<0 || h<0)
621 return BAD_VALUE;
622
623 if ((w && !h) || (!w && h))
624 return BAD_VALUE;
625
626 Mutex::Autolock lock(mMutex);
627 mUserWidth = w;
628 mUserHeight = h;
629 return NO_ERROR;
630}
631
632int Surface::setBuffersFormat(int format)
633{
634 ALOGV("Surface::setBuffersFormat");
635
636 if (format<0)
637 return BAD_VALUE;
638
639 Mutex::Autolock lock(mMutex);
640 mReqFormat = format;
641 return NO_ERROR;
642}
643
644int Surface::setScalingMode(int mode)
645{
646 ATRACE_CALL();
647 ALOGV("Surface::setScalingMode(%d)", mode);
648
649 switch (mode) {
650 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
651 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
652 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
653 break;
654 default:
655 ALOGE("unknown scaling mode: %d", mode);
656 return BAD_VALUE;
657 }
658
659 Mutex::Autolock lock(mMutex);
660 mScalingMode = mode;
661 return NO_ERROR;
662}
663
664int Surface::setBuffersTransform(int transform)
665{
666 ATRACE_CALL();
667 ALOGV("Surface::setBuffersTransform");
668 Mutex::Autolock lock(mMutex);
669 mTransform = transform;
670 return NO_ERROR;
671}
672
673int Surface::setBuffersTimestamp(int64_t timestamp)
674{
675 ALOGV("Surface::setBuffersTimestamp");
676 Mutex::Autolock lock(mMutex);
677 mTimestamp = timestamp;
678 return NO_ERROR;
679}
680
681void Surface::freeAllBuffers() {
682 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
683 mSlots[i].buffer = 0;
684 }
685}
686
687// ----------------------------------------------------------------------
688// the lock/unlock APIs must be used from the same thread
689
690static status_t copyBlt(
691 const sp<GraphicBuffer>& dst,
692 const sp<GraphicBuffer>& src,
693 const Region& reg)
694{
695 // src and dst with, height and format must be identical. no verification
696 // is done here.
697 status_t err;
698 uint8_t const * src_bits = NULL;
699 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
700 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
701
702 uint8_t* dst_bits = NULL;
703 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
704 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
705
706 Region::const_iterator head(reg.begin());
707 Region::const_iterator tail(reg.end());
708 if (head != tail && src_bits && dst_bits) {
709 const size_t bpp = bytesPerPixel(src->format);
710 const size_t dbpr = dst->stride * bpp;
711 const size_t sbpr = src->stride * bpp;
712
713 while (head != tail) {
714 const Rect& r(*head++);
715 ssize_t h = r.height();
716 if (h <= 0) continue;
717 size_t size = r.width() * bpp;
718 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
719 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
720 if (dbpr==sbpr && size==sbpr) {
721 size *= h;
722 h = 1;
723 }
724 do {
725 memcpy(d, s, size);
726 d += dbpr;
727 s += sbpr;
728 } while (--h > 0);
729 }
730 }
731
732 if (src_bits)
733 src->unlock();
734
735 if (dst_bits)
736 dst->unlock();
737
738 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739}
740
Mathias Agopiana138f892010-05-21 17:24:35 -0700741// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800742
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800743status_t Surface::lock(
744 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
745{
746 if (mLockedBuffer != 0) {
747 ALOGE("Surface::lock failed, already locked");
748 return INVALID_OPERATION;
749 }
750
751 if (!mConnectedToCpu) {
752 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
753 if (err) {
754 return err;
755 }
756 // we're intending to do software rendering from this point
757 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
758 }
759
760 ANativeWindowBuffer* out;
761 int fenceFd = -1;
762 status_t err = dequeueBuffer(&out, &fenceFd);
763 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
764 if (err == NO_ERROR) {
765 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
766 sp<Fence> fence(new Fence(fenceFd));
767
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700768 err = fence->waitForever("Surface::lock");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800769 if (err != OK) {
770 ALOGE("Fence::wait failed (%s)", strerror(-err));
771 cancelBuffer(out, fenceFd);
772 return err;
773 }
774
775 const Rect bounds(backBuffer->width, backBuffer->height);
776
777 Region newDirtyRegion;
778 if (inOutDirtyBounds) {
779 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
780 newDirtyRegion.andSelf(bounds);
781 } else {
782 newDirtyRegion.set(bounds);
783 }
784
785 // figure out if we can copy the frontbuffer back
786 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
787 const bool canCopyBack = (frontBuffer != 0 &&
788 backBuffer->width == frontBuffer->width &&
789 backBuffer->height == frontBuffer->height &&
790 backBuffer->format == frontBuffer->format);
791
792 if (canCopyBack) {
793 // copy the area that is invalid and not repainted this round
794 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
795 if (!copyback.isEmpty())
796 copyBlt(backBuffer, frontBuffer, copyback);
797 } else {
798 // if we can't copy-back anything, modify the user's dirty
799 // region to make sure they redraw the whole buffer
800 newDirtyRegion.set(bounds);
801 mDirtyRegion.clear();
802 Mutex::Autolock lock(mMutex);
803 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
804 mSlots[i].dirtyRegion.clear();
805 }
806 }
807
808
809 { // scope for the lock
810 Mutex::Autolock lock(mMutex);
811 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
812 if (backBufferSlot >= 0) {
813 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
814 mDirtyRegion.subtract(dirtyRegion);
815 dirtyRegion = newDirtyRegion;
816 }
817 }
818
819 mDirtyRegion.orSelf(newDirtyRegion);
820 if (inOutDirtyBounds) {
821 *inOutDirtyBounds = newDirtyRegion.getBounds();
822 }
823
824 void* vaddr;
825 status_t res = backBuffer->lock(
826 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
827 newDirtyRegion.bounds(), &vaddr);
828
829 ALOGW_IF(res, "failed locking buffer (handle = %p)",
830 backBuffer->handle);
831
832 if (res != 0) {
833 err = INVALID_OPERATION;
834 } else {
835 mLockedBuffer = backBuffer;
836 outBuffer->width = backBuffer->width;
837 outBuffer->height = backBuffer->height;
838 outBuffer->stride = backBuffer->stride;
839 outBuffer->format = backBuffer->format;
840 outBuffer->bits = vaddr;
841 }
842 }
843 return err;
844}
845
846status_t Surface::unlockAndPost()
847{
848 if (mLockedBuffer == 0) {
849 ALOGE("Surface::unlockAndPost failed, no locked buffer");
850 return INVALID_OPERATION;
851 }
852
853 status_t err = mLockedBuffer->unlock();
854 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
855
856 err = queueBuffer(mLockedBuffer.get(), -1);
857 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
858 mLockedBuffer->handle, strerror(-err));
859
860 mPostedBuffer = mLockedBuffer;
861 mLockedBuffer = 0;
862 return err;
863}
864
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865}; // namespace android