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