intel: Drop intelFlush()
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_read.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 "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"
36
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_blit.h"
40 #include "intel_buffers.h"
41 #include "intel_regions.h"
42 #include "intel_pixel.h"
43 #include "intel_buffer_objects.h"
44
45 /* For many applications, the new ability to pull the source buffers
46 * back out of the GTT and then do the packing/conversion operations
47 * in software will be as much of an improvement as trying to get the
48 * blitter and/or texture engine to do the work.
49 *
50 * This step is gated on private backbuffers.
51 *
52 * Obviously the frontbuffer can't be pulled back, so that is either
53 * an argument for blit/texture readpixels, or for blitting to a
54 * temporary and then pulling that back.
55 *
56 * When the destination is a pbo, however, it's not clear if it is
57 * ever going to be pulled to main memory (though the access param
58 * will be a good hint). So it sounds like we do want to be able to
59 * choose between blit/texture implementation on the gpu and pullback
60 * and cpu-based copying.
61 *
62 * Unless you can magically turn client memory into a PBO for the
63 * duration of this call, there will be a cpu-based copying step in
64 * any case.
65 */
66
67 static GLboolean
68 do_blit_readpixels(GLcontext * ctx,
69 GLint x, GLint y, GLsizei width, GLsizei height,
70 GLenum format, GLenum type,
71 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
72 {
73 struct intel_context *intel = intel_context(ctx);
74 struct intel_region *src = intel_readbuf_region(intel);
75 struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
76 GLuint dst_offset;
77 GLuint rowLength;
78 drm_intel_bo *dst_buffer;
79 GLboolean all;
80 GLint dst_x, dst_y;
81
82 if (INTEL_DEBUG & DEBUG_PIXEL)
83 printf("%s\n", __FUNCTION__);
84
85 if (!src)
86 return GL_FALSE;
87
88 if (!_mesa_is_bufferobj(pack->BufferObj)) {
89 /* PBO only for now:
90 */
91 if (INTEL_DEBUG & DEBUG_PIXEL)
92 printf("%s - not PBO\n", __FUNCTION__);
93 return GL_FALSE;
94 }
95
96
97 if (ctx->_ImageTransferState ||
98 !intel_check_blit_format(src, format, type)) {
99 if (INTEL_DEBUG & DEBUG_PIXEL)
100 printf("%s - bad format for blit\n", __FUNCTION__);
101 return GL_FALSE;
102 }
103
104 if (pack->Alignment != 1 || pack->SwapBytes || pack->LsbFirst) {
105 if (INTEL_DEBUG & DEBUG_PIXEL)
106 printf("%s: bad packing params\n", __FUNCTION__);
107 return GL_FALSE;
108 }
109
110 if (pack->RowLength > 0)
111 rowLength = pack->RowLength;
112 else
113 rowLength = width;
114
115 if (pack->Invert) {
116 if (INTEL_DEBUG & DEBUG_PIXEL)
117 printf("%s: MESA_PACK_INVERT not done yet\n", __FUNCTION__);
118 return GL_FALSE;
119 }
120 else {
121 if (ctx->ReadBuffer->Name == 0)
122 rowLength = -rowLength;
123 }
124
125 dst_offset = (GLintptr) _mesa_image_address(2, pack, pixels, width, height,
126 format, type, 0, 0, 0);
127
128 if (!_mesa_clip_copytexsubimage(ctx,
129 &dst_x, &dst_y,
130 &x, &y,
131 &width, &height)) {
132 return GL_TRUE;
133 }
134
135 intel_prepare_render(intel);
136
137 all = (width * height * src->cpp == dst->Base.Size &&
138 x == 0 && dst_offset == 0);
139
140 dst_x = 0;
141 dst_y = 0;
142
143 dst_buffer = intel_bufferobj_buffer(intel, dst,
144 all ? INTEL_WRITE_FULL :
145 INTEL_WRITE_PART);
146
147 if (ctx->ReadBuffer->Name == 0)
148 y = ctx->ReadBuffer->Height - (y + height);
149
150 if (!intelEmitCopyBlit(intel,
151 src->cpp,
152 src->pitch, src->buffer, 0, src->tiling,
153 rowLength, dst_buffer, dst_offset, GL_FALSE,
154 x, y,
155 dst_x, dst_y,
156 width, height,
157 GL_COPY)) {
158 return GL_FALSE;
159 }
160
161 if (INTEL_DEBUG & DEBUG_PIXEL)
162 printf("%s - DONE\n", __FUNCTION__);
163
164 return GL_TRUE;
165 }
166
167 void
168 intelReadPixels(GLcontext * ctx,
169 GLint x, GLint y, GLsizei width, GLsizei height,
170 GLenum format, GLenum type,
171 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
172 {
173 struct intel_context *intel = intel_context(ctx);
174 GLboolean dirty;
175
176 if (INTEL_DEBUG & DEBUG_PIXEL)
177 fprintf(stderr, "%s\n", __FUNCTION__);
178
179 intel_flush(ctx);
180
181 /* glReadPixels() wont dirty the front buffer, so reset the dirty
182 * flag after calling intel_prepare_render(). */
183 dirty = intel->front_buffer_dirty;
184 intel_prepare_render(intel);
185 intel->front_buffer_dirty = dirty;
186
187 if (do_blit_readpixels
188 (ctx, x, y, width, height, format, type, pack, pixels))
189 return;
190
191 if (INTEL_DEBUG & DEBUG_PIXEL)
192 printf("%s: fallback to swrast\n", __FUNCTION__);
193
194 /* Update Mesa state before calling down into _swrast_ReadPixels, as
195 * the spans code requires the computed buffer states to be up to date,
196 * but _swrast_ReadPixels only updates Mesa state after setting up
197 * the spans code.
198 */
199
200 if (ctx->NewState)
201 _mesa_update_state(ctx);
202
203 _swrast_ReadPixels(ctx, x, y, width, height, format, type, pack, pixels);
204
205 /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
206 intel->front_buffer_dirty = dirty;
207 }