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