blob: 637527b752e0d176abf2e232c6b45f4c5686bcf2 [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");
181 Mutex::Autolock lock(mMutex);
182 int buf = -1;
183 int reqW = mReqWidth ? mReqWidth : mUserWidth;
184 int reqH = mReqHeight ? mReqHeight : mUserHeight;
185 sp<Fence> fence;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700186 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, mSwapIntervalZero,
Jesse Hall4c00cc12013-03-15 21:34:30 -0700187 reqW, reqH, mReqFormat, mReqUsage);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800188 if (result < 0) {
189 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d)"
190 "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
191 result);
192 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700193 }
Dan Stozaac93b3a2017-05-01 16:31:53 -0700194
195 if (buf < 0 || buf >= NUM_BUFFER_SLOTS) {
196 ALOGE("dequeueBuffer: IGraphicBufferProducer returned invalid slot number %d", buf);
197 android_errorWriteLog(0x534e4554, "36991414"); // SafetyNet logging
198 return FAILED_TRANSACTION;
199 }
200
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800201 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700202
203 // this should never happen
204 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
205
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800206 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
207 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700208 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700209
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800210 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
211 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
212 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700213 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800214 return result;
215 }
216 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700217
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218 if (fence->isValid()) {
219 *fenceFd = fence->dup();
220 if (*fenceFd == -1) {
221 ALOGE("dequeueBuffer: error duping fence: %d", errno);
222 // dup() should never fail; something is badly wrong. Soldier on
223 // and hope for the best; the worst that should happen is some
224 // visible corruption that lasts until the next frame.
225 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700226 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800227 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700228 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800229
230 *buffer = gbuf.get();
231 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700232}
233
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800234int Surface::cancelBuffer(android_native_buffer_t* buffer,
235 int fenceFd) {
236 ATRACE_CALL();
237 ALOGV("Surface::cancelBuffer");
238 Mutex::Autolock lock(mMutex);
239 int i = getSlotFromBufferLocked(buffer);
240 if (i < 0) {
241 return i;
242 }
243 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
244 mGraphicBufferProducer->cancelBuffer(i, fence);
245 return OK;
246}
247
248int Surface::getSlotFromBufferLocked(
249 android_native_buffer_t* buffer) const {
250 bool dumpedState = false;
251 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
252 if (mSlots[i].buffer != NULL &&
253 mSlots[i].buffer->handle == buffer->handle) {
254 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700255 }
256 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800257 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
258 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700259}
260
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800261int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer) {
262 ALOGV("Surface::lockBuffer");
263 Mutex::Autolock lock(mMutex);
264 return OK;
265}
Mathias Agopian631f3582010-05-25 17:51:34 -0700266
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800267int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
268 ATRACE_CALL();
269 ALOGV("Surface::queueBuffer");
270 Mutex::Autolock lock(mMutex);
271 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700272 bool isAutoTimestamp = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800273 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700274 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700275 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700276 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
277 timestamp / 1000000.f);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800278 } else {
279 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700280 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800281 int i = getSlotFromBufferLocked(buffer);
282 if (i < 0) {
283 return i;
284 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800287 // Make sure the crop rectangle is entirely inside the buffer.
288 Rect crop;
289 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800291 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
292 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700293 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
294 crop, mScalingMode, mTransform, mSwapIntervalZero, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800295 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
296 if (err != OK) {
297 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
298 }
299 uint32_t numPendingBuffers = 0;
300 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
301 &numPendingBuffers);
tedbo1e7fa9e2011-06-22 15:52:53 -0700302
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800303 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700304
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800305 return err;
306}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700307
Mathias Agopiana67932f2011-04-20 14:20:59 -0700308int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800309 ATRACE_CALL();
310 ALOGV("Surface::query");
311 { // scope for the lock
312 Mutex::Autolock lock(mMutex);
313 switch (what) {
314 case NATIVE_WINDOW_FORMAT:
315 if (mReqFormat) {
316 *value = mReqFormat;
317 return NO_ERROR;
318 }
319 break;
320 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
321 sp<ISurfaceComposer> composer(
322 ComposerService::getComposerService());
323 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
324 *value = 1;
325 } else {
326 *value = 0;
327 }
328 return NO_ERROR;
329 }
330 case NATIVE_WINDOW_CONCRETE_TYPE:
331 *value = NATIVE_WINDOW_SURFACE;
332 return NO_ERROR;
333 case NATIVE_WINDOW_DEFAULT_WIDTH:
334 *value = mUserWidth ? mUserWidth : mDefaultWidth;
335 return NO_ERROR;
336 case NATIVE_WINDOW_DEFAULT_HEIGHT:
337 *value = mUserHeight ? mUserHeight : mDefaultHeight;
338 return NO_ERROR;
339 case NATIVE_WINDOW_TRANSFORM_HINT:
340 *value = mTransformHint;
341 return NO_ERROR;
342 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
343 status_t err = NO_ERROR;
344 if (!mConsumerRunningBehind) {
345 *value = 0;
346 } else {
347 err = mGraphicBufferProducer->query(what, value);
348 if (err == NO_ERROR) {
349 mConsumerRunningBehind = *value;
350 }
351 }
352 return err;
353 }
354 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700355 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800356 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800357}
358
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800359int Surface::perform(int operation, va_list args)
360{
361 int res = NO_ERROR;
362 switch (operation) {
363 case NATIVE_WINDOW_CONNECT:
364 // deprecated. must return NO_ERROR.
365 break;
366 case NATIVE_WINDOW_DISCONNECT:
367 // deprecated. must return NO_ERROR.
368 break;
369 case NATIVE_WINDOW_SET_USAGE:
370 res = dispatchSetUsage(args);
371 break;
372 case NATIVE_WINDOW_SET_CROP:
373 res = dispatchSetCrop(args);
374 break;
375 case NATIVE_WINDOW_SET_BUFFER_COUNT:
376 res = dispatchSetBufferCount(args);
377 break;
378 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
379 res = dispatchSetBuffersGeometry(args);
380 break;
381 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
382 res = dispatchSetBuffersTransform(args);
383 break;
384 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
385 res = dispatchSetBuffersTimestamp(args);
386 break;
387 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
388 res = dispatchSetBuffersDimensions(args);
389 break;
390 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
391 res = dispatchSetBuffersUserDimensions(args);
392 break;
393 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
394 res = dispatchSetBuffersFormat(args);
395 break;
396 case NATIVE_WINDOW_LOCK:
397 res = dispatchLock(args);
398 break;
399 case NATIVE_WINDOW_UNLOCK_AND_POST:
400 res = dispatchUnlockAndPost(args);
401 break;
402 case NATIVE_WINDOW_SET_SCALING_MODE:
403 res = dispatchSetScalingMode(args);
404 break;
405 case NATIVE_WINDOW_API_CONNECT:
406 res = dispatchConnect(args);
407 break;
408 case NATIVE_WINDOW_API_DISCONNECT:
409 res = dispatchDisconnect(args);
410 break;
411 default:
412 res = NAME_NOT_FOUND;
413 break;
414 }
415 return res;
416}
Mathias Agopiana138f892010-05-21 17:24:35 -0700417
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800418int Surface::dispatchConnect(va_list args) {
419 int api = va_arg(args, int);
420 return connect(api);
421}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800422
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800423int Surface::dispatchDisconnect(va_list args) {
424 int api = va_arg(args, int);
425 return disconnect(api);
426}
427
428int Surface::dispatchSetUsage(va_list args) {
429 int usage = va_arg(args, int);
430 return setUsage(usage);
431}
432
433int Surface::dispatchSetCrop(va_list args) {
434 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
435 return setCrop(reinterpret_cast<Rect const*>(rect));
436}
437
438int Surface::dispatchSetBufferCount(va_list args) {
439 size_t bufferCount = va_arg(args, size_t);
440 return setBufferCount(bufferCount);
441}
442
443int Surface::dispatchSetBuffersGeometry(va_list args) {
444 int w = va_arg(args, int);
445 int h = va_arg(args, int);
446 int f = va_arg(args, int);
447 int err = setBuffersDimensions(w, h);
448 if (err != 0) {
449 return err;
450 }
451 return setBuffersFormat(f);
452}
453
454int Surface::dispatchSetBuffersDimensions(va_list args) {
455 int w = va_arg(args, int);
456 int h = va_arg(args, int);
457 return setBuffersDimensions(w, h);
458}
459
460int Surface::dispatchSetBuffersUserDimensions(va_list args) {
461 int w = va_arg(args, int);
462 int h = va_arg(args, int);
463 return setBuffersUserDimensions(w, h);
464}
465
466int Surface::dispatchSetBuffersFormat(va_list args) {
467 int f = va_arg(args, int);
468 return setBuffersFormat(f);
469}
470
471int Surface::dispatchSetScalingMode(va_list args) {
472 int m = va_arg(args, int);
473 return setScalingMode(m);
474}
475
476int Surface::dispatchSetBuffersTransform(va_list args) {
477 int transform = va_arg(args, int);
478 return setBuffersTransform(transform);
479}
480
481int Surface::dispatchSetBuffersTimestamp(va_list args) {
482 int64_t timestamp = va_arg(args, int64_t);
483 return setBuffersTimestamp(timestamp);
484}
485
486int Surface::dispatchLock(va_list args) {
487 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
488 ARect* inOutDirtyBounds = va_arg(args, ARect*);
489 return lock(outBuffer, inOutDirtyBounds);
490}
491
492int Surface::dispatchUnlockAndPost(va_list args) {
493 return unlockAndPost();
494}
495
496
497int Surface::connect(int api) {
498 ATRACE_CALL();
499 ALOGV("Surface::connect");
Mathias Agopian365857d2013-09-11 19:35:45 -0700500 static sp<BBinder> sLife = new BBinder();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800501 Mutex::Autolock lock(mMutex);
502 IGraphicBufferProducer::QueueBufferOutput output;
Mathias Agopian365857d2013-09-11 19:35:45 -0700503 int err = mGraphicBufferProducer->connect(sLife, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800504 if (err == NO_ERROR) {
505 uint32_t numPendingBuffers = 0;
506 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
507 &numPendingBuffers);
508 mConsumerRunningBehind = (numPendingBuffers >= 2);
509 }
510 if (!err && api == NATIVE_WINDOW_API_CPU) {
511 mConnectedToCpu = true;
512 }
513 return err;
514}
515
Mathias Agopian365857d2013-09-11 19:35:45 -0700516
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517int Surface::disconnect(int api) {
518 ATRACE_CALL();
519 ALOGV("Surface::disconnect");
520 Mutex::Autolock lock(mMutex);
521 freeAllBuffers();
522 int err = mGraphicBufferProducer->disconnect(api);
523 if (!err) {
524 mReqFormat = 0;
525 mReqWidth = 0;
526 mReqHeight = 0;
527 mReqUsage = 0;
528 mCrop.clear();
529 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
530 mTransform = 0;
531 if (api == NATIVE_WINDOW_API_CPU) {
532 mConnectedToCpu = false;
533 }
534 }
535 return err;
536}
537
538int Surface::setUsage(uint32_t reqUsage)
539{
540 ALOGV("Surface::setUsage");
541 Mutex::Autolock lock(mMutex);
542 mReqUsage = reqUsage;
543 return OK;
544}
545
546int Surface::setCrop(Rect const* rect)
547{
548 ATRACE_CALL();
549
550 Rect realRect;
551 if (rect == NULL || rect->isEmpty()) {
552 realRect.clear();
553 } else {
554 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800555 }
556
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800557 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
558 realRect.left, realRect.top, realRect.right, realRect.bottom);
559
560 Mutex::Autolock lock(mMutex);
561 mCrop = realRect;
562 return NO_ERROR;
563}
564
565int Surface::setBufferCount(int bufferCount)
566{
567 ATRACE_CALL();
568 ALOGV("Surface::setBufferCount");
569 Mutex::Autolock lock(mMutex);
570
571 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
572 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
573 bufferCount, strerror(-err));
Mathias Agopian90147262010-01-22 11:47:55 -0800574
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700575 if (err == NO_ERROR) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800576 freeAllBuffers();
Mathias Agopian87a96ea2011-08-23 21:09:41 -0700577 }
578
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700579 return err;
580}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700581
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800582int Surface::setBuffersDimensions(int w, int h)
583{
584 ATRACE_CALL();
585 ALOGV("Surface::setBuffersDimensions");
586
587 if (w<0 || h<0)
588 return BAD_VALUE;
589
590 if ((w && !h) || (!w && h))
591 return BAD_VALUE;
592
593 Mutex::Autolock lock(mMutex);
594 mReqWidth = w;
595 mReqHeight = h;
596 return NO_ERROR;
597}
598
599int Surface::setBuffersUserDimensions(int w, int h)
600{
601 ATRACE_CALL();
602 ALOGV("Surface::setBuffersUserDimensions");
603
604 if (w<0 || h<0)
605 return BAD_VALUE;
606
607 if ((w && !h) || (!w && h))
608 return BAD_VALUE;
609
610 Mutex::Autolock lock(mMutex);
611 mUserWidth = w;
612 mUserHeight = h;
613 return NO_ERROR;
614}
615
616int Surface::setBuffersFormat(int format)
617{
618 ALOGV("Surface::setBuffersFormat");
619
620 if (format<0)
621 return BAD_VALUE;
622
623 Mutex::Autolock lock(mMutex);
624 mReqFormat = format;
625 return NO_ERROR;
626}
627
628int Surface::setScalingMode(int mode)
629{
630 ATRACE_CALL();
631 ALOGV("Surface::setScalingMode(%d)", mode);
632
633 switch (mode) {
634 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
635 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
636 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
637 break;
638 default:
639 ALOGE("unknown scaling mode: %d", mode);
640 return BAD_VALUE;
641 }
642
643 Mutex::Autolock lock(mMutex);
644 mScalingMode = mode;
645 return NO_ERROR;
646}
647
648int Surface::setBuffersTransform(int transform)
649{
650 ATRACE_CALL();
651 ALOGV("Surface::setBuffersTransform");
652 Mutex::Autolock lock(mMutex);
653 mTransform = transform;
654 return NO_ERROR;
655}
656
657int Surface::setBuffersTimestamp(int64_t timestamp)
658{
659 ALOGV("Surface::setBuffersTimestamp");
660 Mutex::Autolock lock(mMutex);
661 mTimestamp = timestamp;
662 return NO_ERROR;
663}
664
665void Surface::freeAllBuffers() {
666 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
667 mSlots[i].buffer = 0;
668 }
669}
670
671// ----------------------------------------------------------------------
672// the lock/unlock APIs must be used from the same thread
673
674static status_t copyBlt(
675 const sp<GraphicBuffer>& dst,
676 const sp<GraphicBuffer>& src,
677 const Region& reg)
678{
679 // src and dst with, height and format must be identical. no verification
680 // is done here.
681 status_t err;
682 uint8_t const * src_bits = NULL;
683 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
684 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
685
686 uint8_t* dst_bits = NULL;
687 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
688 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
689
690 Region::const_iterator head(reg.begin());
691 Region::const_iterator tail(reg.end());
692 if (head != tail && src_bits && dst_bits) {
693 const size_t bpp = bytesPerPixel(src->format);
694 const size_t dbpr = dst->stride * bpp;
695 const size_t sbpr = src->stride * bpp;
696
697 while (head != tail) {
698 const Rect& r(*head++);
699 ssize_t h = r.height();
700 if (h <= 0) continue;
701 size_t size = r.width() * bpp;
702 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
703 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
704 if (dbpr==sbpr && size==sbpr) {
705 size *= h;
706 h = 1;
707 }
708 do {
709 memcpy(d, s, size);
710 d += dbpr;
711 s += sbpr;
712 } while (--h > 0);
713 }
714 }
715
716 if (src_bits)
717 src->unlock();
718
719 if (dst_bits)
720 dst->unlock();
721
722 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723}
724
Mathias Agopiana138f892010-05-21 17:24:35 -0700725// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800726
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800727status_t Surface::lock(
728 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
729{
730 if (mLockedBuffer != 0) {
731 ALOGE("Surface::lock failed, already locked");
732 return INVALID_OPERATION;
733 }
734
735 if (!mConnectedToCpu) {
736 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
737 if (err) {
738 return err;
739 }
740 // we're intending to do software rendering from this point
741 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
742 }
743
744 ANativeWindowBuffer* out;
745 int fenceFd = -1;
746 status_t err = dequeueBuffer(&out, &fenceFd);
747 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
748 if (err == NO_ERROR) {
749 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
750 sp<Fence> fence(new Fence(fenceFd));
751
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700752 err = fence->waitForever("Surface::lock");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800753 if (err != OK) {
754 ALOGE("Fence::wait failed (%s)", strerror(-err));
755 cancelBuffer(out, fenceFd);
756 return err;
757 }
758
759 const Rect bounds(backBuffer->width, backBuffer->height);
760
761 Region newDirtyRegion;
762 if (inOutDirtyBounds) {
763 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
764 newDirtyRegion.andSelf(bounds);
765 } else {
766 newDirtyRegion.set(bounds);
767 }
768
769 // figure out if we can copy the frontbuffer back
770 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
771 const bool canCopyBack = (frontBuffer != 0 &&
772 backBuffer->width == frontBuffer->width &&
773 backBuffer->height == frontBuffer->height &&
774 backBuffer->format == frontBuffer->format);
775
776 if (canCopyBack) {
777 // copy the area that is invalid and not repainted this round
778 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
779 if (!copyback.isEmpty())
780 copyBlt(backBuffer, frontBuffer, copyback);
781 } else {
782 // if we can't copy-back anything, modify the user's dirty
783 // region to make sure they redraw the whole buffer
784 newDirtyRegion.set(bounds);
785 mDirtyRegion.clear();
786 Mutex::Autolock lock(mMutex);
787 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
788 mSlots[i].dirtyRegion.clear();
789 }
790 }
791
792
793 { // scope for the lock
794 Mutex::Autolock lock(mMutex);
795 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
796 if (backBufferSlot >= 0) {
797 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
798 mDirtyRegion.subtract(dirtyRegion);
799 dirtyRegion = newDirtyRegion;
800 }
801 }
802
803 mDirtyRegion.orSelf(newDirtyRegion);
804 if (inOutDirtyBounds) {
805 *inOutDirtyBounds = newDirtyRegion.getBounds();
806 }
807
808 void* vaddr;
809 status_t res = backBuffer->lock(
810 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
811 newDirtyRegion.bounds(), &vaddr);
812
813 ALOGW_IF(res, "failed locking buffer (handle = %p)",
814 backBuffer->handle);
815
816 if (res != 0) {
817 err = INVALID_OPERATION;
818 } else {
819 mLockedBuffer = backBuffer;
820 outBuffer->width = backBuffer->width;
821 outBuffer->height = backBuffer->height;
822 outBuffer->stride = backBuffer->stride;
823 outBuffer->format = backBuffer->format;
824 outBuffer->bits = vaddr;
825 }
826 }
827 return err;
828}
829
830status_t Surface::unlockAndPost()
831{
832 if (mLockedBuffer == 0) {
833 ALOGE("Surface::unlockAndPost failed, no locked buffer");
834 return INVALID_OPERATION;
835 }
836
837 status_t err = mLockedBuffer->unlock();
838 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
839
840 err = queueBuffer(mLockedBuffer.get(), -1);
841 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
842 mLockedBuffer->handle, strerror(-err));
843
844 mPostedBuffer = mLockedBuffer;
845 mLockedBuffer = 0;
846 return err;
847}
848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800849}; // namespace android