2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Overlay"
19 #include <hardware/hardware.h>
20 #include <hardware/overlay.h>
25 #include <cutils/log.h>
26 #include <cutils/atomic.h>
28 /*****************************************************************************/
30 struct overlay_control_context_t {
31 struct overlay_control_device_t device;
32 /* our private state goes below here */
35 struct overlay_data_context_t {
36 struct overlay_data_device_t device;
37 /* our private state goes below here */
40 static int overlay_device_open(const struct hw_module_t* module, const char* name,
41 struct hw_device_t** device);
43 static struct hw_module_methods_t overlay_module_methods = {
44 open: overlay_device_open
47 const struct overlay_module_t HAL_MODULE_INFO_SYM = {
49 tag: HARDWARE_MODULE_TAG,
52 id: OVERLAY_HARDWARE_MODULE_ID,
53 name: "Sample Overlay module",
54 author: "The Android Open Source Project",
55 methods: &overlay_module_methods,
59 /*****************************************************************************/
62 * This is the overlay_t object, it is returned to the user and represents
64 * This handles will be passed across processes and possibly given to other
65 * HAL modules (for instance video decode modules).
68 class overlay_object : public overlay_t {
70 struct handle_t : public native_handle {
71 /* add the data fields we need here, for instance: */
78 static overlay_handle_t getHandleRef(struct overlay_t* overlay) {
79 /* returns a reference to the handle, caller doesn't take ownership */
80 return &(static_cast<overlay_object *>(overlay)->mHandle);
85 this->overlay_t::getHandleRef = getHandleRef;
86 mHandle.version = sizeof(native_handle);
88 mHandle.numInts = 2; // extra ints we have in our handle
92 // ****************************************************************************
94 // ****************************************************************************
96 static int overlay_get(struct overlay_control_device_t *dev, int name) {
99 case OVERLAY_MINIFICATION_LIMIT:
100 result = 0; // 0 = no limit
102 case OVERLAY_MAGNIFICATION_LIMIT:
103 result = 0; // 0 = no limit
105 case OVERLAY_SCALING_FRAC_BITS:
106 result = 0; // 0 = infinite
108 case OVERLAY_ROTATION_STEP_DEG:
109 result = 90; // 90 rotation steps (for instance)
111 case OVERLAY_HORIZONTAL_ALIGNMENT:
112 result = 1; // 1-pixel alignment
114 case OVERLAY_VERTICAL_ALIGNMENT:
115 result = 1; // 1-pixel alignment
117 case OVERLAY_WIDTH_ALIGNMENT:
118 result = 1; // 1-pixel alignment
120 case OVERLAY_HEIGHT_ALIGNMENT:
121 result = 1; // 1-pixel alignment
127 static overlay_t* overlay_createOverlay(struct overlay_control_device_t *dev,
128 uint32_t w, uint32_t h, int32_t format)
130 /* check the input params, reject if not supported or invalid */
132 case OVERLAY_FORMAT_RGBA_8888:
133 case OVERLAY_FORMAT_RGB_565:
134 case OVERLAY_FORMAT_BGRA_8888:
135 case OVERLAY_FORMAT_YCbCr_422_SP:
136 case OVERLAY_FORMAT_YCbCr_420_SP:
137 case OVERLAY_FORMAT_YCbCr_422_I:
138 case OVERLAY_FORMAT_YCbCr_420_I:
144 /* Create overlay object. Talk to the h/w here and adjust to what it can
145 * do. the overlay_t returned can be a C++ object, subclassing overlay_t
148 * we probably want to keep a list of the overlay_t created so they can
149 * all be cleaned up in overlay_close().
151 return new overlay_object( /* pass needed params here*/ );
154 static void overlay_destroyOverlay(struct overlay_control_device_t *dev,
157 /* free resources associated with this overlay_t */
161 static int overlay_setPosition(struct overlay_control_device_t *dev,
163 int x, int y, uint32_t w, uint32_t h) {
164 /* set this overlay's position (talk to the h/w) */
168 static int overlay_getPosition(struct overlay_control_device_t *dev,
170 int* x, int* y, uint32_t* w, uint32_t* h) {
171 /* get this overlay's position */
175 static int overlay_setParameter(struct overlay_control_device_t *dev,
176 overlay_t* overlay, int param, int value) {
179 /* set this overlay's parameter (talk to the h/w) */
181 case OVERLAY_ROTATION_DEG:
182 /* if only 90 rotations are supported, the call fails
183 * for other values */
187 case OVERLAY_TRANSFORM:
188 // see OVERLAY_TRANSFORM_*
197 static int overlay_control_close(struct hw_device_t *dev)
199 struct overlay_control_context_t* ctx = (struct overlay_control_context_t*)dev;
201 /* free all resources associated with this device here
202 * in particular the overlay_handle_t, outstanding overlay_t, etc...
209 // ****************************************************************************
211 // ****************************************************************************
213 int overlay_initialize(struct overlay_data_device_t *dev,
214 overlay_handle_t handle)
217 * overlay_handle_t should contain all the information to "inflate" this
218 * overlay. Typically it'll have a file descriptor, informations about
219 * how many buffers are there, etc...
220 * It is also the place to mmap all buffers associated with this overlay
221 * (see getBufferAddress).
223 * NOTE: this function doesn't take ownership of overlay_handle_t
230 int overlay_dequeueBuffer(struct overlay_data_device_t *dev,
231 overlay_buffer_t* buf)
233 /* blocks until a buffer is available and return an opaque structure
234 * representing this buffer.
239 int overlay_queueBuffer(struct overlay_data_device_t *dev,
240 overlay_buffer_t buffer)
242 /* Mark this buffer for posting and recycle or free overlay_buffer_t. */
246 void *overlay_getBufferAddress(struct overlay_data_device_t *dev,
247 overlay_buffer_t buffer)
249 /* this may fail (NULL) if this feature is not supported. In that case,
250 * presumably, there is some other HAL module that can fill the buffer,
251 * using a DSP for instance */
255 static int overlay_data_close(struct hw_device_t *dev)
257 struct overlay_data_context_t* ctx = (struct overlay_data_context_t*)dev;
259 /* free all resources associated with this device here
260 * in particular all pending overlay_buffer_t if needed.
262 * NOTE: overlay_handle_t passed in initialize() is NOT freed and
263 * its file descriptors are not closed (this is the responsibility
271 /*****************************************************************************/
273 static int overlay_device_open(const struct hw_module_t* module, const char* name,
274 struct hw_device_t** device)
276 int status = -EINVAL;
277 if (!strcmp(name, OVERLAY_HARDWARE_CONTROL)) {
278 struct overlay_control_context_t *dev;
279 dev = (overlay_control_context_t*)malloc(sizeof(*dev));
281 /* initialize our state here */
282 memset(dev, 0, sizeof(*dev));
284 /* initialize the procs */
285 dev->device.common.tag = HARDWARE_DEVICE_TAG;
286 dev->device.common.version = 0;
287 dev->device.common.module = const_cast<hw_module_t*>(module);
288 dev->device.common.close = overlay_control_close;
290 dev->device.get = overlay_get;
291 dev->device.createOverlay = overlay_createOverlay;
292 dev->device.destroyOverlay = overlay_destroyOverlay;
293 dev->device.setPosition = overlay_setPosition;
294 dev->device.getPosition = overlay_getPosition;
295 dev->device.setParameter = overlay_setParameter;
297 *device = &dev->device.common;
299 } else if (!strcmp(name, OVERLAY_HARDWARE_DATA)) {
300 struct overlay_data_context_t *dev;
301 dev = (overlay_data_context_t*)malloc(sizeof(*dev));
303 /* initialize our state here */
304 memset(dev, 0, sizeof(*dev));
306 /* initialize the procs */
307 dev->device.common.tag = HARDWARE_DEVICE_TAG;
308 dev->device.common.version = 0;
309 dev->device.common.module = const_cast<hw_module_t*>(module);
310 dev->device.common.close = overlay_data_close;
312 dev->device.initialize = overlay_initialize;
313 dev->device.dequeueBuffer = overlay_dequeueBuffer;
314 dev->device.queueBuffer = overlay_queueBuffer;
315 dev->device.getBufferAddress = overlay_getBufferAddress;
317 *device = &dev->device.common;