1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __HWJPEG_INTERNAL_H__
19 #define __HWJPEG_INTERNAL_H__
20 
21 #ifndef LOG_TAG
22 #error "LOG_TAG is not defined!"
23 #endif
24 
25 #include <cerrno>
26 #include <cstring>
27 
28 #include <unistd.h>
29 #include <log/log.h>
30 #include <time.h>
31 #include <sys/ioctl.h>
32 
33 #ifdef __GNUC__
34 #  define __UNUSED__ __attribute__((__unused__))
35 #else
36 #  define __UNUSED__
37 #endif
38 
39 #ifndef ALOGERR
40 #define ALOGERR(fmt, args...) ((void)ALOG(LOG_ERROR, LOG_TAG, fmt " [%s]", ##args, strerror(errno)))
41 #endif
42 
43 #define V4L2_CID_JPEG_SEC_COMP_QUALITY (V4L2_CID_JPEG_CLASS_BASE + 20)
44 #define V4L2_CID_JPEG_QTABLES2         (V4L2_CID_JPEG_CLASS_BASE + 22)
45 #define V4L2_CID_JPEG_HWFC_ENABLE      (V4L2_CID_JPEG_CLASS_BASE + 25)
46 
47 #define TO_MAIN_SIZE(val)   ((val) & 0xFFFF)
48 #define TO_THUMB_SIZE(val)  (((val) & 0xFFFF) << 16)
49 #define TO_IMAGE_SIZE(main, thumb) (TO_MAIN_SIZE(main) | TO_THUMB_SIZE(thumb))
50 
51 #define PTR_TO_ULONG(ptr)   reinterpret_cast<unsigned long>(ptr)
52 #define PTR_DIFF(ptr1, ptr2) (reinterpret_cast<size_t>(ptr2) - reinterpret_cast<size_t>(ptr1))
53 
54 #define ARRSIZE(v) (sizeof(v) / sizeof(v[0]))
55 
56 #ifndef min
57 template <typename T>
min(T val1,T val2)58 static inline T min(T val1, T val2) {
59     return (val1 > val2) ? val2 : val1;
60 }
61 #endif
62 
63 #ifndef max
64 template <typename T>
max(T val1,T val2)65 static inline T max(T val1, T val2) {
66     return (val1 < val2) ? val2 : val1;
67 }
68 #endif
69 
70 // H/W requires 16-byte alignment
71 #define HW_BASE_ALIGN_BITS 4
72 #define HW_BASE_ALIGN_SIZE (1 << HW_BASE_ALIGN_BITS)
73 #define HW_BASE_ALIGN_MASK ~(HW_BASE_ALIGN_SIZE - 1)
74 
75 class CStopWatch {
76     timespec m_tBegin;
77 public:
78     CStopWatch(bool start = false) {
79         if (start)
80             Start();
81     }
82     bool Start();
83     unsigned long GetElapsed();
84     unsigned long GetElapsedUpdate();
85 };
86 
87 bool WriteToFile(const char *path, const char *data, size_t len);
88 bool WriteToFile(const char *path, int dmabuf, size_t len);
89 #endif //__HWJPEG_INTERNAL_H__
90