The Android Open Source Project | edbf3b6 | 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 | |
| 24 | #include <utils/Parcel.h> |
| 25 | #include <utils/IMemory.h> |
| 26 | #include <utils/IPCThreadState.h> |
| 27 | #include <utils/IServiceManager.h> |
| 28 | |
| 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 | |
| 38 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 39 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 40 | |
| 41 | // --------------------------------------------------------------------------- |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | enum { |
| 46 | GET_CBLK = IBinder::FIRST_CALL_TRANSACTION, |
| 47 | CREATE_SURFACE, |
| 48 | DESTROY_SURFACE, |
| 49 | SET_STATE |
| 50 | }; |
| 51 | |
| 52 | class BpSurfaceFlingerClient : public BpInterface<ISurfaceFlingerClient> |
| 53 | { |
| 54 | public: |
| 55 | BpSurfaceFlingerClient(const sp<IBinder>& impl) |
| 56 | : BpInterface<ISurfaceFlingerClient>(impl) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | virtual void getControlBlocks(sp<IMemory>* ctl) const |
| 61 | { |
| 62 | Parcel data, reply; |
| 63 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 64 | remote()->transact(GET_CBLK, data, &reply); |
| 65 | *ctl = interface_cast<IMemory>(reply.readStrongBinder()); |
| 66 | } |
| 67 | |
| 68 | virtual sp<ISurface> createSurface( surface_data_t* params, |
| 69 | int pid, |
| 70 | DisplayID display, |
| 71 | uint32_t w, |
| 72 | uint32_t h, |
| 73 | PixelFormat format, |
| 74 | uint32_t flags) |
| 75 | { |
| 76 | Parcel data, reply; |
| 77 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 78 | data.writeInt32(pid); |
| 79 | data.writeInt32(display); |
| 80 | data.writeInt32(w); |
| 81 | data.writeInt32(h); |
| 82 | data.writeInt32(format); |
| 83 | data.writeInt32(flags); |
| 84 | remote()->transact(CREATE_SURFACE, data, &reply); |
| 85 | params->readFromParcel(reply); |
| 86 | return interface_cast<ISurface>(reply.readStrongBinder()); |
| 87 | } |
| 88 | |
| 89 | virtual status_t destroySurface(SurfaceID sid) |
| 90 | { |
| 91 | Parcel data, reply; |
| 92 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 93 | data.writeInt32(sid); |
| 94 | remote()->transact(DESTROY_SURFACE, data, &reply); |
| 95 | return reply.readInt32(); |
| 96 | } |
| 97 | |
| 98 | virtual status_t setState(int32_t count, const layer_state_t* states) |
| 99 | { |
| 100 | Parcel data, reply; |
| 101 | data.writeInterfaceToken(ISurfaceFlingerClient::getInterfaceDescriptor()); |
| 102 | data.writeInt32(count); |
| 103 | for (int i=0 ; i<count ; i++) |
| 104 | states[i].write(data); |
| 105 | remote()->transact(SET_STATE, data, &reply); |
| 106 | return reply.readInt32(); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | IMPLEMENT_META_INTERFACE(SurfaceFlingerClient, "android.ui.ISurfaceFlingerClient"); |
| 111 | |
| 112 | // ---------------------------------------------------------------------- |
| 113 | |
| 114 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 115 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 116 | LOGW("Call incorrectly routed to " #interface); \ |
| 117 | return PERMISSION_DENIED; \ |
| 118 | } } while (0) |
| 119 | |
| 120 | status_t BnSurfaceFlingerClient::onTransact( |
| 121 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 122 | { |
| 123 | // codes that don't require permission check |
| 124 | |
| 125 | switch(code) { |
| 126 | case GET_CBLK: { |
| 127 | CHECK_INTERFACE(ISurfaceFlingerClient, data, reply); |
| 128 | sp<IMemory> ctl; |
| 129 | getControlBlocks(&ctl); |
| 130 | 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(); |
| 139 | const int self_pid = getpid(); |
| 140 | if (UNLIKELY(pid != self_pid)) { |
| 141 | // we're called from a different process, do the real check |
| 142 | if (!checkCallingPermission( |
| 143 | String16("android.permission.ACCESS_SURFACE_FLINGER"))) |
| 144 | { |
| 145 | const int uid = ipc->getCallingUid(); |
| 146 | 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(¶ms, 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 | |
| 190 | status_t ISurfaceFlingerClient::surface_data_t::readFromParcel(const Parcel& parcel) |
| 191 | { |
| 192 | token = parcel.readInt32(); |
| 193 | identity = parcel.readInt32(); |
| 194 | heap[0] = interface_cast<IMemoryHeap>(parcel.readStrongBinder()); |
| 195 | heap[1] = interface_cast<IMemoryHeap>(parcel.readStrongBinder()); |
| 196 | return NO_ERROR; |
| 197 | } |
| 198 | |
| 199 | status_t ISurfaceFlingerClient::surface_data_t::writeToParcel(Parcel* parcel) const |
| 200 | { |
| 201 | parcel->writeInt32(token); |
| 202 | parcel->writeInt32(identity); |
| 203 | parcel->writeStrongBinder(heap[0]!=0 ? heap[0]->asBinder() : NULL); |
| 204 | parcel->writeStrongBinder(heap[1]!=0 ? heap[1]->asBinder() : NULL); |
| 205 | return NO_ERROR; |
| 206 | } |
| 207 | |
| 208 | }; // namespace android |