Home | History | Annotate | Download | only in surfaceflinger
      1 /*
      2  * Copyright (C) 2012 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 #include <stdint.h>
     18 #include <sys/types.h>
     19 
     20 #include <binder/PermissionCache.h>
     21 
     22 #include <private/android_filesystem_config.h>
     23 
     24 #include "Client.h"
     25 #include "Layer.h"
     26 #include "LayerBase.h"
     27 #include "SurfaceFlinger.h"
     28 
     29 namespace android {
     30 
     31 // ---------------------------------------------------------------------------
     32 
     33 const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
     34 
     35 // ---------------------------------------------------------------------------
     36 
     37 Client::Client(const sp<SurfaceFlinger>& flinger)
     38     : mFlinger(flinger), mNameGenerator(1)
     39 {
     40 }
     41 
     42 Client::~Client()
     43 {
     44     const size_t count = mLayers.size();
     45     for (size_t i=0 ; i<count ; i++) {
     46         sp<LayerBaseClient> layer(mLayers.valueAt(i).promote());
     47         if (layer != 0) {
     48             mFlinger->removeLayer(layer);
     49         }
     50     }
     51 }
     52 
     53 status_t Client::initCheck() const {
     54     return NO_ERROR;
     55 }
     56 
     57 size_t Client::attachLayer(const sp<LayerBaseClient>& layer)
     58 {
     59     Mutex::Autolock _l(mLock);
     60     size_t name = mNameGenerator++;
     61     mLayers.add(name, layer);
     62     return name;
     63 }
     64 
     65 void Client::detachLayer(const LayerBaseClient* layer)
     66 {
     67     Mutex::Autolock _l(mLock);
     68     // we do a linear search here, because this doesn't happen often
     69     const size_t count = mLayers.size();
     70     for (size_t i=0 ; i<count ; i++) {
     71         if (mLayers.valueAt(i) == layer) {
     72             mLayers.removeItemsAt(i, 1);
     73             break;
     74         }
     75     }
     76 }
     77 sp<LayerBaseClient> Client::getLayerUser(int32_t i) const
     78 {
     79     Mutex::Autolock _l(mLock);
     80     sp<LayerBaseClient> lbc;
     81     wp<LayerBaseClient> layer(mLayers.valueFor(i));
     82     if (layer != 0) {
     83         lbc = layer.promote();
     84         ALOGE_IF(lbc==0, "getLayerUser(name=%d) is dead", int(i));
     85     }
     86     return lbc;
     87 }
     88 
     89 
     90 status_t Client::onTransact(
     91     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
     92 {
     93     // these must be checked
     94      IPCThreadState* ipc = IPCThreadState::self();
     95      const int pid = ipc->getCallingPid();
     96      const int uid = ipc->getCallingUid();
     97      const int self_pid = getpid();
     98      if (CC_UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
     99          // we're called from a different process, do the real check
    100          if (!PermissionCache::checkCallingPermission(sAccessSurfaceFlinger))
    101          {
    102              ALOGE("Permission Denial: "
    103                      "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
    104              return PERMISSION_DENIED;
    105          }
    106      }
    107      return BnSurfaceComposerClient::onTransact(code, data, reply, flags);
    108 }
    109 
    110 
    111 sp<ISurface> Client::createSurface(
    112         ISurfaceComposerClient::surface_data_t* params,
    113         const String8& name,
    114         uint32_t w, uint32_t h, PixelFormat format,
    115         uint32_t flags)
    116 {
    117     /*
    118      * createSurface must be called from the GL thread so that it can
    119      * have access to the GL context.
    120      */
    121 
    122     class MessageCreateLayer : public MessageBase {
    123         sp<ISurface> result;
    124         SurfaceFlinger* flinger;
    125         ISurfaceComposerClient::surface_data_t* params;
    126         Client* client;
    127         const String8& name;
    128         uint32_t w, h;
    129         PixelFormat format;
    130         uint32_t flags;
    131     public:
    132         MessageCreateLayer(SurfaceFlinger* flinger,
    133                 ISurfaceComposerClient::surface_data_t* params,
    134                 const String8& name, Client* client,
    135                 uint32_t w, uint32_t h, PixelFormat format,
    136                 uint32_t flags)
    137             : flinger(flinger), params(params), client(client), name(name),
    138               w(w), h(h), format(format), flags(flags)
    139         {
    140         }
    141         sp<ISurface> getResult() const { return result; }
    142         virtual bool handler() {
    143             result = flinger->createLayer(params, name, client,
    144                     w, h, format, flags);
    145             return true;
    146         }
    147     };
    148 
    149     sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
    150             params, name, this, w, h, format, flags);
    151     mFlinger->postMessageSync(msg);
    152     return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
    153 }
    154 status_t Client::destroySurface(SurfaceID sid) {
    155     return mFlinger->onLayerRemoved(this, sid);
    156 }
    157 
    158 // ---------------------------------------------------------------------------
    159 }; // namespace android
    160