The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/Parcel.h> |
| 25 | #include <binder/IMemory.h> |
| 26 | #include <binder/IPCThreadState.h> |
| 27 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <ui/Point.h> |
| 30 | #include <ui/Rect.h> |
| 31 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/ISurface.h> |
| 33 | #include <surfaceflinger/ISurfaceFlingerClient.h> |
| 34 | #include <private/surfaceflinger/LayerState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | // --------------------------------------------------------------------------- |
| 37 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 38 | /* 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 46 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 47 | |
| 48 | // --------------------------------------------------------------------------- |
| 49 | |
| 50 | namespace android { |
| 51 | |
| 52 | enum { |
| 53 | GET_CBLK = IBinder::FIRST_CALL_TRANSACTION, |
| 54 | CREATE_SURFACE, |
| 55 | DESTROY_SURFACE, |
| 56 | SET_STATE |
| 57 | }; |
| 58 | |
| 59 | class BpSurfaceFlingerClient : public BpInterface<ISurfaceFlingerClient> |
| 60 | { |
| 61 | public: |
| 62 | BpSurfaceFlingerClient(const sp<IBinder>& impl) |
| 63 | : BpInterface<ISurfaceFlingerClient>(impl) |
| 64 | { |
| 65 | } |
| 66 | |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 67 | virtual sp<IMemoryHeap> getControlBlock() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | { |
| 69 | Parcel data, reply; |
| 70 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 71 | remote()->transact(GET_CBLK, data, &reply); |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 72 | return interface_cast<IMemoryHeap>(reply.readStrongBinder()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | virtual sp<ISurface> createSurface( surface_data_t* params, |
| 76 | int pid, |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 77 | const String8& name, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | DisplayID display, |
| 79 | uint32_t w, |
| 80 | uint32_t h, |
| 81 | PixelFormat format, |
| 82 | uint32_t flags) |
| 83 | { |
| 84 | Parcel data, reply; |
| 85 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 86 | data.writeInt32(pid); |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 87 | data.writeString8(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | data.writeInt32(display); |
| 89 | data.writeInt32(w); |
| 90 | data.writeInt32(h); |
| 91 | data.writeInt32(format); |
| 92 | data.writeInt32(flags); |
| 93 | remote()->transact(CREATE_SURFACE, data, &reply); |
| 94 | params->readFromParcel(reply); |
| 95 | return interface_cast<ISurface>(reply.readStrongBinder()); |
| 96 | } |
| 97 | |
| 98 | virtual status_t destroySurface(SurfaceID sid) |
| 99 | { |
| 100 | Parcel data, reply; |
| 101 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 102 | data.writeInt32(sid); |
| 103 | remote()->transact(DESTROY_SURFACE, data, &reply); |
| 104 | return reply.readInt32(); |
| 105 | } |
| 106 | |
| 107 | virtual status_t setState(int32_t count, const layer_state_t* states) |
| 108 | { |
| 109 | Parcel data, reply; |
| 110 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 111 | data.writeInt32(count); |
| 112 | for (int i=0 ; i<count ; i++) |
| 113 | states[i].write(data); |
| 114 | remote()->transact(SET_STATE, data, &reply); |
| 115 | return reply.readInt32(); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | IMPLEMENT_META_INTERFACE(SurfaceFlingerClient, "android.ui.ISurfaceFlingerClient"); |
| 120 | |
| 121 | // ---------------------------------------------------------------------- |
| 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | status_t BnSurfaceFlingerClient::onTransact( |
| 124 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 125 | { |
| 126 | // codes that don't require permission check |
| 127 | |
| 128 | switch(code) { |
| 129 | case GET_CBLK: { |
| 130 | CHECK_INTERFACE(ISurfaceFlingerClient, data, reply); |
Mathias Agopian | d763b5d | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 131 | sp<IMemoryHeap> ctl(getControlBlock()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | reply->writeStrongBinder(ctl->asBinder()); |
| 133 | return NO_ERROR; |
| 134 | } break; |
| 135 | } |
| 136 | |
| 137 | // these must be checked |
| 138 | |
| 139 | IPCThreadState* ipc = IPCThreadState::self(); |
| 140 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 141 | const int uid = ipc->getCallingUid(); |
| 142 | const int self_pid = getpid(); |
| 143 | if (UNLIKELY(pid != self_pid && uid != AID_GRAPHICS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | // we're called from a different process, do the real check |
| 145 | if (!checkCallingPermission( |
| 146 | String16("android.permission.ACCESS_SURFACE_FLINGER"))) |
| 147 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | LOGE("Permission Denial: " |
| 149 | "can't openGlobalTransaction pid=%d, uid=%d", pid, uid); |
| 150 | return PERMISSION_DENIED; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | switch(code) { |
| 155 | case CREATE_SURFACE: { |
| 156 | CHECK_INTERFACE(ISurfaceFlingerClient, data, reply); |
| 157 | surface_data_t params; |
| 158 | int32_t pid = data.readInt32(); |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 159 | String8 name = data.readString8(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | DisplayID display = data.readInt32(); |
| 161 | uint32_t w = data.readInt32(); |
| 162 | uint32_t h = data.readInt32(); |
| 163 | PixelFormat format = data.readInt32(); |
| 164 | uint32_t flags = data.readInt32(); |
Mathias Agopian | 5d26c1e | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 165 | sp<ISurface> s = createSurface(¶ms, pid, name, display, w, h, |
| 166 | format, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | params.writeToParcel(reply); |
| 168 | reply->writeStrongBinder(s->asBinder()); |
| 169 | return NO_ERROR; |
| 170 | } break; |
| 171 | case DESTROY_SURFACE: { |
| 172 | CHECK_INTERFACE(ISurfaceFlingerClient, data, reply); |
| 173 | reply->writeInt32( destroySurface( data.readInt32() ) ); |
| 174 | return NO_ERROR; |
| 175 | } break; |
| 176 | case SET_STATE: { |
| 177 | CHECK_INTERFACE(ISurfaceFlingerClient, data, reply); |
| 178 | int32_t count = data.readInt32(); |
| 179 | layer_state_t* states = new layer_state_t[count]; |
| 180 | for (int i=0 ; i<count ; i++) |
| 181 | states[i].read(data); |
| 182 | status_t err = setState(count, states); |
| 183 | delete [] states; |
| 184 | reply->writeInt32(err); |
| 185 | return NO_ERROR; |
| 186 | } break; |
| 187 | default: |
| 188 | return BBinder::onTransact(code, data, reply, flags); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // ---------------------------------------------------------------------- |
| 193 | |
| 194 | status_t ISurfaceFlingerClient::surface_data_t::readFromParcel(const Parcel& parcel) |
| 195 | { |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 196 | token = parcel.readInt32(); |
| 197 | identity = parcel.readInt32(); |
| 198 | width = parcel.readInt32(); |
| 199 | height = parcel.readInt32(); |
| 200 | format = parcel.readInt32(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | return NO_ERROR; |
| 202 | } |
| 203 | |
| 204 | status_t ISurfaceFlingerClient::surface_data_t::writeToParcel(Parcel* parcel) const |
| 205 | { |
| 206 | parcel->writeInt32(token); |
| 207 | parcel->writeInt32(identity); |
Mathias Agopian | 18b6b49 | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 208 | parcel->writeInt32(width); |
| 209 | parcel->writeInt32(height); |
| 210 | parcel->writeInt32(format); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | return NO_ERROR; |
| 212 | } |
| 213 | |
| 214 | }; // namespace android |