Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / util / u_linear.c
1 /**************************************************************************
2  * 
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 /**
29  * Functions for converting tiled data to linear and vice versa.
30  */
31
32
33 #include "util/u_debug.h"
34 #include "u_linear.h"
35
36 void
37 pipe_linear_to_tile(size_t src_stride, const void *src_ptr,
38                     struct pipe_tile_info *t, void *dst_ptr)
39 {
40    int x, y, z;
41    char *ptr;
42    size_t bytes = t->cols * t->block.size;
43    char *dst_ptr2 = (char *) dst_ptr;
44
45    assert(pipe_linear_check_tile(t));
46
47    /* lets write lineary to the tiled buffer */
48    for (y = 0; y < t->tiles_y; y++) {
49       for (x = 0; x < t->tiles_x; x++) {
50          /* this inner loop could be replace with SSE magic */
51          ptr = (char*)src_ptr + src_stride * t->rows * y + bytes * x;
52          for (z = 0; z < t->rows; z++) {
53             memcpy(dst_ptr2, ptr, bytes);
54             dst_ptr2 += bytes;
55             ptr += src_stride;
56          }
57       }
58    }
59 }
60
61 void pipe_linear_from_tile(struct pipe_tile_info *t, const void *src_ptr,
62                            size_t dst_stride, void *dst_ptr)
63 {
64    int x, y, z;
65    char *ptr;
66    size_t bytes = t->cols * t->block.size;
67    const char *src_ptr2 = (const char *) src_ptr;
68
69    /* lets read lineary from the tiled buffer */
70    for (y = 0; y < t->tiles_y; y++) {
71       for (x = 0; x < t->tiles_x; x++) {
72          /* this inner loop could be replace with SSE magic */
73          ptr = (char*)dst_ptr + dst_stride * t->rows * y + bytes * x;
74          for (z = 0; z < t->rows; z++) {
75             memcpy(ptr, src_ptr2, bytes);
76             src_ptr2 += bytes;
77             ptr += dst_stride;
78          }
79       }
80    }
81 }
82
83 void
84 pipe_linear_fill_info(struct pipe_tile_info *t,
85                       const struct u_linear_format_block *block,
86                       unsigned tile_width, unsigned tile_height,
87                       unsigned tiles_x, unsigned tiles_y)
88 {
89    t->block = *block;
90
91    t->tile.width = tile_width;
92    t->tile.height = tile_height;
93    t->cols = t->tile.width / t->block.width;
94    t->rows = t->tile.height / t->block.height;
95    t->tile.size = t->cols * t->rows * t->block.size;
96
97    t->tiles_x = tiles_x;
98    t->tiles_y = tiles_y;
99    t->stride = t->cols * t->tiles_x * t->block.size;
100    t->size = t->tiles_x * t->tiles_y * t->tile.size;
101 }