summaryrefslogtreecommitdiff
path: root/drivers/video/b2r2/b2r2_utils.c
blob: ad0e7a42fa1720873c3ca41725a00079d3ec99f8 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * ST-Ericsson B2R2 utils
 *
 * Author: Johan Mossberg <johan.xx.mossberg@stericsson.com> for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2.
 */

#include "b2r2_utils.h"

#include "b2r2_debug.h"

#include <video/b2r2_blt.h>

#include <linux/kernel.h>
#include <linux/errno.h>


const s32 b2r2_s32_max = 2147483647;


void b2r2_get_img_bounding_rect(struct b2r2_blt_img *img,
					struct b2r2_blt_rect *bounding_rect)
{
	bounding_rect->x = 0;
	bounding_rect->y = 0;
	bounding_rect->width = img->width;
	bounding_rect->height = img->height;
}


bool b2r2_is_zero_area_rect(struct b2r2_blt_rect *rect)
{
	return rect->width == 0 || rect->height == 0;
}

bool b2r2_is_rect_inside_rect(struct b2r2_blt_rect *rect1,
						struct b2r2_blt_rect *rect2)
{
	return rect1->x >= rect2->x &&
		rect1->y >= rect2->y &&
		rect1->x + rect1->width <= rect2->x + rect2->width &&
		rect1->y + rect1->height <= rect2->y + rect2->height;
}

void b2r2_intersect_rects(struct b2r2_blt_rect *rect1,
	struct b2r2_blt_rect *rect2, struct b2r2_blt_rect *intersection)
{
	struct b2r2_blt_rect tmp_rect;

	tmp_rect.x = max(rect1->x, rect2->x);
	tmp_rect.y = max(rect1->y, rect2->y);
	tmp_rect.width = min(rect1->x + rect1->width, rect2->x + rect2->width)
				- tmp_rect.x;
	if (tmp_rect.width < 0)
		tmp_rect.width = 0;
	tmp_rect.height =
		min(rect1->y + rect1->height, rect2->y + rect2->height) -
				tmp_rect.y;
	if (tmp_rect.height < 0)
		tmp_rect.height = 0;

	*intersection = tmp_rect;
}


int b2r2_get_fmt_bpp(enum b2r2_blt_fmt fmt)
{
	/*
	 * Currently this function is not used that often but if that changes a
	 * lookup table could make it a lot faster.
	 */
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
		return 1;

	case B2R2_BLT_FMT_8_BIT_A8:
		return 8;

	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
		return 12;

	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return 16;

	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return 24;

	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return 32;

	default:
		b2r2_log_err("%s: Internal error! Format %#x not recognized.\n",
			__func__, fmt);
		return 32;
	}
}

int b2r2_get_fmt_y_bpp(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return 8;

	default:
		b2r2_log_err("%s: Internal error! Non YCbCr format supplied.\n",
			__func__);
		return 8;
	}
}


bool b2r2_is_single_plane_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
	case B2R2_BLT_FMT_8_BIT_A8:
	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_independent_pixel_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_1_BIT_A1:
	case B2R2_BLT_FMT_8_BIT_A8:
	case B2R2_BLT_FMT_16_BIT_ARGB4444:
	case B2R2_BLT_FMT_16_BIT_ARGB1555:
	case B2R2_BLT_FMT_16_BIT_RGB565:
	case B2R2_BLT_FMT_24_BIT_RGB888:
	case B2R2_BLT_FMT_24_BIT_ARGB8565:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_ARGB8888:
	case B2R2_BLT_FMT_32_BIT_ABGR8888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcri_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcrsp_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcrp_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr420_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr422_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_Y_CB_Y_CR:
	case B2R2_BLT_FMT_CB_Y_CR_Y:
	case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_ycbcr444_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
	case B2R2_BLT_FMT_32_BIT_AYUV8888:
	case B2R2_BLT_FMT_24_BIT_YUV888:
	case B2R2_BLT_FMT_32_BIT_VUYA8888:
	case B2R2_BLT_FMT_24_BIT_VUY888:
		return true;

	default:
		return false;
	}
}

bool b2r2_is_mb_fmt(enum b2r2_blt_fmt fmt)
{
	switch (fmt) {
	case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
	case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
		return true;

	default:
		return false;
	}
}

u32 b2r2_calc_pitch_from_width(s32 width, enum b2r2_blt_fmt fmt)
{
	if (b2r2_is_single_plane_fmt(fmt)) {
		return (u32)b2r2_div_round_up(width *
				b2r2_get_fmt_bpp(fmt), 8);
	} else if (b2r2_is_ycbcrsp_fmt(fmt) || b2r2_is_ycbcrp_fmt(fmt)) {
		return (u32)b2r2_div_round_up(width *
				b2r2_get_fmt_y_bpp(fmt), 8);
	} else {
		b2r2_log_err("%s: Internal error! "
				"Pitchless format supplied.\n",
				__func__);
		return 0;
	}
}

u32 b2r2_get_img_pitch(struct b2r2_blt_img *img)
{
	if (img->pitch != 0)
		return img->pitch;
	else
		return b2r2_calc_pitch_from_width(img->width, img->fmt);
}

s32 b2r2_get_img_size(struct b2r2_blt_img *img)
{
	if (b2r2_is_single_plane_fmt(img->fmt)) {
		return (s32)b2r2_get_img_pitch(img) * img->height;
	} else if (b2r2_is_ycbcrsp_fmt(img->fmt) ||
			b2r2_is_ycbcrp_fmt(img->fmt)) {
		s32 y_plane_size;

		y_plane_size = (s32)b2r2_get_img_pitch(img) * img->height;

		if (b2r2_is_ycbcr420_fmt(img->fmt)) {
			return y_plane_size + y_plane_size / 2;
		} else if (b2r2_is_ycbcr422_fmt(img->fmt)) {
			return y_plane_size * 2;
		} else if (b2r2_is_ycbcr444_fmt(img->fmt)) {
			return y_plane_size * 3;
		} else {
			b2r2_log_err("%s: Internal error! "
					"Format %#x not recognized.\n",
					__func__, img->fmt);
			return 0;
		}
	} else if (b2r2_is_mb_fmt(img->fmt)) {
		return (img->width * img->height *
				b2r2_get_fmt_bpp(img->fmt)) / 8;
	} else {
		b2r2_log_err("%s: Internal error! "
				"Format %#x not recognized.\n",
				__func__, img->fmt);
		return 0;
	}
}


s32 b2r2_div_round_up(s32 dividend, s32 divisor)
{
	s32 quotient = dividend / divisor;
	if (dividend % divisor != 0)
		quotient++;

	return quotient;
}

bool b2r2_is_aligned(s32 value, s32 alignment)
{
	return value % alignment == 0;
}

s32 b2r2_align_up(s32 value, s32 alignment)
{
	s32 remainder = abs(value) % abs(alignment);
	s32 value_to_add;

	if (remainder > 0) {
		if (value >= 0)
			value_to_add = alignment - remainder;
		else
			value_to_add = remainder;
	} else {
		value_to_add = 0;
	}

	return value + value_to_add;
}