blob: 5f558a139ca25d868e884e8fe4bc6dd2c2ce43c7 [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 <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/Parcel.h>
24#include <binder/IMemory.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
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
Mathias Agopianeb0c86e2009-03-27 18:11:38 -070099 virtual int setOrientation(DisplayID dpy, int orientation, uint32_t flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 {
101 Parcel data, reply;
102 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
103 data.writeInt32(dpy);
104 data.writeInt32(orientation);
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700105 data.writeInt32(flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 remote()->transact(BnSurfaceComposer::SET_ORIENTATION, data, &reply);
107 return reply.readInt32();
108 }
109
110 virtual void bootFinished()
111 {
112 Parcel data, reply;
113 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
114 remote()->transact(BnSurfaceComposer::BOOT_FINISHED, data, &reply);
115 }
116
117 virtual status_t requestGPU(
118 const sp<IGPUCallback>& callback, gpu_info_t* gpu)
119 {
120 Parcel data, reply;
121 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
122 data.writeStrongBinder(callback->asBinder());
123 remote()->transact(BnSurfaceComposer::REQUEST_GPU, data, &reply);
124 gpu->regs = interface_cast<IMemory>(reply.readStrongBinder());
125 gpu->count = reply.readInt32();
126
127 // FIXME: for now, we don't dynamically allocate the regions array
128 size_t maxCount = sizeof(gpu->regions)/sizeof(*gpu->regions);
129 if (gpu->count > maxCount)
130 return BAD_VALUE;
131
132 for (size_t i=0 ; i<gpu->count ; i++) {
133 gpu->regions[i].region = interface_cast<IMemory>(reply.readStrongBinder());
134 gpu->regions[i].reserved = reply.readInt32();
135 }
136 return reply.readInt32();
137 }
138
139 virtual status_t revokeGPU()
140 {
141 Parcel data, reply;
142 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
143 remote()->transact(BnSurfaceComposer::REVOKE_GPU, data, &reply);
144 return reply.readInt32();
145 }
146
147 virtual void signal() const
148 {
149 Parcel data, reply;
150 data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
151 remote()->transact(BnSurfaceComposer::SIGNAL, data, &reply, IBinder::FLAG_ONEWAY);
152 }
153};
154
155IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
156
157// ----------------------------------------------------------------------
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159status_t BnSurfaceComposer::onTransact(
160 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
161{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 switch(code) {
163 case CREATE_CONNECTION: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700164 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 sp<IBinder> b = createConnection()->asBinder();
166 reply->writeStrongBinder(b);
167 } break;
168 case OPEN_GLOBAL_TRANSACTION: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700169 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 openGlobalTransaction();
171 } break;
172 case CLOSE_GLOBAL_TRANSACTION: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700173 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 closeGlobalTransaction();
175 } break;
176 case SET_ORIENTATION: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700177 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 DisplayID dpy = data.readInt32();
179 int orientation = data.readInt32();
Mathias Agopianeb0c86e2009-03-27 18:11:38 -0700180 uint32_t flags = data.readInt32();
181 reply->writeInt32( setOrientation(dpy, orientation, flags) );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 } break;
183 case FREEZE_DISPLAY: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700184 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 DisplayID dpy = data.readInt32();
186 uint32_t flags = data.readInt32();
187 reply->writeInt32( freezeDisplay(dpy, flags) );
188 } break;
189 case UNFREEZE_DISPLAY: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700190 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 DisplayID dpy = data.readInt32();
192 uint32_t flags = data.readInt32();
193 reply->writeInt32( unfreezeDisplay(dpy, flags) );
194 } break;
195 case BOOT_FINISHED: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700196 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 bootFinished();
198 } break;
199 case REVOKE_GPU: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700200 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 reply->writeInt32( revokeGPU() );
202 } break;
203 case SIGNAL: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700204 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 signal();
206 } break;
207 case GET_CBLK: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700208 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 sp<IBinder> b = getCblk()->asBinder();
210 reply->writeStrongBinder(b);
211 } break;
212 case REQUEST_GPU: {
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700213 CHECK_INTERFACE(ISurfaceComposer, data, reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 // TODO: this should be protected by a permission
215 gpu_info_t info;
216 sp<IGPUCallback> callback
217 = interface_cast<IGPUCallback>(data.readStrongBinder());
218 status_t res = requestGPU(callback, &info);
219
220 // FIXME: for now, we don't dynamically allocate the regions array
221 size_t maxCount = sizeof(info.regions)/sizeof(*info.regions);
222 if (info.count > maxCount)
223 return BAD_VALUE;
224
225 reply->writeStrongBinder(info.regs->asBinder());
226 reply->writeInt32(info.count);
227 for (size_t i=0 ; i<info.count ; i++) {
228 reply->writeStrongBinder(info.regions[i].region->asBinder());
229 reply->writeInt32(info.regions[i].reserved);
230 }
231 reply->writeInt32(res);
232 } break;
233 default:
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700234 return BBinder::onTransact(code, data, reply, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236 return NO_ERROR;
237}
238
239// ----------------------------------------------------------------------------
240
241enum {
242 // Note: BOOT_FINISHED must remain this value, it is called by ActivityManagerService.
243 GPU_LOST = IBinder::FIRST_CALL_TRANSACTION
244};
245
246class BpGPUCallback : public BpInterface<IGPUCallback>
247{
248public:
249 BpGPUCallback(const sp<IBinder>& impl)
250 : BpInterface<IGPUCallback>(impl)
251 {
252 }
253
254 virtual void gpuLost()
255 {
256 Parcel data, reply;
257 data.writeInterfaceToken(IGPUCallback::getInterfaceDescriptor());
258 remote()->transact(GPU_LOST, data, &reply, IBinder::FLAG_ONEWAY);
259 }
260};
261
262IMPLEMENT_META_INTERFACE(GPUCallback, "android.ui.IGPUCallback");
263
264status_t BnGPUCallback::onTransact(
265 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
266{
267 switch(code) {
268 case GPU_LOST: {
269 CHECK_INTERFACE(IGPUCallback, data, reply);
270 gpuLost();
271 return NO_ERROR;
272 } break;
273 default:
274 return BBinder::onTransact(code, data, reply, flags);
275 }
276}
277
278};