blob: 88c45b2d1e05e61ade46ce01f43b8b94d09bf611 [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
Dan Stozaf0eaf252014-03-21 13:05:51 -070030#include <gui/IProducerListener.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080031#include <gui/ISurfaceComposer.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080032#include <gui/SurfaceComposerClient.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080033#include <gui/GLConsumer.h>
34#include <gui/Surface.h>
35
36#include <private/gui/ComposerService.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
Mathias Agopiane3c697f2013-02-14 17:11:02 -080040Surface::Surface(
Mathias Agopian595264f2013-07-16 22:56:09 -070041 const sp<IGraphicBufferProducer>& bufferProducer,
42 bool controlledByApp)
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070043 : mGraphicBufferProducer(bufferProducer)
Mathias Agopian62185b72009-04-16 16:19:50 -070044{
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045 // Initialize the ANativeWindow function pointers.
46 ANativeWindow::setSwapInterval = hook_setSwapInterval;
47 ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
48 ANativeWindow::cancelBuffer = hook_cancelBuffer;
49 ANativeWindow::queueBuffer = hook_queueBuffer;
50 ANativeWindow::query = hook_query;
51 ANativeWindow::perform = hook_perform;
52
53 ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
54 ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
55 ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
56 ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
57
58 const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
59 const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
60
61 mReqWidth = 0;
62 mReqHeight = 0;
63 mReqFormat = 0;
64 mReqUsage = 0;
65 mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
66 mCrop.clear();
67 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
68 mTransform = 0;
69 mDefaultWidth = 0;
70 mDefaultHeight = 0;
71 mUserWidth = 0;
72 mUserHeight = 0;
73 mTransformHint = 0;
74 mConsumerRunningBehind = false;
75 mConnectedToCpu = false;
Eino-Ville Talvala7895e902013-08-21 11:53:37 -070076 mProducerControlledByApp = controlledByApp;
Mathias Agopian7cdd7862013-07-18 22:10:56 -070077 mSwapIntervalZero = false;
Mathias Agopian62185b72009-04-16 16:19:50 -070078}
79
Mathias Agopian35ffa6a2013-03-12 18:45:09 -070080Surface::~Surface() {
81 if (mConnectedToCpu) {
82 Surface::disconnect(NATIVE_WINDOW_API_CPU);
83 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -080084}
85
86sp<IGraphicBufferProducer> Surface::getIGraphicBufferProducer() const {
87 return mGraphicBufferProducer;
88}
89
Wonsik Kim0ee14ca2014-03-17 17:46:53 +090090void Surface::setSidebandStream(const sp<NativeHandle>& stream) {
91 mGraphicBufferProducer->setSidebandStream(stream);
92}
93
Mathias Agopiane3c697f2013-02-14 17:11:02 -080094int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
95 Surface* c = getSelf(window);
96 return c->setSwapInterval(interval);
97}
98
99int Surface::hook_dequeueBuffer(ANativeWindow* window,
100 ANativeWindowBuffer** buffer, int* fenceFd) {
101 Surface* c = getSelf(window);
102 return c->dequeueBuffer(buffer, fenceFd);
103}
104
105int Surface::hook_cancelBuffer(ANativeWindow* window,
106 ANativeWindowBuffer* buffer, int fenceFd) {
107 Surface* c = getSelf(window);
108 return c->cancelBuffer(buffer, fenceFd);
109}
110
111int Surface::hook_queueBuffer(ANativeWindow* window,
112 ANativeWindowBuffer* buffer, int fenceFd) {
113 Surface* c = getSelf(window);
114 return c->queueBuffer(buffer, fenceFd);
115}
116
117int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
118 ANativeWindowBuffer** buffer) {
119 Surface* c = getSelf(window);
120 ANativeWindowBuffer* buf;
121 int fenceFd = -1;
122 int result = c->dequeueBuffer(&buf, &fenceFd);
123 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700124 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800125 if (waitResult != OK) {
126 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
127 waitResult);
128 c->cancelBuffer(buf, -1);
129 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700130 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800131 *buffer = buf;
132 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700133}
134
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800135int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
136 ANativeWindowBuffer* buffer) {
137 Surface* c = getSelf(window);
138 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700139}
140
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800141int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
142 ANativeWindowBuffer* buffer) {
143 Surface* c = getSelf(window);
144 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700145}
146
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800147int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
148 ANativeWindowBuffer* buffer) {
149 Surface* c = getSelf(window);
150 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700151}
Mathias Agopian62185b72009-04-16 16:19:50 -0700152
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800153int Surface::hook_query(const ANativeWindow* window,
154 int what, int* value) {
155 const Surface* c = getSelf(window);
156 return c->query(what, value);
157}
158
159int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
160 va_list args;
161 va_start(args, operation);
162 Surface* c = getSelf(window);
163 return c->perform(operation, args);
164}
165
166int Surface::setSwapInterval(int interval) {
167 ATRACE_CALL();
168 // EGL specification states:
169 // interval is silently clamped to minimum and maximum implementation
170 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800171
172 if (interval < minSwapInterval)
173 interval = minSwapInterval;
174
175 if (interval > maxSwapInterval)
176 interval = maxSwapInterval;
177
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700178 mSwapIntervalZero = (interval == 0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800179
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700180 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800181}
182
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700183int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800184 ATRACE_CALL();
185 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800186
187 int reqW;
188 int reqH;
189 bool swapIntervalZero;
190 uint32_t reqFormat;
191 uint32_t reqUsage;
192
193 {
194 Mutex::Autolock lock(mMutex);
195
196 reqW = mReqWidth ? mReqWidth : mUserWidth;
197 reqH = mReqHeight ? mReqHeight : mUserHeight;
198
199 swapIntervalZero = mSwapIntervalZero;
200 reqFormat = mReqFormat;
201 reqUsage = mReqUsage;
202 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
203
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800204 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800205 sp<Fence> fence;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800206 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
207 reqW, reqH, reqFormat, reqUsage);
208
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800209 if (result < 0) {
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800210 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
211 "failed: %d", swapIntervalZero, reqW, reqH, reqFormat, reqUsage,
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800212 result);
213 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700214 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800215
216 Mutex::Autolock lock(mMutex);
217
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800218 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700219
220 // this should never happen
221 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
222
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800223 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
224 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700225 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700226
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800227 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
228 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
229 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700230 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800231 return result;
232 }
233 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700234
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800235 if (fence->isValid()) {
236 *fenceFd = fence->dup();
237 if (*fenceFd == -1) {
238 ALOGE("dequeueBuffer: error duping fence: %d", errno);
239 // dup() should never fail; something is badly wrong. Soldier on
240 // and hope for the best; the worst that should happen is some
241 // visible corruption that lasts until the next frame.
242 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700243 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800244 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700245 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800246
247 *buffer = gbuf.get();
248 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700249}
250
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800251int Surface::cancelBuffer(android_native_buffer_t* buffer,
252 int fenceFd) {
253 ATRACE_CALL();
254 ALOGV("Surface::cancelBuffer");
255 Mutex::Autolock lock(mMutex);
256 int i = getSlotFromBufferLocked(buffer);
257 if (i < 0) {
258 return i;
259 }
260 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
261 mGraphicBufferProducer->cancelBuffer(i, fence);
262 return OK;
263}
264
265int Surface::getSlotFromBufferLocked(
266 android_native_buffer_t* buffer) const {
267 bool dumpedState = false;
268 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
269 if (mSlots[i].buffer != NULL &&
270 mSlots[i].buffer->handle == buffer->handle) {
271 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700272 }
273 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800274 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
275 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700276}
277
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800278int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800279 ALOGV("Surface::lockBuffer");
280 Mutex::Autolock lock(mMutex);
281 return OK;
282}
Mathias Agopian631f3582010-05-25 17:51:34 -0700283
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800284int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
285 ATRACE_CALL();
286 ALOGV("Surface::queueBuffer");
287 Mutex::Autolock lock(mMutex);
288 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700289 bool isAutoTimestamp = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800290 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700291 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700292 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700293 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
294 timestamp / 1000000.f);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800295 } else {
296 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700297 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800298 int i = getSlotFromBufferLocked(buffer);
299 if (i < 0) {
300 return i;
301 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800304 // Make sure the crop rectangle is entirely inside the buffer.
305 Rect crop;
306 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800308 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
309 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700310 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
311 crop, mScalingMode, mTransform, mSwapIntervalZero, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800312 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
313 if (err != OK) {
314 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
315 }
316 uint32_t numPendingBuffers = 0;
317 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
318 &numPendingBuffers);
tedbo1e7fa9e2011-06-22 15:52:53 -0700319
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800320 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700321
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800322 return err;
323}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324
Mathias Agopiana67932f2011-04-20 14:20:59 -0700325int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800326 ATRACE_CALL();
327 ALOGV("Surface::query");
328 { // scope for the lock
329 Mutex::Autolock lock(mMutex);
330 switch (what) {
331 case NATIVE_WINDOW_FORMAT:
332 if (mReqFormat) {
333 *value = mReqFormat;
334 return NO_ERROR;
335 }
336 break;
337 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
338 sp<ISurfaceComposer> composer(
339 ComposerService::getComposerService());
340 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
341 *value = 1;
342 } else {
343 *value = 0;
344 }
345 return NO_ERROR;
346 }
347 case NATIVE_WINDOW_CONCRETE_TYPE:
348 *value = NATIVE_WINDOW_SURFACE;
349 return NO_ERROR;
350 case NATIVE_WINDOW_DEFAULT_WIDTH:
351 *value = mUserWidth ? mUserWidth : mDefaultWidth;
352 return NO_ERROR;
353 case NATIVE_WINDOW_DEFAULT_HEIGHT:
354 *value = mUserHeight ? mUserHeight : mDefaultHeight;
355 return NO_ERROR;
356 case NATIVE_WINDOW_TRANSFORM_HINT:
357 *value = mTransformHint;
358 return NO_ERROR;
359 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
360 status_t err = NO_ERROR;
361 if (!mConsumerRunningBehind) {
362 *value = 0;
363 } else {
364 err = mGraphicBufferProducer->query(what, value);
365 if (err == NO_ERROR) {
366 mConsumerRunningBehind = *value;
367 }
368 }
369 return err;
370 }
371 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700372 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800373 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800374}
375
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800376int Surface::perform(int operation, va_list args)
377{
378 int res = NO_ERROR;
379 switch (operation) {
380 case NATIVE_WINDOW_CONNECT:
381 // deprecated. must return NO_ERROR.
382 break;
383 case NATIVE_WINDOW_DISCONNECT:
384 // deprecated. must return NO_ERROR.
385 break;
386 case NATIVE_WINDOW_SET_USAGE:
387 res = dispatchSetUsage(args);
388 break;
389 case NATIVE_WINDOW_SET_CROP:
390 res = dispatchSetCrop(args);
391 break;
392 case NATIVE_WINDOW_SET_BUFFER_COUNT:
393 res = dispatchSetBufferCount(args);
394 break;
395 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
396 res = dispatchSetBuffersGeometry(args);
397 break;
398 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
399 res = dispatchSetBuffersTransform(args);
400 break;
401 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
402 res = dispatchSetBuffersTimestamp(args);
403 break;
404 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
405 res = dispatchSetBuffersDimensions(args);
406 break;
407 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
408 res = dispatchSetBuffersUserDimensions(args);
409 break;
410 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
411 res = dispatchSetBuffersFormat(args);
412 break;
413 case NATIVE_WINDOW_LOCK:
414 res = dispatchLock(args);
415 break;
416 case NATIVE_WINDOW_UNLOCK_AND_POST:
417 res = dispatchUnlockAndPost(args);
418 break;
419 case NATIVE_WINDOW_SET_SCALING_MODE:
420 res = dispatchSetScalingMode(args);
421 break;
422 case NATIVE_WINDOW_API_CONNECT:
423 res = dispatchConnect(args);
424 break;
425 case NATIVE_WINDOW_API_DISCONNECT:
426 res = dispatchDisconnect(args);
427 break;
428 default:
429 res = NAME_NOT_FOUND;
430 break;
431 }
432 return res;
433}
Mathias Agopiana138f892010-05-21 17:24:35 -0700434
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800435int Surface::dispatchConnect(va_list args) {
436 int api = va_arg(args, int);
437 return connect(api);
438}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800439
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800440int Surface::dispatchDisconnect(va_list args) {
441 int api = va_arg(args, int);
442 return disconnect(api);
443}
444
445int Surface::dispatchSetUsage(va_list args) {
446 int usage = va_arg(args, int);
447 return setUsage(usage);
448}
449
450int Surface::dispatchSetCrop(va_list args) {
451 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
452 return setCrop(reinterpret_cast<Rect const*>(rect));
453}
454
455int Surface::dispatchSetBufferCount(va_list args) {
456 size_t bufferCount = va_arg(args, size_t);
457 return setBufferCount(bufferCount);
458}
459
460int Surface::dispatchSetBuffersGeometry(va_list args) {
461 int w = va_arg(args, int);
462 int h = va_arg(args, int);
463 int f = va_arg(args, int);
464 int err = setBuffersDimensions(w, h);
465 if (err != 0) {
466 return err;
467 }
468 return setBuffersFormat(f);
469}
470
471int Surface::dispatchSetBuffersDimensions(va_list args) {
472 int w = va_arg(args, int);
473 int h = va_arg(args, int);
474 return setBuffersDimensions(w, h);
475}
476
477int Surface::dispatchSetBuffersUserDimensions(va_list args) {
478 int w = va_arg(args, int);
479 int h = va_arg(args, int);
480 return setBuffersUserDimensions(w, h);
481}
482
483int Surface::dispatchSetBuffersFormat(va_list args) {
484 int f = va_arg(args, int);
485 return setBuffersFormat(f);
486}
487
488int Surface::dispatchSetScalingMode(va_list args) {
489 int m = va_arg(args, int);
490 return setScalingMode(m);
491}
492
493int Surface::dispatchSetBuffersTransform(va_list args) {
494 int transform = va_arg(args, int);
495 return setBuffersTransform(transform);
496}
497
498int Surface::dispatchSetBuffersTimestamp(va_list args) {
499 int64_t timestamp = va_arg(args, int64_t);
500 return setBuffersTimestamp(timestamp);
501}
502
503int Surface::dispatchLock(va_list args) {
504 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
505 ARect* inOutDirtyBounds = va_arg(args, ARect*);
506 return lock(outBuffer, inOutDirtyBounds);
507}
508
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800509int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800510 return unlockAndPost();
511}
512
513
514int Surface::connect(int api) {
515 ATRACE_CALL();
516 ALOGV("Surface::connect");
Dan Stozaf0eaf252014-03-21 13:05:51 -0700517 static sp<IProducerListener> listener = new DummyProducerListener();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800518 Mutex::Autolock lock(mMutex);
519 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700520 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800521 if (err == NO_ERROR) {
522 uint32_t numPendingBuffers = 0;
523 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
524 &numPendingBuffers);
525 mConsumerRunningBehind = (numPendingBuffers >= 2);
526 }
527 if (!err && api == NATIVE_WINDOW_API_CPU) {
528 mConnectedToCpu = true;
529 }
530 return err;
531}
532
Mathias Agopian365857d2013-09-11 19:35:45 -0700533
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800534int Surface::disconnect(int api) {
535 ATRACE_CALL();
536 ALOGV("Surface::disconnect");
537 Mutex::Autolock lock(mMutex);
538 freeAllBuffers();
539 int err = mGraphicBufferProducer->disconnect(api);
540 if (!err) {
541 mReqFormat = 0;
542 mReqWidth = 0;
543 mReqHeight = 0;
544 mReqUsage = 0;
545 mCrop.clear();
546 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
547 mTransform = 0;
548 if (api == NATIVE_WINDOW_API_CPU) {
549 mConnectedToCpu = false;
550 }
551 }
552 return err;
553}
554
555int Surface::setUsage(uint32_t reqUsage)
556{
557 ALOGV("Surface::setUsage");
558 Mutex::Autolock lock(mMutex);
559 mReqUsage = reqUsage;
560 return OK;
561}
562
563int Surface::setCrop(Rect const* rect)
564{
565 ATRACE_CALL();
566
567 Rect realRect;
568 if (rect == NULL || rect->isEmpty()) {
569 realRect.clear();
570 } else {
571 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800572 }
573
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800574 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
575 realRect.left, realRect.top, realRect.right, realRect.bottom);
576
577 Mutex::Autolock lock(mMutex);
578 mCrop = realRect;
579 return NO_ERROR;
580}
581
582int Surface::setBufferCount(int bufferCount)
583{
584 ATRACE_CALL();
585 ALOGV("Surface::setBufferCount");
586 Mutex::Autolock lock(mMutex);
587
588 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
589 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
590 bufferCount, strerror(-err));
Mathias Agopian90147262010-01-22 11:47:55 -0800591
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700592 if (err == NO_ERROR) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800593 freeAllBuffers();
Mathias Agopian87a96ea2011-08-23 21:09:41 -0700594 }
595
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700596 return err;
597}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700598
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800599int Surface::setBuffersDimensions(int w, int h)
600{
601 ATRACE_CALL();
602 ALOGV("Surface::setBuffersDimensions");
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 mReqWidth = w;
612 mReqHeight = h;
613 return NO_ERROR;
614}
615
616int Surface::setBuffersUserDimensions(int w, int h)
617{
618 ATRACE_CALL();
619 ALOGV("Surface::setBuffersUserDimensions");
620
621 if (w<0 || h<0)
622 return BAD_VALUE;
623
624 if ((w && !h) || (!w && h))
625 return BAD_VALUE;
626
627 Mutex::Autolock lock(mMutex);
628 mUserWidth = w;
629 mUserHeight = h;
630 return NO_ERROR;
631}
632
633int Surface::setBuffersFormat(int format)
634{
635 ALOGV("Surface::setBuffersFormat");
636
637 if (format<0)
638 return BAD_VALUE;
639
640 Mutex::Autolock lock(mMutex);
641 mReqFormat = format;
642 return NO_ERROR;
643}
644
645int Surface::setScalingMode(int mode)
646{
647 ATRACE_CALL();
648 ALOGV("Surface::setScalingMode(%d)", mode);
649
650 switch (mode) {
651 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
652 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
653 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
654 break;
655 default:
656 ALOGE("unknown scaling mode: %d", mode);
657 return BAD_VALUE;
658 }
659
660 Mutex::Autolock lock(mMutex);
661 mScalingMode = mode;
662 return NO_ERROR;
663}
664
665int Surface::setBuffersTransform(int transform)
666{
667 ATRACE_CALL();
668 ALOGV("Surface::setBuffersTransform");
669 Mutex::Autolock lock(mMutex);
670 mTransform = transform;
671 return NO_ERROR;
672}
673
674int Surface::setBuffersTimestamp(int64_t timestamp)
675{
676 ALOGV("Surface::setBuffersTimestamp");
677 Mutex::Autolock lock(mMutex);
678 mTimestamp = timestamp;
679 return NO_ERROR;
680}
681
682void Surface::freeAllBuffers() {
683 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
684 mSlots[i].buffer = 0;
685 }
686}
687
688// ----------------------------------------------------------------------
689// the lock/unlock APIs must be used from the same thread
690
691static status_t copyBlt(
692 const sp<GraphicBuffer>& dst,
693 const sp<GraphicBuffer>& src,
694 const Region& reg)
695{
696 // src and dst with, height and format must be identical. no verification
697 // is done here.
698 status_t err;
699 uint8_t const * src_bits = NULL;
700 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
701 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
702
703 uint8_t* dst_bits = NULL;
704 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
705 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
706
707 Region::const_iterator head(reg.begin());
708 Region::const_iterator tail(reg.end());
709 if (head != tail && src_bits && dst_bits) {
710 const size_t bpp = bytesPerPixel(src->format);
711 const size_t dbpr = dst->stride * bpp;
712 const size_t sbpr = src->stride * bpp;
713
714 while (head != tail) {
715 const Rect& r(*head++);
716 ssize_t h = r.height();
717 if (h <= 0) continue;
718 size_t size = r.width() * bpp;
719 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
720 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
721 if (dbpr==sbpr && size==sbpr) {
722 size *= h;
723 h = 1;
724 }
725 do {
726 memcpy(d, s, size);
727 d += dbpr;
728 s += sbpr;
729 } while (--h > 0);
730 }
731 }
732
733 if (src_bits)
734 src->unlock();
735
736 if (dst_bits)
737 dst->unlock();
738
739 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
Mathias Agopiana138f892010-05-21 17:24:35 -0700742// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800743
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800744status_t Surface::lock(
745 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
746{
747 if (mLockedBuffer != 0) {
748 ALOGE("Surface::lock failed, already locked");
749 return INVALID_OPERATION;
750 }
751
752 if (!mConnectedToCpu) {
753 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
754 if (err) {
755 return err;
756 }
757 // we're intending to do software rendering from this point
758 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
759 }
760
761 ANativeWindowBuffer* out;
762 int fenceFd = -1;
763 status_t err = dequeueBuffer(&out, &fenceFd);
764 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
765 if (err == NO_ERROR) {
766 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800767 const Rect bounds(backBuffer->width, backBuffer->height);
768
769 Region newDirtyRegion;
770 if (inOutDirtyBounds) {
771 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
772 newDirtyRegion.andSelf(bounds);
773 } else {
774 newDirtyRegion.set(bounds);
775 }
776
777 // figure out if we can copy the frontbuffer back
778 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
779 const bool canCopyBack = (frontBuffer != 0 &&
780 backBuffer->width == frontBuffer->width &&
781 backBuffer->height == frontBuffer->height &&
782 backBuffer->format == frontBuffer->format);
783
784 if (canCopyBack) {
785 // copy the area that is invalid and not repainted this round
786 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
787 if (!copyback.isEmpty())
788 copyBlt(backBuffer, frontBuffer, copyback);
789 } else {
790 // if we can't copy-back anything, modify the user's dirty
791 // region to make sure they redraw the whole buffer
792 newDirtyRegion.set(bounds);
793 mDirtyRegion.clear();
794 Mutex::Autolock lock(mMutex);
795 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
796 mSlots[i].dirtyRegion.clear();
797 }
798 }
799
800
801 { // scope for the lock
802 Mutex::Autolock lock(mMutex);
803 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
804 if (backBufferSlot >= 0) {
805 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
806 mDirtyRegion.subtract(dirtyRegion);
807 dirtyRegion = newDirtyRegion;
808 }
809 }
810
811 mDirtyRegion.orSelf(newDirtyRegion);
812 if (inOutDirtyBounds) {
813 *inOutDirtyBounds = newDirtyRegion.getBounds();
814 }
815
816 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +0300817 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800818 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +0300819 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800820
821 ALOGW_IF(res, "failed locking buffer (handle = %p)",
822 backBuffer->handle);
823
824 if (res != 0) {
825 err = INVALID_OPERATION;
826 } else {
827 mLockedBuffer = backBuffer;
828 outBuffer->width = backBuffer->width;
829 outBuffer->height = backBuffer->height;
830 outBuffer->stride = backBuffer->stride;
831 outBuffer->format = backBuffer->format;
832 outBuffer->bits = vaddr;
833 }
834 }
835 return err;
836}
837
838status_t Surface::unlockAndPost()
839{
840 if (mLockedBuffer == 0) {
841 ALOGE("Surface::unlockAndPost failed, no locked buffer");
842 return INVALID_OPERATION;
843 }
844
Francis Hart8f396012014-04-01 15:30:53 +0300845 int fd = -1;
846 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800847 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
848
Francis Hart8f396012014-04-01 15:30:53 +0300849 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800850 ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
851 mLockedBuffer->handle, strerror(-err));
852
853 mPostedBuffer = mLockedBuffer;
854 mLockedBuffer = 0;
855 return err;
856}
857
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858}; // namespace android