1 /*
2 * Copyright 2011 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_CB_H__
18 #define __GRALLOC_CB_H__
19 
20 #include <hardware/hardware.h>
21 #include <hardware/gralloc.h>
22 #include <cutils/native_handle.h>
23 
24 #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
25 #define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
26 
27 //
28 // Our buffer handle structure
29 //
30 struct cb_handle_t : public native_handle {
31 
cb_handle_tcb_handle_t32     cb_handle_t(int p_fd, int p_ashmemSize, int p_usage,
33                 int p_width, int p_height, int p_frameworkFormat,
34                 int p_format, int p_glFormat, int p_glType) :
35         fd(p_fd),
36         magic(BUFFER_HANDLE_MAGIC),
37         usage(p_usage),
38         width(p_width),
39         height(p_height),
40         frameworkFormat(p_frameworkFormat),
41         format(p_format),
42         glFormat(p_glFormat),
43         glType(p_glType),
44         ashmemSize(p_ashmemSize),
45         ashmemBase(0),
46         ashmemBasePid(0),
47         mappedPid(0),
48         lockedLeft(0),
49         lockedTop(0),
50         lockedWidth(0),
51         lockedHeight(0),
52         hostHandle(0)
53     {
54         version = sizeof(native_handle);
55         numFds = 0;
56         numInts = CB_HANDLE_NUM_INTS(numFds);
57     }
58 
~cb_handle_tcb_handle_t59     ~cb_handle_t() {
60         magic = 0;
61     }
62 
setFdcb_handle_t63     void setFd(int p_fd) {
64         if (p_fd >= 0) {
65             numFds = 1;
66         }
67         else {
68             numFds = 0;
69         }
70         fd = p_fd;
71         numInts = CB_HANDLE_NUM_INTS(numFds);
72     }
73 
validatecb_handle_t74     static bool validate(const cb_handle_t* hnd) {
75         return (hnd &&
76                 hnd->version == sizeof(native_handle) &&
77                 hnd->magic == BUFFER_HANDLE_MAGIC &&
78                 hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds));
79     }
80 
canBePostedcb_handle_t81     bool canBePosted() {
82         return (0 != (usage & GRALLOC_USAGE_HW_FB));
83     }
84 
85     // file-descriptors
86     int fd;  // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
87 
88     // ints
89     int magic;              // magic number in order to validate a pointer to be a cb_handle_t
90     int usage;              // usage bits the buffer was created with
91     int width;              // buffer width
92     int height;             // buffer height
93     int frameworkFormat;    // format requested by the Android framework
94     int format;             // real internal pixel format format
95     int glFormat;           // OpenGL format enum used for host h/w color buffer
96     int glType;             // OpenGL type enum used when uploading to host
97     int ashmemSize;         // ashmem region size for the buffer (0 unless is HW_FB buffer or
98                             //                                    s/w access is needed)
99     union {
100         intptr_t ashmemBase;    // CPU address of the mapped ashmem region
101         uint64_t padding;       // enforce same size on 32-bit/64-bit
102     } __attribute__((aligned(8)));
103 
104     int ashmemBasePid;      // process id which mapped the ashmem region
105     int mappedPid;          // process id which succeeded gralloc_register call
106     int lockedLeft;         // region of buffer locked for s/w write
107     int lockedTop;
108     int lockedWidth;
109     int lockedHeight;
110     uint32_t hostHandle;
111 };
112 
113 
114 #endif //__GRALLOC_CB_H__
115