blob: 293466405aca63c11aa4d666efc98aec67a20294
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 | #include <signal.h> |
17 | #include <stdio.h> |
18 | #include <stdlib.h> |
19 | #include <string.h> |
20 | #include <fcntl.h> |
21 | #include <unistd.h> |
22 | #include <errno.h> |
23 | #include <sys/ioctl.h> |
24 | #include <sys/mman.h> |
25 | #include <sys/select.h> |
26 | #include <linux/videodev2.h> |
27 | #include <sys/time.h> |
28 | |
29 | #include <utils/KeyedVector.h> |
30 | #include <cutils/properties.h> |
31 | #include <sys/types.h> |
32 | #include <sys/stat.h> |
33 | #include <utils/threads.h> |
34 | #include <android/native_window.h> |
35 | #include <system/window.h> |
36 | #include <gralloc_priv.h> |
37 | |
38 | #include <aml_screen.h> |
39 | |
40 | namespace android { |
41 | |
42 | #define NB_BUFFER 6 |
43 | |
44 | struct VideoInfo { |
45 | struct v4l2_capability cap; |
46 | struct v4l2_format format; |
47 | struct v4l2_buffer buf; |
48 | struct v4l2_requestbuffers rb; |
49 | long *mem[NB_BUFFER]; |
50 | unsigned canvas[NB_BUFFER]; |
51 | unsigned refcount[NB_BUFFER]; |
52 | bool isStreaming; |
53 | int width; |
54 | int height; |
55 | int formatIn; |
56 | int framesizeIn; |
57 | int displaymode; |
58 | int dimming_flag; |
59 | }; |
60 | enum State{ |
61 | START, |
62 | PAUSE, |
63 | STOPING, |
64 | STOP, |
65 | }; |
66 | |
67 | enum FrameType{ |
68 | NATIVE_WINDOW_DATA = 0x1, |
69 | CALL_BACK_DATA = 0x2, |
70 | }; |
71 | |
72 | typedef void (*olStateCB)(int state); |
73 | |
74 | typedef void (*app_data_callback)(void *user, aml_screen_buffer_info_t *buff_info); |
75 | |
76 | #define SCREENSOURCE_GRALLOC_USAGE GRALLOC_USAGE_HW_TEXTURE | \ |
77 | GRALLOC_USAGE_HW_RENDER | \ |
78 | GRALLOC_USAGE_SW_READ_RARELY | \ |
79 | GRALLOC_USAGE_SW_WRITE_NEVER |
80 | /** |
81 | * set_port_type() parameter description: |
82 | portType is consisted by 32-bit binary. |
83 | bit 28 : start tvin service flag, 1 : enable, 0 : disable. |
84 | bit 24 : vdin device num : 0 or 1, which means use vdin0 or vdin1. |
85 | bit 15~0 : tvin port type --TVIN_PORT_VIU,TVIN_PORT_HDMI0... |
86 | (port type define in tvin.h) |
87 | */ |
88 | class vdin_screen_source { |
89 | public: |
90 | vdin_screen_source(); |
91 | ~vdin_screen_source(); |
92 | int init(int id); |
93 | int start(); |
94 | int stop(); |
95 | int pause(); |
96 | int get_format(); |
97 | int set_format(int width = 640, int height = 480, int color_format = V4L2_PIX_FMT_NV21); |
98 | int set_rotation(int degree); |
99 | int set_crop(int x, int y, int width, int height); |
100 | int get_amlvideo2_crop(int *x, int *y, int *width, int *height); |
101 | int set_amlvideo2_crop(int x, int y, int width, int height); |
102 | int aquire_buffer(aml_screen_buffer_info_t *buff_info); |
103 | // int inc_buffer_refcount(int* ptr); |
104 | int release_buffer(long* ptr); |
105 | int set_state_callback(olStateCB callback); |
106 | int set_data_callback(app_data_callback callback, void* user); |
107 | int set_preview_window(ANativeWindow* window); |
108 | int set_frame_rate(int frameRate); |
109 | int get_current_sourcesize(int * width,int * height); |
110 | int set_screen_mode(int mode); |
111 | int start_v4l2_device(); |
112 | int stop_v4l2_device(); |
113 | int get_port_type(); |
114 | int set_port_type(unsigned int portType); |
115 | int set_mode(int display_mode); |
116 | int microdimming(long* src, unsigned char *dest); |
117 | private: |
118 | int init_native_window(); |
119 | int workThread(); |
120 | private: |
121 | class WorkThread : public Thread { |
122 | vdin_screen_source* mSource; |
123 | public: |
124 | WorkThread(vdin_screen_source* source) : |
125 | Thread(false), mSource(source) { } |
126 | virtual void onFirstRef() { |
127 | run("vdin screen source work thread", PRIORITY_URGENT_DISPLAY); |
128 | } |
129 | virtual bool threadLoop() { |
130 | mSource->workThread(); |
131 | // loop until we need to quit |
132 | return true; |
133 | } |
134 | }; |
135 | private: |
136 | int mCurrentIndex; |
137 | KeyedVector<long *, long> mBufs; |
138 | KeyedVector<long *, long> mTemp_Bufs; |
139 | int mBufferCount; |
140 | int mFrameWidth; |
141 | int mFrameHeight; |
142 | int mBufferSize; |
143 | unsigned int flex_ratio; |
144 | unsigned int flex_original; |
145 | int mFramecount = 0; |
146 | int m_FrameHeight = 0; |
147 | int m_FrameWidth = 0; |
148 | int m_rest = 0; |
149 | int m_displaymode; |
150 | volatile int mState; |
151 | olStateCB mSetStateCB; |
152 | int mPixelFormat; |
153 | int mNativeWindowPixelFormat; |
154 | sp<ANativeWindow> mANativeWindow; |
155 | sp<WorkThread> mWorkThread; |
156 | mutable Mutex mLock; |
157 | int mCameraHandle; |
158 | struct VideoInfo *mVideoInfo; |
159 | int mFrameType; |
160 | app_data_callback mDataCB; |
161 | bool mOpen; |
162 | void *mUser; |
163 | long * src_temp[4]; |
164 | }; |
165 | |
166 | } |