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 #include "goldfish_dma.h"
25 
26 #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
27 #define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
28 
29 // Tell the emulator which gralloc formats
30 // need special handling.
31 enum EmulatorFrameworkFormat {
32     FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
33     FRAMEWORK_FORMAT_YV12 = 1,
34     FRAMEWORK_FORMAT_YUV_420_888 = 2,
35 };
36 
37 //
38 // Our buffer handle structure
39 //
40 struct cb_handle_t : public native_handle {
41 
cb_handle_tcb_handle_t42     cb_handle_t(int p_fd, int p_ashmemSize, int p_usage,
43                 int p_width, int p_height, int p_frameworkFormat,
44                 int p_format, int p_glFormat, int p_glType,
45                 EmulatorFrameworkFormat p_emuFrameworkFormat) :
46         fd(p_fd),
47         magic(BUFFER_HANDLE_MAGIC),
48         usage(p_usage),
49         width(p_width),
50         height(p_height),
51         frameworkFormat(p_frameworkFormat),
52         format(p_format),
53         glFormat(p_glFormat),
54         glType(p_glType),
55         ashmemSize(p_ashmemSize),
56         ashmemBase(0),
57         ashmemBasePid(0),
58         mappedPid(0),
59         lockedLeft(0),
60         lockedTop(0),
61         lockedWidth(0),
62         lockedHeight(0),
63         hostHandle(0),
64         emuFrameworkFormat(p_emuFrameworkFormat)
65     {
66         goldfish_dma.fd = -1;
67         dmafd = -1;
68         version = sizeof(native_handle);
69         numFds = 0;
70         numInts = CB_HANDLE_NUM_INTS(numFds);
71     }
72 
~cb_handle_tcb_handle_t73     ~cb_handle_t() {
74         magic = 0;
75     }
76 
setFdcb_handle_t77     void setFd(int p_fd) {
78         if (p_fd >= 0) {
79             numFds++;
80         }
81         fd = p_fd;
82         numInts = CB_HANDLE_NUM_INTS(numFds);
83     }
84 
setDmaFdcb_handle_t85     void setDmaFd(int fd) {
86         if (fd >= 0) {
87             numFds++;
88         }
89         dmafd = fd;
90         numInts = CB_HANDLE_NUM_INTS(numFds);
91     }
92 
validatecb_handle_t93     static bool validate(const cb_handle_t* hnd) {
94         return (hnd &&
95                 hnd->version == sizeof(native_handle) &&
96                 hnd->magic == BUFFER_HANDLE_MAGIC &&
97                 hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds));
98     }
99 
canBePostedcb_handle_t100     bool canBePosted() {
101         return (0 != (usage & GRALLOC_USAGE_HW_FB));
102     }
103 
104     // file-descriptors
105     int fd;  // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
106     int dmafd; // goldfish dma fd.
107 
108     // ints
109     int magic;              // magic number in order to validate a pointer to be a cb_handle_t
110     int usage;              // usage bits the buffer was created with
111     int width;              // buffer width
112     int height;             // buffer height
113     int frameworkFormat;    // format requested by the Android framework
114     int format;             // real internal pixel format format
115     int glFormat;           // OpenGL format enum used for host h/w color buffer
116     int glType;             // OpenGL type enum used when uploading to host
117     int ashmemSize;         // ashmem region size for the buffer (0 unless is HW_FB buffer or
118                             //                                    s/w access is needed)
119     union {
120         intptr_t ashmemBase;    // CPU address of the mapped ashmem region
121         uint64_t padding;       // enforce same size on 32-bit/64-bit
122     } __attribute__((aligned(8)));
123 
124     int ashmemBasePid;      // process id which mapped the ashmem region
125     int mappedPid;          // process id which succeeded gralloc_register call
126     int lockedLeft;         // region of buffer locked for s/w write
127     int lockedTop;
128     int lockedWidth;
129     int lockedHeight;
130     uint32_t hostHandle;
131 
132     goldfish_dma_context goldfish_dma;
133     uint32_t goldfish_dma_buf_size;
134     EmulatorFrameworkFormat emuFrameworkFormat;
135 };
136 
137 
138 #endif //__GRALLOC_CB_H__
139