summaryrefslogtreecommitdiff
path: root/mm-video-v4l2/vidc/common/inc/vidc_debug.h
blob: 08425fd4016676e37cfac2b9b6c2509c701a1fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*--------------------------------------------------------------------------
Copyright (c) 2013 - 2017, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of The Linux Foundation nor
      the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------*/

#ifndef __VIDC_DEBUG_H__
#define __VIDC_DEBUG_H__

#ifdef _ANDROID_
#include <cstdio>
#include <pthread.h>
#include <sys/mman.h>

enum {
   PRIO_ERROR=0x1,
   PRIO_INFO=0x1,
   PRIO_HIGH=0x2,
   PRIO_LOW=0x4,
   PRIO_TRACE_HIGH = 0x10,
   PRIO_TRACE_LOW = 0x20,
};

extern int debug_level;

#undef DEBUG_PRINT_ERROR
#define DEBUG_PRINT_ERROR(fmt, args...) ({ \
      if (debug_level & PRIO_ERROR) \
          ALOGE(fmt,##args); \
      })
#undef DEBUG_PRINT_INFO
#define DEBUG_PRINT_INFO(fmt, args...) ({ \
      if (debug_level & PRIO_INFO) \
          ALOGI(fmt,##args); \
      })
#undef DEBUG_PRINT_LOW
#define DEBUG_PRINT_LOW(fmt, args...) ({ \
      if (debug_level & PRIO_LOW) \
          ALOGD(fmt,##args); \
      })
#undef DEBUG_PRINT_HIGH
#define DEBUG_PRINT_HIGH(fmt, args...) ({ \
      if (debug_level & PRIO_HIGH) \
          ALOGD(fmt,##args); \
      })
#else
#define DEBUG_PRINT_ERROR printf
#define DEBUG_PRINT_INFO printf
#define DEBUG_PRINT_LOW printf
#define DEBUG_PRINT_HIGH printf
#endif

#define VALIDATE_OMX_PARAM_DATA(ptr, paramType)                                \
    {                                                                          \
        if (ptr == NULL) { return OMX_ErrorBadParameter; }                     \
        paramType *p = reinterpret_cast<paramType *>(ptr);                     \
        if (p->nSize < sizeof(paramType)) {                                    \
            ALOGE("Insufficient object size(%u) v/s expected(%zu) for type %s",\
                    (unsigned int)p->nSize, sizeof(paramType), #paramType);    \
            return OMX_ErrorBadParameter;                                      \
        }                                                                      \
    }                                                                          \

/*
 * Validate OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE type param
 * *assumes* VALIDATE_OMX_PARAM_DATA checks have passed
 * Checks for nParamCount cannot be generalized here. it is imperative that
 *  the calling code handles it.
 */
#define VALIDATE_OMX_VENDOR_EXTENSION_PARAM_DATA(ext)                                             \
    {                                                                                             \
        if (ext->nParamSizeUsed < 1 || ext->nParamSizeUsed > OMX_MAX_ANDROID_VENDOR_PARAMCOUNT) { \
            ALOGE("VendorExtension: sub-params(%u) not in expected range(%u - %u)",               \
                    ext->nParamSizeUsed, 1, OMX_MAX_ANDROID_VENDOR_PARAMCOUNT);                   \
            return OMX_ErrorBadParameter;                                                         \
        }                                                                                         \
        OMX_U32 expectedSize = (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE) +         \
                ((ext->nParamSizeUsed - 1) * (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_PARAMTYPE));\
        if (ext->nSize < expectedSize) {                                                          \
            ALOGE("VendorExtension: Insifficient size(%u) v/s expected(%u)",                      \
                    ext->nSize, expectedSize);                                                    \
            return OMX_ErrorBadParameter;                                                         \
        }                                                                                         \
    }                                                                                             \

class auto_lock {
    public:
        auto_lock(pthread_mutex_t &lock)
            : mLock(lock) {
                pthread_mutex_lock(&mLock);
            }
        ~auto_lock() {
            pthread_mutex_unlock(&mLock);
        }
    private:
        pthread_mutex_t &mLock;
};

class AutoUnmap {
    void *vaddr;
    int size;

    public:
        AutoUnmap(void *vaddr, int size) {
            this->vaddr = vaddr;
            this->size = size;
        }

        ~AutoUnmap() {
            if (vaddr)
                munmap(vaddr, size);
        }
};

class Signal {
    bool signalled;
    pthread_mutex_t mutex;
    pthread_cond_t condition;
public:
    Signal() {
        signalled = false;
        pthread_cond_init(&condition, NULL);
        pthread_mutex_init(&mutex, NULL);
    }

    ~Signal() {
            pthread_cond_destroy(&condition);
            pthread_mutex_destroy(&mutex);
    }

    void signal() {
        pthread_mutex_lock(&mutex);
        signalled = true;
        pthread_cond_signal(&condition);
        pthread_mutex_unlock(&mutex);
    }

    int wait(uint64_t timeout_nsec) {
        struct timespec ts;

        pthread_mutex_lock(&mutex);
        if (signalled) {
            signalled = false;
            pthread_mutex_unlock(&mutex);
            return 0;
        }
        clock_gettime(CLOCK_REALTIME, &ts);
        ts.tv_sec += timeout_nsec / 1000000000;
        ts.tv_nsec += timeout_nsec % 1000000000;
        if (ts.tv_nsec >= 1000000000) {
            ts.tv_nsec -= 1000000000;
            ts.tv_sec  += 1;
        }
        int ret = pthread_cond_timedwait(&condition, &mutex, &ts);
        signalled = false;
        pthread_mutex_unlock(&mutex);
        return ret;
    }
};

#ifdef _ANDROID_
#define ATRACE_TAG ATRACE_TAG_VIDEO
#include <utils/Trace.h>

class AutoTracer {
    int mPrio;
public:
    AutoTracer(int prio, const char* msg)
        : mPrio(prio) {
        if (debug_level & prio) {
            ATRACE_BEGIN(msg);
        }
    }
    ~AutoTracer() {
        if (debug_level & mPrio) {
            ATRACE_END();
        }
    }
};

#define VIDC_TRACE_NAME_LOW(_name) AutoTracer _tracer(PRIO_TRACE_LOW, _name);
#define VIDC_TRACE_NAME_HIGH(_name) AutoTracer _tracer(PRIO_TRACE_HIGH, _name);

#define VIDC_TRACE_INT_LOW(_name, _int) \
    if (debug_level & PRIO_TRACE_LOW) { \
        ATRACE_INT(_name, _int);        \
    }

#define VIDC_TRACE_INT_HIGH(_name, _int) \
    if (debug_level & PRIO_TRACE_HIGH) { \
        ATRACE_INT(_name, _int);        \
    }

#else // _ANDROID_

#define VIDC_TRACE_NAME_LOW(_name)
#define VIDC_TRACE_NAME_HIGH(_name)
#define VIDC_TRACE_INT_LOW(_name, _int)
#define VIDC_TRACE_INT_HIGH(_name, _int)

#endif // !_ANDROID_

#endif