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