summaryrefslogtreecommitdiff
path: root/aml_screen.cpp (plain)
blob: b35be42b8152ae85f631df5789c1ee45355f62c6
1/*
2 * Copyright (C) 2013 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//#define LOG_NDEBUG 0
18#define LOG_TAG "screen_source"
19#include <hardware/hardware.h>
20#include <hardware/aml_screen.h>
21
22#include <signal.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <errno.h>
29#include <sys/ioctl.h>
30#include <sys/mman.h>
31#include <sys/select.h>
32#include <linux/videodev2.h>
33#include <sys/time.h>
34
35
36#include <errno.h>
37#include <cutils/log.h>
38#include <cutils/atomic.h>
39
40#include "v4l2_vdin.h"
41
42#ifndef LOGD
43#define LOGD ALOGD
44#endif
45#ifndef LOGV
46#define LOGV ALOGV
47#endif
48#ifndef LOGE
49#define LOGE ALOGE
50#endif
51#ifndef LOGI
52#define LOGI ALOGI
53#endif
54
55static unsigned int gAmlScreenOpen = 0;
56static android::Mutex gAmlScreenLock;
57
58/*****************************************************************************/
59
60static int aml_screen_device_open(const struct hw_module_t* module, const char* name,
61 struct hw_device_t** device);
62
63static struct hw_module_methods_t aml_screen_module_methods = {
64 open: aml_screen_device_open
65};
66
67aml_screen_module_t HAL_MODULE_INFO_SYM = {
68 common: {
69 tag: HARDWARE_MODULE_TAG,
70 version_major: 1,
71 version_minor: 0,
72 id: AML_SCREEN_HARDWARE_MODULE_ID,
73 name: "aml screen source module",
74 author: "Amlogic",
75 methods: &aml_screen_module_methods,
76 dso : NULL,
77 reserved : {0},
78 }
79};
80
81/*****************************************************************************/
82
83static int aml_screen_device_close(struct hw_device_t *dev)
84{
85 android::vdin_screen_source* source = NULL;
86 aml_screen_device_t* ctx = (aml_screen_device_t*)dev;
87
88 android::Mutex::Autolock lock(gAmlScreenLock);
89 if (ctx) {
90 if (ctx->priv){
91 source = (android::vdin_screen_source*)ctx->priv;
92 delete source;
93 source = NULL;
94 }
95 free(ctx);
96 }
97 gAmlScreenOpen--;
98 return 0;
99}
100
101int screen_source_start(struct aml_screen_device* dev)
102{
103 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
104 return source->start();
105}
106
107int screen_source_stop(struct aml_screen_device* dev)
108{
109 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
110 return source->stop();
111}
112
113int screen_source_pause(struct aml_screen_device* dev)
114{
115 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
116 return source->pause();
117}
118
119int screen_source_get_format(struct aml_screen_device* dev)
120{
121 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
122 return source->get_format();
123}
124
125int screen_source_set_format(struct aml_screen_device* dev, int width, int height, int pix_format)
126{
127 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
128
129 if ((width > 0) && (height > 0) && ((pix_format == V4L2_PIX_FMT_NV21) || (pix_format == V4L2_PIX_FMT_YUV420))) {
130 return source->set_format(width, height, pix_format);
131 } else {
132 return source->set_format();
133 }
134}
135
136int screen_source_set_rotation(struct aml_screen_device* dev, int degree)
137{
138 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
139 return source->set_rotation(degree);
140}
141
142int screen_source_set_crop(struct aml_screen_device* dev, int x, int y, int width, int height)
143{
144 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
145
146 if ((x >= 0) && (y >= 0) && (width > 0) && (height > 0))
147 return source->set_crop(x, y, width, height);
148
149 return android::BAD_VALUE;
150}
151
152int screen_source_set_amlvideo2_crop(struct aml_screen_device* dev, int x, int y, int width, int height)
153{
154 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
155
156 if ((x >= 0) && (y >= 0) && (width > 0) && (height > 0))
157 return source->set_amlvideo2_crop(x, y, width, height);
158
159 return android::BAD_VALUE;
160}
161
162int screen_source_aquire_buffer(struct aml_screen_device* dev, aml_screen_buffer_info_t* buff_info)
163{
164 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
165
166 return source->aquire_buffer(buff_info);
167}
168
169int screen_source_release_buffer(struct aml_screen_device* dev, long* ptr)
170{
171 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
172 return source->release_buffer(ptr);
173}
174
175int screen_source_set_state_callback(struct aml_screen_device* dev, olStateCB callback)
176{
177 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
178 return source->set_state_callback(callback);
179}
180
181int screen_source_set_preview_window(struct aml_screen_device* dev, ANativeWindow* window)
182{
183 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
184 return source->set_preview_window(window);
185}
186
187int screen_source_set_data_callback(struct aml_screen_device* dev, app_data_callback callback, void* user)
188{
189 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
190 return source->set_data_callback(callback, user);
191}
192
193int screen_source_set_frame_rate(struct aml_screen_device* dev, int frameRate)
194{
195 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
196 return source->set_frame_rate(frameRate);
197}
198
199int screen_source_set_source_type(struct aml_screen_device* dev, SOURCETYPE sourceType)
200{
201 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
202 return source->set_source_type(sourceType);
203}
204
205int screen_source_get_source_type(struct aml_screen_device* dev)
206{
207 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
208 return source->get_source_type();
209}
210
211/* int screen_source_inc_buffer_refcount(struct aml_screen_device* dev, int* ptr)
212{
213 android::vdin_screen_source* source = (android::vdin_screen_source*)dev->priv;
214 return source->inc_buffer_refcount(ptr);
215} */
216
217/*****************************************************************************/
218
219static int aml_screen_device_open(const struct hw_module_t* module, const char* name,
220 struct hw_device_t** device)
221{
222 int status = -EINVAL;
223 android::vdin_screen_source* source = NULL;
224 android::Mutex::Autolock lock(gAmlScreenLock);
225
226 LOGV("aml_screen_device_open");
227
228 if (!strcmp(name, AML_SCREEN_SOURCE)) {
229 if(gAmlScreenOpen > 1){
230 ALOGD("aml screen device already open");
231 *device = NULL;
232 return -EINVAL;
233 }
234
235 aml_screen_device_t *dev = (aml_screen_device_t*)malloc(sizeof(aml_screen_device_t));
236
237 if (!dev) {
238 LOGE("no memory for the screen source device");
239 return -ENOMEM;
240 }
241 /* initialize handle here */
242 memset(dev, 0, sizeof(*dev));
243
244 source = new android::vdin_screen_source;
245 if (!source) {
246 LOGE("no memory for class of vdin_screen_source");
247 free (dev);
248 return -ENOMEM;
249 }
250
251 if (source->init()!= 0) {
252 LOGE("open vdin_screen_source failed!");
253 free (dev);
254 return -1;
255 }
256
257 dev->priv = (void*)source;
258
259 /* initialize the procs */
260 dev->common.tag = HARDWARE_DEVICE_TAG;
261 dev->common.version = 0;
262 dev->common.module = const_cast<hw_module_t*>(module);
263 dev->common.close = aml_screen_device_close;
264
265 dev->ops.start = screen_source_start;
266 dev->ops.stop = screen_source_stop;
267 dev->ops.pause = screen_source_pause;
268 dev->ops.get_format = screen_source_get_format;
269 dev->ops.set_format = screen_source_set_format;
270 dev->ops.set_rotation = screen_source_set_rotation;
271 dev->ops.set_crop = screen_source_set_crop;
272 dev->ops.set_amlvideo2_crop = screen_source_set_amlvideo2_crop;
273 dev->ops.aquire_buffer = screen_source_aquire_buffer;
274 dev->ops.release_buffer = screen_source_release_buffer;
275 dev->ops.setStateCallBack = screen_source_set_state_callback;
276 dev->ops.setPreviewWindow = screen_source_set_preview_window;
277 dev->ops.setDataCallBack = screen_source_set_data_callback;
278 dev->ops.set_frame_rate = screen_source_set_frame_rate;
279 dev->ops.set_source_type = screen_source_set_source_type;
280 dev->ops.get_source_type = screen_source_get_source_type;
281 // dev->ops.inc_buffer_refcount = screen_source_inc_buffer_refcount;
282 *device = &dev->common;
283 status = 0;
284 gAmlScreenOpen++;
285 }
286 return status;
287}
288