sp_surface.c revision 6acd63a4980951727939c0dd545a0324965b3834
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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#include "pipe/p_defines.h"
29#include "pipe/p_util.h"
30#include "pipe/p_inlines.h"
31#include "pipe/p_winsys.h"
32#include "util/p_tile.h"
33#include "sp_context.h"
34#include "sp_surface.h"
35
36
37
38/* Assumes all values are within bounds -- no checking at this level -
39 * do it higher up if required.
40 */
41static void
42sp_surface_copy(struct pipe_context *pipe,
43                unsigned do_flip,
44		struct pipe_surface *dst,
45		unsigned dstx, unsigned dsty,
46		struct pipe_surface *src,
47		unsigned srcx, unsigned srcy, unsigned width, unsigned height)
48{
49   assert( dst->cpp == src->cpp );
50
51   pipe_copy_rect(pipe_surface_map(dst),
52                  dst->cpp,
53                  dst->pitch,
54                  dstx, dsty,
55                  width, height,
56                  pipe_surface_map(src),
57                  do_flip ? -(int) src->pitch : src->pitch,
58                  srcx, do_flip ? 1 - srcy - height : srcy);
59
60   pipe_surface_unmap(src);
61   pipe_surface_unmap(dst);
62}
63
64
65static void *
66get_pointer(struct pipe_surface *dst, void *dst_map, unsigned x, unsigned y)
67{
68   return (char *)dst_map + (y * dst->pitch + x) * dst->cpp;
69}
70
71
72#define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
73
74
75/**
76 * Fill a rectangular sub-region.  Need better logic about when to
77 * push buffers into AGP - will currently do so whenever possible.
78 */
79static void
80sp_surface_fill(struct pipe_context *pipe,
81		struct pipe_surface *dst,
82		unsigned dstx, unsigned dsty,
83		unsigned width, unsigned height, unsigned value)
84{
85   unsigned i, j;
86   void *dst_map = pipe_surface_map(dst);
87
88   assert(dst->pitch > 0);
89   assert(width <= dst->pitch);
90
91
92   switch (dst->cpp) {
93   case 1:
94      {
95	 ubyte *row = get_pointer(dst, dst_map, dstx, dsty);
96         for (i = 0; i < height; i++) {
97            memset(row, value, width);
98	 row += dst->pitch;
99         }
100      }
101      break;
102   case 2:
103      {
104         ushort *row = get_pointer(dst, dst_map, dstx, dsty);
105         for (i = 0; i < height; i++) {
106            for (j = 0; j < width; j++)
107               row[j] = (ushort) value;
108            row += dst->pitch;
109         }
110      }
111      break;
112   case 4:
113      {
114         unsigned *row = get_pointer(dst, dst_map, dstx, dsty);
115         for (i = 0; i < height; i++) {
116            for (j = 0; j < width; j++)
117               row[j] = value;
118            row += dst->pitch;
119         }
120      }
121      break;
122   case 8:
123      {
124         /* expand the 4-byte clear value to an 8-byte value */
125         ushort *row = (ushort *) get_pointer(dst, dst_map, dstx, dsty);
126         ushort val0 = UBYTE_TO_USHORT((value >>  0) & 0xff);
127         ushort val1 = UBYTE_TO_USHORT((value >>  8) & 0xff);
128         ushort val2 = UBYTE_TO_USHORT((value >> 16) & 0xff);
129         ushort val3 = UBYTE_TO_USHORT((value >> 24) & 0xff);
130         val0 = (val0 << 8) | val0;
131         val1 = (val1 << 8) | val1;
132         val2 = (val2 << 8) | val2;
133         val3 = (val3 << 8) | val3;
134         for (i = 0; i < height; i++) {
135            for (j = 0; j < width; j++) {
136               row[j*4+0] = val0;
137               row[j*4+1] = val1;
138               row[j*4+2] = val2;
139               row[j*4+3] = val3;
140            }
141            row += dst->pitch * 4;
142         }
143      }
144      break;
145   default:
146      assert(0);
147      break;
148   }
149
150   pipe_surface_unmap( dst );
151}
152
153
154void
155sp_init_surface_functions(struct softpipe_context *sp)
156{
157   sp->pipe.surface_copy = sp_surface_copy;
158   sp->pipe.surface_fill = sp_surface_fill;
159}
160