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 // tag as surfaceflinger
     18 #define LOG_TAG "SurfaceFlinger"
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <binder/Parcel.h>
     24 #include <binder/IMemory.h>
     25 #include <binder/IPCThreadState.h>
     26 #include <binder/IServiceManager.h>
     27 
     28 #include <gui/BitTube.h>
     29 #include <gui/IDisplayEventConnection.h>
     30 #include <gui/ISurfaceComposer.h>
     31 #include <gui/IGraphicBufferProducer.h>
     32 
     33 #include <private/gui/LayerState.h>
     34 
     35 #include <ui/DisplayInfo.h>
     36 
     37 #include <utils/Log.h>
     38 
     39 // ---------------------------------------------------------------------------
     40 
     41 namespace android {
     42 
     43 class IDisplayEventConnection;
     44 
     45 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
     46 {
     47 public:
     48     BpSurfaceComposer(const sp<IBinder>& impl)
     49         : BpInterface<ISurfaceComposer>(impl)
     50     {
     51     }
     52 
     53     virtual sp<ISurfaceComposerClient> createConnection()
     54     {
     55         uint32_t n;
     56         Parcel data, reply;
     57         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     58         remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
     59         return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
     60     }
     61 
     62     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc()
     63     {
     64         uint32_t n;
     65         Parcel data, reply;
     66         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     67         remote()->transact(BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, data, &reply);
     68         return interface_cast<IGraphicBufferAlloc>(reply.readStrongBinder());
     69     }
     70 
     71     virtual void setTransactionState(
     72             const Vector<ComposerState>& state,
     73             const Vector<DisplayState>& displays,
     74             uint32_t flags)
     75     {
     76         Parcel data, reply;
     77         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     78         {
     79             Vector<ComposerState>::const_iterator b(state.begin());
     80             Vector<ComposerState>::const_iterator e(state.end());
     81             data.writeInt32(state.size());
     82             for ( ; b != e ; ++b ) {
     83                 b->write(data);
     84             }
     85         }
     86         {
     87             Vector<DisplayState>::const_iterator b(displays.begin());
     88             Vector<DisplayState>::const_iterator e(displays.end());
     89             data.writeInt32(displays.size());
     90             for ( ; b != e ; ++b ) {
     91                 b->write(data);
     92             }
     93         }
     94         data.writeInt32(flags);
     95         remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
     96     }
     97 
     98     virtual void bootFinished()
     99     {
    100         Parcel data, reply;
    101         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    102         remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
    103     }
    104 
    105     virtual status_t captureScreen(const sp<IBinder>& display,
    106             const sp<IGraphicBufferProducer>& producer,
    107             uint32_t reqWidth, uint32_t reqHeight,
    108             uint32_t minLayerZ, uint32_t maxLayerZ,
    109             bool isCpuConsumer)
    110     {
    111         Parcel data, reply;
    112         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    113         data.writeStrongBinder(display);
    114         data.writeStrongBinder(producer->asBinder());
    115         data.writeInt32(reqWidth);
    116         data.writeInt32(reqHeight);
    117         data.writeInt32(minLayerZ);
    118         data.writeInt32(maxLayerZ);
    119         data.writeInt32(isCpuConsumer);
    120         remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
    121         return reply.readInt32();
    122     }
    123 
    124     virtual bool authenticateSurfaceTexture(
    125             const sp<IGraphicBufferProducer>& bufferProducer) const
    126     {
    127         Parcel data, reply;
    128         int err = NO_ERROR;
    129         err = data.writeInterfaceToken(
    130                 ISurfaceComposer::getInterfaceDescriptor());
    131         if (err != NO_ERROR) {
    132             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    133                     "interface descriptor: %s (%d)", strerror(-err), -err);
    134             return false;
    135         }
    136         err = data.writeStrongBinder(bufferProducer->asBinder());
    137         if (err != NO_ERROR) {
    138             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    139                     "strong binder to parcel: %s (%d)", strerror(-err), -err);
    140             return false;
    141         }
    142         err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
    143                 &reply);
    144         if (err != NO_ERROR) {
    145             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    146                     "performing transaction: %s (%d)", strerror(-err), -err);
    147             return false;
    148         }
    149         int32_t result = 0;
    150         err = reply.readInt32(&result);
    151         if (err != NO_ERROR) {
    152             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    153                     "retrieving result: %s (%d)", strerror(-err), -err);
    154             return false;
    155         }
    156         return result != 0;
    157     }
    158 
    159     virtual sp<IDisplayEventConnection> createDisplayEventConnection()
    160     {
    161         Parcel data, reply;
    162         sp<IDisplayEventConnection> result;
    163         int err = data.writeInterfaceToken(
    164                 ISurfaceComposer::getInterfaceDescriptor());
    165         if (err != NO_ERROR) {
    166             return result;
    167         }
    168         err = remote()->transact(
    169                 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
    170                 data, &reply);
    171         if (err != NO_ERROR) {
    172             ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
    173                     "transaction: %s (%d)", strerror(-err), -err);
    174             return result;
    175         }
    176         result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
    177         return result;
    178     }
    179 
    180     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
    181     {
    182         Parcel data, reply;
    183         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    184         data.writeString8(displayName);
    185         data.writeInt32(secure ? 1 : 0);
    186         remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
    187         return reply.readStrongBinder();
    188     }
    189 
    190     virtual sp<IBinder> getBuiltInDisplay(int32_t id)
    191     {
    192         Parcel data, reply;
    193         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    194         data.writeInt32(id);
    195         remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
    196         return reply.readStrongBinder();
    197     }
    198 
    199     virtual void blank(const sp<IBinder>& display)
    200     {
    201         Parcel data, reply;
    202         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    203         data.writeStrongBinder(display);
    204         remote()->transact(BnSurfaceComposer::BLANK, data, &reply);
    205     }
    206 
    207     virtual void unblank(const sp<IBinder>& display)
    208     {
    209         Parcel data, reply;
    210         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    211         data.writeStrongBinder(display);
    212         remote()->transact(BnSurfaceComposer::UNBLANK, data, &reply);
    213     }
    214 
    215     virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info)
    216     {
    217         Parcel data, reply;
    218         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    219         data.writeStrongBinder(display);
    220         remote()->transact(BnSurfaceComposer::GET_DISPLAY_INFO, data, &reply);
    221         memcpy(info, reply.readInplace(sizeof(DisplayInfo)), sizeof(DisplayInfo));
    222         return reply.readInt32();
    223     }
    224 };
    225 
    226 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
    227 
    228 // ----------------------------------------------------------------------
    229 
    230 status_t BnSurfaceComposer::onTransact(
    231     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    232 {
    233     switch(code) {
    234         case CREATE_CONNECTION: {
    235             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    236             sp<IBinder> b = createConnection()->asBinder();
    237             reply->writeStrongBinder(b);
    238         } break;
    239         case CREATE_GRAPHIC_BUFFER_ALLOC: {
    240             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    241             sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
    242             reply->writeStrongBinder(b);
    243         } break;
    244         case SET_TRANSACTION_STATE: {
    245             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    246             size_t count = data.readInt32();
    247             ComposerState s;
    248             Vector<ComposerState> state;
    249             state.setCapacity(count);
    250             for (size_t i=0 ; i<count ; i++) {
    251                 s.read(data);
    252                 state.add(s);
    253             }
    254             count = data.readInt32();
    255             DisplayState d;
    256             Vector<DisplayState> displays;
    257             displays.setCapacity(count);
    258             for (size_t i=0 ; i<count ; i++) {
    259                 d.read(data);
    260                 displays.add(d);
    261             }
    262             uint32_t flags = data.readInt32();
    263             setTransactionState(state, displays, flags);
    264         } break;
    265         case BOOT_FINISHED: {
    266             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    267             bootFinished();
    268         } break;
    269         case CAPTURE_SCREEN: {
    270             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    271             sp<IBinder> display = data.readStrongBinder();
    272             sp<IGraphicBufferProducer> producer =
    273                     interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
    274             uint32_t reqWidth = data.readInt32();
    275             uint32_t reqHeight = data.readInt32();
    276             uint32_t minLayerZ = data.readInt32();
    277             uint32_t maxLayerZ = data.readInt32();
    278             bool isCpuConsumer = data.readInt32();
    279             status_t res = captureScreen(display, producer,
    280                     reqWidth, reqHeight, minLayerZ, maxLayerZ,
    281                     isCpuConsumer);
    282             reply->writeInt32(res);
    283         } break;
    284         case AUTHENTICATE_SURFACE: {
    285             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    286             sp<IGraphicBufferProducer> bufferProducer =
    287                     interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
    288             int32_t result = authenticateSurfaceTexture(bufferProducer) ? 1 : 0;
    289             reply->writeInt32(result);
    290         } break;
    291         case CREATE_DISPLAY_EVENT_CONNECTION: {
    292             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    293             sp<IDisplayEventConnection> connection(createDisplayEventConnection());
    294             reply->writeStrongBinder(connection->asBinder());
    295             return NO_ERROR;
    296         } break;
    297         case CREATE_DISPLAY: {
    298             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    299             String8 displayName = data.readString8();
    300             bool secure = bool(data.readInt32());
    301             sp<IBinder> display(createDisplay(displayName, secure));
    302             reply->writeStrongBinder(display);
    303             return NO_ERROR;
    304         } break;
    305         case GET_BUILT_IN_DISPLAY: {
    306             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    307             int32_t id = data.readInt32();
    308             sp<IBinder> display(getBuiltInDisplay(id));
    309             reply->writeStrongBinder(display);
    310             return NO_ERROR;
    311         } break;
    312         case BLANK: {
    313             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    314             sp<IBinder> display = data.readStrongBinder();
    315             blank(display);
    316         } break;
    317         case UNBLANK: {
    318             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    319             sp<IBinder> display = data.readStrongBinder();
    320             unblank(display);
    321         } break;
    322         case GET_DISPLAY_INFO: {
    323             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    324             DisplayInfo info;
    325             sp<IBinder> display = data.readStrongBinder();
    326             status_t result = getDisplayInfo(display, &info);
    327             memcpy(reply->writeInplace(sizeof(DisplayInfo)), &info, sizeof(DisplayInfo));
    328             reply->writeInt32(result);
    329         } break;
    330         default:
    331             return BBinder::onTransact(code, data, reply, flags);
    332     }
    333     return NO_ERROR;
    334 }
    335 
    336 // ----------------------------------------------------------------------------
    337 
    338 };
    339