c1980b316d53004f5f0e93a86f8734269fa698ae
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tile_image.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27
28 #include "lp_tile_soa.h"
29 #include "lp_tile_image.h"
30
31
32 #define BYTES_PER_TILE (TILE_SIZE * TILE_SIZE * 4)
33
34
35 /**
36 * Convert a tiled image into a linear image.
37 * \param src_stride source row stride in bytes (bytes per row of tiles)
38 * \param dst_stride dest row stride in bytes
39 */
40 void
41 lp_tiled_to_linear(const uint8_t *src,
42 uint8_t *dst,
43 unsigned width, unsigned height,
44 enum pipe_format format,
45 unsigned src_stride,
46 unsigned dst_stride)
47 {
48 const unsigned tiles_per_row = src_stride / BYTES_PER_TILE;
49 unsigned i, j;
50
51 for (j = 0; j < height; j += TILE_SIZE) {
52 for (i = 0; i < width; i += TILE_SIZE) {
53 unsigned tile_offset =
54 ((j / TILE_SIZE) * tiles_per_row + i / TILE_SIZE);
55 unsigned byte_offset = tile_offset * BYTES_PER_TILE;
56 const uint8_t *src_tile = src + byte_offset;
57
58 lp_tile_write_4ub(format,
59 src_tile,
60 dst,
61 dst_stride,
62 i, j, TILE_SIZE, TILE_SIZE);
63 }
64 }
65 }
66
67
68 /**
69 * Convert a linear image into a tiled image.
70 * \param src_stride source row stride in bytes
71 * \param dst_stride dest row stride in bytes (bytes per row of tiles)
72 */
73 void
74 lp_linear_to_tiled(const uint8_t *src,
75 uint8_t *dst,
76 unsigned width, unsigned height,
77 enum pipe_format format,
78 unsigned src_stride,
79 unsigned dst_stride)
80 {
81 const unsigned tiles_per_row = dst_stride / BYTES_PER_TILE;
82 unsigned i, j;
83
84 for (j = 0; j < height; j += TILE_SIZE) {
85 for (i = 0; i < width; i += TILE_SIZE) {
86 unsigned tile_offset =
87 ((j / TILE_SIZE) * tiles_per_row + i / TILE_SIZE);
88 unsigned byte_offset = tile_offset * BYTES_PER_TILE;
89 uint8_t *dst_tile = dst + byte_offset;
90
91 lp_tile_read_4ub(format,
92 dst_tile,
93 src,
94 src_stride,
95 i, j, TILE_SIZE, TILE_SIZE);
96 }
97 }
98 }
99
100
101 /**
102 * For testing only.
103 */
104 void
105 test_tiled_linear_conversion(uint8_t *data,
106 enum pipe_format format,
107 unsigned width, unsigned height,
108 unsigned stride)
109 {
110 /* size in tiles */
111 unsigned wt = (width + TILE_SIZE - 1) / TILE_SIZE;
112 unsigned ht = (height + TILE_SIZE - 1) / TILE_SIZE;
113
114 uint8_t *tiled = malloc(wt * ht * TILE_SIZE * TILE_SIZE * 4);
115
116 unsigned tiled_stride = wt * TILE_SIZE * TILE_SIZE * 4;
117
118 lp_linear_to_tiled(data, tiled, width, height, format,
119 stride, tiled_stride);
120
121 lp_tiled_to_linear(tiled, data, width, height, format,
122 tiled_stride, stride);
123
124 free(tiled);
125 }
126