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 ANDROID_UI_PRIVATE_SW_GRALLOC_HANDLE_H
18#define ANDROID_UI_PRIVATE_SW_GRALLOC_HANDLE_H
19
20#include <stdint.h>
21#include <limits.h>
22#include <sys/cdefs.h>
23#include <hardware/gralloc.h>
24#include <errno.h>
25
26#include <cutils/native_handle.h>
27
28namespace android {
29
30/*****************************************************************************/
31
32struct sw_gralloc_handle_t : public native_handle
33{
34    // file-descriptors
35    int     fd;
36    // ints
37    int     magic;
38    int     size;
39    int     base;
40    int     prot;
41    int     pid;
42
43    static const int sNumInts = 5;
44    static const int sNumFds = 1;
45    static const int sMagic = '_sgh';
46
47    sw_gralloc_handle_t() :
48        fd(-1), magic(sMagic), size(0), base(0), prot(0), pid(getpid())
49    {
50        version = sizeof(native_handle);
51        numInts = sNumInts;
52        numFds = sNumFds;
53    }
54    ~sw_gralloc_handle_t() {
55        magic = 0;
56    }
57
58    static int validate(const native_handle* h) {
59        const sw_gralloc_handle_t* hnd = (const sw_gralloc_handle_t*)h;
60        if (!h || h->version != sizeof(native_handle) ||
61                h->numInts != sNumInts || h->numFds != sNumFds ||
62                hnd->magic != sMagic)
63        {
64            return -EINVAL;
65        }
66        return 0;
67    }
68
69    static status_t alloc(uint32_t w, uint32_t h, int format,
70            int usage, buffer_handle_t* handle, int32_t* stride);
71    static status_t free(sw_gralloc_handle_t* hnd);
72    static status_t registerBuffer(sw_gralloc_handle_t* hnd);
73    static status_t unregisterBuffer(sw_gralloc_handle_t* hnd);
74    static status_t lock(sw_gralloc_handle_t* hnd, int usage,
75            int l, int t, int w, int h, void** vaddr);
76    static status_t unlock(sw_gralloc_handle_t* hnd);
77};
78
79/*****************************************************************************/
80
81}; // namespace android
82
83#endif /* ANDROID_UI_PRIVATE_SW_GRALLOC_HANDLE_H */
84