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