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/ISurfaceTexture.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(
    106             const sp<IBinder>& display, sp<IMemoryHeap>* heap,
    107             uint32_t* width, uint32_t* height, PixelFormat* format,
    108             uint32_t reqWidth, uint32_t reqHeight,
    109             uint32_t minLayerZ, uint32_t maxLayerZ)
    110     {
    111         Parcel data, reply;
    112         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    113         data.writeStrongBinder(display);
    114         data.writeInt32(reqWidth);
    115         data.writeInt32(reqHeight);
    116         data.writeInt32(minLayerZ);
    117         data.writeInt32(maxLayerZ);
    118         remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
    119         *heap = interface_cast<IMemoryHeap>(reply.readStrongBinder());
    120         *width = reply.readInt32();
    121         *height = reply.readInt32();
    122         *format = reply.readInt32();
    123         return reply.readInt32();
    124     }
    125 
    126     virtual bool authenticateSurfaceTexture(
    127             const sp<ISurfaceTexture>& surfaceTexture) const
    128     {
    129         Parcel data, reply;
    130         int err = NO_ERROR;
    131         err = data.writeInterfaceToken(
    132                 ISurfaceComposer::getInterfaceDescriptor());
    133         if (err != NO_ERROR) {
    134             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    135                     "interface descriptor: %s (%d)", strerror(-err), -err);
    136             return false;
    137         }
    138         err = data.writeStrongBinder(surfaceTexture->asBinder());
    139         if (err != NO_ERROR) {
    140             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    141                     "strong binder to parcel: %s (%d)", strerror(-err), -err);
    142             return false;
    143         }
    144         err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
    145                 &reply);
    146         if (err != NO_ERROR) {
    147             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    148                     "performing transaction: %s (%d)", strerror(-err), -err);
    149             return false;
    150         }
    151         int32_t result = 0;
    152         err = reply.readInt32(&result);
    153         if (err != NO_ERROR) {
    154             ALOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    155                     "retrieving result: %s (%d)", strerror(-err), -err);
    156             return false;
    157         }
    158         return result != 0;
    159     }
    160 
    161     virtual sp<IDisplayEventConnection> createDisplayEventConnection()
    162     {
    163         Parcel data, reply;
    164         sp<IDisplayEventConnection> result;
    165         int err = data.writeInterfaceToken(
    166                 ISurfaceComposer::getInterfaceDescriptor());
    167         if (err != NO_ERROR) {
    168             return result;
    169         }
    170         err = remote()->transact(
    171                 BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION,
    172                 data, &reply);
    173         if (err != NO_ERROR) {
    174             ALOGE("ISurfaceComposer::createDisplayEventConnection: error performing "
    175                     "transaction: %s (%d)", strerror(-err), -err);
    176             return result;
    177         }
    178         result = interface_cast<IDisplayEventConnection>(reply.readStrongBinder());
    179         return result;
    180     }
    181 
    182     virtual sp<IBinder> createDisplay(const String8& displayName, bool secure)
    183     {
    184         Parcel data, reply;
    185         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    186         data.writeString8(displayName);
    187         data.writeInt32(secure ? 1 : 0);
    188         remote()->transact(BnSurfaceComposer::CREATE_DISPLAY, data, &reply);
    189         return reply.readStrongBinder();
    190     }
    191 
    192     virtual sp<IBinder> getBuiltInDisplay(int32_t id)
    193     {
    194         Parcel data, reply;
    195         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    196         data.writeInt32(id);
    197         remote()->transact(BnSurfaceComposer::GET_BUILT_IN_DISPLAY, data, &reply);
    198         return reply.readStrongBinder();
    199     }
    200 
    201     virtual void blank(const sp<IBinder>& display)
    202     {
    203         Parcel data, reply;
    204         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    205         data.writeStrongBinder(display);
    206         remote()->transact(BnSurfaceComposer::BLANK, data, &reply);
    207     }
    208 
    209     virtual void unblank(const sp<IBinder>& display)
    210     {
    211         Parcel data, reply;
    212         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    213         data.writeStrongBinder(display);
    214         remote()->transact(BnSurfaceComposer::UNBLANK, data, &reply);
    215     }
    216 
    217     virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info)
    218     {
    219         Parcel data, reply;
    220         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    221         data.writeStrongBinder(display);
    222         remote()->transact(BnSurfaceComposer::GET_DISPLAY_INFO, data, &reply);
    223         memcpy(info, reply.readInplace(sizeof(DisplayInfo)), sizeof(DisplayInfo));
    224         return reply.readInt32();
    225     }
    226 };
    227 
    228 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
    229 
    230 // ----------------------------------------------------------------------
    231 
    232 status_t BnSurfaceComposer::onTransact(
    233     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    234 {
    235     switch(code) {
    236         case CREATE_CONNECTION: {
    237             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    238             sp<IBinder> b = createConnection()->asBinder();
    239             reply->writeStrongBinder(b);
    240         } break;
    241         case CREATE_GRAPHIC_BUFFER_ALLOC: {
    242             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    243             sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
    244             reply->writeStrongBinder(b);
    245         } break;
    246         case SET_TRANSACTION_STATE: {
    247             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    248             size_t count = data.readInt32();
    249             ComposerState s;
    250             Vector<ComposerState> state;
    251             state.setCapacity(count);
    252             for (size_t i=0 ; i<count ; i++) {
    253                 s.read(data);
    254                 state.add(s);
    255             }
    256             count = data.readInt32();
    257             DisplayState d;
    258             Vector<DisplayState> displays;
    259             displays.setCapacity(count);
    260             for (size_t i=0 ; i<count ; i++) {
    261                 d.read(data);
    262                 displays.add(d);
    263             }
    264             uint32_t flags = data.readInt32();
    265             setTransactionState(state, displays, flags);
    266         } break;
    267         case BOOT_FINISHED: {
    268             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    269             bootFinished();
    270         } break;
    271         case CAPTURE_SCREEN: {
    272             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    273             sp<IBinder> display = 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             sp<IMemoryHeap> heap;
    279             uint32_t w, h;
    280             PixelFormat f;
    281             status_t res = captureScreen(display, &heap, &w, &h, &f,
    282                     reqWidth, reqHeight, minLayerZ, maxLayerZ);
    283             reply->writeStrongBinder(heap->asBinder());
    284             reply->writeInt32(w);
    285             reply->writeInt32(h);
    286             reply->writeInt32(f);
    287             reply->writeInt32(res);
    288         } break;
    289         case AUTHENTICATE_SURFACE: {
    290             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    291             sp<ISurfaceTexture> surfaceTexture =
    292                     interface_cast<ISurfaceTexture>(data.readStrongBinder());
    293             int32_t result = authenticateSurfaceTexture(surfaceTexture) ? 1 : 0;
    294             reply->writeInt32(result);
    295         } break;
    296         case CREATE_DISPLAY_EVENT_CONNECTION: {
    297             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    298             sp<IDisplayEventConnection> connection(createDisplayEventConnection());
    299             reply->writeStrongBinder(connection->asBinder());
    300             return NO_ERROR;
    301         } break;
    302         case CREATE_DISPLAY: {
    303             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    304             String8 displayName = data.readString8();
    305             bool secure = bool(data.readInt32());
    306             sp<IBinder> display(createDisplay(displayName, secure));
    307             reply->writeStrongBinder(display);
    308             return NO_ERROR;
    309         } break;
    310         case GET_BUILT_IN_DISPLAY: {
    311             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    312             int32_t id = data.readInt32();
    313             sp<IBinder> display(getBuiltInDisplay(id));
    314             reply->writeStrongBinder(display);
    315             return NO_ERROR;
    316         } break;
    317         case BLANK: {
    318             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    319             sp<IBinder> display = data.readStrongBinder();
    320             blank(display);
    321         } break;
    322         case UNBLANK: {
    323             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    324             sp<IBinder> display = data.readStrongBinder();
    325             unblank(display);
    326         } break;
    327         case GET_DISPLAY_INFO: {
    328             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    329             DisplayInfo info;
    330             sp<IBinder> display = data.readStrongBinder();
    331             status_t result = getDisplayInfo(display, &info);
    332             memcpy(reply->writeInplace(sizeof(DisplayInfo)), &info, sizeof(DisplayInfo));
    333             reply->writeInt32(result);
    334         } break;
    335         default:
    336             return BBinder::onTransact(code, data, reply, flags);
    337     }
    338     return NO_ERROR;
    339 }
    340 
    341 // ----------------------------------------------------------------------------
    342 
    343 };
    344