blob: 8cb9189f103d8225cc7669994c5b138d3c70ac29 [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
Dan Stoza29a3e902014-06-20 13:13:57 -070094void Surface::allocateBuffers() {
95 uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
96 uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
97 mGraphicBufferProducer->allocateBuffers(mSwapIntervalZero, mReqWidth,
98 mReqHeight, mReqFormat, mReqUsage);
99}
100
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800101int Surface::hook_setSwapInterval(ANativeWindow* window, int interval) {
102 Surface* c = getSelf(window);
103 return c->setSwapInterval(interval);
104}
105
106int Surface::hook_dequeueBuffer(ANativeWindow* window,
107 ANativeWindowBuffer** buffer, int* fenceFd) {
108 Surface* c = getSelf(window);
109 return c->dequeueBuffer(buffer, fenceFd);
110}
111
112int Surface::hook_cancelBuffer(ANativeWindow* window,
113 ANativeWindowBuffer* buffer, int fenceFd) {
114 Surface* c = getSelf(window);
115 return c->cancelBuffer(buffer, fenceFd);
116}
117
118int Surface::hook_queueBuffer(ANativeWindow* window,
119 ANativeWindowBuffer* buffer, int fenceFd) {
120 Surface* c = getSelf(window);
121 return c->queueBuffer(buffer, fenceFd);
122}
123
124int Surface::hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
125 ANativeWindowBuffer** buffer) {
126 Surface* c = getSelf(window);
127 ANativeWindowBuffer* buf;
128 int fenceFd = -1;
129 int result = c->dequeueBuffer(&buf, &fenceFd);
130 sp<Fence> fence(new Fence(fenceFd));
Mathias Agopianea74d3b2013-05-16 18:03:22 -0700131 int waitResult = fence->waitForever("dequeueBuffer_DEPRECATED");
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800132 if (waitResult != OK) {
133 ALOGE("dequeueBuffer_DEPRECATED: Fence::wait returned an error: %d",
134 waitResult);
135 c->cancelBuffer(buf, -1);
136 return waitResult;
Mathias Agopian62185b72009-04-16 16:19:50 -0700137 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800138 *buffer = buf;
139 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700140}
141
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800142int Surface::hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
143 ANativeWindowBuffer* buffer) {
144 Surface* c = getSelf(window);
145 return c->cancelBuffer(buffer, -1);
Mathias Agopian62185b72009-04-16 16:19:50 -0700146}
147
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800148int Surface::hook_lockBuffer_DEPRECATED(ANativeWindow* window,
149 ANativeWindowBuffer* buffer) {
150 Surface* c = getSelf(window);
151 return c->lockBuffer_DEPRECATED(buffer);
Mathias Agopian62185b72009-04-16 16:19:50 -0700152}
153
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800154int Surface::hook_queueBuffer_DEPRECATED(ANativeWindow* window,
155 ANativeWindowBuffer* buffer) {
156 Surface* c = getSelf(window);
157 return c->queueBuffer(buffer, -1);
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700158}
Mathias Agopian62185b72009-04-16 16:19:50 -0700159
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800160int Surface::hook_query(const ANativeWindow* window,
161 int what, int* value) {
162 const Surface* c = getSelf(window);
163 return c->query(what, value);
164}
165
166int Surface::hook_perform(ANativeWindow* window, int operation, ...) {
167 va_list args;
168 va_start(args, operation);
169 Surface* c = getSelf(window);
170 return c->perform(operation, args);
171}
172
173int Surface::setSwapInterval(int interval) {
174 ATRACE_CALL();
175 // EGL specification states:
176 // interval is silently clamped to minimum and maximum implementation
177 // dependent values before being stored.
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800178
179 if (interval < minSwapInterval)
180 interval = minSwapInterval;
181
182 if (interval > maxSwapInterval)
183 interval = maxSwapInterval;
184
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700185 mSwapIntervalZero = (interval == 0);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800186
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700187 return NO_ERROR;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800188}
189
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700190int Surface::dequeueBuffer(android_native_buffer_t** buffer, int* fenceFd) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800191 ATRACE_CALL();
192 ALOGV("Surface::dequeueBuffer");
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800193
194 int reqW;
195 int reqH;
196 bool swapIntervalZero;
197 uint32_t reqFormat;
198 uint32_t reqUsage;
199
200 {
201 Mutex::Autolock lock(mMutex);
202
203 reqW = mReqWidth ? mReqWidth : mUserWidth;
204 reqH = mReqHeight ? mReqHeight : mUserHeight;
205
206 swapIntervalZero = mSwapIntervalZero;
207 reqFormat = mReqFormat;
208 reqUsage = mReqUsage;
209 } // Drop the lock so that we can still touch the Surface while blocking in IGBP::dequeueBuffer
210
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800211 int buf = -1;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800212 sp<Fence> fence;
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800213 status_t result = mGraphicBufferProducer->dequeueBuffer(&buf, &fence, swapIntervalZero,
214 reqW, reqH, reqFormat, reqUsage);
215
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800216 if (result < 0) {
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800217 ALOGV("dequeueBuffer: IGraphicBufferProducer::dequeueBuffer(%d, %d, %d, %d, %d)"
218 "failed: %d", swapIntervalZero, reqW, reqH, reqFormat, reqUsage,
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800219 result);
220 return result;
Mathias Agopian62185b72009-04-16 16:19:50 -0700221 }
Igor Murashkin0f1889e2014-02-28 18:44:21 -0800222
223 Mutex::Autolock lock(mMutex);
224
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800225 sp<GraphicBuffer>& gbuf(mSlots[buf].buffer);
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700226
227 // this should never happen
228 ALOGE_IF(fence == NULL, "Surface::dequeueBuffer: received null Fence! buf=%d", buf);
229
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800230 if (result & IGraphicBufferProducer::RELEASE_ALL_BUFFERS) {
231 freeAllBuffers();
Mathias Agopian579b3f82010-06-08 19:54:15 -0700232 }
Ted Bonkenburgbd050ab2011-07-15 15:10:10 -0700233
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800234 if ((result & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
235 result = mGraphicBufferProducer->requestBuffer(buf, &gbuf);
236 if (result != NO_ERROR) {
Mathias Agopianba93b3f2013-08-01 15:48:40 -0700237 ALOGE("dequeueBuffer: IGraphicBufferProducer::requestBuffer failed: %d", result);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800238 return result;
239 }
240 }
Mathias Agopian579b3f82010-06-08 19:54:15 -0700241
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800242 if (fence->isValid()) {
243 *fenceFd = fence->dup();
244 if (*fenceFd == -1) {
245 ALOGE("dequeueBuffer: error duping fence: %d", errno);
246 // dup() should never fail; something is badly wrong. Soldier on
247 // and hope for the best; the worst that should happen is some
248 // visible corruption that lasts until the next frame.
249 }
Ted Bonkenburge5d6eb82011-08-09 22:38:41 -0700250 } else {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800251 *fenceFd = -1;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700252 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800253
254 *buffer = gbuf.get();
255 return OK;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700256}
257
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800258int Surface::cancelBuffer(android_native_buffer_t* buffer,
259 int fenceFd) {
260 ATRACE_CALL();
261 ALOGV("Surface::cancelBuffer");
262 Mutex::Autolock lock(mMutex);
263 int i = getSlotFromBufferLocked(buffer);
264 if (i < 0) {
265 return i;
266 }
267 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
268 mGraphicBufferProducer->cancelBuffer(i, fence);
269 return OK;
270}
271
272int Surface::getSlotFromBufferLocked(
273 android_native_buffer_t* buffer) const {
274 bool dumpedState = false;
275 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
276 if (mSlots[i].buffer != NULL &&
277 mSlots[i].buffer->handle == buffer->handle) {
278 return i;
Jamie Gennisaca4e222010-07-15 17:29:15 -0700279 }
280 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800281 ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
282 return BAD_VALUE;
Mathias Agopiana0c30e92010-06-04 18:26:32 -0700283}
284
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800285int Surface::lockBuffer_DEPRECATED(android_native_buffer_t* buffer __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800286 ALOGV("Surface::lockBuffer");
287 Mutex::Autolock lock(mMutex);
288 return OK;
289}
Mathias Agopian631f3582010-05-25 17:51:34 -0700290
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800291int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
292 ATRACE_CALL();
293 ALOGV("Surface::queueBuffer");
294 Mutex::Autolock lock(mMutex);
295 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700296 bool isAutoTimestamp = false;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800297 if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
Andy McFadden4b49e082013-08-02 15:31:45 -0700298 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
Andy McFadden3c256212013-08-16 14:55:39 -0700299 isAutoTimestamp = true;
Andy McFadden4b49e082013-08-02 15:31:45 -0700300 ALOGV("Surface::queueBuffer making up timestamp: %.2f ms",
301 timestamp / 1000000.f);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800302 } else {
303 timestamp = mTimestamp;
Mathias Agopian631f3582010-05-25 17:51:34 -0700304 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800305 int i = getSlotFromBufferLocked(buffer);
306 if (i < 0) {
307 return i;
308 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800311 // Make sure the crop rectangle is entirely inside the buffer.
312 Rect crop;
313 mCrop.intersect(Rect(buffer->width, buffer->height), &crop);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800315 sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
316 IGraphicBufferProducer::QueueBufferOutput output;
Andy McFadden3c256212013-08-16 14:55:39 -0700317 IGraphicBufferProducer::QueueBufferInput input(timestamp, isAutoTimestamp,
318 crop, mScalingMode, mTransform, mSwapIntervalZero, fence);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800319 status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
320 if (err != OK) {
321 ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
322 }
323 uint32_t numPendingBuffers = 0;
324 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
325 &numPendingBuffers);
tedbo1e7fa9e2011-06-22 15:52:53 -0700326
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800327 mConsumerRunningBehind = (numPendingBuffers >= 2);
Mathias Agopian631f3582010-05-25 17:51:34 -0700328
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800329 return err;
330}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700331
Mathias Agopiana67932f2011-04-20 14:20:59 -0700332int Surface::query(int what, int* value) const {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800333 ATRACE_CALL();
334 ALOGV("Surface::query");
335 { // scope for the lock
336 Mutex::Autolock lock(mMutex);
337 switch (what) {
338 case NATIVE_WINDOW_FORMAT:
339 if (mReqFormat) {
340 *value = mReqFormat;
341 return NO_ERROR;
342 }
343 break;
344 case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER: {
345 sp<ISurfaceComposer> composer(
346 ComposerService::getComposerService());
347 if (composer->authenticateSurfaceTexture(mGraphicBufferProducer)) {
348 *value = 1;
349 } else {
350 *value = 0;
351 }
352 return NO_ERROR;
353 }
354 case NATIVE_WINDOW_CONCRETE_TYPE:
355 *value = NATIVE_WINDOW_SURFACE;
356 return NO_ERROR;
357 case NATIVE_WINDOW_DEFAULT_WIDTH:
358 *value = mUserWidth ? mUserWidth : mDefaultWidth;
359 return NO_ERROR;
360 case NATIVE_WINDOW_DEFAULT_HEIGHT:
361 *value = mUserHeight ? mUserHeight : mDefaultHeight;
362 return NO_ERROR;
363 case NATIVE_WINDOW_TRANSFORM_HINT:
364 *value = mTransformHint;
365 return NO_ERROR;
366 case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: {
367 status_t err = NO_ERROR;
368 if (!mConsumerRunningBehind) {
369 *value = 0;
370 } else {
371 err = mGraphicBufferProducer->query(what, value);
372 if (err == NO_ERROR) {
373 mConsumerRunningBehind = *value;
374 }
375 }
376 return err;
377 }
378 }
Jamie Gennis391bbe22011-03-14 15:00:06 -0700379 }
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800380 return mGraphicBufferProducer->query(what, value);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800381}
382
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800383int Surface::perform(int operation, va_list args)
384{
385 int res = NO_ERROR;
386 switch (operation) {
387 case NATIVE_WINDOW_CONNECT:
388 // deprecated. must return NO_ERROR.
389 break;
390 case NATIVE_WINDOW_DISCONNECT:
391 // deprecated. must return NO_ERROR.
392 break;
393 case NATIVE_WINDOW_SET_USAGE:
394 res = dispatchSetUsage(args);
395 break;
396 case NATIVE_WINDOW_SET_CROP:
397 res = dispatchSetCrop(args);
398 break;
399 case NATIVE_WINDOW_SET_BUFFER_COUNT:
400 res = dispatchSetBufferCount(args);
401 break;
402 case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
403 res = dispatchSetBuffersGeometry(args);
404 break;
405 case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
406 res = dispatchSetBuffersTransform(args);
407 break;
408 case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
409 res = dispatchSetBuffersTimestamp(args);
410 break;
411 case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
412 res = dispatchSetBuffersDimensions(args);
413 break;
414 case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
415 res = dispatchSetBuffersUserDimensions(args);
416 break;
417 case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
418 res = dispatchSetBuffersFormat(args);
419 break;
420 case NATIVE_WINDOW_LOCK:
421 res = dispatchLock(args);
422 break;
423 case NATIVE_WINDOW_UNLOCK_AND_POST:
424 res = dispatchUnlockAndPost(args);
425 break;
426 case NATIVE_WINDOW_SET_SCALING_MODE:
427 res = dispatchSetScalingMode(args);
428 break;
429 case NATIVE_WINDOW_API_CONNECT:
430 res = dispatchConnect(args);
431 break;
432 case NATIVE_WINDOW_API_DISCONNECT:
433 res = dispatchDisconnect(args);
434 break;
435 default:
436 res = NAME_NOT_FOUND;
437 break;
438 }
439 return res;
440}
Mathias Agopiana138f892010-05-21 17:24:35 -0700441
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800442int Surface::dispatchConnect(va_list args) {
443 int api = va_arg(args, int);
444 return connect(api);
445}
Mathias Agopian55fa2512010-03-11 15:06:54 -0800446
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800447int Surface::dispatchDisconnect(va_list args) {
448 int api = va_arg(args, int);
449 return disconnect(api);
450}
451
452int Surface::dispatchSetUsage(va_list args) {
453 int usage = va_arg(args, int);
454 return setUsage(usage);
455}
456
457int Surface::dispatchSetCrop(va_list args) {
458 android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
459 return setCrop(reinterpret_cast<Rect const*>(rect));
460}
461
462int Surface::dispatchSetBufferCount(va_list args) {
463 size_t bufferCount = va_arg(args, size_t);
464 return setBufferCount(bufferCount);
465}
466
467int Surface::dispatchSetBuffersGeometry(va_list args) {
468 int w = va_arg(args, int);
469 int h = va_arg(args, int);
470 int f = va_arg(args, int);
471 int err = setBuffersDimensions(w, h);
472 if (err != 0) {
473 return err;
474 }
475 return setBuffersFormat(f);
476}
477
478int Surface::dispatchSetBuffersDimensions(va_list args) {
479 int w = va_arg(args, int);
480 int h = va_arg(args, int);
481 return setBuffersDimensions(w, h);
482}
483
484int Surface::dispatchSetBuffersUserDimensions(va_list args) {
485 int w = va_arg(args, int);
486 int h = va_arg(args, int);
487 return setBuffersUserDimensions(w, h);
488}
489
490int Surface::dispatchSetBuffersFormat(va_list args) {
491 int f = va_arg(args, int);
492 return setBuffersFormat(f);
493}
494
495int Surface::dispatchSetScalingMode(va_list args) {
496 int m = va_arg(args, int);
497 return setScalingMode(m);
498}
499
500int Surface::dispatchSetBuffersTransform(va_list args) {
501 int transform = va_arg(args, int);
502 return setBuffersTransform(transform);
503}
504
505int Surface::dispatchSetBuffersTimestamp(va_list args) {
506 int64_t timestamp = va_arg(args, int64_t);
507 return setBuffersTimestamp(timestamp);
508}
509
510int Surface::dispatchLock(va_list args) {
511 ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
512 ARect* inOutDirtyBounds = va_arg(args, ARect*);
513 return lock(outBuffer, inOutDirtyBounds);
514}
515
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800516int Surface::dispatchUnlockAndPost(va_list args __attribute__((unused))) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800517 return unlockAndPost();
518}
519
520
521int Surface::connect(int api) {
522 ATRACE_CALL();
523 ALOGV("Surface::connect");
Dan Stozaf0eaf252014-03-21 13:05:51 -0700524 static sp<IProducerListener> listener = new DummyProducerListener();
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800525 Mutex::Autolock lock(mMutex);
526 IGraphicBufferProducer::QueueBufferOutput output;
Dan Stozaf0eaf252014-03-21 13:05:51 -0700527 int err = mGraphicBufferProducer->connect(listener, api, mProducerControlledByApp, &output);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800528 if (err == NO_ERROR) {
529 uint32_t numPendingBuffers = 0;
530 output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint,
531 &numPendingBuffers);
532 mConsumerRunningBehind = (numPendingBuffers >= 2);
533 }
534 if (!err && api == NATIVE_WINDOW_API_CPU) {
535 mConnectedToCpu = true;
536 }
537 return err;
538}
539
Mathias Agopian365857d2013-09-11 19:35:45 -0700540
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800541int Surface::disconnect(int api) {
542 ATRACE_CALL();
543 ALOGV("Surface::disconnect");
544 Mutex::Autolock lock(mMutex);
545 freeAllBuffers();
546 int err = mGraphicBufferProducer->disconnect(api);
547 if (!err) {
548 mReqFormat = 0;
549 mReqWidth = 0;
550 mReqHeight = 0;
551 mReqUsage = 0;
552 mCrop.clear();
553 mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
554 mTransform = 0;
555 if (api == NATIVE_WINDOW_API_CPU) {
556 mConnectedToCpu = false;
557 }
558 }
559 return err;
560}
561
562int Surface::setUsage(uint32_t reqUsage)
563{
564 ALOGV("Surface::setUsage");
565 Mutex::Autolock lock(mMutex);
566 mReqUsage = reqUsage;
567 return OK;
568}
569
570int Surface::setCrop(Rect const* rect)
571{
572 ATRACE_CALL();
573
574 Rect realRect;
575 if (rect == NULL || rect->isEmpty()) {
576 realRect.clear();
577 } else {
578 realRect = *rect;
Mathias Agopian55fa2512010-03-11 15:06:54 -0800579 }
580
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800581 ALOGV("Surface::setCrop rect=[%d %d %d %d]",
582 realRect.left, realRect.top, realRect.right, realRect.bottom);
583
584 Mutex::Autolock lock(mMutex);
585 mCrop = realRect;
586 return NO_ERROR;
587}
588
589int Surface::setBufferCount(int bufferCount)
590{
591 ATRACE_CALL();
592 ALOGV("Surface::setBufferCount");
593 Mutex::Autolock lock(mMutex);
594
595 status_t err = mGraphicBufferProducer->setBufferCount(bufferCount);
596 ALOGE_IF(err, "IGraphicBufferProducer::setBufferCount(%d) returned %s",
597 bufferCount, strerror(-err));
Mathias Agopian90147262010-01-22 11:47:55 -0800598
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700599 if (err == NO_ERROR) {
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800600 freeAllBuffers();
Mathias Agopian87a96ea2011-08-23 21:09:41 -0700601 }
602
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700603 return err;
604}
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700605
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800606int Surface::setBuffersDimensions(int w, int h)
607{
608 ATRACE_CALL();
609 ALOGV("Surface::setBuffersDimensions");
610
611 if (w<0 || h<0)
612 return BAD_VALUE;
613
614 if ((w && !h) || (!w && h))
615 return BAD_VALUE;
616
617 Mutex::Autolock lock(mMutex);
618 mReqWidth = w;
619 mReqHeight = h;
620 return NO_ERROR;
621}
622
623int Surface::setBuffersUserDimensions(int w, int h)
624{
625 ATRACE_CALL();
626 ALOGV("Surface::setBuffersUserDimensions");
627
628 if (w<0 || h<0)
629 return BAD_VALUE;
630
631 if ((w && !h) || (!w && h))
632 return BAD_VALUE;
633
634 Mutex::Autolock lock(mMutex);
635 mUserWidth = w;
636 mUserHeight = h;
637 return NO_ERROR;
638}
639
640int Surface::setBuffersFormat(int format)
641{
642 ALOGV("Surface::setBuffersFormat");
643
644 if (format<0)
645 return BAD_VALUE;
646
647 Mutex::Autolock lock(mMutex);
648 mReqFormat = format;
649 return NO_ERROR;
650}
651
652int Surface::setScalingMode(int mode)
653{
654 ATRACE_CALL();
655 ALOGV("Surface::setScalingMode(%d)", mode);
656
657 switch (mode) {
658 case NATIVE_WINDOW_SCALING_MODE_FREEZE:
659 case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
660 case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
661 break;
662 default:
663 ALOGE("unknown scaling mode: %d", mode);
664 return BAD_VALUE;
665 }
666
667 Mutex::Autolock lock(mMutex);
668 mScalingMode = mode;
669 return NO_ERROR;
670}
671
672int Surface::setBuffersTransform(int transform)
673{
674 ATRACE_CALL();
675 ALOGV("Surface::setBuffersTransform");
676 Mutex::Autolock lock(mMutex);
677 mTransform = transform;
678 return NO_ERROR;
679}
680
681int Surface::setBuffersTimestamp(int64_t timestamp)
682{
683 ALOGV("Surface::setBuffersTimestamp");
684 Mutex::Autolock lock(mMutex);
685 mTimestamp = timestamp;
686 return NO_ERROR;
687}
688
689void Surface::freeAllBuffers() {
690 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
691 mSlots[i].buffer = 0;
692 }
693}
694
695// ----------------------------------------------------------------------
696// the lock/unlock APIs must be used from the same thread
697
698static status_t copyBlt(
699 const sp<GraphicBuffer>& dst,
700 const sp<GraphicBuffer>& src,
701 const Region& reg)
702{
703 // src and dst with, height and format must be identical. no verification
704 // is done here.
705 status_t err;
706 uint8_t const * src_bits = NULL;
707 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
708 ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
709
710 uint8_t* dst_bits = NULL;
711 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
712 ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
713
714 Region::const_iterator head(reg.begin());
715 Region::const_iterator tail(reg.end());
716 if (head != tail && src_bits && dst_bits) {
717 const size_t bpp = bytesPerPixel(src->format);
718 const size_t dbpr = dst->stride * bpp;
719 const size_t sbpr = src->stride * bpp;
720
721 while (head != tail) {
722 const Rect& r(*head++);
723 ssize_t h = r.height();
724 if (h <= 0) continue;
725 size_t size = r.width() * bpp;
726 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
727 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
728 if (dbpr==sbpr && size==sbpr) {
729 size *= h;
730 h = 1;
731 }
732 do {
733 memcpy(d, s, size);
734 d += dbpr;
735 s += sbpr;
736 } while (--h > 0);
737 }
738 }
739
740 if (src_bits)
741 src->unlock();
742
743 if (dst_bits)
744 dst->unlock();
745
746 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747}
748
Mathias Agopiana138f892010-05-21 17:24:35 -0700749// ----------------------------------------------------------------------------
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800750
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800751status_t Surface::lock(
752 ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
753{
754 if (mLockedBuffer != 0) {
755 ALOGE("Surface::lock failed, already locked");
756 return INVALID_OPERATION;
757 }
758
759 if (!mConnectedToCpu) {
760 int err = Surface::connect(NATIVE_WINDOW_API_CPU);
761 if (err) {
762 return err;
763 }
764 // we're intending to do software rendering from this point
765 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
766 }
767
768 ANativeWindowBuffer* out;
769 int fenceFd = -1;
770 status_t err = dequeueBuffer(&out, &fenceFd);
771 ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
772 if (err == NO_ERROR) {
773 sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800774 const Rect bounds(backBuffer->width, backBuffer->height);
775
776 Region newDirtyRegion;
777 if (inOutDirtyBounds) {
778 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
779 newDirtyRegion.andSelf(bounds);
780 } else {
781 newDirtyRegion.set(bounds);
782 }
783
784 // figure out if we can copy the frontbuffer back
785 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
786 const bool canCopyBack = (frontBuffer != 0 &&
787 backBuffer->width == frontBuffer->width &&
788 backBuffer->height == frontBuffer->height &&
789 backBuffer->format == frontBuffer->format);
790
791 if (canCopyBack) {
792 // copy the area that is invalid and not repainted this round
793 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
794 if (!copyback.isEmpty())
795 copyBlt(backBuffer, frontBuffer, copyback);
796 } else {
797 // if we can't copy-back anything, modify the user's dirty
798 // region to make sure they redraw the whole buffer
799 newDirtyRegion.set(bounds);
800 mDirtyRegion.clear();
801 Mutex::Autolock lock(mMutex);
802 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
803 mSlots[i].dirtyRegion.clear();
804 }
805 }
806
807
808 { // scope for the lock
809 Mutex::Autolock lock(mMutex);
810 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
811 if (backBufferSlot >= 0) {
812 Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
813 mDirtyRegion.subtract(dirtyRegion);
814 dirtyRegion = newDirtyRegion;
815 }
816 }
817
818 mDirtyRegion.orSelf(newDirtyRegion);
819 if (inOutDirtyBounds) {
820 *inOutDirtyBounds = newDirtyRegion.getBounds();
821 }
822
823 void* vaddr;
Francis Hart8f396012014-04-01 15:30:53 +0300824 status_t res = backBuffer->lockAsync(
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800825 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
Francis Hart8f396012014-04-01 15:30:53 +0300826 newDirtyRegion.bounds(), &vaddr, fenceFd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800827
828 ALOGW_IF(res, "failed locking buffer (handle = %p)",
829 backBuffer->handle);
830
831 if (res != 0) {
832 err = INVALID_OPERATION;
833 } else {
834 mLockedBuffer = backBuffer;
835 outBuffer->width = backBuffer->width;
836 outBuffer->height = backBuffer->height;
837 outBuffer->stride = backBuffer->stride;
838 outBuffer->format = backBuffer->format;
839 outBuffer->bits = vaddr;
840 }
841 }
842 return err;
843}
844
845status_t Surface::unlockAndPost()
846{
847 if (mLockedBuffer == 0) {
848 ALOGE("Surface::unlockAndPost failed, no locked buffer");
849 return INVALID_OPERATION;
850 }
851
Francis Hart8f396012014-04-01 15:30:53 +0300852 int fd = -1;
853 status_t err = mLockedBuffer->unlockAsync(&fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800854 ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
855
Francis Hart8f396012014-04-01 15:30:53 +0300856 err = queueBuffer(mLockedBuffer.get(), fd);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800857 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