1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/image.h"
33 #include "main/bufferobj.h"
34 #include "main/state.h"
35 #include "swrast/swrast.h"
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_blit.h"
41 #include "intel_buffers.h"
42 #include "intel_regions.h"
43 #include "intel_pixel.h"
44 #include "intel_buffer_objects.h"
46 /* For many applications, the new ability to pull the source buffers
47 * back out of the GTT and then do the packing/conversion operations
48 * in software will be as much of an improvement as trying to get the
49 * blitter and/or texture engine to do the work.
51 * This step is gated on private backbuffers.
53 * Obviously the frontbuffer can't be pulled back, so that is either
54 * an argument for blit/texture readpixels, or for blitting to a
55 * temporary and then pulling that back.
57 * When the destination is a pbo, however, it's not clear if it is
58 * ever going to be pulled to main memory (though the access param
59 * will be a good hint). So it sounds like we do want to be able to
60 * choose between blit/texture implementation on the gpu and pullback
61 * and cpu-based copying.
63 * Unless you can magically turn client memory into a PBO for the
64 * duration of this call, there will be a cpu-based copying step in
70 do_texture_readpixels(GLcontext
* ctx
,
71 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
72 GLenum format
, GLenum type
,
73 const struct gl_pixelstore_attrib
*pack
,
74 struct intel_region
*dest_region
)
77 struct intel_context
*intel
= intel_context(ctx
);
78 intelScreenPrivate
*screen
= intel
->intelScreen
;
79 GLint pitch
= pack
->RowLength
? pack
->RowLength
: width
;
80 __DRIdrawable
*dPriv
= intel
->driDrawable
;
82 GLenum glTextureFormat
;
83 int destFormat
, depthFormat
, destPitch
;
86 if (INTEL_DEBUG
& DEBUG_PIXEL
)
87 fprintf(stderr
, "%s\n", __FUNCTION__
);
90 if (ctx
->_ImageTransferState
||
91 pack
->SwapBytes
|| pack
->LsbFirst
|| !pack
->Invert
) {
92 if (INTEL_DEBUG
& DEBUG_PIXEL
)
93 fprintf(stderr
, "%s: check_color failed\n", __FUNCTION__
);
97 intel
->vtbl
.meta_texrect_source(intel
, intel_readbuf_region(intel
));
99 if (!intel
->vtbl
.meta_render_dest(intel
, dest_region
, type
, format
)) {
100 if (INTEL_DEBUG
& DEBUG_PIXEL
)
101 fprintf(stderr
, "%s: couldn't set dest %s/%s\n",
103 _mesa_lookup_enum_by_nr(type
),
104 _mesa_lookup_enum_by_nr(format
));
108 if (intel
->driDrawable
->numClipRects
) {
109 intel
->vtbl
.install_meta_state(intel
);
110 intel
->vtbl
.meta_no_depth_write(intel
);
111 intel
->vtbl
.meta_no_stencil_write(intel
);
113 if (!driClipRectToFramebuffer(ctx
->ReadBuffer
, &x
, &y
, &width
, &height
)) {
114 SET_STATE(i830
, state
);
115 if (INTEL_DEBUG
& DEBUG_PIXEL
)
116 fprintf(stderr
, "%s: cliprect failed\n", __FUNCTION__
);
120 y
= dPriv
->h
- y
- height
;
125 /* Set the frontbuffer up as a large rectangular texture.
127 intel
->vtbl
.meta_tex_rect_source(intel
, src_region
, textureFormat
);
130 intel
->vtbl
.meta_texture_blend_replace(i830
, glTextureFormat
);
133 /* Set the 3d engine to draw into the destination region:
136 intel
->vtbl
.meta_draw_region(intel
, dest_region
);
137 intel
->vtbl
.meta_draw_format(intel
, destFormat
, depthFormat
); /* ?? */
140 /* Draw a single quad, no cliprects:
142 intel
->vtbl
.meta_disable_cliprects(intel
);
144 intel
->vtbl
.draw_quad(intel
,
146 0x00ff00ff, x
, x
+ width
, y
, y
+ height
);
148 intel
->vtbl
.leave_meta_state(intel
);
151 intel_region_wait_fence(ctx
, dest_region
); /* required by GL */
162 do_blit_readpixels(GLcontext
* ctx
,
163 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
164 GLenum format
, GLenum type
,
165 const struct gl_pixelstore_attrib
*pack
, GLvoid
* pixels
)
167 struct intel_context
*intel
= intel_context(ctx
);
168 struct intel_region
*src
= intel_readbuf_region(intel
);
169 struct intel_buffer_object
*dst
= intel_buffer_object(pack
->BufferObj
);
173 if (INTEL_DEBUG
& DEBUG_PIXEL
)
174 _mesa_printf("%s\n", __FUNCTION__
);
179 if (!_mesa_is_bufferobj(pack
->BufferObj
)) {
182 if (INTEL_DEBUG
& DEBUG_PIXEL
)
183 _mesa_printf("%s - not PBO\n", __FUNCTION__
);
188 if (ctx
->_ImageTransferState
||
189 !intel_check_blit_format(src
, format
, type
)) {
190 if (INTEL_DEBUG
& DEBUG_PIXEL
)
191 _mesa_printf("%s - bad format for blit\n", __FUNCTION__
);
195 if (pack
->Alignment
!= 1 || pack
->SwapBytes
|| pack
->LsbFirst
) {
196 if (INTEL_DEBUG
& DEBUG_PIXEL
)
197 _mesa_printf("%s: bad packing params\n", __FUNCTION__
);
201 if (pack
->RowLength
> 0)
202 rowLength
= pack
->RowLength
;
207 if (INTEL_DEBUG
& DEBUG_PIXEL
)
208 _mesa_printf("%s: MESA_PACK_INVERT not done yet\n", __FUNCTION__
);
212 rowLength
= -rowLength
;
215 dst_offset
= (GLintptr
) _mesa_image_address(2, pack
, pixels
, width
, height
,
216 format
, type
, 0, 0, 0);
219 /* Although the blits go on the command buffer, need to do this and
220 * fire with lock held to guarentee cliprects are correct.
222 intelFlush(&intel
->ctx
);
224 if (intel
->driReadDrawable
->numClipRects
) {
225 GLboolean all
= (width
* height
* src
->cpp
== dst
->Base
.Size
&&
226 x
== 0 && dst_offset
== 0);
228 dri_bo
*dst_buffer
= intel_bufferobj_buffer(intel
, dst
,
229 all
? INTEL_WRITE_FULL
:
231 __DRIdrawable
*dPriv
= intel
->driReadDrawable
;
232 int nbox
= dPriv
->numClipRects
;
233 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
234 drm_clip_rect_t rect
;
235 drm_clip_rect_t src_rect
;
238 src_rect
.x1
= dPriv
->x
+ x
;
239 src_rect
.y1
= dPriv
->y
+ dPriv
->h
- (y
+ height
);
240 src_rect
.x2
= src_rect
.x1
+ width
;
241 src_rect
.y2
= src_rect
.y1
+ height
;
245 for (i
= 0; i
< nbox
; i
++) {
246 if (!intel_intersect_cliprects(&rect
, &src_rect
, &box
[i
]))
249 if (!intelEmitCopyBlit(intel
,
251 src
->pitch
, src
->buffer
, 0, src
->tiling
,
252 rowLength
, dst_buffer
, dst_offset
, GL_FALSE
,
255 rect
.x1
- src_rect
.x1
,
256 rect
.y2
- src_rect
.y2
,
257 rect
.x2
- rect
.x1
, rect
.y2
- rect
.y1
,
264 if (INTEL_DEBUG
& DEBUG_PIXEL
)
265 _mesa_printf("%s - DONE\n", __FUNCTION__
);
271 intelReadPixels(GLcontext
* ctx
,
272 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
273 GLenum format
, GLenum type
,
274 const struct gl_pixelstore_attrib
*pack
, GLvoid
* pixels
)
276 if (INTEL_DEBUG
& DEBUG_PIXEL
)
277 fprintf(stderr
, "%s\n", __FUNCTION__
);
281 if (do_blit_readpixels
282 (ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
))
286 if (do_texture_readpixels
287 (ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
))
290 (void)do_blit_readpixels
;
291 (void)do_texture_readpixels
;
294 if (INTEL_DEBUG
& DEBUG_PIXEL
)
295 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__
);
297 /* Update Mesa state before calling down into _swrast_ReadPixels, as
298 * the spans code requires the computed buffer states to be up to date,
299 * but _swrast_ReadPixels only updates Mesa state after setting up
304 _mesa_update_state(ctx
);
306 _swrast_ReadPixels(ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);