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 <private/surfaceflinger/LayerState.h>
     29 
     30 #include <surfaceflinger/ISurfaceComposer.h>
     31 
     32 #include <ui/DisplayInfo.h>
     33 
     34 #include <gui/ISurfaceTexture.h>
     35 
     36 #include <utils/Log.h>
     37 
     38 // ---------------------------------------------------------------------------
     39 
     40 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
     41 #define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
     42 
     43 // ---------------------------------------------------------------------------
     44 
     45 namespace android {
     46 
     47 class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
     48 {
     49 public:
     50     BpSurfaceComposer(const sp<IBinder>& impl)
     51         : BpInterface<ISurfaceComposer>(impl)
     52     {
     53     }
     54 
     55     virtual sp<ISurfaceComposerClient> createConnection()
     56     {
     57         uint32_t n;
     58         Parcel data, reply;
     59         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     60         remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
     61         return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
     62     }
     63 
     64     virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc()
     65     {
     66         uint32_t n;
     67         Parcel data, reply;
     68         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     69         remote()->transact(BnSurfaceComposer::CREATE_GRAPHIC_BUFFER_ALLOC, data, &reply);
     70         return interface_cast<IGraphicBufferAlloc>(reply.readStrongBinder());
     71     }
     72 
     73     virtual sp<IMemoryHeap> getCblk() const
     74     {
     75         Parcel data, reply;
     76         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     77         remote()->transact(BnSurfaceComposer::GET_CBLK, data, &reply);
     78         return interface_cast<IMemoryHeap>(reply.readStrongBinder());
     79     }
     80 
     81     virtual void setTransactionState(const Vector<ComposerState>& state,
     82             int orientation)
     83     {
     84         Parcel data, reply;
     85         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
     86         Vector<ComposerState>::const_iterator b(state.begin());
     87         Vector<ComposerState>::const_iterator e(state.end());
     88         data.writeInt32(state.size());
     89         for ( ; b != e ; ++b ) {
     90             b->write(data);
     91         }
     92         data.writeInt32(orientation);
     93         remote()->transact(BnSurfaceComposer::SET_TRANSACTION_STATE, data, &reply);
     94     }
     95 
     96     virtual void bootFinished()
     97     {
     98         Parcel data, reply;
     99         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    100         remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
    101     }
    102 
    103     virtual status_t captureScreen(DisplayID dpy,
    104             sp<IMemoryHeap>* heap,
    105             uint32_t* width, uint32_t* height, PixelFormat* format,
    106             uint32_t reqWidth, uint32_t reqHeight,
    107             uint32_t minLayerZ, uint32_t maxLayerZ)
    108     {
    109         Parcel data, reply;
    110         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    111         data.writeInt32(dpy);
    112         data.writeInt32(reqWidth);
    113         data.writeInt32(reqHeight);
    114         data.writeInt32(minLayerZ);
    115         data.writeInt32(maxLayerZ);
    116         remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
    117         *heap = interface_cast<IMemoryHeap>(reply.readStrongBinder());
    118         *width = reply.readInt32();
    119         *height = reply.readInt32();
    120         *format = reply.readInt32();
    121         return reply.readInt32();
    122     }
    123 
    124     virtual status_t turnElectronBeamOff(int32_t mode)
    125     {
    126         Parcel data, reply;
    127         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    128         data.writeInt32(mode);
    129         remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_OFF, data, &reply);
    130         return reply.readInt32();
    131     }
    132 
    133     virtual status_t turnElectronBeamOn(int32_t mode)
    134     {
    135         Parcel data, reply;
    136         data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
    137         data.writeInt32(mode);
    138         remote()->transact(BnSurfaceComposer::TURN_ELECTRON_BEAM_ON, data, &reply);
    139         return reply.readInt32();
    140     }
    141 
    142     virtual bool authenticateSurfaceTexture(
    143             const sp<ISurfaceTexture>& surfaceTexture) const
    144     {
    145         Parcel data, reply;
    146         int err = NO_ERROR;
    147         err = data.writeInterfaceToken(
    148                 ISurfaceComposer::getInterfaceDescriptor());
    149         if (err != NO_ERROR) {
    150             LOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    151                     "interface descriptor: %s (%d)", strerror(-err), -err);
    152             return false;
    153         }
    154         err = data.writeStrongBinder(surfaceTexture->asBinder());
    155         if (err != NO_ERROR) {
    156             LOGE("ISurfaceComposer::authenticateSurfaceTexture: error writing "
    157                     "strong binder to parcel: %s (%d)", strerror(-err), -err);
    158             return false;
    159         }
    160         err = remote()->transact(BnSurfaceComposer::AUTHENTICATE_SURFACE, data,
    161                 &reply);
    162         if (err != NO_ERROR) {
    163             LOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    164                     "performing transaction: %s (%d)", strerror(-err), -err);
    165             return false;
    166         }
    167         int32_t result = 0;
    168         err = reply.readInt32(&result);
    169         if (err != NO_ERROR) {
    170             LOGE("ISurfaceComposer::authenticateSurfaceTexture: error "
    171                     "retrieving result: %s (%d)", strerror(-err), -err);
    172             return false;
    173         }
    174         return result != 0;
    175     }
    176 };
    177 
    178 IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
    179 
    180 // ----------------------------------------------------------------------
    181 
    182 status_t BnSurfaceComposer::onTransact(
    183     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    184 {
    185     switch(code) {
    186         case CREATE_CONNECTION: {
    187             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    188             sp<IBinder> b = createConnection()->asBinder();
    189             reply->writeStrongBinder(b);
    190         } break;
    191         case CREATE_GRAPHIC_BUFFER_ALLOC: {
    192             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    193             sp<IBinder> b = createGraphicBufferAlloc()->asBinder();
    194             reply->writeStrongBinder(b);
    195         } break;
    196         case SET_TRANSACTION_STATE: {
    197             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    198             size_t count = data.readInt32();
    199             ComposerState s;
    200             Vector<ComposerState> state;
    201             state.setCapacity(count);
    202             for (size_t i=0 ; i<count ; i++) {
    203                 s.read(data);
    204                 state.add(s);
    205             }
    206             int orientation = data.readInt32();
    207             setTransactionState(state, orientation);
    208         } break;
    209         case BOOT_FINISHED: {
    210             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    211             bootFinished();
    212         } break;
    213         case GET_CBLK: {
    214             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    215             sp<IBinder> b = getCblk()->asBinder();
    216             reply->writeStrongBinder(b);
    217         } break;
    218         case CAPTURE_SCREEN: {
    219             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    220             DisplayID dpy = data.readInt32();
    221             uint32_t reqWidth = data.readInt32();
    222             uint32_t reqHeight = data.readInt32();
    223             uint32_t minLayerZ = data.readInt32();
    224             uint32_t maxLayerZ = data.readInt32();
    225             sp<IMemoryHeap> heap;
    226             uint32_t w, h;
    227             PixelFormat f;
    228             status_t res = captureScreen(dpy, &heap, &w, &h, &f,
    229                     reqWidth, reqHeight, minLayerZ, maxLayerZ);
    230             reply->writeStrongBinder(heap->asBinder());
    231             reply->writeInt32(w);
    232             reply->writeInt32(h);
    233             reply->writeInt32(f);
    234             reply->writeInt32(res);
    235         } break;
    236         case TURN_ELECTRON_BEAM_OFF: {
    237             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    238             int32_t mode = data.readInt32();
    239             status_t res = turnElectronBeamOff(mode);
    240             reply->writeInt32(res);
    241         } break;
    242         case TURN_ELECTRON_BEAM_ON: {
    243             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    244             int32_t mode = data.readInt32();
    245             status_t res = turnElectronBeamOn(mode);
    246             reply->writeInt32(res);
    247         } break;
    248         case AUTHENTICATE_SURFACE: {
    249             CHECK_INTERFACE(ISurfaceComposer, data, reply);
    250             sp<ISurfaceTexture> surfaceTexture =
    251                     interface_cast<ISurfaceTexture>(data.readStrongBinder());
    252             int32_t result = authenticateSurfaceTexture(surfaceTexture) ? 1 : 0;
    253             reply->writeInt32(result);
    254         } break;
    255         default:
    256             return BBinder::onTransact(code, data, reply, flags);
    257     }
    258     return NO_ERROR;
    259 }
    260 
    261 // ----------------------------------------------------------------------------
    262 
    263 };
    264