blob: f51ca7a953c7b9ad7b61dbb2a6c67dbfa997547f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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"
18
19#include <stdint.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/threads.h>
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080028#include <utils/CallStack.h>
29#include <binder/IPCThreadState.h>
30#include <binder/IMemory.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include <utils/Log.h>
32
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080033#include <ui/DisplayInfo.h>
34#include <ui/GraphicBuffer.h>
35#include <ui/GraphicBufferMapper.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <ui/ISurface.h>
37#include <ui/Surface.h>
38#include <ui/SurfaceComposerClient.h>
39#include <ui/Rect.h>
40
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080041#include <pixelflinger/pixelflinger.h>
42
43#include <private/ui/SharedBufferStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044#include <private/ui/LayerState.h>
45
46namespace android {
47
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080048// ----------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -080050static status_t copyBlt(
51 const sp<GraphicBuffer>& dst,
52 const sp<GraphicBuffer>& src,
53 const Region& reg)
54{
55 status_t err;
56 uint8_t const * src_bits = NULL;
57 err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
58 LOGE_IF(err, "error locking src buffer %s", strerror(-err));
59
60 uint8_t* dst_bits = NULL;
61 err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
62 LOGE_IF(err, "error locking dst buffer %s", strerror(-err));
63
64 Region::const_iterator head(reg.begin());
65 Region::const_iterator tail(reg.end());
66 if (head != tail && src_bits && dst_bits) {
67 // NOTE: dst and src must be the same format
68 const size_t bpp = bytesPerPixel(src->format);
69 const size_t dbpr = dst->stride * bpp;
70 const size_t sbpr = src->stride * bpp;
71
72 while (head != tail) {
73 const Rect& r(*head++);
74 ssize_t h = r.height();
75 if (h <= 0) continue;
76 size_t size = r.width() * bpp;
77 uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
78 uint8_t * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
79 if (dbpr==sbpr && size==sbpr) {
80 size *= h;
81 h = 1;
82 }
83 do {
84 memcpy(d, s, size);
85 d += dbpr;
86 s += sbpr;
87 } while (--h > 0);
88 }
89 }
90
91 if (src_bits)
92 src->unlock();
93
94 if (dst_bits)
95 dst->unlock();
96
97 return err;
98}
99
100// ============================================================================
101// SurfaceControl
102// ============================================================================
103
104SurfaceControl::SurfaceControl(
105 const sp<SurfaceComposerClient>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 const sp<ISurface>& surface,
107 const ISurfaceFlingerClient::surface_data_t& data,
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800108 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 : mClient(client), mSurface(surface),
110 mToken(data.token), mIdentity(data.identity),
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800111 mWidth(data.width), mHeight(data.height), mFormat(data.format),
112 mFlags(flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800114}
115
116SurfaceControl::~SurfaceControl()
117{
118 destroy();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119}
120
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800121void SurfaceControl::destroy()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800123 if (isValid()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 mClient->destroySurface(mToken);
125 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800126
127 // clear all references and trigger an IPC now, to make sure things
128 // happen without delay, since these resources are quite heavy.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mClient.clear();
130 mSurface.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 IPCThreadState::self()->flushCommands();
132}
133
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800134void SurfaceControl::clear()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800136 // here, the window manager tells us explicitly that we should destroy
137 // the surface's resource. Soon after this call, it will also release
138 // its last reference (which will call the dtor); however, it is possible
139 // that a client living in the same process still holds references which
140 // would delay the call to the dtor -- that is why we need this explicit
141 // "clear()" call.
142 destroy();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800145bool SurfaceControl::isSameSurface(
146 const sp<SurfaceControl>& lhs, const sp<SurfaceControl>& rhs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
148 if (lhs == 0 || rhs == 0)
149 return false;
150 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
151}
152
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800153status_t SurfaceControl::setLayer(int32_t layer) {
154 const sp<SurfaceComposerClient>& client(mClient);
155 if (client == 0) return NO_INIT;
156 status_t err = validate(client->mControl);
157 if (err < 0) return err;
158 return client->setLayer(mToken, layer);
159}
160status_t SurfaceControl::setPosition(int32_t x, int32_t y) {
161 const sp<SurfaceComposerClient>& client(mClient);
162 if (client == 0) return NO_INIT;
163 status_t err = validate(client->mControl);
164 if (err < 0) return err;
165 return client->setPosition(mToken, x, y);
166}
167status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
168 const sp<SurfaceComposerClient>& client(mClient);
169 if (client == 0) return NO_INIT;
170 status_t err = validate(client->mControl);
171 if (err < 0) return err;
172 return client->setSize(mToken, w, h);
173}
174status_t SurfaceControl::hide() {
175 const sp<SurfaceComposerClient>& client(mClient);
176 if (client == 0) return NO_INIT;
177 status_t err = validate(client->mControl);
178 if (err < 0) return err;
179 return client->hide(mToken);
180}
181status_t SurfaceControl::show(int32_t layer) {
182 const sp<SurfaceComposerClient>& client(mClient);
183 if (client == 0) return NO_INIT;
184 status_t err = validate(client->mControl);
185 if (err < 0) return err;
186 return client->show(mToken, layer);
187}
188status_t SurfaceControl::freeze() {
189 const sp<SurfaceComposerClient>& client(mClient);
190 if (client == 0) return NO_INIT;
191 status_t err = validate(client->mControl);
192 if (err < 0) return err;
193 return client->freeze(mToken);
194}
195status_t SurfaceControl::unfreeze() {
196 const sp<SurfaceComposerClient>& client(mClient);
197 if (client == 0) return NO_INIT;
198 status_t err = validate(client->mControl);
199 if (err < 0) return err;
200 return client->unfreeze(mToken);
201}
202status_t SurfaceControl::setFlags(uint32_t flags, uint32_t mask) {
203 const sp<SurfaceComposerClient>& client(mClient);
204 if (client == 0) return NO_INIT;
205 status_t err = validate(client->mControl);
206 if (err < 0) return err;
207 return client->setFlags(mToken, flags, mask);
208}
209status_t SurfaceControl::setTransparentRegionHint(const Region& transparent) {
210 const sp<SurfaceComposerClient>& client(mClient);
211 if (client == 0) return NO_INIT;
212 status_t err = validate(client->mControl);
213 if (err < 0) return err;
214 return client->setTransparentRegionHint(mToken, transparent);
215}
216status_t SurfaceControl::setAlpha(float alpha) {
217 const sp<SurfaceComposerClient>& client(mClient);
218 if (client == 0) return NO_INIT;
219 status_t err = validate(client->mControl);
220 if (err < 0) return err;
221 return client->setAlpha(mToken, alpha);
222}
223status_t SurfaceControl::setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
224 const sp<SurfaceComposerClient>& client(mClient);
225 if (client == 0) return NO_INIT;
226 status_t err = validate(client->mControl);
227 if (err < 0) return err;
228 return client->setMatrix(mToken, dsdx, dtdx, dsdy, dtdy);
229}
230status_t SurfaceControl::setFreezeTint(uint32_t tint) {
231 const sp<SurfaceComposerClient>& client(mClient);
232 if (client == 0) return NO_INIT;
233 status_t err = validate(client->mControl);
234 if (err < 0) return err;
235 return client->setFreezeTint(mToken, tint);
236}
237
238status_t SurfaceControl::validate(SharedClient const* cblk) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800239{
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800240 if (mToken<0 || mClient==0) {
241 LOGE("invalid token (%d, identity=%u) or client (%p)",
242 mToken, mIdentity, mClient.get());
243 return NO_INIT;
244 }
245 if (cblk == 0) {
246 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
247 return NO_INIT;
248 }
249 status_t err = cblk->validate(mToken);
250 if (err != NO_ERROR) {
251 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
252 mToken, mIdentity, err, strerror(-err));
253 return err;
254 }
255 uint32_t identity = cblk->getIdentity(mToken);
256 if (mIdentity != identity) {
257 LOGE("using an invalid surface id=%d, identity=%u should be %d",
258 mToken, mIdentity, identity);
259 return NO_INIT;
260 }
261 return NO_ERROR;
262}
263
264status_t SurfaceControl::writeSurfaceToParcel(
265 const sp<SurfaceControl>& control, Parcel* parcel)
266{
267 uint32_t flags = 0;
268 uint32_t format = 0;
269 SurfaceID token = -1;
270 uint32_t identity = 0;
271 uint32_t width = 0;
272 uint32_t height = 0;
273 sp<SurfaceComposerClient> client;
274 sp<ISurface> sur;
275 if (SurfaceControl::isValid(control)) {
276 token = control->mToken;
277 identity = control->mIdentity;
278 client = control->mClient;
279 sur = control->mSurface;
280 width = control->mWidth;
281 height = control->mHeight;
282 format = control->mFormat;
283 flags = control->mFlags;
284 }
285 parcel->writeStrongBinder(client!=0 ? client->connection() : NULL);
286 parcel->writeStrongBinder(sur!=0 ? sur->asBinder() : NULL);
287 parcel->writeInt32(token);
288 parcel->writeInt32(identity);
289 parcel->writeInt32(width);
290 parcel->writeInt32(height);
291 parcel->writeInt32(format);
292 parcel->writeInt32(flags);
293 return NO_ERROR;
294}
295
296sp<Surface> SurfaceControl::getSurface() const
297{
298 Mutex::Autolock _l(mLock);
299 if (mSurfaceData == 0) {
300 mSurfaceData = new Surface(const_cast<SurfaceControl*>(this));
301 }
302 return mSurfaceData;
303}
304
305// ============================================================================
306// Surface
307// ============================================================================
308
309Surface::Surface(const sp<SurfaceControl>& surface)
310 : mClient(surface->mClient), mSurface(surface->mSurface),
311 mToken(surface->mToken), mIdentity(surface->mIdentity),
312 mFormat(surface->mFormat), mFlags(surface->mFlags),
313 mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL),
314 mWidth(surface->mWidth), mHeight(surface->mHeight)
315{
316 mSharedBufferClient = new SharedBufferClient(
317 mClient->mControl, mToken, 2, mIdentity);
318
319 init();
320}
321
322Surface::Surface(const Parcel& parcel)
323 : mBufferMapper(GraphicBufferMapper::get()), mSharedBufferClient(NULL)
324{
325 sp<IBinder> clientBinder = parcel.readStrongBinder();
326 mSurface = interface_cast<ISurface>(parcel.readStrongBinder());
327 mToken = parcel.readInt32();
328 mIdentity = parcel.readInt32();
329 mWidth = parcel.readInt32();
330 mHeight = parcel.readInt32();
331 mFormat = parcel.readInt32();
332 mFlags = parcel.readInt32();
333
334 // FIXME: what does that mean if clientBinder is NULL here?
335 if (clientBinder != NULL) {
336 mClient = SurfaceComposerClient::clientForConnection(clientBinder);
337
338 mSharedBufferClient = new SharedBufferClient(
339 mClient->mControl, mToken, 2, mIdentity);
340 }
341
342 init();
343}
344
345void Surface::init()
346{
347 android_native_window_t::setSwapInterval = setSwapInterval;
348 android_native_window_t::dequeueBuffer = dequeueBuffer;
349 android_native_window_t::lockBuffer = lockBuffer;
350 android_native_window_t::queueBuffer = queueBuffer;
351 android_native_window_t::query = query;
352 android_native_window_t::perform = perform;
353 mSwapRectangle.makeInvalid();
354 DisplayInfo dinfo;
355 SurfaceComposerClient::getDisplayInfo(0, &dinfo);
356 const_cast<float&>(android_native_window_t::xdpi) = dinfo.xdpi;
357 const_cast<float&>(android_native_window_t::ydpi) = dinfo.ydpi;
358 // FIXME: set real values here
359 const_cast<int&>(android_native_window_t::minSwapInterval) = 1;
360 const_cast<int&>(android_native_window_t::maxSwapInterval) = 1;
361 const_cast<uint32_t&>(android_native_window_t::flags) = 0;
362 // be default we request a hardware surface
363 mUsage = GRALLOC_USAGE_HW_RENDER;
364 mNeedFullUpdate = false;
365}
366
367Surface::~Surface()
368{
369 // this is a client-side operation, the surface is destroyed, unmap
370 // its buffers in this process.
371 for (int i=0 ; i<2 ; i++) {
372 if (mBuffers[i] != 0 && mBuffers[i]->handle != 0) {
373 getBufferMapper().unregisterBuffer(mBuffers[i]->handle);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 }
375 }
Jean-Baptiste Querucc8c35c2009-11-12 18:45:53 -0800376
377 // clear all references and trigger an IPC now, to make sure things
378 // happen without delay, since these resources are quite heavy.
379 mClient.clear();
380 mSurface.clear();
381 delete mSharedBufferClient;
382 IPCThreadState::self()->flushCommands();
383}
384
385sp<SurfaceComposerClient> Surface::getClient() const {
386 return mClient;
387}
388
389sp<ISurface> Surface::getISurface() const {
390 return mSurface;
391}
392
393bool Surface::isValid() {
394 return mToken>=0 && mClient!=0;
395}
396
397status_t Surface::validate(SharedClient const* cblk) const
398{
399 sp<SurfaceComposerClient> client(getClient());
400 if (mToken<0 || mClient==0) {
401 LOGE("invalid token (%d, identity=%u) or client (%p)",
402 mToken, mIdentity, client.get());
403 return NO_INIT;
404 }
405 if (cblk == 0) {
406 LOGE("cblk is null (surface id=%d, identity=%u)", mToken, mIdentity);
407 return NO_INIT;
408 }
409 status_t err = cblk->validate(mToken);
410 if (err != NO_ERROR) {
411 LOGE("surface (id=%d, identity=%u) is invalid, err=%d (%s)",
412 mToken, mIdentity, err, strerror(-err));
413 return err;
414 }
415 uint32_t identity = cblk->getIdentity(mToken);
416 if (mIdentity != identity) {
417 LOGE("using an invalid surface id=%d, identity=%u should be %d",
418 mToken, mIdentity, identity);
419 return NO_INIT;
420 }
421 return NO_ERROR;
422}
423
424
425bool Surface::isSameSurface(
426 const sp<Surface>& lhs, const sp<Surface>& rhs)
427{
428 if (lhs == 0 || rhs == 0)
429 return false;
430
431 return lhs->mSurface->asBinder() == rhs->mSurface->asBinder();
432}
433
434// ----------------------------------------------------------------------------
435
436int Surface::setSwapInterval(android_native_window_t* window, int interval) {
437 return 0;
438}
439
440int Surface::dequeueBuffer(android_native_window_t* window,
441 android_native_buffer_t** buffer) {
442 Surface* self = getSelf(window);
443 return self->dequeueBuffer(buffer);
444}
445
446int Surface::lockBuffer(android_native_window_t* window,
447 android_native_buffer_t* buffer) {
448 Surface* self = getSelf(window);
449 return self->lockBuffer(buffer);
450}
451
452int Surface::queueBuffer(android_native_window_t* window,
453 android_native_buffer_t* buffer) {
454 Surface* self = getSelf(window);
455 return self->queueBuffer(buffer);
456}
457
458int Surface::query(android_native_window_t* window,
459 int what, int* value) {
460 Surface* self = getSelf(window);
461 return self->query(what, value);
462}
463
464int Surface::perform(android_native_window_t* window,
465 int operation, ...) {
466 va_list args;
467 va_start(args, operation);
468 Surface* self = getSelf(window);
469 int res = self->perform(operation, args);
470 va_end(args);
471 return res;
472}
473
474// ----------------------------------------------------------------------------
475
476status_t Surface::dequeueBuffer(sp<GraphicBuffer>* buffer) {
477 android_native_buffer_t* out;
478 status_t err = dequeueBuffer(&out);
479 if (err == NO_ERROR) {
480 *buffer = GraphicBuffer::getSelf(out);
481 }
482 return err;
483}
484
485// ----------------------------------------------------------------------------
486
487
488int Surface::dequeueBuffer(android_native_buffer_t** buffer)
489{
490 sp<SurfaceComposerClient> client(getClient());
491 status_t err = validate(client->mControl);
492 if (err != NO_ERROR)
493 return err;
494
495 ssize_t bufIdx = mSharedBufferClient->dequeue();
496 if (bufIdx < 0) {
497 LOGE("error dequeuing a buffer (%s)", strerror(bufIdx));
498 return bufIdx;
499 }
500
501 // below we make sure we AT LEAST have the usage flags we want
502 const uint32_t usage(getUsage());
503 const sp<GraphicBuffer>& backBuffer(mBuffers[bufIdx]);
504 if (backBuffer == 0 ||
505 ((uint32_t(backBuffer->usage) & usage) != usage) ||
506 mSharedBufferClient->needNewBuffer(bufIdx))
507 {
508 err = getBufferLocked(bufIdx, usage);
509 LOGE_IF(err, "getBufferLocked(%ld, %08x) failed (%s)",
510 bufIdx, usage, strerror(-err));
511 if (err == NO_ERROR) {
512 // reset the width/height with the what we get from the buffer
513 mWidth = uint32_t(backBuffer->width);
514 mHeight = uint32_t(backBuffer->height);
515 }
516 }
517
518 // if we still don't have a buffer here, we probably ran out of memory
519 if (!err && backBuffer==0) {
520 err = NO_MEMORY;
521 }
522
523 if (err == NO_ERROR) {
524 mDirtyRegion.set(backBuffer->width, backBuffer->height);
525 *buffer = backBuffer.get();
526 } else {
527 mSharedBufferClient->undoDequeue(bufIdx);
528 }
529
530 return err;
531}
532
533int Surface::lockBuffer(android_native_buffer_t* buffer)
534{
535 sp<SurfaceComposerClient> client(getClient());
536 status_t err = validate(client->mControl);
537 if (err != NO_ERROR)
538 return err;
539
540 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
541 err = mSharedBufferClient->lock(bufIdx);
542 LOGE_IF(err, "error locking buffer %d (%s)", bufIdx, strerror(-err));
543 return err;
544}
545
546int Surface::queueBuffer(android_native_buffer_t* buffer)
547{
548 sp<SurfaceComposerClient> client(getClient());
549 status_t err = validate(client->mControl);
550 if (err != NO_ERROR)
551 return err;
552
553 if (mSwapRectangle.isValid()) {
554 mDirtyRegion.set(mSwapRectangle);
555 }
556
557 int32_t bufIdx = GraphicBuffer::getSelf(buffer)->getIndex();
558 mSharedBufferClient->setDirtyRegion(bufIdx, mDirtyRegion);
559 err = mSharedBufferClient->queue(bufIdx);
560 LOGE_IF(err, "error queuing buffer %d (%s)", bufIdx, strerror(-err));
561
562 if (err == NO_ERROR) {
563 // FIXME: can we avoid this IPC if we know there is one pending?
564 client->signalServer();
565 }
566 return err;
567}
568
569int Surface::query(int what, int* value)
570{
571 switch (what) {
572 case NATIVE_WINDOW_WIDTH:
573 *value = int(mWidth);
574 return NO_ERROR;
575 case NATIVE_WINDOW_HEIGHT:
576 *value = int(mHeight);
577 return NO_ERROR;
578 case NATIVE_WINDOW_FORMAT:
579 *value = int(mFormat);
580 return NO_ERROR;
581 }
582 return BAD_VALUE;
583}
584
585int Surface::perform(int operation, va_list args)
586{
587 int res = NO_ERROR;
588 switch (operation) {
589 case NATIVE_WINDOW_SET_USAGE:
590 setUsage( va_arg(args, int) );
591 break;
592 default:
593 res = NAME_NOT_FOUND;
594 break;
595 }
596 return res;
597}
598
599void Surface::setUsage(uint32_t reqUsage)
600{
601 Mutex::Autolock _l(mSurfaceLock);
602 mUsage = reqUsage;
603}
604
605uint32_t Surface::getUsage() const
606{
607 Mutex::Autolock _l(mSurfaceLock);
608 return mUsage;
609}
610
611// ----------------------------------------------------------------------------
612
613status_t Surface::lock(SurfaceInfo* info, bool blocking) {
614 return Surface::lock(info, NULL, blocking);
615}
616
617status_t Surface::lock(SurfaceInfo* other, Region* dirtyIn, bool blocking)
618{
619 if (mApiLock.tryLock() != NO_ERROR) {
620 LOGE("calling Surface::lock() from different threads!");
621 CallStack stack;
622 stack.update();
623 stack.dump("Surface::lock called from different threads");
624 return WOULD_BLOCK;
625 }
626
627 // we're intending to do software rendering from this point
628 setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
629
630 sp<GraphicBuffer> backBuffer;
631 status_t err = dequeueBuffer(&backBuffer);
632 LOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
633 if (err == NO_ERROR) {
634 err = lockBuffer(backBuffer.get());
635 LOGE_IF(err, "lockBuffer (idx=%d) failed (%s)",
636 backBuffer->getIndex(), strerror(-err));
637 if (err == NO_ERROR) {
638 // we handle copy-back here...
639
640 const Rect bounds(backBuffer->width, backBuffer->height);
641 Region scratch(bounds);
642 Region& newDirtyRegion(dirtyIn ? *dirtyIn : scratch);
643
644 if (mNeedFullUpdate) {
645 // reset newDirtyRegion to bounds when a buffer is reallocated
646 // it would be better if this information was associated with
647 // the buffer and made available to outside of Surface.
648 // This will do for now though.
649 mNeedFullUpdate = false;
650 newDirtyRegion.set(bounds);
651 } else {
652 newDirtyRegion.andSelf(bounds);
653 }
654
655 const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
656 if (frontBuffer !=0 &&
657 backBuffer->width == frontBuffer->width &&
658 backBuffer->height == frontBuffer->height &&
659 !(mFlags & ISurfaceComposer::eDestroyBackbuffer))
660 {
661 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
662 if (!copyback.isEmpty() && frontBuffer!=0) {
663 // copy front to back
664 copyBlt(backBuffer, frontBuffer, copyback);
665 }
666 }
667
668 mDirtyRegion = newDirtyRegion;
669 mOldDirtyRegion = newDirtyRegion;
670
671 void* vaddr;
672 status_t res = backBuffer->lock(
673 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
674 newDirtyRegion.bounds(), &vaddr);
675
676 LOGW_IF(res, "failed locking buffer (handle = %p)",
677 backBuffer->handle);
678
679 mLockedBuffer = backBuffer;
680 other->w = backBuffer->width;
681 other->h = backBuffer->height;
682 other->s = backBuffer->stride;
683 other->usage = backBuffer->usage;
684 other->format = backBuffer->format;
685 other->bits = vaddr;
686 }
687 }
688 mApiLock.unlock();
689 return err;
690}
691
692status_t Surface::unlockAndPost()
693{
694 if (mLockedBuffer == 0) {
695 LOGE("unlockAndPost failed, no locked buffer");
696 return BAD_VALUE;
697 }
698
699 status_t err = mLockedBuffer->unlock();
700 LOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
701
702 err = queueBuffer(mLockedBuffer.get());
703 LOGE_IF(err, "queueBuffer (idx=%d) failed (%s)",
704 mLockedBuffer->getIndex(), strerror(-err));
705
706 mPostedBuffer = mLockedBuffer;
707 mLockedBuffer = 0;
708 return err;
709}
710
711void Surface::setSwapRectangle(const Rect& r) {
712 Mutex::Autolock _l(mSurfaceLock);
713 mSwapRectangle = r;
714}
715
716status_t Surface::getBufferLocked(int index, int usage)
717{
718 sp<ISurface> s(mSurface);
719 if (s == 0) return NO_INIT;
720
721 status_t err = NO_MEMORY;
722
723 // free the current buffer
724 sp<GraphicBuffer>& currentBuffer(mBuffers[index]);
725 if (currentBuffer != 0) {
726 getBufferMapper().unregisterBuffer(currentBuffer->handle);
727 currentBuffer.clear();
728 }
729
730 sp<GraphicBuffer> buffer = s->requestBuffer(index, usage);
731 LOGE_IF(buffer==0,
732 "ISurface::getBuffer(%d, %08x) returned NULL",
733 index, usage);
734 if (buffer != 0) { // this should never happen by construction
735 LOGE_IF(buffer->handle == NULL,
736 "Surface (identity=%d) requestBuffer(%d, %08x) returned"
737 "a buffer with a null handle", mIdentity, index, usage);
738 err = mSharedBufferClient->getStatus();
739 LOGE_IF(err, "Surface (identity=%d) state = %d", mIdentity, err);
740 if (!err && buffer->handle != NULL) {
741 err = getBufferMapper().registerBuffer(buffer->handle);
742 LOGW_IF(err, "registerBuffer(...) failed %d (%s)",
743 err, strerror(-err));
744 if (err == NO_ERROR) {
745 currentBuffer = buffer;
746 currentBuffer->setIndex(index);
747 mNeedFullUpdate = true;
748 }
749 } else {
750 err = err<0 ? err : NO_MEMORY;
751 }
752 }
753 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754}
755
756}; // namespace android
757