SurfaceComposerClient.cpp revision 1f6078aef71b1d3f080cd565adbec350c71088dd
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    void setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height);
170
171    static void setAnimationTransaction() {
172        Composer::getInstance().setAnimationTransactionImpl();
173    }
174
175    static void openGlobalTransaction() {
176        Composer::getInstance().openGlobalTransactionImpl();
177    }
178
179    static void closeGlobalTransaction(bool synchronous) {
180        Composer::getInstance().closeGlobalTransactionImpl(synchronous);
181    }
182};
183
184ANDROID_SINGLETON_STATIC_INSTANCE(Composer);
185
186// ---------------------------------------------------------------------------
187
188sp<IBinder> Composer::createDisplay(const String8& displayName, bool secure) {
189    return ComposerService::getComposerService()->createDisplay(displayName,
190            secure);
191}
192
193void Composer::destroyDisplay(const sp<IBinder>& display) {
194    return ComposerService::getComposerService()->destroyDisplay(display);
195}
196
197sp<IBinder> Composer::getBuiltInDisplay(int32_t id) {
198    return ComposerService::getComposerService()->getBuiltInDisplay(id);
199}
200
201void Composer::openGlobalTransactionImpl() {
202    { // scope for the lock
203        Mutex::Autolock _l(mLock);
204        mTransactionNestCount += 1;
205    }
206}
207
208void Composer::closeGlobalTransactionImpl(bool synchronous) {
209    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
210
211    Vector<ComposerState> transaction;
212    Vector<DisplayState> displayTransaction;
213    uint32_t flags = 0;
214
215    { // scope for the lock
216        Mutex::Autolock _l(mLock);
217        mForceSynchronous |= synchronous;
218        if (!mTransactionNestCount) {
219            ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
220                    "call to openGlobalTransaction().");
221        } else if (--mTransactionNestCount) {
222            return;
223        }
224
225        transaction = mComposerStates;
226        mComposerStates.clear();
227
228        displayTransaction = mDisplayStates;
229        mDisplayStates.clear();
230
231        if (mForceSynchronous) {
232            flags |= ISurfaceComposer::eSynchronous;
233        }
234        if (mAnimation) {
235            flags |= ISurfaceComposer::eAnimation;
236        }
237
238        mForceSynchronous = false;
239        mAnimation = false;
240    }
241
242   sm->setTransactionState(transaction, displayTransaction, flags);
243}
244
245void Composer::setAnimationTransactionImpl() {
246    Mutex::Autolock _l(mLock);
247    mAnimation = true;
248}
249
250layer_state_t* Composer::getLayerStateLocked(
251        const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
252
253    ComposerState s;
254    s.client = client->mClient;
255    s.state.surface = id;
256
257    ssize_t index = mComposerStates.indexOf(s);
258    if (index < 0) {
259        // we don't have it, add an initialized layer_state to our list
260        index = mComposerStates.add(s);
261    }
262
263    ComposerState* const out = mComposerStates.editArray();
264    return &(out[index].state);
265}
266
267status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
268        const sp<IBinder>& id, float x, float y) {
269    Mutex::Autolock _l(mLock);
270    layer_state_t* s = getLayerStateLocked(client, id);
271    if (!s)
272        return BAD_INDEX;
273    s->what |= layer_state_t::ePositionChanged;
274    s->x = x;
275    s->y = y;
276    return NO_ERROR;
277}
278
279status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
280        const sp<IBinder>& id, uint32_t w, uint32_t h) {
281    Mutex::Autolock _l(mLock);
282    layer_state_t* s = getLayerStateLocked(client, id);
283    if (!s)
284        return BAD_INDEX;
285    s->what |= layer_state_t::eSizeChanged;
286    s->w = w;
287    s->h = h;
288
289    // Resizing a surface makes the transaction synchronous.
290    mForceSynchronous = true;
291
292    return NO_ERROR;
293}
294
295status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
296        const sp<IBinder>& id, int32_t z) {
297    Mutex::Autolock _l(mLock);
298    layer_state_t* s = getLayerStateLocked(client, id);
299    if (!s)
300        return BAD_INDEX;
301    s->what |= layer_state_t::eLayerChanged;
302    s->z = z;
303    return NO_ERROR;
304}
305
306status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
307        const sp<IBinder>& id, uint32_t flags,
308        uint32_t mask) {
309    Mutex::Autolock _l(mLock);
310    layer_state_t* s = getLayerStateLocked(client, id);
311    if (!s)
312        return BAD_INDEX;
313    if (mask & layer_state_t::eLayerOpaque) {
314        s->what |= layer_state_t::eOpacityChanged;
315    }
316    if (mask & layer_state_t::eLayerHidden) {
317        s->what |= layer_state_t::eVisibilityChanged;
318    }
319    s->flags &= ~mask;
320    s->flags |= (flags & mask);
321    s->mask |= mask;
322    return NO_ERROR;
323}
324
325status_t Composer::setTransparentRegionHint(
326        const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
327        const Region& transparentRegion) {
328    Mutex::Autolock _l(mLock);
329    layer_state_t* s = getLayerStateLocked(client, id);
330    if (!s)
331        return BAD_INDEX;
332    s->what |= layer_state_t::eTransparentRegionChanged;
333    s->transparentRegion = transparentRegion;
334    return NO_ERROR;
335}
336
337status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
338        const sp<IBinder>& id, float alpha) {
339    Mutex::Autolock _l(mLock);
340    layer_state_t* s = getLayerStateLocked(client, id);
341    if (!s)
342        return BAD_INDEX;
343    s->what |= layer_state_t::eAlphaChanged;
344    s->alpha = alpha;
345    return NO_ERROR;
346}
347
348status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
349        const sp<IBinder>& id, uint32_t layerStack) {
350    Mutex::Autolock _l(mLock);
351    layer_state_t* s = getLayerStateLocked(client, id);
352    if (!s)
353        return BAD_INDEX;
354    s->what |= layer_state_t::eLayerStackChanged;
355    s->layerStack = layerStack;
356    return NO_ERROR;
357}
358
359status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
360        const sp<IBinder>& id, float dsdx, float dtdx,
361        float dsdy, float dtdy) {
362    Mutex::Autolock _l(mLock);
363    layer_state_t* s = getLayerStateLocked(client, id);
364    if (!s)
365        return BAD_INDEX;
366    s->what |= layer_state_t::eMatrixChanged;
367    layer_state_t::matrix22_t matrix;
368    matrix.dsdx = dsdx;
369    matrix.dtdx = dtdx;
370    matrix.dsdy = dsdy;
371    matrix.dtdy = dtdy;
372    s->matrix = matrix;
373    return NO_ERROR;
374}
375
376status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
377        const sp<IBinder>& id, const Rect& crop) {
378    Mutex::Autolock _l(mLock);
379    layer_state_t* s = getLayerStateLocked(client, id);
380    if (!s)
381        return BAD_INDEX;
382    s->what |= layer_state_t::eCropChanged;
383    s->crop = crop;
384    return NO_ERROR;
385}
386
387// ---------------------------------------------------------------------------
388
389DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
390    DisplayState s;
391    s.token = token;
392    ssize_t index = mDisplayStates.indexOf(s);
393    if (index < 0) {
394        // we don't have it, add an initialized layer_state to our list
395        s.what = 0;
396        index = mDisplayStates.add(s);
397    }
398    return mDisplayStates.editItemAt(index);
399}
400
401void Composer::setDisplaySurface(const sp<IBinder>& token,
402        const sp<IGraphicBufferProducer>& bufferProducer) {
403    Mutex::Autolock _l(mLock);
404    DisplayState& s(getDisplayStateLocked(token));
405    s.surface = bufferProducer;
406    s.what |= DisplayState::eSurfaceChanged;
407}
408
409void Composer::setDisplayLayerStack(const sp<IBinder>& token,
410        uint32_t layerStack) {
411    Mutex::Autolock _l(mLock);
412    DisplayState& s(getDisplayStateLocked(token));
413    s.layerStack = layerStack;
414    s.what |= DisplayState::eLayerStackChanged;
415}
416
417void Composer::setDisplayProjection(const sp<IBinder>& token,
418        uint32_t orientation,
419        const Rect& layerStackRect,
420        const Rect& displayRect) {
421    Mutex::Autolock _l(mLock);
422    DisplayState& s(getDisplayStateLocked(token));
423    s.orientation = orientation;
424    s.viewport = layerStackRect;
425    s.frame = displayRect;
426    s.what |= DisplayState::eDisplayProjectionChanged;
427    mForceSynchronous = true; // TODO: do we actually still need this?
428}
429
430void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
431    Mutex::Autolock _l(mLock);
432    DisplayState& s(getDisplayStateLocked(token));
433    s.width = width;
434    s.height = height;
435    s.what |= DisplayState::eDisplaySizeChanged;
436}
437
438// ---------------------------------------------------------------------------
439
440SurfaceComposerClient::SurfaceComposerClient()
441    : mStatus(NO_INIT), mComposer(Composer::getInstance())
442{
443}
444
445void SurfaceComposerClient::onFirstRef() {
446    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
447    if (sm != 0) {
448        sp<ISurfaceComposerClient> conn = sm->createConnection();
449        if (conn != 0) {
450            mClient = conn;
451            mStatus = NO_ERROR;
452        }
453    }
454}
455
456SurfaceComposerClient::~SurfaceComposerClient() {
457    dispose();
458}
459
460status_t SurfaceComposerClient::initCheck() const {
461    return mStatus;
462}
463
464sp<IBinder> SurfaceComposerClient::connection() const {
465    return (mClient != 0) ? mClient->asBinder() : 0;
466}
467
468status_t SurfaceComposerClient::linkToComposerDeath(
469        const sp<IBinder::DeathRecipient>& recipient,
470        void* cookie, uint32_t flags) {
471    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
472    return sm->asBinder()->linkToDeath(recipient, cookie, flags);
473}
474
475void SurfaceComposerClient::dispose() {
476    // this can be called more than once.
477    sp<ISurfaceComposerClient> client;
478    Mutex::Autolock _lm(mLock);
479    if (mClient != 0) {
480        client = mClient; // hold ref while lock is held
481        mClient.clear();
482    }
483    mStatus = NO_INIT;
484}
485
486sp<SurfaceControl> SurfaceComposerClient::createSurface(
487        const String8& name,
488        uint32_t w,
489        uint32_t h,
490        PixelFormat format,
491        uint32_t flags)
492{
493    sp<SurfaceControl> sur;
494    if (mStatus == NO_ERROR) {
495        sp<IBinder> handle;
496        sp<IGraphicBufferProducer> gbp;
497        status_t err = mClient->createSurface(name, w, h, format, flags,
498                &handle, &gbp);
499        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
500        if (err == NO_ERROR) {
501            sur = new SurfaceControl(this, handle, gbp);
502        }
503    }
504    return sur;
505}
506
507sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
508        bool secure) {
509    return Composer::getInstance().createDisplay(displayName, secure);
510}
511
512void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
513    Composer::getInstance().destroyDisplay(display);
514}
515
516sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
517    return Composer::getInstance().getBuiltInDisplay(id);
518}
519
520status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
521    if (mStatus != NO_ERROR)
522        return mStatus;
523    status_t err = mClient->destroySurface(sid);
524    return err;
525}
526
527status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
528    if (mStatus != NO_ERROR) {
529        return mStatus;
530    }
531    return mClient->clearLayerFrameStats(token);
532}
533
534status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
535        FrameStats* outStats) const {
536    if (mStatus != NO_ERROR) {
537        return mStatus;
538    }
539    return mClient->getLayerFrameStats(token, outStats);
540}
541
542inline Composer& SurfaceComposerClient::getComposer() {
543    return mComposer;
544}
545
546// ----------------------------------------------------------------------------
547
548void SurfaceComposerClient::openGlobalTransaction() {
549    Composer::openGlobalTransaction();
550}
551
552void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
553    Composer::closeGlobalTransaction(synchronous);
554}
555
556void SurfaceComposerClient::setAnimationTransaction() {
557    Composer::setAnimationTransaction();
558}
559
560// ----------------------------------------------------------------------------
561
562status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
563    return getComposer().setCrop(this, id, crop);
564}
565
566status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
567    return getComposer().setPosition(this, id, x, y);
568}
569
570status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
571    return getComposer().setSize(this, id, w, h);
572}
573
574status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, int32_t z) {
575    return getComposer().setLayer(this, id, z);
576}
577
578status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
579    return getComposer().setFlags(this, id,
580            layer_state_t::eLayerHidden,
581            layer_state_t::eLayerHidden);
582}
583
584status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
585    return getComposer().setFlags(this, id,
586            0,
587            layer_state_t::eLayerHidden);
588}
589
590status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
591        uint32_t mask) {
592    return getComposer().setFlags(this, id, flags, mask);
593}
594
595status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
596        const Region& transparentRegion) {
597    return getComposer().setTransparentRegionHint(this, id, transparentRegion);
598}
599
600status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
601    return getComposer().setAlpha(this, id, alpha);
602}
603
604status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
605    return getComposer().setLayerStack(this, id, layerStack);
606}
607
608status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
609        float dsdy, float dtdy) {
610    return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
611}
612
613// ----------------------------------------------------------------------------
614
615void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
616        const sp<IGraphicBufferProducer>& bufferProducer) {
617    Composer::getInstance().setDisplaySurface(token, bufferProducer);
618}
619
620void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
621        uint32_t layerStack) {
622    Composer::getInstance().setDisplayLayerStack(token, layerStack);
623}
624
625void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
626        uint32_t orientation,
627        const Rect& layerStackRect,
628        const Rect& displayRect) {
629    Composer::getInstance().setDisplayProjection(token, orientation,
630            layerStackRect, displayRect);
631}
632
633void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
634        uint32_t width, uint32_t height) {
635    Composer::getInstance().setDisplaySize(token, width, height);
636}
637
638// ----------------------------------------------------------------------------
639
640status_t SurfaceComposerClient::getDisplayConfigs(
641        const sp<IBinder>& display, Vector<DisplayInfo>* configs)
642{
643    return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
644}
645
646status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
647        DisplayInfo* info) {
648    Vector<DisplayInfo> configs;
649    status_t result = getDisplayConfigs(display, &configs);
650    if (result != NO_ERROR) {
651        return result;
652    }
653
654    int activeId = getActiveConfig(display);
655    if (activeId < 0) {
656        ALOGE("No active configuration found");
657        return NAME_NOT_FOUND;
658    }
659
660    *info = configs[activeId];
661    return NO_ERROR;
662}
663
664int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
665    return ComposerService::getComposerService()->getActiveConfig(display);
666}
667
668status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
669    return ComposerService::getComposerService()->setActiveConfig(display, id);
670}
671
672void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
673        int mode) {
674    ComposerService::getComposerService()->setPowerMode(token, mode);
675}
676
677status_t SurfaceComposerClient::clearAnimationFrameStats() {
678    return ComposerService::getComposerService()->clearAnimationFrameStats();
679}
680
681status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
682    return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
683}
684
685// ----------------------------------------------------------------------------
686
687status_t ScreenshotClient::capture(
688        const sp<IBinder>& display,
689        const sp<IGraphicBufferProducer>& producer,
690        Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
691        uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
692    sp<ISurfaceComposer> s(ComposerService::getComposerService());
693    if (s == NULL) return NO_INIT;
694    return s->captureScreen(display, producer, sourceCrop,
695            reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
696}
697
698ScreenshotClient::ScreenshotClient()
699    : mHaveBuffer(false) {
700    memset(&mBuffer, 0, sizeof(mBuffer));
701}
702
703ScreenshotClient::~ScreenshotClient() {
704    ScreenshotClient::release();
705}
706
707sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
708    if (mCpuConsumer == NULL) {
709        sp<IGraphicBufferConsumer> consumer;
710        BufferQueue::createBufferQueue(&mProducer, &consumer);
711        mCpuConsumer = new CpuConsumer(consumer, 1);
712        mCpuConsumer->setName(String8("ScreenshotClient"));
713    }
714    return mCpuConsumer;
715}
716
717status_t ScreenshotClient::update(const sp<IBinder>& display,
718        Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
719        uint32_t minLayerZ, uint32_t maxLayerZ,
720        bool useIdentityTransform) {
721    sp<ISurfaceComposer> s(ComposerService::getComposerService());
722    if (s == NULL) return NO_INIT;
723    sp<CpuConsumer> cpuConsumer = getCpuConsumer();
724
725    if (mHaveBuffer) {
726        mCpuConsumer->unlockBuffer(mBuffer);
727        memset(&mBuffer, 0, sizeof(mBuffer));
728        mHaveBuffer = false;
729    }
730
731    status_t err = s->captureScreen(display, mProducer, sourceCrop,
732            reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
733
734    if (err == NO_ERROR) {
735        err = mCpuConsumer->lockNextBuffer(&mBuffer);
736        if (err == NO_ERROR) {
737            mHaveBuffer = true;
738        }
739    }
740    return err;
741}
742
743status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
744        bool useIdentityTransform) {
745    return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1UL,
746            useIdentityTransform);
747}
748
749status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
750        uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
751    return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
752            0, -1UL, useIdentityTransform);
753}
754
755void ScreenshotClient::release() {
756    if (mHaveBuffer) {
757        mCpuConsumer->unlockBuffer(mBuffer);
758        memset(&mBuffer, 0, sizeof(mBuffer));
759        mHaveBuffer = false;
760    }
761    mCpuConsumer.clear();
762}
763
764void const* ScreenshotClient::getPixels() const {
765    return mBuffer.data;
766}
767
768uint32_t ScreenshotClient::getWidth() const {
769    return mBuffer.width;
770}
771
772uint32_t ScreenshotClient::getHeight() const {
773    return mBuffer.height;
774}
775
776PixelFormat ScreenshotClient::getFormat() const {
777    return mBuffer.format;
778}
779
780uint32_t ScreenshotClient::getStride() const {
781    return mBuffer.stride;
782}
783
784size_t ScreenshotClient::getSize() const {
785    return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
786}
787
788// ----------------------------------------------------------------------------
789}; // namespace android
790