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