OSDN Git Service

Enabled cropping support in SurfaceTexture
[android-x86/frameworks-native.git] / libs / gui / SurfaceTextureClient.cpp
1 /*
2  * Copyright (C) 2010 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 "SurfaceTextureClient"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
20
21 #include <android/native_window.h>
22
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25
26 #include <gui/ISurfaceComposer.h>
27 #include <gui/SurfaceComposerClient.h>
28 #include <gui/SurfaceTexture.h>
29 #include <gui/SurfaceTextureClient.h>
30
31 #include <private/gui/ComposerService.h>
32
33 namespace android {
34
35 SurfaceTextureClient::SurfaceTextureClient(
36         const sp<ISurfaceTexture>& surfaceTexture)
37 {
38     SurfaceTextureClient::init();
39     SurfaceTextureClient::setISurfaceTexture(surfaceTexture);
40 }
41
42 // see SurfaceTextureClient.h
43 SurfaceTextureClient::SurfaceTextureClient(const
44          sp<SurfaceTexture>& surfaceTexture)
45 {
46     SurfaceTextureClient::init();
47     SurfaceTextureClient::setISurfaceTexture(surfaceTexture->getBufferQueue());
48 }
49
50 SurfaceTextureClient::SurfaceTextureClient() {
51     SurfaceTextureClient::init();
52 }
53
54 SurfaceTextureClient::~SurfaceTextureClient() {
55     if (mConnectedToCpu) {
56         SurfaceTextureClient::disconnect(NATIVE_WINDOW_API_CPU);
57     }
58 }
59
60 void SurfaceTextureClient::init() {
61     // Initialize the ANativeWindow function pointers.
62     ANativeWindow::setSwapInterval  = hook_setSwapInterval;
63     ANativeWindow::dequeueBuffer    = hook_dequeueBuffer;
64     ANativeWindow::cancelBuffer     = hook_cancelBuffer;
65     ANativeWindow::lockBuffer       = hook_lockBuffer;
66     ANativeWindow::queueBuffer      = hook_queueBuffer;
67     ANativeWindow::query            = hook_query;
68     ANativeWindow::perform          = hook_perform;
69
70     const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
71     const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
72
73     mReqWidth = 0;
74     mReqHeight = 0;
75     mReqFormat = 0;
76     mReqUsage = 0;
77     mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
78     mCrop.clear();
79     mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
80     mTransform = 0;
81     mDefaultWidth = 0;
82     mDefaultHeight = 0;
83     mUserWidth = 0;
84     mUserHeight = 0;
85     mTransformHint = 0;
86     mConnectedToCpu = false;
87 }
88
89 void SurfaceTextureClient::setISurfaceTexture(
90         const sp<ISurfaceTexture>& surfaceTexture)
91 {
92     mSurfaceTexture = surfaceTexture;
93 }
94
95 sp<ISurfaceTexture> SurfaceTextureClient::getISurfaceTexture() const {
96     return mSurfaceTexture;
97 }
98
99 int SurfaceTextureClient::hook_setSwapInterval(ANativeWindow* window, int interval) {
100     SurfaceTextureClient* c = getSelf(window);
101     return c->setSwapInterval(interval);
102 }
103
104 int SurfaceTextureClient::hook_dequeueBuffer(ANativeWindow* window,
105         ANativeWindowBuffer** buffer) {
106     SurfaceTextureClient* c = getSelf(window);
107     return c->dequeueBuffer(buffer);
108 }
109
110 int SurfaceTextureClient::hook_cancelBuffer(ANativeWindow* window,
111         ANativeWindowBuffer* buffer) {
112     SurfaceTextureClient* c = getSelf(window);
113     return c->cancelBuffer(buffer);
114 }
115
116 int SurfaceTextureClient::hook_lockBuffer(ANativeWindow* window,
117         ANativeWindowBuffer* buffer) {
118     SurfaceTextureClient* c = getSelf(window);
119     return c->lockBuffer(buffer);
120 }
121
122 int SurfaceTextureClient::hook_queueBuffer(ANativeWindow* window,
123         ANativeWindowBuffer* buffer) {
124     SurfaceTextureClient* c = getSelf(window);
125     return c->queueBuffer(buffer);
126 }
127
128 int SurfaceTextureClient::hook_query(const ANativeWindow* window,
129                                 int what, int* value) {
130     const SurfaceTextureClient* c = getSelf(window);
131     return c->query(what, value);
132 }
133
134 int SurfaceTextureClient::hook_perform(ANativeWindow* window, int operation, ...) {
135     va_list args;
136     va_start(args, operation);
137     SurfaceTextureClient* c = getSelf(window);
138     return c->perform(operation, args);
139 }
140
141 int SurfaceTextureClient::setSwapInterval(int interval) {
142     ATRACE_CALL();
143     // EGL specification states:
144     //  interval is silently clamped to minimum and maximum implementation
145     //  dependent values before being stored.
146     // Although we don't have to, we apply the same logic here.
147
148     if (interval < minSwapInterval)
149         interval = minSwapInterval;
150
151     if (interval > maxSwapInterval)
152         interval = maxSwapInterval;
153
154     status_t res = mSurfaceTexture->setSynchronousMode(interval ? true : false);
155
156     return res;
157 }
158
159 int SurfaceTextureClient::dequeueBuffer(android_native_buffer_t** buffer) {
160     ATRACE_CALL();
161     ALOGV("SurfaceTextureClient::dequeueBuffer");
162     Mutex::Autolock lock(mMutex);
163     int buf = -1;
164     int reqW = mReqWidth ? mReqWidth : mUserWidth;
165     int reqH = mReqHeight ? mReqHeight : mUserHeight;
166     status_t result = mSurfaceTexture->dequeueBuffer(&buf, reqW, reqH,
167             mReqFormat, mReqUsage);
168     if (result < 0) {
169         ALOGV("dequeueBuffer: ISurfaceTexture::dequeueBuffer(%d, %d, %d, %d)"
170              "failed: %d", mReqWidth, mReqHeight, mReqFormat, mReqUsage,
171              result);
172         return result;
173     }
174     sp<GraphicBuffer>& gbuf(mSlots[buf]);
175     if (result & ISurfaceTexture::RELEASE_ALL_BUFFERS) {
176         freeAllBuffers();
177     }
178
179     if ((result & ISurfaceTexture::BUFFER_NEEDS_REALLOCATION) || gbuf == 0) {
180         result = mSurfaceTexture->requestBuffer(buf, &gbuf);
181         if (result != NO_ERROR) {
182             ALOGE("dequeueBuffer: ISurfaceTexture::requestBuffer failed: %d",
183                     result);
184             return result;
185         }
186     }
187     *buffer = gbuf.get();
188     return OK;
189 }
190
191 int SurfaceTextureClient::cancelBuffer(android_native_buffer_t* buffer) {
192     ATRACE_CALL();
193     ALOGV("SurfaceTextureClient::cancelBuffer");
194     Mutex::Autolock lock(mMutex);
195     int i = getSlotFromBufferLocked(buffer);
196     if (i < 0) {
197         return i;
198     }
199     mSurfaceTexture->cancelBuffer(i);
200     return OK;
201 }
202
203 int SurfaceTextureClient::getSlotFromBufferLocked(
204         android_native_buffer_t* buffer) const {
205     bool dumpedState = false;
206     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
207         if (mSlots[i] != NULL && mSlots[i]->handle == buffer->handle) {
208             return i;
209         }
210     }
211     ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
212     return BAD_VALUE;
213 }
214
215 int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
216     ALOGV("SurfaceTextureClient::lockBuffer");
217     Mutex::Autolock lock(mMutex);
218     return OK;
219 }
220
221 int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
222     ATRACE_CALL();
223     ALOGV("SurfaceTextureClient::queueBuffer");
224     Mutex::Autolock lock(mMutex);
225     int64_t timestamp;
226     if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
227         timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
228         ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
229              timestamp / 1000000.f);
230     } else {
231         timestamp = mTimestamp;
232     }
233     int i = getSlotFromBufferLocked(buffer);
234     if (i < 0) {
235         return i;
236     }
237
238     ISurfaceTexture::QueueBufferOutput output;
239     ISurfaceTexture::QueueBufferInput input(timestamp,
240             mCrop, mScalingMode, mTransform);
241     status_t err = mSurfaceTexture->queueBuffer(i, input, &output);
242     if (err != OK)  {
243         ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
244     }
245     output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint);
246     return err;
247 }
248
249 int SurfaceTextureClient::query(int what, int* value) const {
250     ATRACE_CALL();
251     ALOGV("SurfaceTextureClient::query");
252     { // scope for the lock
253         Mutex::Autolock lock(mMutex);
254         switch (what) {
255             case NATIVE_WINDOW_FORMAT:
256                 if (mReqFormat) {
257                     *value = mReqFormat;
258                     return NO_ERROR;
259                 }
260                 break;
261             case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
262                 {
263                     sp<ISurfaceComposer> composer(
264                             ComposerService::getComposerService());
265                     if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
266                         *value = 1;
267                     } else {
268                         *value = 0;
269                     }
270                 }
271                 return NO_ERROR;
272             case NATIVE_WINDOW_CONCRETE_TYPE:
273                 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
274                 return NO_ERROR;
275             case NATIVE_WINDOW_DEFAULT_WIDTH:
276                 *value = mUserWidth ? mUserWidth : mDefaultWidth;
277                 return NO_ERROR;
278             case NATIVE_WINDOW_DEFAULT_HEIGHT:
279                 *value = mUserHeight ? mUserHeight : mDefaultHeight;
280                 return NO_ERROR;
281             case NATIVE_WINDOW_TRANSFORM_HINT:
282                 *value = mTransformHint;
283                 return NO_ERROR;
284         }
285     }
286     return mSurfaceTexture->query(what, value);
287 }
288
289 int SurfaceTextureClient::perform(int operation, va_list args)
290 {
291     int res = NO_ERROR;
292     switch (operation) {
293     case NATIVE_WINDOW_CONNECT:
294         // deprecated. must return NO_ERROR.
295         break;
296     case NATIVE_WINDOW_DISCONNECT:
297         // deprecated. must return NO_ERROR.
298         break;
299     case NATIVE_WINDOW_SET_USAGE:
300         res = dispatchSetUsage(args);
301         break;
302     case NATIVE_WINDOW_SET_CROP:
303         res = dispatchSetCrop(args);
304         break;
305     case NATIVE_WINDOW_SET_BUFFER_COUNT:
306         res = dispatchSetBufferCount(args);
307         break;
308     case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
309         res = dispatchSetBuffersGeometry(args);
310         break;
311     case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
312         res = dispatchSetBuffersTransform(args);
313         break;
314     case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
315         res = dispatchSetBuffersTimestamp(args);
316         break;
317     case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
318         res = dispatchSetBuffersDimensions(args);
319         break;
320     case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
321         res = dispatchSetBuffersUserDimensions(args);
322         break;
323     case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
324         res = dispatchSetBuffersFormat(args);
325         break;
326     case NATIVE_WINDOW_LOCK:
327         res = dispatchLock(args);
328         break;
329     case NATIVE_WINDOW_UNLOCK_AND_POST:
330         res = dispatchUnlockAndPost(args);
331         break;
332     case NATIVE_WINDOW_SET_SCALING_MODE:
333         res = dispatchSetScalingMode(args);
334         break;
335     case NATIVE_WINDOW_API_CONNECT:
336         res = dispatchConnect(args);
337         break;
338     case NATIVE_WINDOW_API_DISCONNECT:
339         res = dispatchDisconnect(args);
340         break;
341     default:
342         res = NAME_NOT_FOUND;
343         break;
344     }
345     return res;
346 }
347
348 int SurfaceTextureClient::dispatchConnect(va_list args) {
349     int api = va_arg(args, int);
350     return connect(api);
351 }
352
353 int SurfaceTextureClient::dispatchDisconnect(va_list args) {
354     int api = va_arg(args, int);
355     return disconnect(api);
356 }
357
358 int SurfaceTextureClient::dispatchSetUsage(va_list args) {
359     int usage = va_arg(args, int);
360     return setUsage(usage);
361 }
362
363 int SurfaceTextureClient::dispatchSetCrop(va_list args) {
364     android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
365     return setCrop(reinterpret_cast<Rect const*>(rect));
366 }
367
368 int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
369     size_t bufferCount = va_arg(args, size_t);
370     return setBufferCount(bufferCount);
371 }
372
373 int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
374     int w = va_arg(args, int);
375     int h = va_arg(args, int);
376     int f = va_arg(args, int);
377     int err = setBuffersDimensions(w, h);
378     if (err != 0) {
379         return err;
380     }
381     return setBuffersFormat(f);
382 }
383
384 int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
385     int w = va_arg(args, int);
386     int h = va_arg(args, int);
387     return setBuffersDimensions(w, h);
388 }
389
390 int SurfaceTextureClient::dispatchSetBuffersUserDimensions(va_list args) {
391     int w = va_arg(args, int);
392     int h = va_arg(args, int);
393     return setBuffersUserDimensions(w, h);
394 }
395
396 int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
397     int f = va_arg(args, int);
398     return setBuffersFormat(f);
399 }
400
401 int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
402     int m = va_arg(args, int);
403     return setScalingMode(m);
404 }
405
406 int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
407     int transform = va_arg(args, int);
408     return setBuffersTransform(transform);
409 }
410
411 int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
412     int64_t timestamp = va_arg(args, int64_t);
413     return setBuffersTimestamp(timestamp);
414 }
415
416 int SurfaceTextureClient::dispatchLock(va_list args) {
417     ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
418     ARect* inOutDirtyBounds = va_arg(args, ARect*);
419     return lock(outBuffer, inOutDirtyBounds);
420 }
421
422 int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
423     return unlockAndPost();
424 }
425
426
427 int SurfaceTextureClient::connect(int api) {
428     ATRACE_CALL();
429     ALOGV("SurfaceTextureClient::connect");
430     Mutex::Autolock lock(mMutex);
431     int err = mSurfaceTexture->connect(api,
432             &mDefaultWidth, &mDefaultHeight, &mTransformHint);
433     if (!err && api == NATIVE_WINDOW_API_CPU) {
434         mConnectedToCpu = true;
435     }
436     return err;
437 }
438
439 int SurfaceTextureClient::disconnect(int api) {
440     ATRACE_CALL();
441     ALOGV("SurfaceTextureClient::disconnect");
442     Mutex::Autolock lock(mMutex);
443     freeAllBuffers();
444     int err = mSurfaceTexture->disconnect(api);
445     if (!err) {
446         mReqFormat = 0;
447         mReqWidth = 0;
448         mReqHeight = 0;
449         mReqUsage = 0;
450         mCrop.clear();
451         mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
452         mTransform = 0;
453         if (api == NATIVE_WINDOW_API_CPU) {
454             mConnectedToCpu = false;
455         }
456     }
457     return err;
458 }
459
460 int SurfaceTextureClient::setUsage(uint32_t reqUsage)
461 {
462     ALOGV("SurfaceTextureClient::setUsage");
463     Mutex::Autolock lock(mMutex);
464     mReqUsage = reqUsage;
465     return OK;
466 }
467
468 int SurfaceTextureClient::setCrop(Rect const* rect)
469 {
470     ATRACE_CALL();
471     ALOGV("SurfaceTextureClient::setCrop");
472
473     Rect realRect;
474     if (rect == NULL || rect->isEmpty()) {
475         realRect.clear();
476     } else {
477         realRect = *rect;
478     }
479
480     Mutex::Autolock lock(mMutex);
481     mCrop = *rect;
482     return NO_ERROR;
483 }
484
485 int SurfaceTextureClient::setBufferCount(int bufferCount)
486 {
487     ATRACE_CALL();
488     ALOGV("SurfaceTextureClient::setBufferCount");
489     Mutex::Autolock lock(mMutex);
490
491     status_t err = mSurfaceTexture->setBufferCount(bufferCount);
492     ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
493             bufferCount, strerror(-err));
494
495     if (err == NO_ERROR) {
496         freeAllBuffers();
497     }
498
499     return err;
500 }
501
502 int SurfaceTextureClient::setBuffersDimensions(int w, int h)
503 {
504     ATRACE_CALL();
505     ALOGV("SurfaceTextureClient::setBuffersDimensions");
506
507     if (w<0 || h<0)
508         return BAD_VALUE;
509
510     if ((w && !h) || (!w && h))
511         return BAD_VALUE;
512
513     Mutex::Autolock lock(mMutex);
514     mReqWidth = w;
515     mReqHeight = h;
516     mCrop.clear();
517     return NO_ERROR;
518 }
519
520 int SurfaceTextureClient::setBuffersUserDimensions(int w, int h)
521 {
522     ATRACE_CALL();
523     ALOGV("SurfaceTextureClient::setBuffersUserDimensions");
524
525     if (w<0 || h<0)
526         return BAD_VALUE;
527
528     if ((w && !h) || (!w && h))
529         return BAD_VALUE;
530
531     Mutex::Autolock lock(mMutex);
532     mUserWidth = w;
533     mUserHeight = h;
534     mCrop.clear();
535     return NO_ERROR;
536 }
537
538 int SurfaceTextureClient::setBuffersFormat(int format)
539 {
540     ALOGV("SurfaceTextureClient::setBuffersFormat");
541
542     if (format<0)
543         return BAD_VALUE;
544
545     Mutex::Autolock lock(mMutex);
546     mReqFormat = format;
547     return NO_ERROR;
548 }
549
550 int SurfaceTextureClient::setScalingMode(int mode)
551 {
552     ATRACE_CALL();
553     ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
554
555     switch (mode) {
556         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
557         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
558         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
559             break;
560         default:
561             ALOGE("unknown scaling mode: %d", mode);
562             return BAD_VALUE;
563     }
564
565     Mutex::Autolock lock(mMutex);
566     mScalingMode = mode;
567     return NO_ERROR;
568 }
569
570 int SurfaceTextureClient::setBuffersTransform(int transform)
571 {
572     ATRACE_CALL();
573     ALOGV("SurfaceTextureClient::setBuffersTransform");
574     Mutex::Autolock lock(mMutex);
575     mTransform = transform;
576     return NO_ERROR;
577 }
578
579 int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
580 {
581     ALOGV("SurfaceTextureClient::setBuffersTimestamp");
582     Mutex::Autolock lock(mMutex);
583     mTimestamp = timestamp;
584     return NO_ERROR;
585 }
586
587 void SurfaceTextureClient::freeAllBuffers() {
588     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
589         mSlots[i] = 0;
590     }
591 }
592
593 // ----------------------------------------------------------------------
594 // the lock/unlock APIs must be used from the same thread
595
596 static status_t copyBlt(
597         const sp<GraphicBuffer>& dst,
598         const sp<GraphicBuffer>& src,
599         const Region& reg)
600 {
601     // src and dst with, height and format must be identical. no verification
602     // is done here.
603     status_t err;
604     uint8_t const * src_bits = NULL;
605     err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
606     ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
607
608     uint8_t* dst_bits = NULL;
609     err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
610     ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
611
612     Region::const_iterator head(reg.begin());
613     Region::const_iterator tail(reg.end());
614     if (head != tail && src_bits && dst_bits) {
615         const size_t bpp = bytesPerPixel(src->format);
616         const size_t dbpr = dst->stride * bpp;
617         const size_t sbpr = src->stride * bpp;
618
619         while (head != tail) {
620             const Rect& r(*head++);
621             ssize_t h = r.height();
622             if (h <= 0) continue;
623             size_t size = r.width() * bpp;
624             uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
625             uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
626             if (dbpr==sbpr && size==sbpr) {
627                 size *= h;
628                 h = 1;
629             }
630             do {
631                 memcpy(d, s, size);
632                 d += dbpr;
633                 s += sbpr;
634             } while (--h > 0);
635         }
636     }
637
638     if (src_bits)
639         src->unlock();
640
641     if (dst_bits)
642         dst->unlock();
643
644     return err;
645 }
646
647 // ----------------------------------------------------------------------------
648
649 status_t SurfaceTextureClient::lock(
650         ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
651 {
652     if (mLockedBuffer != 0) {
653         ALOGE("Surface::lock failed, already locked");
654         return INVALID_OPERATION;
655     }
656
657     if (!mConnectedToCpu) {
658         int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
659         if (err) {
660             return err;
661         }
662         // we're intending to do software rendering from this point
663         setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
664     }
665
666     ANativeWindowBuffer* out;
667     status_t err = dequeueBuffer(&out);
668     ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
669     if (err == NO_ERROR) {
670         sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
671         err = lockBuffer(backBuffer.get());
672         ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
673                 backBuffer->handle, strerror(-err));
674         if (err == NO_ERROR) {
675             const Rect bounds(backBuffer->width, backBuffer->height);
676
677             Region newDirtyRegion;
678             if (inOutDirtyBounds) {
679                 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
680                 newDirtyRegion.andSelf(bounds);
681             } else {
682                 newDirtyRegion.set(bounds);
683             }
684
685             // figure out if we can copy the frontbuffer back
686             const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
687             const bool canCopyBack = (frontBuffer != 0 &&
688                     backBuffer->width  == frontBuffer->width &&
689                     backBuffer->height == frontBuffer->height &&
690                     backBuffer->format == frontBuffer->format);
691
692             if (canCopyBack) {
693                 // copy the area that is invalid and not repainted this round
694                 const Region copyback(mOldDirtyRegion.subtract(newDirtyRegion));
695                 if (!copyback.isEmpty())
696                     copyBlt(backBuffer, frontBuffer, copyback);
697             } else {
698                 // if we can't copy-back anything, modify the user's dirty
699                 // region to make sure they redraw the whole buffer
700                 newDirtyRegion.set(bounds);
701             }
702
703             // keep track of the are of the buffer that is "clean"
704             // (ie: that will be redrawn)
705             mOldDirtyRegion = newDirtyRegion;
706
707             if (inOutDirtyBounds) {
708                 *inOutDirtyBounds = newDirtyRegion.getBounds();
709             }
710
711             void* vaddr;
712             status_t res = backBuffer->lock(
713                     GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
714                     newDirtyRegion.bounds(), &vaddr);
715
716             ALOGW_IF(res, "failed locking buffer (handle = %p)",
717                     backBuffer->handle);
718
719             mLockedBuffer = backBuffer;
720             outBuffer->width  = backBuffer->width;
721             outBuffer->height = backBuffer->height;
722             outBuffer->stride = backBuffer->stride;
723             outBuffer->format = backBuffer->format;
724             outBuffer->bits   = vaddr;
725         }
726     }
727     return err;
728 }
729
730 status_t SurfaceTextureClient::unlockAndPost()
731 {
732     if (mLockedBuffer == 0) {
733         ALOGE("Surface::unlockAndPost failed, no locked buffer");
734         return INVALID_OPERATION;
735     }
736
737     status_t err = mLockedBuffer->unlock();
738     ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
739
740     err = queueBuffer(mLockedBuffer.get());
741     ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
742             mLockedBuffer->handle, strerror(-err));
743
744     mPostedBuffer = mLockedBuffer;
745     mLockedBuffer = 0;
746     return err;
747 }
748
749 }; // namespace android