summaryrefslogtreecommitdiffstats
path: root/libs/ui/ISurfaceFlingerClient.cpp
blob: dd6a798b9899ac58c903fa31af11fb505d490315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// tag as surfaceflinger
#define LOG_TAG "SurfaceFlinger"

#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>

#include <utils/Parcel.h>
#include <utils/IMemory.h>
#include <utils/IPCThreadState.h>
#include <utils/IServiceManager.h>

#include <ui/ISurface.h>
#include <ui/ISurfaceFlingerClient.h>
#include <ui/Point.h>
#include <ui/Rect.h>

#include <private/ui/LayerState.h>

// ---------------------------------------------------------------------------

#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))

// ---------------------------------------------------------------------------

namespace android {

enum {
    GET_CBLK = IBinder::FIRST_CALL_TRANSACTION,
    CREATE_SURFACE,
    DESTROY_SURFACE,
    SET_STATE
};

class BpSurfaceFlingerClient : public BpInterface<ISurfaceFlingerClient>
{
public:
    BpSurfaceFlingerClient(const sp<IBinder>& impl)
        : BpInterface<ISurfaceFlingerClient>(impl)
    {
    }

    virtual void getControlBlocks(sp<IMemory>* ctl) const
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
        remote()->transact(GET_CBLK, data, &reply);
        *ctl  = interface_cast<IMemory>(reply.readStrongBinder());
    }

    virtual sp<ISurface> createSurface( surface_data_t* params,
                                        int pid,
                                        DisplayID display,
                                        uint32_t w,
                                        uint32_t h,
                                        PixelFormat format,
                                        uint32_t flags)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
        data.writeInt32(pid);
        data.writeInt32(display);
        data.writeInt32(w);
        data.writeInt32(h);
        data.writeInt32(format);
        data.writeInt32(flags);
        remote()->transact(CREATE_SURFACE, data, &reply);
        params->readFromParcel(reply);
        return interface_cast<ISurface>(reply.readStrongBinder());
    }
                                    
    virtual status_t destroySurface(SurfaceID sid)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
        data.writeInt32(sid);
        remote()->transact(DESTROY_SURFACE, data, &reply);
        return reply.readInt32();
    }

    virtual status_t setState(int32_t count, const layer_state_t* states)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
        data.writeInt32(count);
        for (int i=0 ; i<count ; i++)
            states[i].write(data);
        remote()->transact(SET_STATE, data, &reply);
        return reply.readInt32();
    }
};

IMPLEMENT_META_INTERFACE(SurfaceFlingerClient, "android.ui.ISurfaceFlingerClient");

// ----------------------------------------------------------------------

#define CHECK_INTERFACE(interface, data, reply) \
        do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
            LOGW("Call incorrectly routed to " #interface); \
            return PERMISSION_DENIED; \
        } } while (0)

status_t BnSurfaceFlingerClient::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    // codes that don't require permission check

    switch(code) {
        case GET_CBLK: {
            CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
            sp<IMemory> ctl;
            getControlBlocks(&ctl);
            reply->writeStrongBinder(ctl->asBinder());
            return NO_ERROR;
        } break;
    }

    // these must be checked
     
     IPCThreadState* ipc = IPCThreadState::self();
     const int pid = ipc->getCallingPid();
     const int self_pid    = getpid();
     if (UNLIKELY(pid != self_pid)) {
         // we're called from a different process, do the real check
         if (!checkCallingPermission(
                 String16("android.permission.ACCESS_SURFACE_FLINGER")))
         {
             const int uid = ipc->getCallingUid();
             LOGE("Permission Denial: "
                     "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
             return PERMISSION_DENIED;
         }
     }
   
     switch(code) {
        case CREATE_SURFACE: {
            CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
            surface_data_t params;
            int32_t pid = data.readInt32();
            DisplayID display = data.readInt32();
            uint32_t w = data.readInt32();
            uint32_t h = data.readInt32();
            PixelFormat format = data.readInt32();
            uint32_t flags = data.readInt32();
            sp<ISurface> s = createSurface(&params, pid, display, w, h, format, flags);
            params.writeToParcel(reply);
            reply->writeStrongBinder(s->asBinder());
            return NO_ERROR;
        } break;
        case DESTROY_SURFACE: {
            CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
            reply->writeInt32( destroySurface( data.readInt32() ) );
            return NO_ERROR;
        } break;
        case SET_STATE: {
            CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
            int32_t count = data.readInt32();
            layer_state_t* states = new layer_state_t[count];
            for (int i=0 ; i<count ; i++)
                states[i].read(data);
            status_t err = setState(count, states);
            delete [] states;
            reply->writeInt32(err);
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

// ----------------------------------------------------------------------

status_t ISurfaceFlingerClient::surface_data_t::readFromParcel(const Parcel& parcel)
{
    token = parcel.readInt32();
    identity  = parcel.readInt32();
    heap[0] = interface_cast<IMemoryHeap>(parcel.readStrongBinder());
    heap[1] = interface_cast<IMemoryHeap>(parcel.readStrongBinder());
    return NO_ERROR;
}

status_t ISurfaceFlingerClient::surface_data_t::writeToParcel(Parcel* parcel) const
{
    parcel->writeInt32(token);
    parcel->writeInt32(identity);
    parcel->writeStrongBinder(heap[0]!=0 ? heap[0]->asBinder() : NULL);
    parcel->writeStrongBinder(heap[1]!=0 ? heap[1]->asBinder() : NULL);
    return NO_ERROR;
}

}; // namespace android