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    IInterface::asBinder(mComposerService)->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            uint32_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, uint32_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            mask & layer_state_t::eLayerHidden ||
315            mask & layer_state_t::eLayerSecure) {
316        s->what |= layer_state_t::eFlagsChanged;
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(static_cast<size_t>(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
429void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
430    Mutex::Autolock _l(mLock);
431    DisplayState& s(getDisplayStateLocked(token));
432    s.width = width;
433    s.height = height;
434    s.what |= DisplayState::eDisplaySizeChanged;
435}
436
437// ---------------------------------------------------------------------------
438
439SurfaceComposerClient::SurfaceComposerClient()
440    : mStatus(NO_INIT), mComposer(Composer::getInstance())
441{
442}
443
444void SurfaceComposerClient::onFirstRef() {
445    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
446    if (sm != 0) {
447        sp<ISurfaceComposerClient> conn = sm->createConnection();
448        if (conn != 0) {
449            mClient = conn;
450            mStatus = NO_ERROR;
451        }
452    }
453}
454
455SurfaceComposerClient::~SurfaceComposerClient() {
456    dispose();
457}
458
459status_t SurfaceComposerClient::initCheck() const {
460    return mStatus;
461}
462
463sp<IBinder> SurfaceComposerClient::connection() const {
464    return IInterface::asBinder(mClient);
465}
466
467status_t SurfaceComposerClient::linkToComposerDeath(
468        const sp<IBinder::DeathRecipient>& recipient,
469        void* cookie, uint32_t flags) {
470    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
471    return IInterface::asBinder(sm)->linkToDeath(recipient, cookie, flags);
472}
473
474void SurfaceComposerClient::dispose() {
475    // this can be called more than once.
476    sp<ISurfaceComposerClient> client;
477    Mutex::Autolock _lm(mLock);
478    if (mClient != 0) {
479        client = mClient; // hold ref while lock is held
480        mClient.clear();
481    }
482    mStatus = NO_INIT;
483}
484
485sp<SurfaceControl> SurfaceComposerClient::createSurface(
486        const String8& name,
487        uint32_t w,
488        uint32_t h,
489        PixelFormat format,
490        uint32_t flags)
491{
492    sp<SurfaceControl> sur;
493    if (mStatus == NO_ERROR) {
494        sp<IBinder> handle;
495        sp<IGraphicBufferProducer> gbp;
496        status_t err = mClient->createSurface(name, w, h, format, flags,
497                &handle, &gbp);
498        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
499        if (err == NO_ERROR) {
500            sur = new SurfaceControl(this, handle, gbp);
501        }
502    }
503    return sur;
504}
505
506sp<IBinder> SurfaceComposerClient::createDisplay(const String8& displayName,
507        bool secure) {
508    return Composer::getInstance().createDisplay(displayName, secure);
509}
510
511void SurfaceComposerClient::destroyDisplay(const sp<IBinder>& display) {
512    Composer::getInstance().destroyDisplay(display);
513}
514
515sp<IBinder> SurfaceComposerClient::getBuiltInDisplay(int32_t id) {
516    return Composer::getInstance().getBuiltInDisplay(id);
517}
518
519status_t SurfaceComposerClient::destroySurface(const sp<IBinder>& sid) {
520    if (mStatus != NO_ERROR)
521        return mStatus;
522    status_t err = mClient->destroySurface(sid);
523    return err;
524}
525
526status_t SurfaceComposerClient::clearLayerFrameStats(const sp<IBinder>& token) const {
527    if (mStatus != NO_ERROR) {
528        return mStatus;
529    }
530    return mClient->clearLayerFrameStats(token);
531}
532
533status_t SurfaceComposerClient::getLayerFrameStats(const sp<IBinder>& token,
534        FrameStats* outStats) const {
535    if (mStatus != NO_ERROR) {
536        return mStatus;
537    }
538    return mClient->getLayerFrameStats(token, outStats);
539}
540
541inline Composer& SurfaceComposerClient::getComposer() {
542    return mComposer;
543}
544
545// ----------------------------------------------------------------------------
546
547void SurfaceComposerClient::openGlobalTransaction() {
548    Composer::openGlobalTransaction();
549}
550
551void SurfaceComposerClient::closeGlobalTransaction(bool synchronous) {
552    Composer::closeGlobalTransaction(synchronous);
553}
554
555void SurfaceComposerClient::setAnimationTransaction() {
556    Composer::setAnimationTransaction();
557}
558
559// ----------------------------------------------------------------------------
560
561status_t SurfaceComposerClient::setCrop(const sp<IBinder>& id, const Rect& crop) {
562    return getComposer().setCrop(this, id, crop);
563}
564
565status_t SurfaceComposerClient::setPosition(const sp<IBinder>& id, float x, float y) {
566    return getComposer().setPosition(this, id, x, y);
567}
568
569status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
570    return getComposer().setSize(this, id, w, h);
571}
572
573status_t SurfaceComposerClient::setLayer(const sp<IBinder>& id, uint32_t z) {
574    return getComposer().setLayer(this, id, z);
575}
576
577status_t SurfaceComposerClient::hide(const sp<IBinder>& id) {
578    return getComposer().setFlags(this, id,
579            layer_state_t::eLayerHidden,
580            layer_state_t::eLayerHidden);
581}
582
583status_t SurfaceComposerClient::show(const sp<IBinder>& id) {
584    return getComposer().setFlags(this, id,
585            0,
586            layer_state_t::eLayerHidden);
587}
588
589status_t SurfaceComposerClient::setFlags(const sp<IBinder>& id, uint32_t flags,
590        uint32_t mask) {
591    return getComposer().setFlags(this, id, flags, mask);
592}
593
594status_t SurfaceComposerClient::setTransparentRegionHint(const sp<IBinder>& id,
595        const Region& transparentRegion) {
596    return getComposer().setTransparentRegionHint(this, id, transparentRegion);
597}
598
599status_t SurfaceComposerClient::setAlpha(const sp<IBinder>& id, float alpha) {
600    return getComposer().setAlpha(this, id, alpha);
601}
602
603status_t SurfaceComposerClient::setLayerStack(const sp<IBinder>& id, uint32_t layerStack) {
604    return getComposer().setLayerStack(this, id, layerStack);
605}
606
607status_t SurfaceComposerClient::setMatrix(const sp<IBinder>& id, float dsdx, float dtdx,
608        float dsdy, float dtdy) {
609    return getComposer().setMatrix(this, id, dsdx, dtdx, dsdy, dtdy);
610}
611
612// ----------------------------------------------------------------------------
613
614void SurfaceComposerClient::setDisplaySurface(const sp<IBinder>& token,
615        const sp<IGraphicBufferProducer>& bufferProducer) {
616    Composer::getInstance().setDisplaySurface(token, bufferProducer);
617}
618
619void SurfaceComposerClient::setDisplayLayerStack(const sp<IBinder>& token,
620        uint32_t layerStack) {
621    Composer::getInstance().setDisplayLayerStack(token, layerStack);
622}
623
624void SurfaceComposerClient::setDisplayProjection(const sp<IBinder>& token,
625        uint32_t orientation,
626        const Rect& layerStackRect,
627        const Rect& displayRect) {
628    Composer::getInstance().setDisplayProjection(token, orientation,
629            layerStackRect, displayRect);
630}
631
632void SurfaceComposerClient::setDisplaySize(const sp<IBinder>& token,
633        uint32_t width, uint32_t height) {
634    Composer::getInstance().setDisplaySize(token, width, height);
635}
636
637// ----------------------------------------------------------------------------
638
639status_t SurfaceComposerClient::getDisplayConfigs(
640        const sp<IBinder>& display, Vector<DisplayInfo>* configs)
641{
642    return ComposerService::getComposerService()->getDisplayConfigs(display, configs);
643}
644
645status_t SurfaceComposerClient::getDisplayInfo(const sp<IBinder>& display,
646        DisplayInfo* info) {
647    Vector<DisplayInfo> configs;
648    status_t result = getDisplayConfigs(display, &configs);
649    if (result != NO_ERROR) {
650        return result;
651    }
652
653    int activeId = getActiveConfig(display);
654    if (activeId < 0) {
655        ALOGE("No active configuration found");
656        return NAME_NOT_FOUND;
657    }
658
659    *info = configs[static_cast<size_t>(activeId)];
660    return NO_ERROR;
661}
662
663int SurfaceComposerClient::getActiveConfig(const sp<IBinder>& display) {
664    return ComposerService::getComposerService()->getActiveConfig(display);
665}
666
667status_t SurfaceComposerClient::setActiveConfig(const sp<IBinder>& display, int id) {
668    return ComposerService::getComposerService()->setActiveConfig(display, id);
669}
670
671void SurfaceComposerClient::setDisplayPowerMode(const sp<IBinder>& token,
672        int mode) {
673    ComposerService::getComposerService()->setPowerMode(token, mode);
674}
675
676status_t SurfaceComposerClient::clearAnimationFrameStats() {
677    return ComposerService::getComposerService()->clearAnimationFrameStats();
678}
679
680status_t SurfaceComposerClient::getAnimationFrameStats(FrameStats* outStats) {
681    return ComposerService::getComposerService()->getAnimationFrameStats(outStats);
682}
683
684// ----------------------------------------------------------------------------
685
686status_t ScreenshotClient::capture(
687        const sp<IBinder>& display,
688        const sp<IGraphicBufferProducer>& producer,
689        Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
690        uint32_t minLayerZ, uint32_t maxLayerZ, bool useIdentityTransform) {
691    sp<ISurfaceComposer> s(ComposerService::getComposerService());
692    if (s == NULL) return NO_INIT;
693    return s->captureScreen(display, producer, sourceCrop,
694            reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
695}
696
697ScreenshotClient::ScreenshotClient()
698    : mHaveBuffer(false) {
699    memset(&mBuffer, 0, sizeof(mBuffer));
700}
701
702ScreenshotClient::~ScreenshotClient() {
703    ScreenshotClient::release();
704}
705
706sp<CpuConsumer> ScreenshotClient::getCpuConsumer() const {
707    if (mCpuConsumer == NULL) {
708        sp<IGraphicBufferConsumer> consumer;
709        BufferQueue::createBufferQueue(&mProducer, &consumer);
710        mCpuConsumer = new CpuConsumer(consumer, 1);
711        mCpuConsumer->setName(String8("ScreenshotClient"));
712    }
713    return mCpuConsumer;
714}
715
716status_t ScreenshotClient::update(const sp<IBinder>& display,
717        Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
718        uint32_t minLayerZ, uint32_t maxLayerZ,
719        bool useIdentityTransform, uint32_t rotation) {
720    sp<ISurfaceComposer> s(ComposerService::getComposerService());
721    if (s == NULL) return NO_INIT;
722    sp<CpuConsumer> cpuConsumer = getCpuConsumer();
723
724    if (mHaveBuffer) {
725        mCpuConsumer->unlockBuffer(mBuffer);
726        memset(&mBuffer, 0, sizeof(mBuffer));
727        mHaveBuffer = false;
728    }
729
730    status_t err = s->captureScreen(display, mProducer, sourceCrop,
731            reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform,
732            static_cast<ISurfaceComposer::Rotation>(rotation));
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,
744        Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
745        uint32_t minLayerZ, uint32_t maxLayerZ,
746        bool useIdentityTransform) {
747
748    return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
749            minLayerZ, maxLayerZ, useIdentityTransform, ISurfaceComposer::eRotateNone);
750}
751
752status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
753        bool useIdentityTransform) {
754    return ScreenshotClient::update(display, sourceCrop, 0, 0, 0, -1U,
755            useIdentityTransform, ISurfaceComposer::eRotateNone);
756}
757
758status_t ScreenshotClient::update(const sp<IBinder>& display, Rect sourceCrop,
759        uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform) {
760    return ScreenshotClient::update(display, sourceCrop, reqWidth, reqHeight,
761            0, -1U, useIdentityTransform, ISurfaceComposer::eRotateNone);
762}
763
764void ScreenshotClient::release() {
765    if (mHaveBuffer) {
766        mCpuConsumer->unlockBuffer(mBuffer);
767        memset(&mBuffer, 0, sizeof(mBuffer));
768        mHaveBuffer = false;
769    }
770    mCpuConsumer.clear();
771}
772
773void const* ScreenshotClient::getPixels() const {
774    return mBuffer.data;
775}
776
777uint32_t ScreenshotClient::getWidth() const {
778    return mBuffer.width;
779}
780
781uint32_t ScreenshotClient::getHeight() const {
782    return mBuffer.height;
783}
784
785PixelFormat ScreenshotClient::getFormat() const {
786    return mBuffer.format;
787}
788
789uint32_t ScreenshotClient::getStride() const {
790    return mBuffer.stride;
791}
792
793size_t ScreenshotClient::getSize() const {
794    return mBuffer.stride * mBuffer.height * bytesPerPixel(mBuffer.format);
795}
796
797// ----------------------------------------------------------------------------
798}; // namespace android
799