Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / intel / intel_pixel_bitmap.c
1 /**************************************************************************
2  * 
3  * Copyright 2006 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 portionsalloc
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 "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/colormac.h"
32 #include "main/condrender.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/pbo.h"
36 #include "main/bufferobj.h"
37 #include "main/state.h"
38 #include "main/texobj.h"
39 #include "main/context.h"
40 #include "swrast/swrast.h"
41 #include "drivers/common/meta.h"
42
43 #include "intel_screen.h"
44 #include "intel_context.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_blit.h"
47 #include "intel_regions.h"
48 #include "intel_buffers.h"
49 #include "intel_pixel.h"
50 #include "intel_reg.h"
51
52
53 #define FILE_DEBUG_FLAG DEBUG_PIXEL
54
55
56 /* Unlike the other intel_pixel_* functions, the expectation here is
57  * that the incoming data is not in a PBO.  With the XY_TEXT blit
58  * method, there's no benefit haveing it in a PBO, but we could
59  * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
60  * PBO bitmaps.  I think they are probably pretty rare though - I
61  * wonder if Xgl uses them?
62  */
63 static const GLubyte *map_pbo( struct gl_context *ctx,
64                                GLsizei width, GLsizei height,
65                                const struct gl_pixelstore_attrib *unpack,
66                                const GLubyte *bitmap )
67 {
68    GLubyte *buf;
69
70    if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
71                                   GL_COLOR_INDEX, GL_BITMAP,
72                                   INT_MAX, (const GLvoid *) bitmap)) {
73       _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
74       return NULL;
75    }
76
77    buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
78                                            GL_READ_ONLY_ARB,
79                                            unpack->BufferObj);
80    if (!buf) {
81       _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
82       return NULL;
83    }
84
85    return ADD_POINTERS(buf, bitmap);
86 }
87
88 static GLboolean test_bit( const GLubyte *src, GLuint bit )
89 {
90    return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
91 }
92
93 static void set_bit( GLubyte *dest, GLuint bit )
94 {
95    dest[bit/8] |= 1 << (bit % 8);
96 }
97
98 /* Extract a rectangle's worth of data from the bitmap.  Called
99  * per chunk of HW-sized bitmap.
100  */
101 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
102                               const struct gl_pixelstore_attrib *unpack,
103                               const GLubyte *bitmap,
104                               GLuint x, GLuint y, 
105                               GLuint w, GLuint h,
106                               GLubyte *dest,
107                               GLuint row_align,
108                               GLboolean invert)
109 {
110    GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
111    GLuint mask = unpack->LsbFirst ? 0 : 7;
112    GLuint bit = 0;
113    GLint row, col;
114    GLint first, last;
115    GLint incr;
116    GLuint count = 0;
117
118    DBG("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
119        __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
120
121    if (invert) {
122       first = h-1;
123       last = 0;
124       incr = -1;
125    }
126    else {
127       first = 0;
128       last = h-1;
129       incr = 1;
130    }
131
132    /* Require that dest be pre-zero'd.
133     */
134    for (row = first; row != (last+incr); row += incr) {
135       const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap, 
136                                                     width, height, 
137                                                     GL_COLOR_INDEX, GL_BITMAP, 
138                                                     y + row, x);
139
140       for (col = 0; col < w; col++, bit++) {
141          if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
142             set_bit(dest, bit ^ 7);
143             count++;
144          }
145       }
146
147       if (row_align)
148          bit = ALIGN(bit, row_align);
149    }
150
151    return count;
152 }
153
154 /**
155  * Returns the low Y value of the vertical range given, flipped according to
156  * whether the framebuffer is or not.
157  */
158 static INLINE int
159 y_flip(struct gl_framebuffer *fb, int y, int height)
160 {
161    if (fb->Name != 0)
162       return y;
163    else
164       return fb->Height - y - height;
165 }
166
167 /*
168  * Render a bitmap.
169  */
170 static GLboolean
171 do_blit_bitmap( struct gl_context *ctx, 
172                 GLint dstx, GLint dsty,
173                 GLsizei width, GLsizei height,
174                 const struct gl_pixelstore_attrib *unpack,
175                 const GLubyte *bitmap )
176 {
177    struct intel_context *intel = intel_context(ctx);
178    struct intel_region *dst;
179    struct gl_framebuffer *fb = ctx->DrawBuffer;
180    GLfloat tmpColor[4];
181    GLubyte ubcolor[4];
182    GLuint color;
183    GLsizei bitmap_width = width;
184    GLsizei bitmap_height = height;
185    GLint px, py;
186    GLuint stipple[32];
187    GLint orig_dstx = dstx;
188    GLint orig_dsty = dsty;
189
190    /* Update draw buffer bounds */
191    _mesa_update_state(ctx);
192
193    if (ctx->Depth.Test) {
194       /* The blit path produces incorrect results when depth testing is on.
195        * It seems the blit Z coord is always 1.0 (the far plane) so fragments
196        * will likely be obscured by other, closer geometry.
197        */
198       return GL_FALSE;
199    }
200
201    intel_prepare_render(intel);
202    dst = intel_drawbuf_region(intel);
203
204    if (!dst)
205        return GL_FALSE;
206
207    if (_mesa_is_bufferobj(unpack->BufferObj)) {
208       bitmap = map_pbo(ctx, width, height, unpack, bitmap);
209       if (bitmap == NULL)
210          return GL_TRUE;        /* even though this is an error, we're done */
211    }
212
213    COPY_4V(tmpColor, ctx->Current.RasterColor);
214
215    if (_mesa_need_secondary_color(ctx)) {
216        ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
217    }
218
219    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
220    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
221    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
222    UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
223
224    if (dst->cpp == 2)
225       color = PACK_COLOR_565(ubcolor[0], ubcolor[1], ubcolor[2]);
226    else
227       color = PACK_COLOR_8888(ubcolor[3], ubcolor[0], ubcolor[1], ubcolor[2]);
228
229    if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
230       return GL_FALSE;
231
232    /* Clip to buffer bounds and scissor. */
233    if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
234                              fb->_Xmax, fb->_Ymax,
235                              &dstx, &dsty, &width, &height))
236       goto out;
237
238    dsty = y_flip(fb, dsty, height);
239
240 #define DY 32
241 #define DX 32
242
243    /* Chop it all into chunks that can be digested by hardware: */
244    for (py = 0; py < height; py += DY) {
245       for (px = 0; px < width; px += DX) {
246          int h = MIN2(DY, height - py);
247          int w = MIN2(DX, width - px);
248          GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
249          GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
250             ctx->Color.LogicOp : GL_COPY;
251
252          assert(sz <= sizeof(stipple));
253          memset(stipple, 0, sz);
254
255          /* May need to adjust this when padding has been introduced in
256           * sz above:
257           *
258           * Have to translate destination coordinates back into source
259           * coordinates.
260           */
261          if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
262                              bitmap,
263                              -orig_dstx + (dstx + px),
264                              -orig_dsty + y_flip(fb, dsty + py, h),
265                              w, h,
266                              (GLubyte *)stipple,
267                              8,
268                              fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
269             continue;
270
271          if (!intelEmitImmediateColorExpandBlit(intel,
272                                                 dst->cpp,
273                                                 (GLubyte *)stipple,
274                                                 sz,
275                                                 color,
276                                                 dst->pitch,
277                                                 dst->buffer,
278                                                 0,
279                                                 dst->tiling,
280                                                 dstx + px,
281                                                 dsty + py,
282                                                 w, h,
283                                                 logic_op)) {
284             return GL_FALSE;
285          }
286       }
287    }
288 out:
289
290    if (unlikely(INTEL_DEBUG & DEBUG_SYNC))
291       intel_batchbuffer_flush(intel);
292
293    if (_mesa_is_bufferobj(unpack->BufferObj)) {
294       /* done with PBO so unmap it now */
295       ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
296                               unpack->BufferObj);
297    }
298
299    intel_check_front_buffer_rendering(intel);
300
301    return GL_TRUE;
302 }
303
304
305 /* There are a large number of possible ways to implement bitmap on
306  * this hardware, most of them have some sort of drawback.  Here are a
307  * few that spring to mind:
308  * 
309  * Blit:
310  *    - XY_MONO_SRC_BLT_CMD
311  *         - use XY_SETUP_CLIP_BLT for cliprect clipping.
312  *    - XY_TEXT_BLT
313  *    - XY_TEXT_IMMEDIATE_BLT
314  *         - blit per cliprect, subject to maximum immediate data size.
315  *    - XY_COLOR_BLT 
316  *         - per pixel or run of pixels
317  *    - XY_PIXEL_BLT
318  *         - good for sparse bitmaps
319  *
320  * 3D engine:
321  *    - Point per pixel
322  *    - Translate bitmap to an alpha texture and render as a quad
323  *    - Chop bitmap up into 32x32 squares and render w/polygon stipple.
324  */
325 void
326 intelBitmap(struct gl_context * ctx,
327             GLint x, GLint y,
328             GLsizei width, GLsizei height,
329             const struct gl_pixelstore_attrib *unpack,
330             const GLubyte * pixels)
331 {
332    struct intel_context *intel = intel_context(ctx);
333
334    if (!_mesa_check_conditional_render(ctx))
335       return;
336
337    if (do_blit_bitmap(ctx, x, y, width, height,
338                           unpack, pixels))
339       return;
340
341    /* FIXME */
342    if (intel->gen == 6)
343        return _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
344
345    _mesa_meta_Bitmap(ctx, x, y, width, height, unpack, pixels);
346 }