SurfaceComposerClient.cpp revision 4125a4ffaf374ca9c0773f256998557d3325343e
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceComposerClient"
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Errors.h>
23#include <utils/Log.h>
24#include <utils/Singleton.h>
25#include <utils/SortedVector.h>
26#include <utils/String8.h>
27#include <utils/threads.h>
28
29#include <binder/IMemory.h>
30#include <binder/IServiceManager.h>
31
32#include <ui/DisplayInfo.h>
33
34#include <gui/CpuConsumer.h>
35#include <gui/IGraphicBufferProducer.h>
36#include <gui/ISurfaceComposer.h>
37#include <gui/ISurfaceComposerClient.h>
38#include <gui/SurfaceComposerClient.h>
39
40#include <private/gui/ComposerService.h>
41#include <private/gui/LayerState.h>
42
43namespace android {
44// ---------------------------------------------------------------------------
45
46ANDROID_SINGLETON_STATIC_INSTANCE(ComposerService);
47
48ComposerService::ComposerService()
49: Singleton<ComposerService>() {
50    Mutex::Autolock _l(mLock);
51    connectLocked();
52}
53
54void ComposerService::connectLocked() {
55    const String16 name("SurfaceFlinger");
56    while (getService(name, &mComposerService) != NO_ERROR) {
57        usleep(250000);
58    }
59    assert(mComposerService != NULL);
60
61    // Create the death listener.
62    class DeathObserver : public IBinder::DeathRecipient {
63        ComposerService& mComposerService;
64        virtual void binderDied(const wp<IBinder>& who) {
65            ALOGW("ComposerService remote (surfaceflinger) died [%p]",
66                  who.unsafe_get());
67            mComposerService.composerServiceDied();
68        }
69     public:
70        DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
71    };
72
73    mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
74    mComposerService->asBinder()->linkToDeath(mDeathObserver);
75}
76
77/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
78    ComposerService& instance = ComposerService::getInstance();
79    Mutex::Autolock _l(instance.mLock);
80    if (instance.mComposerService == NULL) {
81        ComposerService::getInstance().connectLocked();
82        assert(instance.mComposerService != NULL);
83        ALOGD("ComposerService reconnected");
84    }
85    return instance.mComposerService;
86}
87
88void ComposerService::composerServiceDied()
89{
90    Mutex::Autolock _l(mLock);
91    mComposerService = NULL;
92    mDeathObserver = NULL;
93}
94
95// ---------------------------------------------------------------------------
96
97static inline
98int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
99    if (lhs.client < rhs.client)  return -1;
100    if (lhs.client > rhs.client)  return 1;
101    if (lhs.state.surface < rhs.state.surface)  return -1;
102    if (lhs.state.surface > rhs.state.surface)  return 1;
103    return 0;
104}
105
106static inline
107int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
108    return compare_type(lhs.token, rhs.token);
109}
110
111class Composer : public Singleton<Composer>
112{
113    friend class Singleton<Composer>;
114
115    mutable Mutex               mLock;
116    SortedVector<ComposerState> mComposerStates;
117    SortedVector<DisplayState > mDisplayStates;
118    uint32_t                    mForceSynchronous;
119    uint32_t                    mTransactionNestCount;
120    bool                        mAnimation;
121
122    Composer() : Singleton<Composer>(),
123        mForceSynchronous(0), mTransactionNestCount(0),
124        mAnimation(false)
125    { }
126
127    void openGlobalTransactionImpl();
128    void closeGlobalTransactionImpl(bool synchronous);
129    void setAnimationTransactionImpl();
130
131    layer_state_t* getLayerStateLocked(
132            const sp<SurfaceComposerClient>& client, const sp<IBinder>& id);
133
134    DisplayState& getDisplayStateLocked(const sp<IBinder>& token);
135
136public:
137    sp<IBinder> createDisplay(const String8& displayName, bool secure);
138    void destroyDisplay(const sp<IBinder>& display);
139    sp<IBinder> getBuiltInDisplay(int32_t id);
140
141    status_t setPosition(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
142            float x, float y);
143    status_t setSize(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
144            uint32_t w, uint32_t h);
145    status_t setLayer(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
146            int32_t z);
147    status_t setFlags(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
148            uint32_t flags, uint32_t mask);
149    status_t setTransparentRegionHint(
150            const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
151            const Region& transparentRegion);
152    status_t setAlpha(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
153            float alpha);
154    status_t setMatrix(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
155            float dsdx, float dtdx, float dsdy, float dtdy);
156    status_t setOrientation(int orientation);
157    status_t setCrop(const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
158            const Rect& crop);
159    status_t setLayerStack(const sp<SurfaceComposerClient>& client,
160            const sp<IBinder>& id, uint32_t layerStack);
161
162    void setDisplaySurface(const sp<IBinder>& token,
163            const sp<IGraphicBufferProducer>& bufferProducer);
164    void setDisplayLayerStack(const sp<IBinder>& token, uint32_t layerStack);
165    void setDisplayProjection(const sp<IBinder>& token,
166            uint32_t orientation,
167            const Rect& layerStackRect,
168            const Rect& displayRect);
169
170    static void setAnimationTransaction() {
171        Composer::getInstance().setAnimationTransactionImpl();
172    }
173
174    static void openGlobalTransaction() {
175        Composer::getInstance().openGlobalTransactionImpl();
176    }
177
178    static void closeGlobalTransaction(bool synchronous) {
179        Composer::getInstance().closeGlobalTransactionImpl(synchronous);
180    }
181};
182
183ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
184
185// ---------------------------------------------------------------------------
186
187sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
188    return ComposerService::getComposerService()->createDisplay(displayName,
189            secure);
190}
191
192void Composer::destroyDisplay(const sp<IBinder>& display) {
193    return ComposerService::getComposerService()->destroyDisplay(display);
194}
195
196sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
197    return ComposerService::getComposerService()->getBuiltInDisplay(id);
198}
199
200void Composer::openGlobalTransactionImpl() {
201    { // scope for the lock
202        Mutex::Autolock _l(mLock);
203        mTransactionNestCount += 1;
204    }
205}
206
207void Composer::closeGlobalTransactionImpl(bool synchronous) {
208    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
209
210    Vector<ComposerState> transaction;
211    Vector<DisplayState> displayTransaction;
212    uint32_t flags = 0;
213
214    { // scope for the lock
215        Mutex::Autolock _l(mLock);
216        mForceSynchronous |= synchronous;
217        if (!mTransactionNestCount) {
218            ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
219                    "call to openGlobalTransaction().");
220        } else if (--mTransactionNestCount) {
221            return;
222        }
223
224        transaction = mComposerStates;
225        mComposerStates.clear();
226
227        displayTransaction = mDisplayStates;
228        mDisplayStates.clear();
229
230        if (mForceSynchronous) {
231            flags |= ISurfaceComposer::eSynchronous;
232        }
233        if (mAnimation) {
234            flags |= ISurfaceComposer::eAnimation;
235        }
236
237        mForceSynchronous = false;
238        mAnimation = false;
239    }
240
241   sm->setTransactionState(transaction, displayTransaction, flags);
242}
243
244void Composer::setAnimationTransactionImpl() {
245    Mutex::Autolock _l(mLock);
246    mAnimation = true;
247}
248
249layer_state_t* Composer::getLayerStateLocked(
250        const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
251
252    ComposerState s;
253    s.client = client->mClient;
254    s.state.surface = id;
255
256    ssize_t index = mComposerStates.indexOf(s);
257    if (index < 0) {
258        // we don't have it, add an initialized layer_state to our list
259        index = mComposerStates.add(s);
260    }
261
262    ComposerState* const out = mComposerStates.editArray();
263    return &(out[index].state);
264}
265
266status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
267        const sp<IBinder>& id, float x, float y) {
268    Mutex::Autolock _l(mLock);
269    layer_state_t* s = getLayerStateLocked(client, id);
270    if (!s)
271        return BAD_INDEX;
272    s->what |= layer_state_t::ePositionChanged;
273    s->x = x;
274    s->y = y;
275    return NO_ERROR;
276}
277
278status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
279        const sp<IBinder>& id, uint32_t w, uint32_t h) {
280    Mutex::Autolock _l(mLock);
281    layer_state_t* s = getLayerStateLocked(client, id);
282    if (!s)
283        return BAD_INDEX;
284    s->what |= layer_state_t::eSizeChanged;
285    s->w = w;
286    s->h = h;
287
288    // Resizing a surface makes the transaction synchronous.
289    mForceSynchronous = true;
290
291    return NO_ERROR;
292}
293
294status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
295        const sp<IBinder>& id, int32_t z) {
296    Mutex::Autolock _l(mLock);
297    layer_state_t* s = getLayerStateLocked(client, id);
298    if (!s)
299        return BAD_INDEX;
300    s->what |= layer_state_t::eLayerChanged;
301    s->z = z;
302    return NO_ERROR;
303}
304
305status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
306        const sp<IBinder>& id, uint32_t flags,
307        uint32_t mask) {
308    Mutex::Autolock _l(mLock);
309    layer_state_t* s = getLayerStateLocked(client, id);
310    if (!s)
311        return BAD_INDEX;
312    if (mask & layer_state_t::eLayerOpaque) {
313        s->what |= layer_state_t::eOpacityChanged;
314    }
315    if (mask & layer_state_t::eLayerHidden) {
316        s->what |= layer_state_t::eVisibilityChanged;
317    }
318    s->flags &= ~mask;
319    s->flags |= (flags & mask);
320    s->mask |= mask;
321    return NO_ERROR;
322}
323
324status_t Composer::setTransparentRegionHint(
325        const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
326        const Region& transparentRegion) {
327    Mutex::Autolock _l(mLock);
328    layer_state_t* s = getLayerStateLocked(client, id);
329    if (!s)
330        return BAD_INDEX;
331    s->what |= layer_state_t::eTransparentRegionChanged;
332    s->transparentRegion = transparentRegion;
333    return NO_ERROR;
334}
335
336status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
337        const sp<IBinder>& id, float alpha) {
338    Mutex::Autolock _l(mLock);
339    layer_state_t* s = getLayerStateLocked(client, id);
340    if (!s)
341        return BAD_INDEX;
342    s->what |= layer_state_t::eAlphaChanged;
343    s->alpha = alpha;
344    return NO_ERROR;
345}
346
347status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
348        const sp<IBinder>& id, uint32_t layerStack) {
349    Mutex::Autolock _l(mLock);
350    layer_state_t* s = getLayerStateLocked(client, id);
351    if (!s)
352        return BAD_INDEX;
353    s->what |= layer_state_t::eLayerStackChanged;
354    s->layerStack = layerStack;
355    return NO_ERROR;
356}
357
358status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
359        const sp<IBinder>& id, float dsdx, float dtdx,
360        float dsdy, float dtdy) {
361    Mutex::Autolock _l(mLock);
362    layer_state_t* s = getLayerStateLocked(client, id);
363    if (!s)
364        return BAD_INDEX;
365    s->what |= layer_state_t::eMatrixChanged;
366    layer_state_t::matrix22_t matrix;
367    matrix.dsdx = dsdx;
368    matrix.dtdx = dtdx;
369    matrix.dsdy = dsdy;
370    matrix.dtdy = dtdy;
371    s->matrix = matrix;
372    return NO_ERROR;
373}
374
375status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
376        const sp<IBinder>& id, const Rect& crop) {
377    Mutex::Autolock _l(mLock);
378    layer_state_t* s = getLayerStateLocked(client, id);
379    if (!s)
380        return BAD_INDEX;
381    s->what |= layer_state_t::eCropChanged;
382    s->crop = crop;
383    return NO_ERROR;
384}
385
386// ---------------------------------------------------------------------------
387
388DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
389    DisplayState s;
390    s.token = token;
391    ssize_t index = mDisplayStates.indexOf(s);
392    if (index < 0) {
393        // we don't have it, add an initialized layer_state to our list
394        s.what = 0;
395        index = mDisplayStates.add(s);
396    }
397    return mDisplayStates.editItemAt(index);
398}
399
400void Composer::setDisplaySurface(const sp<IBinder>& token,
401        const sp<IGraphicBufferProducer>& bufferProducer) {
402    Mutex::Autolock _l(mLock);
403    DisplayState& s(getDisplayStateLocked(token));
404    s.surface = bufferProducer;
405    s.what |= DisplayState::eSurfaceChanged;
406}
407
408void Composer::setDisplayLayerStack(const sp<IBinder>& token,
409        uint32_t layerStack) {
410    Mutex::Autolock _l(mLock);
411    DisplayState& s(getDisplayStateLocked(token));
412    s.layerStack = layerStack;
413    s.what |= DisplayState::eLayerStackChanged;
414}
415
416void Composer::setDisplayProjection(const sp<IBinder>& token,
417        uint32_t orientation,
418        const Rect& layerStackRect,
419        const Rect& displayRect) {
420    Mutex::Autolock _l(mLock);
421    DisplayState& s(getDisplayStateLocked(token));
422    s.orientation = orientation;
423    s.viewport = layerStackRect;
424    s.frame = displayRect;
425    s.what |= DisplayState::eDisplayProjectionChanged;
426    mForceSynchronous = true; // TODO: do we actually still need this?
427}
428
429// ---------------------------------------------------------------------------
430
431SurfaceComposerClient::SurfaceComposerClient()
432    : mStatus(NO_INIT), mComposer(Composer::getInstance())
433{
434}
435
436void SurfaceComposerClient::onFirstRef() {
437    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
438    if (sm != 0) {
439        sp<ISurfaceComposerClient> conn = sm->createConnection();
440        if (conn != 0) {
441            mClient = conn;
442            mStatus = NO_ERROR;
443        }
444    }
445}
446
447SurfaceComposerClient::~SurfaceComposerClient() {
448    dispose();
449}
450
451status_t SurfaceComposerClient::initCheck() const {
452    return mStatus;
453}
454
455sp<IBinder> SurfaceComposerClient::connection() const {
456    return (mClient != 0) ? mClient->asBinder() : 0;
457}
458
459status_t SurfaceComposerClient::linkToComposerDeath(
460        const sp<IBinder::DeathRecipient>& recipient,
461        void* cookie, uint32_t flags) {
462    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
463    return sm->asBinder()->linkToDeath(recipient, cookie, flags);
464}
465
466void SurfaceComposerClient::dispose() {
467    // this can be called more than once.
468    sp<ISurfaceComposerClient> client;
469    Mutex::Autolock _lm(mLock);
470    if (mClient != 0) {
471        client = mClient; // hold ref while lock is held
472        mClient.clear();
473    }
474    mStatus = NO_INIT;
475}
476
477sp<SurfaceControl> SurfaceComposerClient::createSurface(
478        const String8& name,
479        uint32_t w,
480        uint32_t h,
481        PixelFormat format,
482        uint32_t flags)
483{
484    sp<SurfaceControl> sur;
485    if (mStatus == NO_ERROR) {
486        sp<IBinder> handle;
487        sp<IGraphicBufferProducer> gbp;
488        status_t err = mClient->createSurface(name, w, h, format, flags,
489                &handle, &gbp);
490        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
491        if (err == NO_ERROR) {
492            sur = new SurfaceControl(this, handle, gbp);
493        }
494    }
495    return sur;
496}
497
498sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
499        bool secure) {
500    return Composer::getInstance().createDisplay(displayName, secure);
501}
502
503void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
504    Composer::getInstance().destroyDisplay(display);
505}
506
507sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
508    return Composer::getInstance().getBuiltInDisplay(id);
509}
510
511status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
512    if (mStatus != NO_ERROR)
513        return mStatus;
514    status_t err = mClient->destroySurface(sid);
515    return err;
516}
517
518inline Composer& SurfaceComposerClient::getComposer() {
519    return mComposer;
520}
521
522// ----------------------------------------------------------------------------
523
524void SurfaceComposerClient::openGlobalTransaction() {
525    Composer::openGlobalTransaction();
526}
527
528void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
529    Composer::closeGlobalTransaction(synchronous);
530}
531
532void SurfaceComposerClient::setAnimationTransaction() {
533    Composer::setAnimationTransaction();
534}
535
536// ----------------------------------------------------------------------------
537
538status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
539    return getComposer().setCrop(this, id, crop);
540}
541
542status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
543    return getComposer().setPosition(this, id, x, y);
544}
545
546status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
547    return getComposer().setSize(this, id, w, h);
548}
549
550status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
551    return getComposer().setLayer(this, id, z);
552}
553
554status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
555    return getComposer().setFlags(this, id,
556            layer_state_t::eLayerHidden,
557            layer_state_t::eLayerHidden);
558}
559
560status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
561    return getComposer().setFlags(this, id,
562            0,
563            layer_state_t::eLayerHidden);
564}
565
566status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
567        uint32_t mask) {
568    return getComposer().setFlags(this, id, flags, mask);
569}
570
571status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
572        const Region& transparentRegion) {
573    return getComposer().setTransparentRegionHint(this, id, transparentRegion);
574}
575
576status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
577    return getComposer().setAlpha(this, id, alpha);
578}
579
580status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
581    return getComposer().setLayerStack(this, id, layerStack);
582}
583
584status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
585        float dsdy, float dtdy) {
586    return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
587}
588
589// ----------------------------------------------------------------------------
590
591void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
592        const sp<IGraphicBufferProducer>& bufferProducer) {
593    Composer::getInstance().setDisplaySurface(token, bufferProducer);
594}
595
596void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
597        uint32_t layerStack) {
598    Composer::getInstance().setDisplayLayerStack(token, layerStack);
599}
600
601void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
602        uint32_t orientation,
603        const Rect& layerStackRect,
604        const Rect& displayRect) {
605    Composer::getInstance().setDisplayProjection(token, orientation,
606            layerStackRect, displayRect);
607}
608
609// ----------------------------------------------------------------------------
610
611status_t SurfaceComposerClient::getDisplayInfo(
612        const sp<IBinder>& display, DisplayInfo* info)
613{
614    return ComposerService::getComposerService()->getDisplayInfo(display, info);
615}
616
617void SurfaceComposerClient::blankDisplay(const sp<IBinder>& token) {
618    ComposerService::getComposerService()->blank(token);
619}
620
621void SurfaceComposerClient::unblankDisplay(const sp<IBinder>& token) {
622    ComposerService::getComposerService()->unblank(token);
623}
624
625// ----------------------------------------------------------------------------
626
627status_t ScreenshotClient::capture(
628        const sp<IBinder>& display,
629        const sp<IGraphicBufferProducer>& producer,
630        uint32_t reqWidth, uint32_t reqHeight,
631        uint32_t minLayerZ, uint32_t maxLayerZ) {
632    sp<ISurfaceComposer> s(ComposerService::getComposerService());
633    if (s == NULL) return NO_INIT;
634    return s->captureScreen(display, producer,
635            reqWidth, reqHeight, minLayerZ, maxLayerZ);
636}
637
638ScreenshotClient::ScreenshotClient()
639    : mHaveBuffer(false) {
640    memset(&mBuffer, 0, sizeof(mBuffer));
641}
642
643ScreenshotClient::~ScreenshotClient() {
644    ScreenshotClient::release();
645}
646
647sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
648    if (mCpuConsumer == NULL) {
649        mBufferQueue = new BufferQueue();
650        mCpuConsumer = new CpuConsumer(mBufferQueue, 1);
651        mCpuConsumer->setName(String8("ScreenshotClient"));
652    }
653    return mCpuConsumer;
654}
655
656status_t ScreenshotClient::update(const sp<IBinder>& display,
657        uint32_t reqWidth, uint32_t reqHeight,
658        uint32_t minLayerZ, uint32_t maxLayerZ) {
659    sp<ISurfaceComposer> s(ComposerService::getComposerService());
660    if (s == NULL) return NO_INIT;
661    sp<CpuConsumer> cpuConsumer = getCpuConsumer();
662
663    if (mHaveBuffer) {
664        mCpuConsumer->unlockBuffer(mBuffer);
665        memset(&mBuffer, 0, sizeof(mBuffer));
666        mHaveBuffer = false;
667    }
668
669    status_t err = s->captureScreen(display, mBufferQueue,
670            reqWidth, reqHeight, minLayerZ, maxLayerZ);
671
672    if (err == NO_ERROR) {
673        err = mCpuConsumer->lockNextBuffer(&mBuffer);
674        if (err == NO_ERROR) {
675            mHaveBuffer = true;
676        }
677    }
678    return err;
679}
680
681status_t ScreenshotClient::update(const sp<IBinder>& display) {
682    return ScreenshotClient::update(display, 0, 0, 0, -1UL);
683}
684
685status_t ScreenshotClient::update(const sp<IBinder>& display,
686        uint32_t reqWidth, uint32_t reqHeight) {
687    return ScreenshotClient::update(display, reqWidth, reqHeight, 0, -1UL);
688}
689
690void ScreenshotClient::release() {
691    if (mHaveBuffer) {
692        mCpuConsumer->unlockBuffer(mBuffer);
693        memset(&mBuffer, 0, sizeof(mBuffer));
694        mHaveBuffer = false;
695    }
696    mCpuConsumer.clear();
697}
698
699void const* ScreenshotClient::getPixels() const {
700    return mBuffer.data;
701}
702
703uint32_t ScreenshotClient::getWidth() const {
704    return mBuffer.width;
705}
706
707uint32_t ScreenshotClient::getHeight() const {
708    return mBuffer.height;
709}
710
711PixelFormat ScreenshotClient::getFormat() const {
712    return mBuffer.format;
713}
714
715uint32_t ScreenshotClient::getStride() const {
716    return mBuffer.stride;
717}
718
719size_t ScreenshotClient::getSize() const {
720    return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
721}
722
723// ----------------------------------------------------------------------------
724}; // namespace android
725