summaryrefslogtreecommitdiff
path: root/GraphicBuffer.cpp
blob: 963d8db7a0066cee3f13c8b5dd471fc75816c072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2016 Rockchip Electronics Co.Ltd
 * Authors:
 *	Zhiqin Wei <wzq@rock-chips.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */
#include "GraphicBuffer.h"
#include <gralloc_rga.h>
gralloc_module_t const *mAllocMod = NULL;

enum {
	GET_HANDLE_FD           = GRALLOC_MODULE_PERFORM_GET_HADNLE_PRIME_FD,
	GET_HADNLE_ATTRIBUTES   = GRALLOC_MODULE_PERFORM_GET_HADNLE_ATTRIBUTES,
	//GET_INTERNAL_FORMAT     = GRALLOC_MODULE_PERFORM_GET_INTERNAL_FORMAT,
	//GET_HADNLE_USAGE        = GRALLOC_MODULE_PERFORM_GET_USAGE,
};
// ---------------------------------------------------------------------------
int RkInitAllocModle()
{
	const hw_module_t *allocMod = NULL;
	int ret = 0;

	if (mAllocMod)
		return 0;

	ret= hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &allocMod);
	ALOGE_IF(ret, "FATAL:can't find the %s module",GRALLOC_HARDWARE_MODULE_ID);
	if (ret == 0)
		mAllocMod = reinterpret_cast<gralloc_module_t const *>(allocMod);

	return ret;
}

int RkRgaGetHandleFd(buffer_handle_t handle, int *fd)
{
	int op = GET_HANDLE_FD;
	int ret = 0;

	if (!mAllocMod)
		ret = RkInitAllocModle();

	if (ret)
		return ret;

	if (mAllocMod->perform)
		mAllocMod->perform(mAllocMod, op, handle, fd);
	else
		return -ENODEV;

	if (ret)
		ALOGE("GraphicBufferGetHandldFd fail %d for:%s",ret,strerror(ret));
	else if (false) {
		ALOGD("fd = %d",*fd);
		fprintf(stderr,"fd = %d\n", *fd);
	}

	return ret;
}

int RkRgaGetHandleAttributes(buffer_handle_t handle,
		std::vector<int> *attrs)
{
	int op = GET_HADNLE_ATTRIBUTES;
	int ret = 0;

	if (!mAllocMod)
		ret = RkInitAllocModle();

	if (ret)
		return ret;

	if(!mAllocMod->perform)
		return -ENODEV;

#if RK3368_DRM
	int w,h,pixel_stride,format,size;

	op = GRALLOC_MODULE_PERFORM_GET_HADNLE_WIDTH;
	mAllocMod->perform(mAllocMod, op, handle, &w);
	op = GRALLOC_MODULE_PERFORM_GET_HADNLE_HEIGHT;
	mAllocMod->perform(mAllocMod, op, handle, &h);
	op = GRALLOC_MODULE_PERFORM_GET_HADNLE_STRIDE;
	mAllocMod->perform(mAllocMod, op, handle, &pixel_stride);
	op = GRALLOC_MODULE_PERFORM_GET_HADNLE_FORMAT;
	mAllocMod->perform(mAllocMod, op, handle, &format);
	op = GRALLOC_MODULE_PERFORM_GET_HADNLE_SIZE;
	mAllocMod->perform(mAllocMod, op, handle, &size);

	//add to attrs.
	attrs->emplace_back(w);
	attrs->emplace_back(h);
	attrs->emplace_back(pixel_stride);
	attrs->emplace_back(format);
	attrs->emplace_back(size);

#else
	mAllocMod->perform(mAllocMod, op, handle, attrs);

	if (ret)
		ALOGE("GraphicBufferGetHandldAttributes fail %d for:%s",ret,strerror(ret));
	else if (false) {
		ALOGD("%d,%d,%d,%d,%d,%d",attrs->at(0),attrs->at(1),attrs->at(2),
				attrs->at(3),attrs->at(4),attrs->at(5));
		fprintf(stderr,"%d,%d,%d,%d,%d,%d\n",
				attrs->at(0),attrs->at(1),attrs->at(2),
				attrs->at(3),attrs->at(4),attrs->at(5));
	}
#endif

	return ret;
}

int RkRgaGetHandleMapAddress(buffer_handle_t handle,
		void **buf)
{
	int usage = GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK;
	int ret = 0;

	if (!mAllocMod)
		ret = RkInitAllocModle();

	if (ret)
		return ret;

	if (mAllocMod->lock)
		ret = mAllocMod->lock(mAllocMod, handle, usage, 0, 0, 0, 0, buf);
	else
		return -ENODEV;

	if (ret)
		ALOGE("GetHandleMapAddress fail %d for:%s",ret,strerror(ret));

	return ret;
}

int RkRgaGetRgaFormat(int format)
{
	switch (format)
	{
		case HAL_PIXEL_FORMAT_RGB_565:
			return RK_FORMAT_RGB_565;
		case HAL_PIXEL_FORMAT_RGB_888:
			return RK_FORMAT_RGB_888;
		case HAL_PIXEL_FORMAT_RGBA_8888:
			return RK_FORMAT_RGBA_8888;
		case HAL_PIXEL_FORMAT_RGBX_8888:
			return RK_FORMAT_RGBX_8888;
		case HAL_PIXEL_FORMAT_BGRA_8888:
			return RK_FORMAT_BGRA_8888;
		case HAL_PIXEL_FORMAT_YCrCb_420_SP:
			return RK_FORMAT_YCrCb_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12:
			return RK_FORMAT_YCbCr_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12_VIDEO:
			return RK_FORMAT_YCbCr_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12_10:
			return RK_FORMAT_YCbCr_420_SP_10B; //0x20
		default:
			ALOGE("Is unsupport format now,please fix");
			return -1;
	}
}


int RkRgaGetRgaFormatFromAndroid(int format)
{
	switch (format)
	{
		case HAL_PIXEL_FORMAT_RGB_565:
			return RK_FORMAT_RGB_565;
		case HAL_PIXEL_FORMAT_RGB_888:
			return RK_FORMAT_RGB_888;
		case HAL_PIXEL_FORMAT_RGBA_8888:
			return RK_FORMAT_RGBA_8888;
		case HAL_PIXEL_FORMAT_RGBX_8888:
			return RK_FORMAT_RGBX_8888;
		case HAL_PIXEL_FORMAT_BGRA_8888:
			return RK_FORMAT_BGRA_8888;
		case HAL_PIXEL_FORMAT_YCrCb_420_SP:
			return RK_FORMAT_YCrCb_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12:
			return RK_FORMAT_YCbCr_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12_VIDEO:
			return RK_FORMAT_YCbCr_420_SP;
		case HAL_PIXEL_FORMAT_YCrCb_NV12_10:
			return RK_FORMAT_YCbCr_420_SP_10B;//0x20
		default:
			ALOGE("Is unsupport format now,please fix");
			return -1;
	}
}