blob: bff33f380a188d60e3183a7d073cf81371b105ae [file] [log] [blame]
keunyoungb85b2752013-03-08 12:28:03 -08001/*
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
Weilun Du8b38e8b2018-10-03 10:25:21 -070024#include "qemu_pipe.h"
Lingfeng Yang88c170c2016-11-30 00:52:35 +000025
keunyoungb85b2752013-03-08 12:28:03 -080026#define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
27#define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int))
28
Lingfeng Yang88c170c2016-11-30 00:52:35 +000029// Tell the emulator which gralloc formats
30// need special handling.
31enum EmulatorFrameworkFormat {
32 FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
33 FRAMEWORK_FORMAT_YV12 = 1,
Roman Kiryanov76835be2019-04-15 15:33:09 -070034 FRAMEWORK_FORMAT_YUV_420_888 = 2, // (Y+)(U+)(V+)
Lingfeng Yang88c170c2016-11-30 00:52:35 +000035};
36
keunyoungb85b2752013-03-08 12:28:03 -080037//
38// Our buffer handle structure
39//
40struct cb_handle_t : public native_handle {
41
42 cb_handle_t(int p_fd, int p_ashmemSize, int p_usage,
Eino-Ville Talvalaa6000782013-05-04 16:45:22 -070043 int p_width, int p_height, int p_frameworkFormat,
Lingfeng Yang88c170c2016-11-30 00:52:35 +000044 int p_format, int p_glFormat, int p_glType,
45 EmulatorFrameworkFormat p_emuFrameworkFormat) :
keunyoungb85b2752013-03-08 12:28:03 -080046 fd(p_fd),
47 magic(BUFFER_HANDLE_MAGIC),
48 usage(p_usage),
49 width(p_width),
50 height(p_height),
Eino-Ville Talvalaa6000782013-05-04 16:45:22 -070051 frameworkFormat(p_frameworkFormat),
keunyoungb85b2752013-03-08 12:28:03 -080052 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),
Lingfeng Yang88c170c2016-11-30 00:52:35 +000063 hostHandle(0),
64 emuFrameworkFormat(p_emuFrameworkFormat)
keunyoungb85b2752013-03-08 12:28:03 -080065 {
Weilun Du8b38e8b2018-10-03 10:25:21 -070066 refcount_pipe_fd = QEMU_PIPE_INVALID_HANDLE;
keunyoungb85b2752013-03-08 12:28:03 -080067 version = sizeof(native_handle);
68 numFds = 0;
69 numInts = CB_HANDLE_NUM_INTS(numFds);
70 }
71
72 ~cb_handle_t() {
73 magic = 0;
74 }
75
76 void setFd(int p_fd) {
77 if (p_fd >= 0) {
Lingfeng Yang88c170c2016-11-30 00:52:35 +000078 numFds++;
keunyoungb85b2752013-03-08 12:28:03 -080079 }
80 fd = p_fd;
81 numInts = CB_HANDLE_NUM_INTS(numFds);
82 }
83
Weilun Du8b38e8b2018-10-03 10:25:21 -070084 bool hasRefcountPipe() {
85 return qemu_pipe_valid(refcount_pipe_fd);
86 }
87
88 void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) {
89 if (qemu_pipe_valid(fd)) {
Lingfeng Yang88c170c2016-11-30 00:52:35 +000090 numFds++;
91 }
Weilun Du8b38e8b2018-10-03 10:25:21 -070092 refcount_pipe_fd = fd;
Lingfeng Yang88c170c2016-11-30 00:52:35 +000093 numInts = CB_HANDLE_NUM_INTS(numFds);
94 }
95
Alex Vallée0c7d2752014-09-24 14:05:22 -040096 static bool validate(const cb_handle_t* hnd) {
97 return (hnd &&
keunyoungb85b2752013-03-08 12:28:03 -080098 hnd->version == sizeof(native_handle) &&
99 hnd->magic == BUFFER_HANDLE_MAGIC &&
100 hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds));
101 }
102
103 bool canBePosted() {
104 return (0 != (usage & GRALLOC_USAGE_HW_FB));
105 }
106
107 // file-descriptors
108 int fd; // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
Weilun Du8b38e8b2018-10-03 10:25:21 -0700109 QEMU_PIPE_HANDLE refcount_pipe_fd; // goldfish pipe service for gralloc refcounting fd.
keunyoungb85b2752013-03-08 12:28:03 -0800110
111 // ints
112 int magic; // magic number in order to validate a pointer to be a cb_handle_t
113 int usage; // usage bits the buffer was created with
114 int width; // buffer width
115 int height; // buffer height
Eino-Ville Talvalaa6000782013-05-04 16:45:22 -0700116 int frameworkFormat; // format requested by the Android framework
keunyoungb85b2752013-03-08 12:28:03 -0800117 int format; // real internal pixel format format
118 int glFormat; // OpenGL format enum used for host h/w color buffer
119 int glType; // OpenGL type enum used when uploading to host
120 int ashmemSize; // ashmem region size for the buffer (0 unless is HW_FB buffer or
121 // s/w access is needed)
Tina Zhang82bacbd2014-05-26 14:03:48 +0800122 union {
123 intptr_t ashmemBase; // CPU address of the mapped ashmem region
124 uint64_t padding; // enforce same size on 32-bit/64-bit
125 } __attribute__((aligned(8)));
126
keunyoungb85b2752013-03-08 12:28:03 -0800127 int ashmemBasePid; // process id which mapped the ashmem region
128 int mappedPid; // process id which succeeded gralloc_register call
129 int lockedLeft; // region of buffer locked for s/w write
130 int lockedTop;
131 int lockedWidth;
132 int lockedHeight;
133 uint32_t hostHandle;
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000134
Lingfeng Yang88c170c2016-11-30 00:52:35 +0000135 EmulatorFrameworkFormat emuFrameworkFormat;
keunyoungb85b2752013-03-08 12:28:03 -0800136};
137
138
139#endif //__GRALLOC_CB_H__