1/*--------------------------------------------------------------------------
2Copyright (c) 2013 - 2016, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6    * Redistributions of source code must retain the above copyright
7      notice, this list of conditions and the following disclaimer.
8    * Redistributions in binary form must reproduce the above copyright
9      notice, this list of conditions and the following disclaimer in the
10      documentation and/or other materials provided with the distribution.
11    * Neither the name of The Linux Foundation nor
12      the names of its contributors may be used to endorse or promote
13      products derived from this software without specific prior written
14      permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27--------------------------------------------------------------------------*/
28
29#ifndef __VIDC_DEBUG_H__
30#define __VIDC_DEBUG_H__
31
32#ifdef _ANDROID_
33#include <cstdio>
34#include <pthread.h>
35#include <sys/mman.h>
36
37enum {
38   PRIO_ERROR=0x1,
39   PRIO_INFO=0x1,
40   PRIO_HIGH=0x2,
41   PRIO_LOW=0x4,
42   PRIO_TRACE_HIGH = 0x10,
43   PRIO_TRACE_LOW = 0x20,
44};
45
46extern int debug_level;
47
48#undef DEBUG_PRINT_ERROR
49#define DEBUG_PRINT_ERROR(fmt, args...) ({ \
50      if (debug_level & PRIO_ERROR) \
51          ALOGE(fmt,##args); \
52      })
53#undef DEBUG_PRINT_INFO
54#define DEBUG_PRINT_INFO(fmt, args...) ({ \
55      if (debug_level & PRIO_INFO) \
56          ALOGI(fmt,##args); \
57      })
58#undef DEBUG_PRINT_LOW
59#define DEBUG_PRINT_LOW(fmt, args...) ({ \
60      if (debug_level & PRIO_LOW) \
61          ALOGD(fmt,##args); \
62      })
63#undef DEBUG_PRINT_HIGH
64#define DEBUG_PRINT_HIGH(fmt, args...) ({ \
65      if (debug_level & PRIO_HIGH) \
66          ALOGD(fmt,##args); \
67      })
68#else
69#define DEBUG_PRINT_ERROR printf
70#define DEBUG_PRINT_INFO printf
71#define DEBUG_PRINT_LOW printf
72#define DEBUG_PRINT_HIGH printf
73#endif
74
75#define VALIDATE_OMX_PARAM_DATA(ptr, paramType)                                \
76    {                                                                          \
77        if (ptr == NULL) { return OMX_ErrorBadParameter; }                     \
78        paramType *p = reinterpret_cast<paramType *>(ptr);                     \
79        if (p->nSize < sizeof(paramType)) {                                    \
80            ALOGE("Insufficient object size(%u) v/s expected(%zu) for type %s",\
81                    (unsigned int)p->nSize, sizeof(paramType), #paramType);    \
82            return OMX_ErrorBadParameter;                                      \
83        }                                                                      \
84    }                                                                          \
85
86/*
87 * Validate OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE type param
88 * *assumes* VALIDATE_OMX_PARAM_DATA checks have passed
89 * Checks for nParamCount cannot be generalized here. it is imperative that
90 *  the calling code handles it.
91 */
92#define VALIDATE_OMX_VENDOR_EXTENSION_PARAM_DATA(ext)                                             \
93    {                                                                                             \
94        if (ext->nParamSizeUsed < 1 || ext->nParamSizeUsed > OMX_MAX_ANDROID_VENDOR_PARAMCOUNT) { \
95            ALOGE("VendorExtension: sub-params(%u) not in expected range(%u - %u)",               \
96                    ext->nParamSizeUsed, 1, OMX_MAX_ANDROID_VENDOR_PARAMCOUNT);                   \
97            return OMX_ErrorBadParameter;                                                         \
98        }                                                                                         \
99        OMX_U32 expectedSize = (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE) +         \
100                ((ext->nParamSizeUsed - 1) * (OMX_U32)sizeof(OMX_CONFIG_ANDROID_VENDOR_PARAMTYPE));\
101        if (ext->nSize < expectedSize) {                                                          \
102            ALOGE("VendorExtension: Insifficient size(%u) v/s expected(%u)",                      \
103                    ext->nSize, expectedSize);                                                    \
104            return OMX_ErrorBadParameter;                                                         \
105        }                                                                                         \
106    }                                                                                             \
107
108class auto_lock {
109    public:
110        auto_lock(pthread_mutex_t &lock)
111            : mLock(lock) {
112                pthread_mutex_lock(&mLock);
113            }
114        ~auto_lock() {
115            pthread_mutex_unlock(&mLock);
116        }
117    private:
118        pthread_mutex_t &mLock;
119};
120
121class AutoUnmap {
122    void *vaddr;
123    int size;
124
125    public:
126        AutoUnmap(void *vaddr, int size) {
127            this->vaddr = vaddr;
128            this->size = size;
129        }
130
131        ~AutoUnmap() {
132            if (vaddr)
133                munmap(vaddr, size);
134        }
135};
136
137#ifdef _ANDROID_
138#define ATRACE_TAG ATRACE_TAG_VIDEO
139#include <utils/Trace.h>
140
141class AutoTracer {
142    int mPrio;
143public:
144    AutoTracer(int prio, const char* msg)
145        : mPrio(prio) {
146        if (debug_level & prio) {
147            ATRACE_BEGIN(msg);
148        }
149    }
150    ~AutoTracer() {
151        if (debug_level & mPrio) {
152            ATRACE_END();
153        }
154    }
155};
156
157#define VIDC_TRACE_NAME_LOW(_name) AutoTracer _tracer(PRIO_TRACE_LOW, _name);
158#define VIDC_TRACE_NAME_HIGH(_name) AutoTracer _tracer(PRIO_TRACE_HIGH, _name);
159
160#define VIDC_TRACE_INT_LOW(_name, _int) \
161    if (debug_level & PRIO_TRACE_LOW) { \
162        ATRACE_INT(_name, _int);        \
163    }
164
165#define VIDC_TRACE_INT_HIGH(_name, _int) \
166    if (debug_level & PRIO_TRACE_HIGH) { \
167        ATRACE_INT(_name, _int);        \
168    }
169
170#else // _ANDROID_
171
172#define VIDC_TRACE_NAME_LOW(_name)
173#define VIDC_TRACE_NAME_HIGH(_name)
174#define VIDC_TRACE_INT_LOW(_name, _int)
175#define VIDC_TRACE_INT_HIGH(_name, _int)
176
177#endif // !_ANDROID_
178
179#endif
180