Home | History | Annotate | Download | only in surfaceflinger_client
      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 <unistd.h>
     21 #include <fcntl.h>
     22 #include <errno.h>
     23 #include <sys/types.h>
     24 #include <sys/stat.h>
     25 
     26 #include <cutils/memory.h>
     27 
     28 #include <utils/Atomic.h>
     29 #include <utils/Errors.h>
     30 #include <utils/threads.h>
     31 #include <utils/KeyedVector.h>
     32 #include <utils/Log.h>
     33 
     34 #include <binder/IServiceManager.h>
     35 #include <binder/IMemory.h>
     36 
     37 #include <ui/DisplayInfo.h>
     38 #include <ui/Rect.h>
     39 
     40 #include <surfaceflinger/ISurfaceComposer.h>
     41 #include <surfaceflinger/ISurfaceFlingerClient.h>
     42 #include <surfaceflinger/ISurface.h>
     43 #include <surfaceflinger/SurfaceComposerClient.h>
     44 
     45 #include <private/surfaceflinger/LayerState.h>
     46 #include <private/surfaceflinger/SharedBufferStack.h>
     47 
     48 #define VERBOSE(...)	((void)0)
     49 //#define VERBOSE			LOGD
     50 
     51 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
     52 #define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
     53 
     54 namespace android {
     55 
     56 // ---------------------------------------------------------------------------
     57 
     58 // Must not be holding SurfaceComposerClient::mLock when acquiring gLock here.
     59 static Mutex                                                gLock;
     60 static sp<ISurfaceComposer>                                 gSurfaceManager;
     61 static DefaultKeyedVector< sp<IBinder>, sp<SurfaceComposerClient> > gActiveConnections;
     62 static SortedVector<sp<SurfaceComposerClient> >             gOpenTransactions;
     63 static sp<IMemoryHeap>                                      gServerCblkMemory;
     64 static volatile surface_flinger_cblk_t*                     gServerCblk;
     65 
     66 static sp<ISurfaceComposer> getComposerService()
     67 {
     68     sp<ISurfaceComposer> sc;
     69     Mutex::Autolock _l(gLock);
     70     if (gSurfaceManager != 0) {
     71         sc = gSurfaceManager;
     72     } else {
     73         // release the lock while we're waiting...
     74         gLock.unlock();
     75 
     76         sp<IBinder> binder;
     77         sp<IServiceManager> sm = defaultServiceManager();
     78         do {
     79             binder = sm->getService(String16("SurfaceFlinger"));
     80             if (binder == 0) {
     81                 LOGW("SurfaceFlinger not published, waiting...");
     82                 usleep(500000); // 0.5 s
     83             }
     84         } while(binder == 0);
     85 
     86         // grab the lock again for updating gSurfaceManager
     87         gLock.lock();
     88         if (gSurfaceManager == 0) {
     89             sc = interface_cast<ISurfaceComposer>(binder);
     90             gSurfaceManager = sc;
     91         } else {
     92             sc = gSurfaceManager;
     93         }
     94     }
     95     return sc;
     96 }
     97 
     98 static volatile surface_flinger_cblk_t const * get_cblk()
     99 {
    100     if (gServerCblk == 0) {
    101         sp<ISurfaceComposer> sm(getComposerService());
    102         Mutex::Autolock _l(gLock);
    103         if (gServerCblk == 0) {
    104             gServerCblkMemory = sm->getCblk();
    105             LOGE_IF(gServerCblkMemory==0, "Can't get server control block");
    106             gServerCblk = (surface_flinger_cblk_t *)gServerCblkMemory->getBase();
    107             LOGE_IF(gServerCblk==0, "Can't get server control block address");
    108         }
    109     }
    110     return gServerCblk;
    111 }
    112 
    113 // ---------------------------------------------------------------------------
    114 
    115 static inline int compare_type( const layer_state_t& lhs,
    116                                 const layer_state_t& rhs) {
    117     if (lhs.surface < rhs.surface)  return -1;
    118     if (lhs.surface > rhs.surface)  return 1;
    119     return 0;
    120 }
    121 
    122 SurfaceComposerClient::SurfaceComposerClient()
    123 {
    124     sp<ISurfaceComposer> sm(getComposerService());
    125     if (sm == 0) {
    126         _init(0, 0);
    127         return;
    128     }
    129 
    130     _init(sm, sm->createConnection());
    131 
    132     if (mClient != 0) {
    133         Mutex::Autolock _l(gLock);
    134         VERBOSE("Adding client %p to map", this);
    135         gActiveConnections.add(mClient->asBinder(), this);
    136     }
    137 }
    138 
    139 SurfaceComposerClient::SurfaceComposerClient(
    140         const sp<ISurfaceComposer>& sm, const sp<IBinder>& conn)
    141 {
    142     _init(sm, interface_cast<ISurfaceFlingerClient>(conn));
    143 }
    144 
    145 
    146 status_t SurfaceComposerClient::linkToComposerDeath(
    147         const sp<IBinder::DeathRecipient>& recipient,
    148         void* cookie, uint32_t flags)
    149 {
    150     sp<ISurfaceComposer> sm(getComposerService());
    151     return sm->asBinder()->linkToDeath(recipient, cookie, flags);
    152 }
    153 
    154 void SurfaceComposerClient::_init(
    155         const sp<ISurfaceComposer>& sm, const sp<ISurfaceFlingerClient>& conn)
    156 {
    157     VERBOSE("Creating client %p, conn %p", this, conn.get());
    158 
    159     mPrebuiltLayerState = 0;
    160     mTransactionOpen = 0;
    161     mStatus = NO_ERROR;
    162     mControl = 0;
    163 
    164     mClient = conn;
    165     if (mClient == 0) {
    166         mStatus = NO_INIT;
    167         return;
    168     }
    169 
    170     mControlMemory = mClient->getControlBlock();
    171     mSignalServer = sm;
    172     mControl = static_cast<SharedClient *>(mControlMemory->getBase());
    173 }
    174 
    175 SurfaceComposerClient::~SurfaceComposerClient()
    176 {
    177     VERBOSE("Destroying client %p, conn %p", this, mClient.get());
    178     dispose();
    179 }
    180 
    181 status_t SurfaceComposerClient::initCheck() const
    182 {
    183     return mStatus;
    184 }
    185 
    186 sp<IBinder> SurfaceComposerClient::connection() const
    187 {
    188     return (mClient != 0) ? mClient->asBinder() : 0;
    189 }
    190 
    191 sp<SurfaceComposerClient>
    192 SurfaceComposerClient::clientForConnection(const sp<IBinder>& conn)
    193 {
    194     sp<SurfaceComposerClient> client;
    195 
    196     { // scope for lock
    197         Mutex::Autolock _l(gLock);
    198         client = gActiveConnections.valueFor(conn);
    199     }
    200 
    201     if (client == 0) {
    202         // Need to make a new client.
    203         sp<ISurfaceComposer> sm(getComposerService());
    204         client = new SurfaceComposerClient(sm, conn);
    205         if (client != 0 && client->initCheck() == NO_ERROR) {
    206             Mutex::Autolock _l(gLock);
    207             gActiveConnections.add(conn, client);
    208             //LOGD("we have %d connections", gActiveConnections.size());
    209         } else {
    210             client.clear();
    211         }
    212     }
    213 
    214     return client;
    215 }
    216 
    217 void SurfaceComposerClient::dispose()
    218 {
    219     // this can be called more than once.
    220 
    221     sp<IMemoryHeap>             controlMemory;
    222     sp<ISurfaceFlingerClient>   client;
    223 
    224     {
    225         Mutex::Autolock _lg(gLock);
    226         Mutex::Autolock _lm(mLock);
    227 
    228         mSignalServer = 0;
    229 
    230         if (mClient != 0) {
    231             client = mClient;
    232             mClient.clear();
    233 
    234             ssize_t i = gActiveConnections.indexOfKey(client->asBinder());
    235             if (i >= 0 && gActiveConnections.valueAt(i) == this) {
    236                 VERBOSE("Removing client %p from map at %d", this, int(i));
    237                 gActiveConnections.removeItemsAt(i);
    238             }
    239         }
    240 
    241         delete mPrebuiltLayerState;
    242         mPrebuiltLayerState = 0;
    243         controlMemory = mControlMemory;
    244         mControlMemory.clear();
    245         mControl = 0;
    246         mStatus = NO_INIT;
    247     }
    248 }
    249 
    250 status_t SurfaceComposerClient::getDisplayInfo(
    251         DisplayID dpy, DisplayInfo* info)
    252 {
    253     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
    254         return BAD_VALUE;
    255 
    256     volatile surface_flinger_cblk_t const * cblk = get_cblk();
    257     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
    258 
    259     info->w              = dcblk->w;
    260     info->h              = dcblk->h;
    261     info->orientation    = dcblk->orientation;
    262     info->xdpi           = dcblk->xdpi;
    263     info->ydpi           = dcblk->ydpi;
    264     info->fps            = dcblk->fps;
    265     info->density        = dcblk->density;
    266     return getPixelFormatInfo(dcblk->format, &(info->pixelFormatInfo));
    267 }
    268 
    269 ssize_t SurfaceComposerClient::getDisplayWidth(DisplayID dpy)
    270 {
    271     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
    272         return BAD_VALUE;
    273     volatile surface_flinger_cblk_t const * cblk = get_cblk();
    274     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
    275     return dcblk->w;
    276 }
    277 
    278 ssize_t SurfaceComposerClient::getDisplayHeight(DisplayID dpy)
    279 {
    280     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
    281         return BAD_VALUE;
    282     volatile surface_flinger_cblk_t const * cblk = get_cblk();
    283     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
    284     return dcblk->h;
    285 }
    286 
    287 ssize_t SurfaceComposerClient::getDisplayOrientation(DisplayID dpy)
    288 {
    289     if (uint32_t(dpy)>=NUM_DISPLAY_MAX)
    290         return BAD_VALUE;
    291     volatile surface_flinger_cblk_t const * cblk = get_cblk();
    292     volatile display_cblk_t const * dcblk = cblk->displays + dpy;
    293     return dcblk->orientation;
    294 }
    295 
    296 ssize_t SurfaceComposerClient::getNumberOfDisplays()
    297 {
    298     volatile surface_flinger_cblk_t const * cblk = get_cblk();
    299     uint32_t connected = cblk->connected;
    300     int n = 0;
    301     while (connected) {
    302         if (connected&1) n++;
    303         connected >>= 1;
    304     }
    305     return n;
    306 }
    307 
    308 
    309 void SurfaceComposerClient::signalServer()
    310 {
    311     mSignalServer->signal();
    312 }
    313 
    314 sp<SurfaceControl> SurfaceComposerClient::createSurface(
    315         int pid,
    316         DisplayID display,
    317         uint32_t w,
    318         uint32_t h,
    319         PixelFormat format,
    320         uint32_t flags)
    321 {
    322     String8 name;
    323     const size_t SIZE = 128;
    324     char buffer[SIZE];
    325     snprintf(buffer, SIZE, "<pid_%d>", getpid());
    326     name.append(buffer);
    327 
    328     return SurfaceComposerClient::createSurface(pid, name, display,
    329             w, h, format, flags);
    330 
    331 }
    332 
    333 sp<SurfaceControl> SurfaceComposerClient::createSurface(
    334         int pid,
    335         const String8& name,
    336         DisplayID display,
    337         uint32_t w,
    338         uint32_t h,
    339         PixelFormat format,
    340         uint32_t flags)
    341 {
    342     sp<SurfaceControl> result;
    343     if (mStatus == NO_ERROR) {
    344         ISurfaceFlingerClient::surface_data_t data;
    345         sp<ISurface> surface = mClient->createSurface(&data, pid, name,
    346                 display, w, h, format, flags);
    347         if (surface != 0) {
    348             if (uint32_t(data.token) < NUM_LAYERS_MAX) {
    349                 result = new SurfaceControl(this, surface, data, w, h, format, flags);
    350             }
    351         }
    352     }
    353     return result;
    354 }
    355 
    356 status_t SurfaceComposerClient::destroySurface(SurfaceID sid)
    357 {
    358     if (mStatus != NO_ERROR)
    359         return mStatus;
    360 
    361     // it's okay to destroy a surface while a transaction is open,
    362     // (transactions really are a client-side concept)
    363     // however, this indicates probably a misuse of the API or a bug
    364     // in the client code.
    365     LOGW_IF(mTransactionOpen,
    366          "Destroying surface while a transaction is open. "
    367          "Client %p: destroying surface %d, mTransactionOpen=%d",
    368          this, sid, mTransactionOpen);
    369 
    370     status_t err = mClient->destroySurface(sid);
    371     return err;
    372 }
    373 
    374 void SurfaceComposerClient::openGlobalTransaction()
    375 {
    376     Mutex::Autolock _l(gLock);
    377 
    378     if (gOpenTransactions.size()) {
    379         LOGE("openGlobalTransaction() called more than once. skipping.");
    380         return;
    381     }
    382 
    383     const size_t N = gActiveConnections.size();
    384     VERBOSE("openGlobalTransaction (%ld clients)", N);
    385     for (size_t i=0; i<N; i++) {
    386         sp<SurfaceComposerClient> client(gActiveConnections.valueAt(i));
    387         if (gOpenTransactions.indexOf(client) < 0) {
    388             if (client->openTransaction() == NO_ERROR) {
    389                 if (gOpenTransactions.add(client) < 0) {
    390                     // Ooops!
    391                     LOGE(   "Unable to add a SurfaceComposerClient "
    392                             "to the global transaction set (out of memory?)");
    393                     client->closeTransaction();
    394                     // let it go, it'll fail later when the user
    395                     // tries to do something with the transaction
    396                 }
    397             } else {
    398                 LOGE("openTransaction on client %p failed", client.get());
    399                 // let it go, it'll fail later when the user
    400                 // tries to do something with the transaction
    401             }
    402         }
    403     }
    404 }
    405 
    406 void SurfaceComposerClient::closeGlobalTransaction()
    407 {
    408     gLock.lock();
    409         SortedVector< sp<SurfaceComposerClient> > clients(gOpenTransactions);
    410         gOpenTransactions.clear();
    411     gLock.unlock();
    412 
    413     const size_t N = clients.size();
    414     VERBOSE("closeGlobalTransaction (%ld clients)", N);
    415 
    416     sp<ISurfaceComposer> sm(getComposerService());
    417     sm->openGlobalTransaction();
    418     for (size_t i=0; i<N; i++) {
    419         clients[i]->closeTransaction();
    420     }
    421     sm->closeGlobalTransaction();
    422 
    423 }
    424 
    425 
    426 status_t SurfaceComposerClient::freezeDisplay(DisplayID dpy, uint32_t flags)
    427 {
    428     sp<ISurfaceComposer> sm(getComposerService());
    429     return sm->freezeDisplay(dpy, flags);
    430 }
    431 
    432 status_t SurfaceComposerClient::unfreezeDisplay(DisplayID dpy, uint32_t flags)
    433 {
    434     sp<ISurfaceComposer> sm(getComposerService());
    435     return sm->unfreezeDisplay(dpy, flags);
    436 }
    437 
    438 int SurfaceComposerClient::setOrientation(DisplayID dpy,
    439         int orientation, uint32_t flags)
    440 {
    441     sp<ISurfaceComposer> sm(getComposerService());
    442     return sm->setOrientation(dpy, orientation, flags);
    443 }
    444 
    445 status_t SurfaceComposerClient::openTransaction()
    446 {
    447     if (mStatus != NO_ERROR)
    448         return mStatus;
    449     Mutex::Autolock _l(mLock);
    450     VERBOSE(   "openTransaction (client %p, mTransactionOpen=%d)",
    451             this, mTransactionOpen);
    452     mTransactionOpen++;
    453     if (mPrebuiltLayerState == 0) {
    454         mPrebuiltLayerState = new layer_state_t;
    455     }
    456     return NO_ERROR;
    457 }
    458 
    459 
    460 status_t SurfaceComposerClient::closeTransaction()
    461 {
    462     if (mStatus != NO_ERROR)
    463         return mStatus;
    464 
    465     Mutex::Autolock _l(mLock);
    466 
    467     VERBOSE(   "closeTransaction (client %p, mTransactionOpen=%d)",
    468             this, mTransactionOpen);
    469 
    470     if (mTransactionOpen <= 0) {
    471         LOGE(   "closeTransaction (client %p, mTransactionOpen=%d) "
    472                 "called more times than openTransaction()",
    473                 this, mTransactionOpen);
    474         return INVALID_OPERATION;
    475     }
    476 
    477     if (mTransactionOpen >= 2) {
    478         mTransactionOpen--;
    479         return NO_ERROR;
    480     }
    481 
    482     mTransactionOpen = 0;
    483     const ssize_t count = mStates.size();
    484     if (count) {
    485         mClient->setState(count, mStates.array());
    486         mStates.clear();
    487     }
    488     return NO_ERROR;
    489 }
    490 
    491 layer_state_t* SurfaceComposerClient::_get_state_l(SurfaceID index)
    492 {
    493     // API usage error, do nothing.
    494     if (mTransactionOpen<=0) {
    495         LOGE("Not in transaction (client=%p, SurfaceID=%d, mTransactionOpen=%d",
    496                 this, int(index), mTransactionOpen);
    497         return 0;
    498     }
    499 
    500     // use mPrebuiltLayerState just to find out if we already have it
    501     layer_state_t& dummy = *mPrebuiltLayerState;
    502     dummy.surface = index;
    503     ssize_t i = mStates.indexOf(dummy);
    504     if (i < 0) {
    505         // we don't have it, add an initialized layer_state to our list
    506         i = mStates.add(dummy);
    507     }
    508     return mStates.editArray() + i;
    509 }
    510 
    511 layer_state_t* SurfaceComposerClient::_lockLayerState(SurfaceID id)
    512 {
    513     layer_state_t* s;
    514     mLock.lock();
    515     s = _get_state_l(id);
    516     if (!s) mLock.unlock();
    517     return s;
    518 }
    519 
    520 void SurfaceComposerClient::_unlockLayerState()
    521 {
    522     mLock.unlock();
    523 }
    524 
    525 status_t SurfaceComposerClient::setPosition(SurfaceID id, int32_t x, int32_t y)
    526 {
    527     layer_state_t* s = _lockLayerState(id);
    528     if (!s) return BAD_INDEX;
    529     s->what |= ISurfaceComposer::ePositionChanged;
    530     s->x = x;
    531     s->y = y;
    532     _unlockLayerState();
    533     return NO_ERROR;
    534 }
    535 
    536 status_t SurfaceComposerClient::setSize(SurfaceID id, uint32_t w, uint32_t h)
    537 {
    538     layer_state_t* s = _lockLayerState(id);
    539     if (!s) return BAD_INDEX;
    540     s->what |= ISurfaceComposer::eSizeChanged;
    541     s->w = w;
    542     s->h = h;
    543     _unlockLayerState();
    544     return NO_ERROR;
    545 }
    546 
    547 status_t SurfaceComposerClient::setLayer(SurfaceID id, int32_t z)
    548 {
    549     layer_state_t* s = _lockLayerState(id);
    550     if (!s) return BAD_INDEX;
    551     s->what |= ISurfaceComposer::eLayerChanged;
    552     s->z = z;
    553     _unlockLayerState();
    554     return NO_ERROR;
    555 }
    556 
    557 status_t SurfaceComposerClient::hide(SurfaceID id)
    558 {
    559     return setFlags(id, ISurfaceComposer::eLayerHidden,
    560             ISurfaceComposer::eLayerHidden);
    561 }
    562 
    563 status_t SurfaceComposerClient::show(SurfaceID id, int32_t)
    564 {
    565     return setFlags(id, 0, ISurfaceComposer::eLayerHidden);
    566 }
    567 
    568 status_t SurfaceComposerClient::freeze(SurfaceID id)
    569 {
    570     return setFlags(id, ISurfaceComposer::eLayerFrozen,
    571             ISurfaceComposer::eLayerFrozen);
    572 }
    573 
    574 status_t SurfaceComposerClient::unfreeze(SurfaceID id)
    575 {
    576     return setFlags(id, 0, ISurfaceComposer::eLayerFrozen);
    577 }
    578 
    579 status_t SurfaceComposerClient::setFlags(SurfaceID id,
    580         uint32_t flags, uint32_t mask)
    581 {
    582     layer_state_t* s = _lockLayerState(id);
    583     if (!s) return BAD_INDEX;
    584     s->what |= ISurfaceComposer::eVisibilityChanged;
    585     s->flags &= ~mask;
    586     s->flags |= (flags & mask);
    587     s->mask |= mask;
    588     _unlockLayerState();
    589     return NO_ERROR;
    590 }
    591 
    592 status_t SurfaceComposerClient::setTransparentRegionHint(
    593         SurfaceID id, const Region& transparentRegion)
    594 {
    595     layer_state_t* s = _lockLayerState(id);
    596     if (!s) return BAD_INDEX;
    597     s->what |= ISurfaceComposer::eTransparentRegionChanged;
    598     s->transparentRegion = transparentRegion;
    599     _unlockLayerState();
    600     return NO_ERROR;
    601 }
    602 
    603 status_t SurfaceComposerClient::setAlpha(SurfaceID id, float alpha)
    604 {
    605     layer_state_t* s = _lockLayerState(id);
    606     if (!s) return BAD_INDEX;
    607     s->what |= ISurfaceComposer::eAlphaChanged;
    608     s->alpha = alpha;
    609     _unlockLayerState();
    610     return NO_ERROR;
    611 }
    612 
    613 status_t SurfaceComposerClient::setMatrix(
    614         SurfaceID id,
    615         float dsdx, float dtdx,
    616         float dsdy, float dtdy )
    617 {
    618     layer_state_t* s = _lockLayerState(id);
    619     if (!s) return BAD_INDEX;
    620     s->what |= ISurfaceComposer::eMatrixChanged;
    621     layer_state_t::matrix22_t matrix;
    622     matrix.dsdx = dsdx;
    623     matrix.dtdx = dtdx;
    624     matrix.dsdy = dsdy;
    625     matrix.dtdy = dtdy;
    626     s->matrix = matrix;
    627     _unlockLayerState();
    628     return NO_ERROR;
    629 }
    630 
    631 status_t SurfaceComposerClient::setFreezeTint(SurfaceID id, uint32_t tint)
    632 {
    633     layer_state_t* s = _lockLayerState(id);
    634     if (!s) return BAD_INDEX;
    635     s->what |= ISurfaceComposer::eFreezeTintChanged;
    636     s->tint = tint;
    637     _unlockLayerState();
    638     return NO_ERROR;
    639 }
    640 
    641 }; // namespace android
    642 
    643