OSDN Git Service

Merge "Support tokenizing arbitrary content."
[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].buffer);
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].buffer != NULL &&
208                 mSlots[i].buffer->handle == buffer->handle) {
209             return i;
210         }
211     }
212     ALOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle);
213     return BAD_VALUE;
214 }
215
216 int SurfaceTextureClient::lockBuffer(android_native_buffer_t* buffer) {
217     ALOGV("SurfaceTextureClient::lockBuffer");
218     Mutex::Autolock lock(mMutex);
219     return OK;
220 }
221
222 int SurfaceTextureClient::queueBuffer(android_native_buffer_t* buffer) {
223     ATRACE_CALL();
224     ALOGV("SurfaceTextureClient::queueBuffer");
225     Mutex::Autolock lock(mMutex);
226     int64_t timestamp;
227     if (mTimestamp == NATIVE_WINDOW_TIMESTAMP_AUTO) {
228         timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
229         ALOGV("SurfaceTextureClient::queueBuffer making up timestamp: %.2f ms",
230              timestamp / 1000000.f);
231     } else {
232         timestamp = mTimestamp;
233     }
234     int i = getSlotFromBufferLocked(buffer);
235     if (i < 0) {
236         return i;
237     }
238
239     ISurfaceTexture::QueueBufferOutput output;
240     ISurfaceTexture::QueueBufferInput input(timestamp,
241             mCrop, mScalingMode, mTransform);
242     status_t err = mSurfaceTexture->queueBuffer(i, input, &output);
243     if (err != OK)  {
244         ALOGE("queueBuffer: error queuing buffer to SurfaceTexture, %d", err);
245     }
246     output.deflate(&mDefaultWidth, &mDefaultHeight, &mTransformHint);
247     return err;
248 }
249
250 int SurfaceTextureClient::query(int what, int* value) const {
251     ATRACE_CALL();
252     ALOGV("SurfaceTextureClient::query");
253     { // scope for the lock
254         Mutex::Autolock lock(mMutex);
255         switch (what) {
256             case NATIVE_WINDOW_FORMAT:
257                 if (mReqFormat) {
258                     *value = mReqFormat;
259                     return NO_ERROR;
260                 }
261                 break;
262             case NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER:
263                 {
264                     sp<ISurfaceComposer> composer(
265                             ComposerService::getComposerService());
266                     if (composer->authenticateSurfaceTexture(mSurfaceTexture)) {
267                         *value = 1;
268                     } else {
269                         *value = 0;
270                     }
271                 }
272                 return NO_ERROR;
273             case NATIVE_WINDOW_CONCRETE_TYPE:
274                 *value = NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT;
275                 return NO_ERROR;
276             case NATIVE_WINDOW_DEFAULT_WIDTH:
277                 *value = mUserWidth ? mUserWidth : mDefaultWidth;
278                 return NO_ERROR;
279             case NATIVE_WINDOW_DEFAULT_HEIGHT:
280                 *value = mUserHeight ? mUserHeight : mDefaultHeight;
281                 return NO_ERROR;
282             case NATIVE_WINDOW_TRANSFORM_HINT:
283                 *value = mTransformHint;
284                 return NO_ERROR;
285         }
286     }
287     return mSurfaceTexture->query(what, value);
288 }
289
290 int SurfaceTextureClient::perform(int operation, va_list args)
291 {
292     int res = NO_ERROR;
293     switch (operation) {
294     case NATIVE_WINDOW_CONNECT:
295         // deprecated. must return NO_ERROR.
296         break;
297     case NATIVE_WINDOW_DISCONNECT:
298         // deprecated. must return NO_ERROR.
299         break;
300     case NATIVE_WINDOW_SET_USAGE:
301         res = dispatchSetUsage(args);
302         break;
303     case NATIVE_WINDOW_SET_CROP:
304         res = dispatchSetCrop(args);
305         break;
306     case NATIVE_WINDOW_SET_BUFFER_COUNT:
307         res = dispatchSetBufferCount(args);
308         break;
309     case NATIVE_WINDOW_SET_BUFFERS_GEOMETRY:
310         res = dispatchSetBuffersGeometry(args);
311         break;
312     case NATIVE_WINDOW_SET_BUFFERS_TRANSFORM:
313         res = dispatchSetBuffersTransform(args);
314         break;
315     case NATIVE_WINDOW_SET_BUFFERS_TIMESTAMP:
316         res = dispatchSetBuffersTimestamp(args);
317         break;
318     case NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS:
319         res = dispatchSetBuffersDimensions(args);
320         break;
321     case NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS:
322         res = dispatchSetBuffersUserDimensions(args);
323         break;
324     case NATIVE_WINDOW_SET_BUFFERS_FORMAT:
325         res = dispatchSetBuffersFormat(args);
326         break;
327     case NATIVE_WINDOW_LOCK:
328         res = dispatchLock(args);
329         break;
330     case NATIVE_WINDOW_UNLOCK_AND_POST:
331         res = dispatchUnlockAndPost(args);
332         break;
333     case NATIVE_WINDOW_SET_SCALING_MODE:
334         res = dispatchSetScalingMode(args);
335         break;
336     case NATIVE_WINDOW_API_CONNECT:
337         res = dispatchConnect(args);
338         break;
339     case NATIVE_WINDOW_API_DISCONNECT:
340         res = dispatchDisconnect(args);
341         break;
342     default:
343         res = NAME_NOT_FOUND;
344         break;
345     }
346     return res;
347 }
348
349 int SurfaceTextureClient::dispatchConnect(va_list args) {
350     int api = va_arg(args, int);
351     return connect(api);
352 }
353
354 int SurfaceTextureClient::dispatchDisconnect(va_list args) {
355     int api = va_arg(args, int);
356     return disconnect(api);
357 }
358
359 int SurfaceTextureClient::dispatchSetUsage(va_list args) {
360     int usage = va_arg(args, int);
361     return setUsage(usage);
362 }
363
364 int SurfaceTextureClient::dispatchSetCrop(va_list args) {
365     android_native_rect_t const* rect = va_arg(args, android_native_rect_t*);
366     return setCrop(reinterpret_cast<Rect const*>(rect));
367 }
368
369 int SurfaceTextureClient::dispatchSetBufferCount(va_list args) {
370     size_t bufferCount = va_arg(args, size_t);
371     return setBufferCount(bufferCount);
372 }
373
374 int SurfaceTextureClient::dispatchSetBuffersGeometry(va_list args) {
375     int w = va_arg(args, int);
376     int h = va_arg(args, int);
377     int f = va_arg(args, int);
378     int err = setBuffersDimensions(w, h);
379     if (err != 0) {
380         return err;
381     }
382     return setBuffersFormat(f);
383 }
384
385 int SurfaceTextureClient::dispatchSetBuffersDimensions(va_list args) {
386     int w = va_arg(args, int);
387     int h = va_arg(args, int);
388     return setBuffersDimensions(w, h);
389 }
390
391 int SurfaceTextureClient::dispatchSetBuffersUserDimensions(va_list args) {
392     int w = va_arg(args, int);
393     int h = va_arg(args, int);
394     return setBuffersUserDimensions(w, h);
395 }
396
397 int SurfaceTextureClient::dispatchSetBuffersFormat(va_list args) {
398     int f = va_arg(args, int);
399     return setBuffersFormat(f);
400 }
401
402 int SurfaceTextureClient::dispatchSetScalingMode(va_list args) {
403     int m = va_arg(args, int);
404     return setScalingMode(m);
405 }
406
407 int SurfaceTextureClient::dispatchSetBuffersTransform(va_list args) {
408     int transform = va_arg(args, int);
409     return setBuffersTransform(transform);
410 }
411
412 int SurfaceTextureClient::dispatchSetBuffersTimestamp(va_list args) {
413     int64_t timestamp = va_arg(args, int64_t);
414     return setBuffersTimestamp(timestamp);
415 }
416
417 int SurfaceTextureClient::dispatchLock(va_list args) {
418     ANativeWindow_Buffer* outBuffer = va_arg(args, ANativeWindow_Buffer*);
419     ARect* inOutDirtyBounds = va_arg(args, ARect*);
420     return lock(outBuffer, inOutDirtyBounds);
421 }
422
423 int SurfaceTextureClient::dispatchUnlockAndPost(va_list args) {
424     return unlockAndPost();
425 }
426
427
428 int SurfaceTextureClient::connect(int api) {
429     ATRACE_CALL();
430     ALOGV("SurfaceTextureClient::connect");
431     Mutex::Autolock lock(mMutex);
432     int err = mSurfaceTexture->connect(api,
433             &mDefaultWidth, &mDefaultHeight, &mTransformHint);
434     if (!err && api == NATIVE_WINDOW_API_CPU) {
435         mConnectedToCpu = true;
436     }
437     return err;
438 }
439
440 int SurfaceTextureClient::disconnect(int api) {
441     ATRACE_CALL();
442     ALOGV("SurfaceTextureClient::disconnect");
443     Mutex::Autolock lock(mMutex);
444     freeAllBuffers();
445     int err = mSurfaceTexture->disconnect(api);
446     if (!err) {
447         mReqFormat = 0;
448         mReqWidth = 0;
449         mReqHeight = 0;
450         mReqUsage = 0;
451         mCrop.clear();
452         mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
453         mTransform = 0;
454         if (api == NATIVE_WINDOW_API_CPU) {
455             mConnectedToCpu = false;
456         }
457     }
458     return err;
459 }
460
461 int SurfaceTextureClient::setUsage(uint32_t reqUsage)
462 {
463     ALOGV("SurfaceTextureClient::setUsage");
464     Mutex::Autolock lock(mMutex);
465     mReqUsage = reqUsage;
466     return OK;
467 }
468
469 int SurfaceTextureClient::setCrop(Rect const* rect)
470 {
471     ATRACE_CALL();
472     ALOGV("SurfaceTextureClient::setCrop");
473
474     Rect realRect;
475     if (rect == NULL || rect->isEmpty()) {
476         realRect.clear();
477     } else {
478         realRect = *rect;
479     }
480
481     Mutex::Autolock lock(mMutex);
482     mCrop = *rect;
483     return NO_ERROR;
484 }
485
486 int SurfaceTextureClient::setBufferCount(int bufferCount)
487 {
488     ATRACE_CALL();
489     ALOGV("SurfaceTextureClient::setBufferCount");
490     Mutex::Autolock lock(mMutex);
491
492     status_t err = mSurfaceTexture->setBufferCount(bufferCount);
493     ALOGE_IF(err, "ISurfaceTexture::setBufferCount(%d) returned %s",
494             bufferCount, strerror(-err));
495
496     if (err == NO_ERROR) {
497         freeAllBuffers();
498     }
499
500     return err;
501 }
502
503 int SurfaceTextureClient::setBuffersDimensions(int w, int h)
504 {
505     ATRACE_CALL();
506     ALOGV("SurfaceTextureClient::setBuffersDimensions");
507
508     if (w<0 || h<0)
509         return BAD_VALUE;
510
511     if ((w && !h) || (!w && h))
512         return BAD_VALUE;
513
514     Mutex::Autolock lock(mMutex);
515     mReqWidth = w;
516     mReqHeight = h;
517     mCrop.clear();
518     return NO_ERROR;
519 }
520
521 int SurfaceTextureClient::setBuffersUserDimensions(int w, int h)
522 {
523     ATRACE_CALL();
524     ALOGV("SurfaceTextureClient::setBuffersUserDimensions");
525
526     if (w<0 || h<0)
527         return BAD_VALUE;
528
529     if ((w && !h) || (!w && h))
530         return BAD_VALUE;
531
532     Mutex::Autolock lock(mMutex);
533     mUserWidth = w;
534     mUserHeight = h;
535     mCrop.clear();
536     return NO_ERROR;
537 }
538
539 int SurfaceTextureClient::setBuffersFormat(int format)
540 {
541     ALOGV("SurfaceTextureClient::setBuffersFormat");
542
543     if (format<0)
544         return BAD_VALUE;
545
546     Mutex::Autolock lock(mMutex);
547     mReqFormat = format;
548     return NO_ERROR;
549 }
550
551 int SurfaceTextureClient::setScalingMode(int mode)
552 {
553     ATRACE_CALL();
554     ALOGV("SurfaceTextureClient::setScalingMode(%d)", mode);
555
556     switch (mode) {
557         case NATIVE_WINDOW_SCALING_MODE_FREEZE:
558         case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
559         case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP:
560             break;
561         default:
562             ALOGE("unknown scaling mode: %d", mode);
563             return BAD_VALUE;
564     }
565
566     Mutex::Autolock lock(mMutex);
567     mScalingMode = mode;
568     return NO_ERROR;
569 }
570
571 int SurfaceTextureClient::setBuffersTransform(int transform)
572 {
573     ATRACE_CALL();
574     ALOGV("SurfaceTextureClient::setBuffersTransform");
575     Mutex::Autolock lock(mMutex);
576     mTransform = transform;
577     return NO_ERROR;
578 }
579
580 int SurfaceTextureClient::setBuffersTimestamp(int64_t timestamp)
581 {
582     ALOGV("SurfaceTextureClient::setBuffersTimestamp");
583     Mutex::Autolock lock(mMutex);
584     mTimestamp = timestamp;
585     return NO_ERROR;
586 }
587
588 void SurfaceTextureClient::freeAllBuffers() {
589     for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
590         mSlots[i].buffer = 0;
591     }
592 }
593
594 // ----------------------------------------------------------------------
595 // the lock/unlock APIs must be used from the same thread
596
597 static status_t copyBlt(
598         const sp<GraphicBuffer>& dst,
599         const sp<GraphicBuffer>& src,
600         const Region& reg)
601 {
602     // src and dst with, height and format must be identical. no verification
603     // is done here.
604     status_t err;
605     uint8_t const * src_bits = NULL;
606     err = src->lock(GRALLOC_USAGE_SW_READ_OFTEN, reg.bounds(), (void**)&src_bits);
607     ALOGE_IF(err, "error locking src buffer %s", strerror(-err));
608
609     uint8_t* dst_bits = NULL;
610     err = dst->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, reg.bounds(), (void**)&dst_bits);
611     ALOGE_IF(err, "error locking dst buffer %s", strerror(-err));
612
613     Region::const_iterator head(reg.begin());
614     Region::const_iterator tail(reg.end());
615     if (head != tail && src_bits && dst_bits) {
616         const size_t bpp = bytesPerPixel(src->format);
617         const size_t dbpr = dst->stride * bpp;
618         const size_t sbpr = src->stride * bpp;
619
620         while (head != tail) {
621             const Rect& r(*head++);
622             ssize_t h = r.height();
623             if (h <= 0) continue;
624             size_t size = r.width() * bpp;
625             uint8_t const * s = src_bits + (r.left + src->stride * r.top) * bpp;
626             uint8_t       * d = dst_bits + (r.left + dst->stride * r.top) * bpp;
627             if (dbpr==sbpr && size==sbpr) {
628                 size *= h;
629                 h = 1;
630             }
631             do {
632                 memcpy(d, s, size);
633                 d += dbpr;
634                 s += sbpr;
635             } while (--h > 0);
636         }
637     }
638
639     if (src_bits)
640         src->unlock();
641
642     if (dst_bits)
643         dst->unlock();
644
645     return err;
646 }
647
648 // ----------------------------------------------------------------------------
649
650 status_t SurfaceTextureClient::lock(
651         ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds)
652 {
653     if (mLockedBuffer != 0) {
654         ALOGE("Surface::lock failed, already locked");
655         return INVALID_OPERATION;
656     }
657
658     if (!mConnectedToCpu) {
659         int err = SurfaceTextureClient::connect(NATIVE_WINDOW_API_CPU);
660         if (err) {
661             return err;
662         }
663         // we're intending to do software rendering from this point
664         setUsage(GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
665     }
666
667     ANativeWindowBuffer* out;
668     status_t err = dequeueBuffer(&out);
669     ALOGE_IF(err, "dequeueBuffer failed (%s)", strerror(-err));
670     if (err == NO_ERROR) {
671         sp<GraphicBuffer> backBuffer(GraphicBuffer::getSelf(out));
672         err = lockBuffer(backBuffer.get());
673         ALOGE_IF(err, "lockBuffer (handle=%p) failed (%s)",
674                 backBuffer->handle, strerror(-err));
675         if (err == NO_ERROR) {
676             const Rect bounds(backBuffer->width, backBuffer->height);
677
678             Region newDirtyRegion;
679             if (inOutDirtyBounds) {
680                 newDirtyRegion.set(static_cast<Rect const&>(*inOutDirtyBounds));
681                 newDirtyRegion.andSelf(bounds);
682             } else {
683                 newDirtyRegion.set(bounds);
684             }
685
686             // figure out if we can copy the frontbuffer back
687             const sp<GraphicBuffer>& frontBuffer(mPostedBuffer);
688             const bool canCopyBack = (frontBuffer != 0 &&
689                     backBuffer->width  == frontBuffer->width &&
690                     backBuffer->height == frontBuffer->height &&
691                     backBuffer->format == frontBuffer->format);
692
693             if (canCopyBack) {
694                 // copy the area that is invalid and not repainted this round
695                 const Region copyback(mDirtyRegion.subtract(newDirtyRegion));
696                 if (!copyback.isEmpty())
697                     copyBlt(backBuffer, frontBuffer, copyback);
698             } else {
699                 // if we can't copy-back anything, modify the user's dirty
700                 // region to make sure they redraw the whole buffer
701                 newDirtyRegion.set(bounds);
702                 mDirtyRegion.clear();
703                 Mutex::Autolock lock(mMutex);
704                 for (size_t i=0 ; i<NUM_BUFFER_SLOTS ; i++) {
705                     mSlots[i].dirtyRegion.clear();
706                 }
707             }
708
709
710             { // scope for the lock
711                 Mutex::Autolock lock(mMutex);
712                 int backBufferSlot(getSlotFromBufferLocked(backBuffer.get()));
713                 if (backBufferSlot >= 0) {
714                     Region& dirtyRegion(mSlots[backBufferSlot].dirtyRegion);
715                     mDirtyRegion.subtract(dirtyRegion);
716                     dirtyRegion = newDirtyRegion;
717                 }
718             }
719
720             mDirtyRegion.orSelf(newDirtyRegion);
721             if (inOutDirtyBounds) {
722                 *inOutDirtyBounds = newDirtyRegion.getBounds();
723             }
724
725             void* vaddr;
726             status_t res = backBuffer->lock(
727                     GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
728                     newDirtyRegion.bounds(), &vaddr);
729
730             ALOGW_IF(res, "failed locking buffer (handle = %p)",
731                     backBuffer->handle);
732
733             mLockedBuffer = backBuffer;
734             outBuffer->width  = backBuffer->width;
735             outBuffer->height = backBuffer->height;
736             outBuffer->stride = backBuffer->stride;
737             outBuffer->format = backBuffer->format;
738             outBuffer->bits   = vaddr;
739         }
740     }
741     return err;
742 }
743
744 status_t SurfaceTextureClient::unlockAndPost()
745 {
746     if (mLockedBuffer == 0) {
747         ALOGE("Surface::unlockAndPost failed, no locked buffer");
748         return INVALID_OPERATION;
749     }
750
751     status_t err = mLockedBuffer->unlock();
752     ALOGE_IF(err, "failed unlocking buffer (%p)", mLockedBuffer->handle);
753
754     err = queueBuffer(mLockedBuffer.get());
755     ALOGE_IF(err, "queueBuffer (handle=%p) failed (%s)",
756             mLockedBuffer->handle, strerror(-err));
757
758     mPostedBuffer = mLockedBuffer;
759     mLockedBuffer = 0;
760     return err;
761 }
762
763 }; // namespace android