1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3b8e80941Smrg * Copyright (c) 2018 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4b8e80941Smrg * Copyright (c) 2018 Vasily Khoruzhick <anarsoul@gmail.com>
5b8e80941Smrg *
6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
8b8e80941Smrg * to deal in the Software without restriction, including without limitation
9b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
10b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
11b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
12b8e80941Smrg *
13b8e80941Smrg * The above copyright notice and this permission notice (including the
14b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
15b8e80941Smrg * of the Software.
16b8e80941Smrg *
17b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23b8e80941Smrg * DEALINGS IN THE SOFTWARE.
24b8e80941Smrg *
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#include "lima_tiling.h"
28b8e80941Smrg
29b8e80941Smrguint32_t space_filler[16][16] = {
30b8e80941Smrg   { 0,   1,   4,   5,   16,  17,  20,  21,  64,  65,  68,  69,  80,  81,  84,  85, },
31b8e80941Smrg   { 3,   2,   7,   6,   19,  18,  23,  22,  67,  66,  71,  70,  83,  82,  87,  86, },
32b8e80941Smrg   { 12,  13,  8,   9,   28,  29,  24,  25,  76,  77,  72,  73,  92,  93,  88,  89, },
33b8e80941Smrg   { 15,  14,  11,  10,  31,  30,  27,  26,  79,  78,  75,  74,  95,  94,  91,  90, },
34b8e80941Smrg   { 48,  49,  52,  53,  32,  33,  36,  37,  112, 113, 116, 117, 96,  97,  100, 101, },
35b8e80941Smrg   { 51,  50,  55,  54,  35,  34,  39,  38,  115, 114, 119, 118, 99,  98,  103, 102, },
36b8e80941Smrg   { 60,  61,  56,  57,  44,  45,  40,  41,  124, 125, 120, 121, 108, 109, 104, 105, },
37b8e80941Smrg   { 63,  62,  59,  58,  47,  46,  43,  42,  127, 126, 123, 122, 111, 110, 107, 106, },
38b8e80941Smrg   { 192, 193, 196, 197, 208, 209, 212, 213, 128, 129, 132, 133, 144, 145, 148, 149, },
39b8e80941Smrg   { 195, 194, 199, 198, 211, 210, 215, 214, 131, 130, 135, 134, 147, 146, 151, 150, },
40b8e80941Smrg   { 204, 205, 200, 201, 220, 221, 216, 217, 140, 141, 136, 137, 156, 157, 152, 153, },
41b8e80941Smrg   { 207, 206, 203, 202, 223, 222, 219, 218, 143, 142, 139, 138, 159, 158, 155, 154, },
42b8e80941Smrg   { 240, 241, 244, 245, 224, 225, 228, 229, 176, 177, 180, 181, 160, 161, 164, 165, },
43b8e80941Smrg   { 243, 242, 247, 246, 227, 226, 231, 230, 179, 178, 183, 182, 163, 162, 167, 166, },
44b8e80941Smrg   { 252, 253, 248, 249, 236, 237, 232, 233, 188, 189, 184, 185, 172, 173, 168, 169, },
45b8e80941Smrg   { 255, 254, 251, 250, 239, 238, 235, 234, 191, 190, 187, 186, 175, 174, 171, 170, },
46b8e80941Smrg};
47b8e80941Smrg
48b8e80941Smrgstatic void
49b8e80941Smrglima_store_tiled_image_bpp4(void *dst, const void *src,
50b8e80941Smrg                               const struct pipe_box *box,
51b8e80941Smrg                               uint32_t dst_stride,
52b8e80941Smrg                               uint32_t src_stride)
53b8e80941Smrg{
54b8e80941Smrg   for (int y = box->y, src_y = 0; src_y < box->height; ++y, ++src_y) {
55b8e80941Smrg      int block_y = y & ~0x0f;
56b8e80941Smrg      int rem_y = y & 0x0F;
57b8e80941Smrg      int block_start_s = block_y * dst_stride;
58b8e80941Smrg      int source_start = src_y * src_stride;
59b8e80941Smrg
60b8e80941Smrg      for (int x = box->x, src_x = 0; src_x < box->width; ++x, ++src_x) {
61b8e80941Smrg         int block_x_s = (x >> 4) * 256;
62b8e80941Smrg         int rem_x = x & 0x0F;
63b8e80941Smrg
64b8e80941Smrg         int index = space_filler[rem_y][rem_x];
65b8e80941Smrg         const uint32_t *source = src + source_start + 4 * src_x;
66b8e80941Smrg         uint32_t *dest = dst + block_start_s + 4 * (block_x_s + index);
67b8e80941Smrg
68b8e80941Smrg         *dest = *source;
69b8e80941Smrg      }
70b8e80941Smrg   }
71b8e80941Smrg}
72b8e80941Smrg
73b8e80941Smrgstatic void
74b8e80941Smrglima_store_tiled_image_generic(void *dst, const void *src,
75b8e80941Smrg                               const struct pipe_box *box,
76b8e80941Smrg                               uint32_t dst_stride,
77b8e80941Smrg                               uint32_t src_stride,
78b8e80941Smrg                               uint32_t bpp)
79b8e80941Smrg{
80b8e80941Smrg   for (int y = box->y, src_y = 0; src_y < box->height; ++y, ++src_y) {
81b8e80941Smrg      int block_y = y & ~0x0f;
82b8e80941Smrg      int rem_y = y & 0x0F;
83b8e80941Smrg      int block_start_s = block_y * dst_stride;
84b8e80941Smrg      int source_start = src_y * src_stride;
85b8e80941Smrg
86b8e80941Smrg      for (int x = box->x, src_x = 0; src_x < box->width; ++x, ++src_x) {
87b8e80941Smrg         int block_x_s = (x >> 4) * 256;
88b8e80941Smrg         int rem_x = x & 0x0F;
89b8e80941Smrg
90b8e80941Smrg         int index = space_filler[rem_y][rem_x];
91b8e80941Smrg         const uint8_t *src8 = src;
92b8e80941Smrg         const uint8_t *source = &src8[source_start + bpp * src_x];
93b8e80941Smrg         uint8_t *dest = dst + block_start_s + bpp * (block_x_s + index);
94b8e80941Smrg
95b8e80941Smrg         for (int b = 0; b < bpp; ++b)
96b8e80941Smrg            dest[b] = source[b];
97b8e80941Smrg      }
98b8e80941Smrg   }
99b8e80941Smrg}
100b8e80941Smrg
101b8e80941Smrgstatic void
102b8e80941Smrglima_load_tiled_image_bpp4(void *dst, const void *src,
103b8e80941Smrg                              const struct pipe_box *box,
104b8e80941Smrg                              uint32_t dst_stride,
105b8e80941Smrg                              uint32_t src_stride)
106b8e80941Smrg{
107b8e80941Smrg   for (int y = box->y, dest_y = 0; dest_y < box->height; ++y, ++dest_y) {
108b8e80941Smrg      int block_y = y & ~0x0f;
109b8e80941Smrg      int rem_y = y & 0x0F;
110b8e80941Smrg      int block_start_s = block_y * src_stride;
111b8e80941Smrg      int dest_start = dest_y * dst_stride;
112b8e80941Smrg
113b8e80941Smrg      for (int x = box->x, dest_x = 0; dest_x < box->width; ++x, ++dest_x) {
114b8e80941Smrg         int block_x_s = (x >> 4) * 256;
115b8e80941Smrg         int rem_x = x & 0x0F;
116b8e80941Smrg
117b8e80941Smrg         int index = space_filler[rem_y][rem_x];
118b8e80941Smrg         uint32_t *dest = dst + dest_start + 4 * dest_x;
119b8e80941Smrg         const uint32_t *source = src + block_start_s + 4 * (block_x_s + index);
120b8e80941Smrg
121b8e80941Smrg         *dest = *source;
122b8e80941Smrg      }
123b8e80941Smrg   }
124b8e80941Smrg}
125b8e80941Smrg
126b8e80941Smrgstatic void
127b8e80941Smrglima_load_tiled_image_generic(void *dst, const void *src,
128b8e80941Smrg                              const struct pipe_box *box,
129b8e80941Smrg                              uint32_t dst_stride,
130b8e80941Smrg                              uint32_t src_stride,
131b8e80941Smrg                              uint32_t bpp)
132b8e80941Smrg{
133b8e80941Smrg   for (int y = box->y, dest_y = 0; dest_y < box->height; ++y, ++dest_y) {
134b8e80941Smrg      int block_y = y & ~0x0f;
135b8e80941Smrg      int rem_y = y & 0x0F;
136b8e80941Smrg      int block_start_s = block_y * src_stride;
137b8e80941Smrg      int dest_start = dest_y * dst_stride;
138b8e80941Smrg
139b8e80941Smrg      for (int x = box->x, dest_x = 0; dest_x < box->width; ++x, ++dest_x) {
140b8e80941Smrg         int block_x_s = (x >> 4) * 256;
141b8e80941Smrg         int rem_x = x & 0x0F;
142b8e80941Smrg
143b8e80941Smrg         int index = space_filler[rem_y][rem_x];
144b8e80941Smrg         uint8_t *dst8 = dst;
145b8e80941Smrg         uint8_t *dest = &dst8[dest_start + bpp * dest_x];
146b8e80941Smrg         const uint8_t *source = src + block_start_s + bpp * (block_x_s + index);
147b8e80941Smrg
148b8e80941Smrg         for (int b = 0; b < bpp; ++b)
149b8e80941Smrg            dest[b] = source[b];
150b8e80941Smrg      }
151b8e80941Smrg   }
152b8e80941Smrg}
153b8e80941Smrg
154b8e80941Smrgvoid
155b8e80941Smrglima_store_tiled_image(void *dst, const void *src,
156b8e80941Smrg                           const struct pipe_box *box,
157b8e80941Smrg                           uint32_t dst_stride,
158b8e80941Smrg                           uint32_t src_stride,
159b8e80941Smrg                           uint32_t bpp)
160b8e80941Smrg{
161b8e80941Smrg	switch (bpp) {
162b8e80941Smrg	case 4:
163b8e80941Smrg		lima_store_tiled_image_bpp4(dst, src, box, dst_stride, src_stride);
164b8e80941Smrg		break;
165b8e80941Smrg	default:
166b8e80941Smrg		lima_store_tiled_image_generic(dst, src, box, dst_stride, src_stride, bpp);
167b8e80941Smrg	}
168b8e80941Smrg}
169b8e80941Smrg
170b8e80941Smrgvoid
171b8e80941Smrglima_load_tiled_image(void *dst, const void *src,
172b8e80941Smrg                           const struct pipe_box *box,
173b8e80941Smrg                           uint32_t dst_stride,
174b8e80941Smrg                           uint32_t src_stride,
175b8e80941Smrg                           uint32_t bpp)
176b8e80941Smrg{
177b8e80941Smrg	switch (bpp) {
178b8e80941Smrg	case 4:
179b8e80941Smrg		lima_load_tiled_image_bpp4(dst, src, box, dst_stride, src_stride);
180b8e80941Smrg		break;
181b8e80941Smrg	default:
182b8e80941Smrg		lima_load_tiled_image_generic(dst, src, box, dst_stride, src_stride, bpp);
183b8e80941Smrg	}
184b8e80941Smrg}
185