OSDN Git Service

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