gralloc_priv.h revision 876b4e8b84087ad99f499c064758a2660a990e5c
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#ifdef HAVE_ANDROID_OS      // just want PAGE_SIZE define
22# include <asm/page.h>
23#else
24# include <sys/user.h>
25#endif
26#include <limits.h>
27#include <sys/cdefs.h>
28#include <hardware/gralloc.h>
29#include <pthread.h>
30#include <errno.h>
31#include <unistd.h>
32
33#include <cutils/native_handle.h>
34
35#include <linux/fb.h>
36
37/*****************************************************************************/
38
39struct private_module_t;
40struct private_handle_t;
41
42inline size_t roundUpToPageSize(size_t x) {
43    return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
44}
45
46int mapFrameBufferLocked(struct private_module_t* module);
47int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd);
48
49/*****************************************************************************/
50
51class Locker {
52    pthread_mutex_t mutex;
53public:
54    class Autolock {
55        Locker& locker;
56    public:
57        inline Autolock(Locker& locker) : locker(locker) {  locker.lock(); }
58        inline ~Autolock() { locker.unlock(); }
59    };
60    inline Locker()        { pthread_mutex_init(&mutex, 0); }
61    inline ~Locker()       { pthread_mutex_destroy(&mutex); }
62    inline void lock()     { pthread_mutex_lock(&mutex); }
63    inline void unlock()   { pthread_mutex_unlock(&mutex); }
64};
65
66/*****************************************************************************/
67
68struct private_handle_t;
69
70struct private_module_t {
71    gralloc_module_t base;
72
73    private_handle_t* framebuffer;
74    uint32_t flags;
75    uint32_t numBuffers;
76    uint32_t bufferMask;
77    pthread_mutex_t lock;
78    buffer_handle_t currentBuffer;
79    int pmem_master;
80    void* pmem_master_base;
81
82    struct fb_var_screeninfo info;
83    struct fb_fix_screeninfo finfo;
84    float xdpi;
85    float ydpi;
86    float fps;
87
88    enum {
89        // flag to indicate we'll post this buffer
90        PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
91    };
92};
93
94/*****************************************************************************/
95
96struct private_handle_t : public native_handle
97{
98    enum {
99        PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
100        PRIV_FLAGS_USES_PMEM   = 0x00000002,
101    };
102
103    enum {
104        LOCK_STATE_WRITE     =   1<<31,
105        LOCK_STATE_MAPPED    =   1<<30,
106        LOCK_STATE_READ_MASK =   0x3FFFFFFF
107    };
108
109    // file-descriptors
110    int     fd;
111    // ints
112    int     magic;
113    int     flags;
114    int     size;
115    int     offset;
116
117    // FIXME: the attributes below should be out-of-line
118    int     base;
119    int     lockState;
120    int     writeOwner;
121    int     pid;
122
123    static const int sNumInts = 8;
124    static const int sNumFds = 1;
125    static const int sMagic = 0x3141592;
126
127    private_handle_t(int fd, int size, int flags) :
128        fd(fd), magic(sMagic), flags(flags), size(size), offset(0),
129        base(0), lockState(0), writeOwner(0), pid(getpid())
130    {
131        version = sizeof(native_handle);
132        numInts = sNumInts;
133        numFds = sNumFds;
134    }
135    ~private_handle_t() {
136        magic = 0;
137    }
138
139    bool usesPhysicallyContiguousMemory() {
140        return (flags & PRIV_FLAGS_USES_PMEM) != 0;
141    }
142
143    static int validate(const native_handle* h) {
144        const private_handle_t* hnd = (const private_handle_t*)h;
145        if (!h || h->version != sizeof(native_handle) ||
146                h->numInts != sNumInts || h->numFds != sNumFds ||
147                hnd->magic != sMagic)
148        {
149            LOGE("invalid gralloc handle (at %p)", h);
150            return -EINVAL;
151        }
152        return 0;
153    }
154
155    static private_handle_t* dynamicCast(const native_handle* in) {
156        if (validate(in) == 0) {
157            return (private_handle_t*) in;
158        }
159        return NULL;
160    }
161};
162
163#endif /* GRALLOC_PRIV_H_ */
164