1 /*
2 * xcam_common.h - xcam common and utilities
3 *
4 * Copyright (c) 2014 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #ifndef XCAM_COMMON_H
22 #define XCAM_COMMON_H
23
24 #include <string.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <math.h>
33 #include "xcam_defs.h"
34
35 #if defined(__linux__)
36 #include <pthread.h>
37 #endif
38
39 XCAM_BEGIN_DECLARE
40
41 typedef enum {
42 XCAM_RETURN_NO_ERROR = 0,
43 XCAM_RETURN_BYPASS = 1,
44
45 /* errors */
46 XCAM_RETURN_ERROR_FAILED = -1,
47 XCAM_RETURN_ERROR_PARAM = -2,
48 XCAM_RETURN_ERROR_MEM = -3,
49 XCAM_RETURN_ERROR_FILE = -4,
50 XCAM_RETURN_ERROR_ANALYZER = -5,
51 XCAM_RETURN_ERROR_ISP = -6,
52 XCAM_RETURN_ERROR_SENSOR = -7,
53 XCAM_RETURN_ERROR_THREAD = -8,
54 XCAM_RETURN_ERROR_IOCTL = -9,
55 XCAM_RETURN_ERROR_ORDER = -10,
56 XCAM_RETURN_ERROR_TIMEOUT = -20,
57 XCAM_RETURN_ERROR_OUTOFRANGE = -21,
58 XCAM_RETURN_ERROR_UNKNOWN = -255,
59 } XCamReturn;
60
61 #define xcam_malloc_type(TYPE) (TYPE*)(xcam_malloc(sizeof(TYPE)))
62 #define xcam_malloc_type_array(TYPE, num) (TYPE*)(xcam_malloc(sizeof(TYPE) * (num)))
63
64 #define xcam_malloc0_type(TYPE) (TYPE*)(xcam_malloc0(sizeof(TYPE)))
65 #define xcam_malloc0_type_array(TYPE, num) (TYPE*)(xcam_malloc0(sizeof(TYPE) * (num)))
66
67 #define xcam_mem_clear(v_stack) memset(&(v_stack), 0, sizeof(v_stack))
68
69 void * xcam_malloc (size_t size);
70 void * xcam_malloc0 (size_t size);
71
72 void xcam_free (void *ptr);
73
74 /*
75 * return, 0 successfully
76 * else, check errno
77 */
78 int xcam_device_ioctl (int fd, unsigned long cmd, void *arg);
79 const char *xcam_fourcc_to_string (uint32_t fourcc);
80
81 static inline uint32_t
xcam_ceil(uint32_t value,const uint32_t align)82 xcam_ceil (uint32_t value, const uint32_t align) {
83 return (value + align - 1) / align * align;
84 }
85
86 static inline uint32_t
xcam_around(uint32_t value,const uint32_t align)87 xcam_around (uint32_t value, const uint32_t align) {
88 return (value + align / 2) / align * align;
89 }
90
91 static inline uint32_t
xcam_floor(uint32_t value,const uint32_t align)92 xcam_floor (uint32_t value, const uint32_t align) {
93 return value / align * align;
94 }
95
96 // return true or false
97 static inline int
xcam_ret_is_ok(XCamReturn err)98 xcam_ret_is_ok (XCamReturn err) {
99 return err >= XCAM_RETURN_NO_ERROR;
100 }
101
102 //format to [0 ~ 360]
103 static inline float
format_angle(float angle)104 format_angle (float angle)
105 {
106 if (angle < 0.0f)
107 angle += 360.0f;
108 if (angle >= 360.0f)
109 angle -= 360.0f;
110
111 XCAM_ASSERT (angle >= 0.0f && angle < 360.0f);
112 return angle;
113 }
114
115 XCAM_END_DECLARE
116
117 #endif //XCAM_COMMON_H
118
119