blob: 0fea6f9388a09995a13d1299fda2dceaa0e0fb91 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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 <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Parcel.h>
24#include <utils/IMemory.h>
25#include <utils/IPCThreadState.h>
26#include <utils/IServiceManager.h>
27
28#include <ui/ISurfaceComposer.h>
29#include <ui/DisplayInfo.h>
30
31// ---------------------------------------------------------------------------
32
33#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
34#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
35
36// ---------------------------------------------------------------------------
37
38namespace android {
39
40class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
41{
42public:
43 BpSurfaceComposer(const sp<IBinder>& impl)
44 : BpInterface<ISurfaceComposer>(impl)
45 {
46 }
47
48 virtual sp<ISurfaceFlingerClient> createConnection()
49 {
50 uint32_t n;
51 Parcel data, reply;
52 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
53 remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
54 return interface_cast<ISurfaceFlingerClient>(reply.readStrongBinder());
55 }
56
57 virtual sp<IMemory> getCblk() const
58 {
59 Parcel data, reply;
60 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
61 remote()->transact(BnSurfaceComposer::GET_CBLK, data, &reply);
62 return interface_cast<IMemory>(reply.readStrongBinder());
63 }
64
65 virtual void openGlobalTransaction()
66 {
67 Parcel data, reply;
68 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
69 remote()->transact(BnSurfaceComposer::OPEN_GLOBAL_TRANSACTION, data, &reply);
70 }
71
72 virtual void closeGlobalTransaction()
73 {
74 Parcel data, reply;
75 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
76 remote()->transact(BnSurfaceComposer::CLOSE_GLOBAL_TRANSACTION, data, &reply);
77 }
78
79 virtual status_t freezeDisplay(DisplayID dpy, uint32_t flags)
80 {
81 Parcel data, reply;
82 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
83 data.writeInt32(dpy);
84 data.writeInt32(flags);
85 remote()->transact(BnSurfaceComposer::FREEZE_DISPLAY, data, &reply);
86 return reply.readInt32();
87 }
88
89 virtual status_t unfreezeDisplay(DisplayID dpy, uint32_t flags)
90 {
91 Parcel data, reply;
92 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
93 data.writeInt32(dpy);
94 data.writeInt32(flags);
95 remote()->transact(BnSurfaceComposer::UNFREEZE_DISPLAY, data, &reply);
96 return reply.readInt32();
97 }
98
99 virtual int setOrientation(DisplayID dpy, int orientation)
100 {
101 Parcel data, reply;
102 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
103 data.writeInt32(dpy);
104 data.writeInt32(orientation);
105 remote()->transact(BnSurfaceComposer::SET_ORIENTATION, data, &reply);
106 return reply.readInt32();
107 }
108
109 virtual void bootFinished()
110 {
111 Parcel data, reply;
112 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
113 remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
114 }
115
116 virtual status_t requestGPU(
117 const sp<IGPUCallback>& callback, gpu_info_t* gpu)
118 {
119 Parcel data, reply;
120 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
121 data.writeStrongBinder(callback->asBinder());
122 remote()->transact(BnSurfaceComposer::REQUEST_GPU, data, &reply);
123 gpu->regs = interface_cast<IMemory>(reply.readStrongBinder());
124 gpu->count = reply.readInt32();
125
126 // FIXME: for now, we don't dynamically allocate the regions array
127 size_t maxCount = sizeof(gpu->regions)/sizeof(*gpu->regions);
128 if (gpu->count > maxCount)
129 return BAD_VALUE;
130
131 for (size_t i=0 ; i<gpu->count ; i++) {
132 gpu->regions[i].region = interface_cast<IMemory>(reply.readStrongBinder());
133 gpu->regions[i].reserved = reply.readInt32();
134 }
135 return reply.readInt32();
136 }
137
138 virtual status_t revokeGPU()
139 {
140 Parcel data, reply;
141 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
142 remote()->transact(BnSurfaceComposer::REVOKE_GPU, data, &reply);
143 return reply.readInt32();
144 }
145
146 virtual void signal() const
147 {
148 Parcel data, reply;
149 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
150 remote()->transact(BnSurfaceComposer::SIGNAL, data, &reply, IBinder::FLAG_ONEWAY);
151 }
152};
153
154IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
155
156// ----------------------------------------------------------------------
157
158#define CHECK_INTERFACE(interface, data, reply) \
159 do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
160 LOGW("Call incorrectly routed to " #interface); \
161 return PERMISSION_DENIED; \
162 } } while (0)
163
164status_t BnSurfaceComposer::onTransact(
165 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
166{
167 status_t err = BnInterface<ISurfaceComposer>::onTransact(code, data, reply, flags);
168 if (err == NO_ERROR)
169 return err;
170
171 CHECK_INTERFACE(ISurfaceComposer, data, reply);
172
173 switch(code) {
174 case CREATE_CONNECTION: {
175 sp<IBinder> b = createConnection()->asBinder();
176 reply->writeStrongBinder(b);
177 } break;
178 case OPEN_GLOBAL_TRANSACTION: {
179 openGlobalTransaction();
180 } break;
181 case CLOSE_GLOBAL_TRANSACTION: {
182 closeGlobalTransaction();
183 } break;
184 case SET_ORIENTATION: {
185 DisplayID dpy = data.readInt32();
186 int orientation = data.readInt32();
187 reply->writeInt32( setOrientation(dpy, orientation) );
188 } break;
189 case FREEZE_DISPLAY: {
190 DisplayID dpy = data.readInt32();
191 uint32_t flags = data.readInt32();
192 reply->writeInt32( freezeDisplay(dpy, flags) );
193 } break;
194 case UNFREEZE_DISPLAY: {
195 DisplayID dpy = data.readInt32();
196 uint32_t flags = data.readInt32();
197 reply->writeInt32( unfreezeDisplay(dpy, flags) );
198 } break;
199 case BOOT_FINISHED: {
200 bootFinished();
201 } break;
202 case REVOKE_GPU: {
203 reply->writeInt32( revokeGPU() );
204 } break;
205 case SIGNAL: {
206 signal();
207 } break;
208 case GET_CBLK: {
209 sp<IBinder> b = getCblk()->asBinder();
210 reply->writeStrongBinder(b);
211 } break;
212 case REQUEST_GPU: {
213 // TODO: this should be protected by a permission
214 gpu_info_t info;
215 sp<IGPUCallback> callback
216 = interface_cast<IGPUCallback>(data.readStrongBinder());
217 status_t res = requestGPU(callback, &info);
218
219 // FIXME: for now, we don't dynamically allocate the regions array
220 size_t maxCount = sizeof(info.regions)/sizeof(*info.regions);
221 if (info.count > maxCount)
222 return BAD_VALUE;
223
224 reply->writeStrongBinder(info.regs->asBinder());
225 reply->writeInt32(info.count);
226 for (size_t i=0 ; i<info.count ; i++) {
227 reply->writeStrongBinder(info.regions[i].region->asBinder());
228 reply->writeInt32(info.regions[i].reserved);
229 }
230 reply->writeInt32(res);
231 } break;
232 default:
233 return UNKNOWN_TRANSACTION;
234 }
235 return NO_ERROR;
236}
237
238// ----------------------------------------------------------------------------
239
240enum {
241 // Note: BOOT_FINISHED must remain this value, it is called by ActivityManagerService.
242 GPU_LOST = IBinder::FIRST_CALL_TRANSACTION
243};
244
245class BpGPUCallback : public BpInterface<IGPUCallback>
246{
247public:
248 BpGPUCallback(const sp<IBinder>& impl)
249 : BpInterface<IGPUCallback>(impl)
250 {
251 }
252
253 virtual void gpuLost()
254 {
255 Parcel data, reply;
256 data.writeInterfaceToken(IGPUCallback::getInterfaceDescriptor());
257 remote()->transact(GPU_LOST, data, &reply, IBinder::FLAG_ONEWAY);
258 }
259};
260
261IMPLEMENT_META_INTERFACE(GPUCallback, "android.ui.IGPUCallback");
262
263status_t BnGPUCallback::onTransact(
264 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
265{
266 switch(code) {
267 case GPU_LOST: {
268 CHECK_INTERFACE(IGPUCallback, data, reply);
269 gpuLost();
270 return NO_ERROR;
271 } break;
272 default:
273 return BBinder::onTransact(code, data, reply, flags);
274 }
275}
276
277};