blob: 4a6a1d7012edaff372b307829c3bd8ca0b230415 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 <stdio.h>
21#include <stdint.h>
22#include <sys/types.h>
23
Mathias Agopian07952722009-05-19 19:08:10 -070024#include <binder/Parcel.h>
25#include <binder/IMemory.h>
26#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
29#include <ui/ISurface.h>
30#include <ui/ISurfaceFlingerClient.h>
31#include <ui/Point.h>
32#include <ui/Rect.h>
33
34#include <private/ui/LayerState.h>
35
36// ---------------------------------------------------------------------------
37
Mathias Agopian627e7b52009-05-21 19:21:59 -070038/* ideally AID_GRAPHICS would be in a semi-public header
39 * or there would be a way to map a user/group name to its id
40 */
41#ifndef AID_GRAPHICS
42#define AID_GRAPHICS 1003
43#endif
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
46#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
47
48// ---------------------------------------------------------------------------
49
50namespace android {
51
52enum {
53 GET_CBLK = IBinder::FIRST_CALL_TRANSACTION,
54 CREATE_SURFACE,
55 DESTROY_SURFACE,
56 SET_STATE
57};
58
59class BpSurfaceFlingerClient : public BpInterface<ISurfaceFlingerClient>
60{
61public:
62 BpSurfaceFlingerClient(const sp<IBinder>& impl)
63 : BpInterface<ISurfaceFlingerClient>(impl)
64 {
65 }
66
Mathias Agopiand763b5d2009-07-02 18:11:53 -070067 virtual sp<IMemoryHeap> getControlBlock() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 {
69 Parcel data, reply;
70 data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
71 remote()->transact(GET_CBLK, data, &reply);
Mathias Agopiand763b5d2009-07-02 18:11:53 -070072 return interface_cast<IMemoryHeap>(reply.readStrongBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 }
74
75 virtual sp<ISurface> createSurface( surface_data_t* params,
76 int pid,
77 DisplayID display,
78 uint32_t w,
79 uint32_t h,
80 PixelFormat format,
81 uint32_t flags)
82 {
83 Parcel data, reply;
84 data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
85 data.writeInt32(pid);
86 data.writeInt32(display);
87 data.writeInt32(w);
88 data.writeInt32(h);
89 data.writeInt32(format);
90 data.writeInt32(flags);
91 remote()->transact(CREATE_SURFACE, data, &reply);
92 params->readFromParcel(reply);
93 return interface_cast<ISurface>(reply.readStrongBinder());
94 }
95
96 virtual status_t destroySurface(SurfaceID sid)
97 {
98 Parcel data, reply;
99 data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
100 data.writeInt32(sid);
101 remote()->transact(DESTROY_SURFACE, data, &reply);
102 return reply.readInt32();
103 }
104
105 virtual status_t setState(int32_t count, const layer_state_t* states)
106 {
107 Parcel data, reply;
108 data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor());
109 data.writeInt32(count);
110 for (int i=0 ; i<count ; i++)
111 states[i].write(data);
112 remote()->transact(SET_STATE, data, &reply);
113 return reply.readInt32();
114 }
115};
116
117IMPLEMENT_META_INTERFACE(SurfaceFlingerClient, "android.ui.ISurfaceFlingerClient");
118
119// ----------------------------------------------------------------------
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121status_t BnSurfaceFlingerClient::onTransact(
122 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
123{
124 // codes that don't require permission check
125
126 switch(code) {
127 case GET_CBLK: {
128 CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700129 sp<IMemoryHeap> ctl(getControlBlock());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 reply->writeStrongBinder(ctl->asBinder());
131 return NO_ERROR;
132 } break;
133 }
134
135 // these must be checked
136
137 IPCThreadState* ipc = IPCThreadState::self();
138 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700139 const int uid = ipc->getCallingUid();
140 const int self_pid = getpid();
141 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 // we're called from a different process, do the real check
143 if (!checkCallingPermission(
144 String16("android.permission.ACCESS_SURFACE_FLINGER")))
145 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 LOGE("Permission Denial: "
147 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
148 return PERMISSION_DENIED;
149 }
150 }
151
152 switch(code) {
153 case CREATE_SURFACE: {
154 CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
155 surface_data_t params;
156 int32_t pid = data.readInt32();
157 DisplayID display = data.readInt32();
158 uint32_t w = data.readInt32();
159 uint32_t h = data.readInt32();
160 PixelFormat format = data.readInt32();
161 uint32_t flags = data.readInt32();
162 sp<ISurface> s = createSurface(&params, pid, display, w, h, format, flags);
163 params.writeToParcel(reply);
164 reply->writeStrongBinder(s->asBinder());
165 return NO_ERROR;
166 } break;
167 case DESTROY_SURFACE: {
168 CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
169 reply->writeInt32( destroySurface( data.readInt32() ) );
170 return NO_ERROR;
171 } break;
172 case SET_STATE: {
173 CHECK_INTERFACE(ISurfaceFlingerClient, data, reply);
174 int32_t count = data.readInt32();
175 layer_state_t* states = new layer_state_t[count];
176 for (int i=0 ; i<count ; i++)
177 states[i].read(data);
178 status_t err = setState(count, states);
179 delete [] states;
180 reply->writeInt32(err);
181 return NO_ERROR;
182 } break;
183 default:
184 return BBinder::onTransact(code, data, reply, flags);
185 }
186}
187
188// ----------------------------------------------------------------------
189
190status_t ISurfaceFlingerClient::surface_data_t::readFromParcel(const Parcel& parcel)
191{
Mathias Agopian18b6b492009-08-19 17:46:26 -0700192 token = parcel.readInt32();
193 identity = parcel.readInt32();
194 width = parcel.readInt32();
195 height = parcel.readInt32();
196 format = parcel.readInt32();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 return NO_ERROR;
198}
199
200status_t ISurfaceFlingerClient::surface_data_t::writeToParcel(Parcel* parcel) const
201{
202 parcel->writeInt32(token);
203 parcel->writeInt32(identity);
Mathias Agopian18b6b492009-08-19 17:46:26 -0700204 parcel->writeInt32(width);
205 parcel->writeInt32(height);
206 parcel->writeInt32(format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 return NO_ERROR;
208}
209
210}; // namespace android