summaryrefslogtreecommitdiff
path: root/board/st/u8500/mcde_display_image.c
blob: fb05ce98f546a3c45e883e823996c22143bb0140 (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
/*
* Copyright (C) ST-Ericsson SA 2010
*
* Author: Torbjorn Svensson <torbjorn.x.svensson@stericsson.com>
* for ST-Ericsson.
*
* License terms: GNU General Public License (GPL), version 2.
*/

#include <common.h>
#include <command.h>
#include <linux/err.h>
#include <part.h>
#include <mmc.h>
#include <bmp_layout.h>
#include <asm/arch/common.h>
#include "mcde.h"
#include "mcde_display.h"

/* bmp compression constants */
#define BI_RGB		0
#define BI_RLE8		1	/* RLE 8-bit/pixel */
#define BI_RLE4		2	/* RLE 4-bit/pixel */
#define BI_BITFIELDS	3

extern struct mcde_display_device main_display;

static uint32_t read_unaligned32(uint32_t *val)
{
	uint32_t ret;
	memcpy(&ret, val, sizeof(int32_t));
	return ret;
}

static void copy_indexed(u16 *bmp_dst, u8 *bmp_start,
				int dst_pitch, int src_pitch,
				u32 width, u32 height, u8 *palette)
{
	/*
	 * Copy image from flash area to overlay area and
	 * convert format from 8-bit indexed to rgb565.
	 */
	int i;
	int j;
	int o;
	int pad;
	u16 val16;
	u32 w;
	u8 r;
	u8 g;
	u8 b;
	u8 *bmp_src;

	pad = (dst_pitch - 2 * width) / 2;

	/* expanding 8 bit indexes to 16 bit rgb */
	for (i = height - 1, o = 0; i >= 0; i--) {
		bmp_src = (u8 *)(bmp_start + i * src_pitch);
		for (j = 0; j < width; j++) {
			w = bmp_src[j];		/* get index */
			w <<= 2;		/* make offset */
			w += (u32)palette;	/* make address */
			r = *((u8 *)w++);
			g = *((u8 *)w++);
			b = *((u8 *)w);
			val16 = r >> 3;
			val16 |= (g >> 2) << 5;
			val16 |= (b >> 3) << 11;
			bmp_dst[o++] = val16;
		}
		o += pad;
	}
}

static void copy_rgb565(u8 *bmp_dst, u8 *bmp_start,
				int dst_pitch, int src_pitch,
				u32 width, u32 height)
{
	int i;
	u8 *bmp_src;

	/* point after source data */
	bmp_src = (u8 *)(bmp_start + (height * src_pitch));
	for (i = 0; i < height; i++) {
		bmp_src -= src_pitch;
		memcpy(bmp_dst, bmp_src, src_pitch);
		bmp_dst += dst_pitch;
	}
}

static void copy_rgb(u16 *bmp_dst, u8 *bmp_start,
				int dst_pitch, int src_pitch,
				u32 width, u32 height, u32 bitspp)
{
	/*
	 * Copy image from flash area to overlay area and
	 * convert format from (a)rgb888(8) to rgb565.
	 */
	int i;
	int j;
	int o;
	int pad;
	u16 val16;
	u8 r;
	u8 g;
	u8 b;
	u8 *bmp_src;
	u32 src_inc = bitspp / 8;

	pad = (dst_pitch - 2 * width) / 2;

	/* convert to 16 bit */
	for (i = height - 1, o = 0; i >= 0; i--) {
		bmp_src = (u8 *)(bmp_start + i * src_pitch);
		for (j = 0; j < (width * src_inc); j += src_inc) {
			r = bmp_src[j];
			g = bmp_src[j + 1];
			b = bmp_src[j + 2];
			val16 = r >> 3;
			val16 |= (g >> 2) << 5;
			val16 |= (b >> 3) << 11;
			bmp_dst[o++] = val16;
		}
		o += pad;
	}
}

int mcde_display_image(struct mcde_chnl_state *chnl)
{
	int err = 0;
	struct mmc *emmc_dev;
	u32 address = (CONFIG_SYS_VIDEO_FB_ADRS + 2 *
		CONFIG_SYS_DISPLAY_NATIVE_X_RES *
		CONFIG_SYS_DISPLAY_NATIVE_Y_RES); /* after frame buffer */
	u8 *bmp_start;
	struct bmp_header *bmp_header;
	u32 bmp_offset;
	u32 dib_bytesz;
	u32 dib_width;
	u32 dib_height;
	u32 dib_compression;
	u16 dib_bitspp;
	int src_pitch;
	int dst_pitch;
	u32 dib_header_size;
	u8 *palette;
	u32 palette_size;
	u8 *bmp_src;
	u8 *bmp_dst;
	u8 *ovly_mem = (u8 *)CONFIG_SYS_VIDEO_FB_ADRS;	/* frame buffer */
	struct mcde_ovly_state *ovly;
	u32 xpos = 0;
	u32 ypos = 0;

	debug("%s: Enter\n", __func__);
	emmc_dev = find_mmc_device(CONFIG_EMMC_DEV_NUM);
	if (emmc_dev == NULL) {
		printf("mcde_display_image: emmc not found.\n");
		return -ENODEV;
	}

	if (toc_load_toc_entry(&emmc_dev->block_dev, MCDE_TOC_SPLASH_NAME, 0,
			       0, address)) {
		printf("mcde_display_image: no splash image found.\n");
		return -ENOENT;
	}

	/* get bmp_image */
	bmp_start = (u8 *)address;
	debug("%s: bmp start = 0x%p\n", __func__, (void *)bmp_start);

	/* check BMP magic */
	bmp_header = (struct bmp_header *)bmp_start;
	if (bmp_header->signature[0] != 'B' ||
		bmp_header->signature[1] != 'M') {
		printf("%s: unsupported filetype, must be BMP\n", __func__);
		return -EILSEQ;
	}

	/* get offset to bitmap-data from the BMP header */
	bmp_offset = read_unaligned32(&bmp_header->data_offset);
	debug("bmp filesz = %d\n",
		read_unaligned32(&bmp_header->file_size));
	debug("bmp offset = %d\n", bmp_offset);

	dib_width = read_unaligned32((uint32_t *)&bmp_header->width);
	dib_height = read_unaligned32((uint32_t *)&bmp_header->height);
	dib_bytesz = read_unaligned32(&bmp_header->image_size);
	dib_bitspp = bmp_header->bit_count;
	dib_header_size = read_unaligned32(&bmp_header->size);
	debug("dib header_sz = %d\n", dib_header_size);
	debug("dib width = %d\n", dib_width);
	debug("dib height = %d\n", dib_height);
	debug("dib nplanes = %d\n", bmp_header->planes);
	debug("dib bitspp = %d\n", dib_bitspp);
	dib_compression = read_unaligned32(&bmp_header->compression);
	debug("dib compress_type = %d\n", dib_compression);
	debug("dib dib_bytesz = %d\n", dib_bytesz);

	/* calculate palette address */
	palette = ((u8 *)&bmp_header->size + dib_header_size);
	palette_size = ((bmp_start + bmp_offset) - palette);
	debug("palette size = %d\n", palette_size);
	/* if same as image start: no palette */
	if (palette_size == 0)
		palette = NULL;
	debug("palette = 0x%08x\n", (u32)palette);

	/* check validity */
	if ((dib_width > main_display.native_x_res) ||
			(dib_height > main_display.native_y_res)) {
		printf("%s: image to large, must be [%d,%d] or smaller\n",
			__func__, main_display.native_x_res,
			main_display.native_y_res);
		err++;
	}
	if (bmp_header->planes != 1) {
		printf("%s: unsupported nplanes, must be 1\n", __func__);
		err++;
	}

	src_pitch = dib_width * dib_bitspp / 8;
	src_pitch = (src_pitch + 3) >> 2;	/* pad to 32-bit boundary */
	src_pitch <<= 2;
	debug("src_pitch=%d\n", src_pitch);

	if (dib_compression != BI_RGB && dib_compression != BI_BITFIELDS)
		err++;

	if (err != 0)
		return -EINVAL;

	/* set new pitch */
	dst_pitch = dib_width * 16 / 8;		/* dest is 16 bpp */
	dst_pitch = (dst_pitch + 7) >> 3;	/* pad to 64-bit boundary */
	dst_pitch <<= 3;
	debug("dst_pitch=%d\n", dst_pitch);

	/* image is stored upside-down in the file */
	bmp_dst = ovly_mem;
	bmp_src = (u8 *)(bmp_start + bmp_offset);
	debug("bmp copy dst=0x%08x, src=0x%08x, len=%d\n",
		(uint32_t)bmp_dst, (uint32_t)bmp_src, dib_bytesz);

	switch (dib_bitspp) {
	case 8:
		copy_indexed((u16 *)bmp_dst, bmp_src, dst_pitch, src_pitch,
					dib_width, dib_height, palette);
		break;
	case 16:
		copy_rgb565(bmp_dst, bmp_src, dst_pitch, src_pitch,
					dib_width, dib_height);
		break;
	case 24:
	case 32:
		copy_rgb((u16 *)bmp_dst, bmp_src, dst_pitch, src_pitch,
					dib_width, dib_height, dib_bitspp);
		break;
	default:
		printf("%s: unsupported bitspp=%d\n", __func__, dib_bitspp);
		return -EINVAL;
	}
	debug("%s: image OK\n", __func__);

	/* dss_enable_overlay */
	ovly = mcde_ovly_get(chnl);
	if (IS_ERR(ovly)) {
		err = PTR_ERR(ovly);
		printf("%s: Failed to get channel\n", __func__);
		return -err;
	}
	debug("ovly=%p ovly_mem=%p\n", (void *)ovly, (void *)ovly_mem);
	mcde_ovly_set_source_buf(ovly, (u32)ovly_mem);
	mcde_ovly_set_source_info(ovly, dst_pitch,
					main_display.default_pixel_format);
	mcde_ovly_set_source_area(ovly, 0, 0, dib_width, dib_height);
	if (dib_width == main_display.native_x_res)
		xpos = 0;
	else
		xpos = (main_display.native_x_res - dib_width) / 2;

	if (dib_height == main_display.native_y_res)
		ypos = 0;
	else
		ypos = (main_display.native_y_res - dib_height) / 2;
	mcde_ovly_set_dest_pos(ovly, xpos, ypos, 0);
	mcde_ovly_apply(ovly);

	return mcde_turn_on_display();
}