blob: ea38e08a555f4a94016b314d9fe9021624ed8fb9 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <ui/Point.h>
30#include <ui/Rect.h>
31
Mathias Agopian000479f2010-02-09 17:46:37 -080032#include <surfaceflinger/ISurface.h>
Mathias Agopian770492c2010-05-28 14:22:23 -070033#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080034#include <private/surfaceflinger/LayerState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
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,
Mathias Agopian7623da42010-06-01 15:12:58 -070054 GET_TOKEN,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 CREATE_SURFACE,
56 DESTROY_SURFACE,
57 SET_STATE
58};
59
Mathias Agopian770492c2010-05-28 14:22:23 -070060class BpSurfaceComposerClient : public BpInterface<ISurfaceComposerClient>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061{
62public:
Mathias Agopian770492c2010-05-28 14:22:23 -070063 BpSurfaceComposerClient(const sp<IBinder>& impl)
64 : BpInterface<ISurfaceComposerClient>(impl)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 {
66 }
67
Mathias Agopiand763b5d2009-07-02 18:11:53 -070068 virtual sp<IMemoryHeap> getControlBlock() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 {
70 Parcel data, reply;
Mathias Agopian770492c2010-05-28 14:22:23 -070071 data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 remote()->transact(GET_CBLK, data, &reply);
Mathias Agopiand763b5d2009-07-02 18:11:53 -070073 return interface_cast<IMemoryHeap>(reply.readStrongBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 }
75
Mathias Agopian7623da42010-06-01 15:12:58 -070076 virtual ssize_t getTokenForSurface(const sp<ISurface>& sur) const
77 {
78 Parcel data, reply;
79 data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());
80 data.writeStrongBinder(sur->asBinder());
81 remote()->transact(GET_TOKEN, data, &reply);
82 return reply.readInt32();
83 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 virtual sp<ISurface> createSurface( surface_data_t* params,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080086 const String8& name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 DisplayID display,
88 uint32_t w,
89 uint32_t h,
90 PixelFormat format,
91 uint32_t flags)
92 {
93 Parcel data, reply;
Mathias Agopian770492c2010-05-28 14:22:23 -070094 data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());
Mathias Agopian5d26c1e2010-03-01 16:09:43 -080095 data.writeString8(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 data.writeInt32(display);
97 data.writeInt32(w);
98 data.writeInt32(h);
99 data.writeInt32(format);
100 data.writeInt32(flags);
101 remote()->transact(CREATE_SURFACE, data, &reply);
102 params->readFromParcel(reply);
103 return interface_cast<ISurface>(reply.readStrongBinder());
104 }
Mathias Agopian770492c2010-05-28 14:22:23 -0700105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 virtual status_t destroySurface(SurfaceID sid)
107 {
108 Parcel data, reply;
Mathias Agopian770492c2010-05-28 14:22:23 -0700109 data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 data.writeInt32(sid);
111 remote()->transact(DESTROY_SURFACE, data, &reply);
112 return reply.readInt32();
113 }
114
115 virtual status_t setState(int32_t count, const layer_state_t* states)
116 {
117 Parcel data, reply;
Mathias Agopian770492c2010-05-28 14:22:23 -0700118 data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 data.writeInt32(count);
120 for (int i=0 ; i<count ; i++)
121 states[i].write(data);
122 remote()->transact(SET_STATE, data, &reply);
123 return reply.readInt32();
124 }
125};
126
Mathias Agopian770492c2010-05-28 14:22:23 -0700127IMPLEMENT_META_INTERFACE(SurfaceComposerClient, "android.ui.ISurfaceComposerClient");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129// ----------------------------------------------------------------------
130
Mathias Agopian770492c2010-05-28 14:22:23 -0700131status_t BnSurfaceComposerClient::onTransact(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
133{
134 // codes that don't require permission check
135
136 switch(code) {
137 case GET_CBLK: {
Mathias Agopian770492c2010-05-28 14:22:23 -0700138 CHECK_INTERFACE(ISurfaceComposerClient, data, reply);
Mathias Agopiand763b5d2009-07-02 18:11:53 -0700139 sp<IMemoryHeap> ctl(getControlBlock());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 reply->writeStrongBinder(ctl->asBinder());
141 return NO_ERROR;
142 } break;
Mathias Agopian7623da42010-06-01 15:12:58 -0700143 case GET_TOKEN: {
144 CHECK_INTERFACE(ISurfaceComposerClient, data, reply);
145 sp<ISurface> sur = interface_cast<ISurface>(data.readStrongBinder());
146 ssize_t token = getTokenForSurface(sur);
147 reply->writeInt32(token);
148 return NO_ERROR;
149 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
151
152 // these must be checked
Mathias Agopian770492c2010-05-28 14:22:23 -0700153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 IPCThreadState* ipc = IPCThreadState::self();
155 const int pid = ipc->getCallingPid();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700156 const int uid = ipc->getCallingUid();
157 const int self_pid = getpid();
Mathias Agopian492277d2011-02-15 17:54:56 -0800158 if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS && uid != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // we're called from a different process, do the real check
160 if (!checkCallingPermission(
161 String16("android.permission.ACCESS_SURFACE_FLINGER")))
162 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 LOGE("Permission Denial: "
164 "can't openGlobalTransaction pid=%d, uid=%d", pid, uid);
165 return PERMISSION_DENIED;
166 }
167 }
Mathias Agopian770492c2010-05-28 14:22:23 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 switch(code) {
170 case CREATE_SURFACE: {
Mathias Agopian770492c2010-05-28 14:22:23 -0700171 CHECK_INTERFACE(ISurfaceComposerClient, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 surface_data_t params;
Mathias Agopian5d26c1e2010-03-01 16:09:43 -0800173 String8 name = data.readString8();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 DisplayID display = data.readInt32();
175 uint32_t w = data.readInt32();
176 uint32_t h = data.readInt32();
177 PixelFormat format = data.readInt32();
178 uint32_t flags = data.readInt32();
Mathias Agopian9638e5c2011-04-20 14:19:32 -0700179 sp<ISurface> s = createSurface(&params, name, display, w, h,
Mathias Agopian5d26c1e2010-03-01 16:09:43 -0800180 format, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 params.writeToParcel(reply);
182 reply->writeStrongBinder(s->asBinder());
183 return NO_ERROR;
184 } break;
185 case DESTROY_SURFACE: {
Mathias Agopian770492c2010-05-28 14:22:23 -0700186 CHECK_INTERFACE(ISurfaceComposerClient, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 reply->writeInt32( destroySurface( data.readInt32() ) );
188 return NO_ERROR;
189 } break;
190 case SET_STATE: {
Mathias Agopian770492c2010-05-28 14:22:23 -0700191 CHECK_INTERFACE(ISurfaceComposerClient, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 int32_t count = data.readInt32();
193 layer_state_t* states = new layer_state_t[count];
194 for (int i=0 ; i<count ; i++)
195 states[i].read(data);
196 status_t err = setState(count, states);
197 delete [] states;
198 reply->writeInt32(err);
199 return NO_ERROR;
200 } break;
201 default:
202 return BBinder::onTransact(code, data, reply, flags);
203 }
204}
205
206// ----------------------------------------------------------------------
207
Mathias Agopian770492c2010-05-28 14:22:23 -0700208status_t ISurfaceComposerClient::surface_data_t::readFromParcel(const Parcel& parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209{
Mathias Agopian18b6b492009-08-19 17:46:26 -0700210 token = parcel.readInt32();
211 identity = parcel.readInt32();
212 width = parcel.readInt32();
213 height = parcel.readInt32();
214 format = parcel.readInt32();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 return NO_ERROR;
216}
217
Mathias Agopian770492c2010-05-28 14:22:23 -0700218status_t ISurfaceComposerClient::surface_data_t::writeToParcel(Parcel* parcel) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219{
220 parcel->writeInt32(token);
221 parcel->writeInt32(identity);
Mathias Agopian18b6b492009-08-19 17:46:26 -0700222 parcel->writeInt32(width);
223 parcel->writeInt32(height);
224 parcel->writeInt32(format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 return NO_ERROR;
226}
227
228}; // namespace android