1/*
2 * Copyright (C) 2008 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
17#ifndef GRALLOC_PRIV_H_
18#define GRALLOC_PRIV_H_
19
20#include <stdint.h>
21#include <limits.h>
22#include <sys/cdefs.h>
23#include <hardware/gralloc.h>
24#include <pthread.h>
25#include <errno.h>
26#include <unistd.h>
27
28#include <cutils/native_handle.h>
29
30#include <linux/fb.h>
31
32/*****************************************************************************/
33
34struct private_module_t;
35struct private_handle_t;
36typedef int ion_user_handle_t;
37
38struct private_module_t {
39    gralloc_module_t base;
40
41    struct private_handle_t* framebuffer;
42    uint32_t flags;
43    uint32_t numBuffers;
44    uint32_t bufferMask;
45    pthread_mutex_t lock;
46    unsigned int refcount;
47    buffer_handle_t currentBuffer;
48    int ionfd;
49
50    int xres;
51    int yres;
52    int line_length;
53    float xdpi;
54    float ydpi;
55    float fps;
56    void *queue;
57    pthread_mutex_t queue_lock;
58
59};
60
61/*****************************************************************************/
62
63#ifdef __cplusplus
64struct private_handle_t : public native_handle {
65#else
66struct private_handle_t {
67    struct native_handle nativeHandle;
68#endif
69
70// set if using video encoding colorspace
71#define GRALLOC_USAGE_PRIVATE_CHROMA (GRALLOC_USAGE_PRIVATE_0)
72
73    enum {
74        PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
75        PRIV_FLAGS_USES_UMP    = 0x00000002,
76        PRIV_FLAGS_USES_ION    = 0x00000020
77    };
78
79    // file-descriptors
80    int     fd;
81    int     fd1;
82    int     fd2;
83    // ints
84    int     magic;
85    int     flags;
86    int     size;
87    int     offset;
88
89    int     format;
90    int     width;
91    int     height;
92    int     stride;
93    int     vstride;
94    int     gamut;
95    int     chroma;
96
97    // FIXME: the attributes below should be out-of-line
98    void    *base;
99    void    *base1;
100    void    *base2;
101    ion_user_handle_t handle;
102    ion_user_handle_t handle1;
103    ion_user_handle_t handle2;
104
105#ifdef __cplusplus
106    static const int sNumFds = 3;
107    static const int sNumInts = 17;
108    static const int sMagic = 0x3141592;
109
110
111    private_handle_t(int fd, int size, int flags, int w,
112		     int h, int format, int stride, int vstride) :
113        fd(fd), fd1(-1), fd2(-1), magic(sMagic), flags(flags), size(size),
114        offset(0), format(format), width(w), height(h), stride(stride),
115        vstride(vstride), gamut(0), chroma(0), base(0), handle(0), handle1(0),
116        handle2(0)
117    {
118        version = sizeof(native_handle);
119        numInts = sNumInts + 2;
120        numFds = sNumFds - 2;
121    }
122
123    private_handle_t(int fd, int fd1, int size, int flags, int w,
124		     int h, int format, int stride, int vstride) :
125        fd(fd), fd1(fd1), fd2(-1), magic(sMagic), flags(flags), size(size),
126        offset(0), format(format), width(w), height(h), stride(stride),
127        vstride(vstride), gamut(0), chroma(0), base(0), base1(0), base2(0),
128        handle(0), handle1(0), handle2(0)
129    {
130        version = sizeof(native_handle);
131        numInts = sNumInts + 1;
132        numFds = sNumFds - 1;
133    }
134
135    private_handle_t(int fd, int fd1, int fd2, int size, int flags, int w,
136		     int h, int format, int stride, int vstride) :
137        fd(fd), fd1(fd1), fd2(fd2), magic(sMagic), flags(flags), size(size),
138        offset(0), format(format), width(w), height(h), stride(stride),
139        vstride(vstride), gamut(0), chroma(0), base(0), base1(0), base2(0),
140        handle(0), handle1(0), handle2(0)
141    {
142        version = sizeof(native_handle);
143        numInts = sNumInts;
144        numFds = sNumFds;
145    }
146    ~private_handle_t() {
147        magic = 0;
148    }
149
150    static int validate(const native_handle* h) {
151        const private_handle_t* hnd = (const private_handle_t*)h;
152        if (!h || h->version != sizeof(native_handle) ||
153            hnd->numInts + hnd->numFds != sNumInts + sNumFds ||
154            hnd->magic != sMagic)
155        {
156            ALOGE("invalid gralloc handle (at %p)", reinterpret_cast<void *>(const_cast<native_handle *>(h)));
157            return -EINVAL;
158        }
159        return 0;
160    }
161
162    static private_handle_t* dynamicCast(const native_handle* in)
163    {
164        if (validate(in) == 0)
165            return (private_handle_t*) in;
166
167        return NULL;
168    }
169
170#endif
171};
172
173#endif /* GRALLOC_PRIV_H_ */
174