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