s/Tungsten Graphics/VMware/
[mesa.git] / src / mesa / drivers / dri / i965 / intel_pixel_draw.c
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/mtypes.h"
32 #include "main/condrender.h"
33 #include "main/fbobject.h"
34 #include "main/teximage.h"
35 #include "main/texobj.h"
36 #include "main/texstate.h"
37 #include "main/bufferobj.h"
38 #include "swrast/swrast.h"
39 #include "drivers/common/meta.h"
40
41 #include "brw_context.h"
42 #include "intel_screen.h"
43 #include "intel_blit.h"
44 #include "intel_buffers.h"
45 #include "intel_fbo.h"
46 #include "intel_mipmap_tree.h"
47 #include "intel_regions.h"
48 #include "intel_pixel.h"
49 #include "intel_buffer_objects.h"
50
51 #define FILE_DEBUG_FLAG DEBUG_PIXEL
52
53 static bool
54 do_blit_drawpixels(struct gl_context * ctx,
55 GLint x, GLint y, GLsizei width, GLsizei height,
56 GLenum format, GLenum type,
57 const struct gl_pixelstore_attrib *unpack,
58 const GLvoid * pixels)
59 {
60 struct brw_context *brw = brw_context(ctx);
61 struct intel_buffer_object *src = intel_buffer_object(unpack->BufferObj);
62 GLuint src_offset;
63 drm_intel_bo *src_buffer;
64
65 DBG("%s\n", __FUNCTION__);
66
67 if (!intel_check_blit_fragment_ops(ctx, false))
68 return false;
69
70 if (ctx->DrawBuffer->_NumColorDrawBuffers != 1) {
71 DBG("%s: fallback due to MRT\n", __FUNCTION__);
72 return false;
73 }
74
75 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
76 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
77
78 if (!_mesa_format_matches_format_and_type(irb->mt->format, format, type,
79 false)) {
80 DBG("%s: bad format for blit\n", __FUNCTION__);
81 return false;
82 }
83
84 if (unpack->SwapBytes || unpack->LsbFirst ||
85 unpack->SkipPixels || unpack->SkipRows) {
86 DBG("%s: bad packing params\n", __FUNCTION__);
87 return false;
88 }
89
90 int src_stride = _mesa_image_row_stride(unpack, width, format, type);
91 bool src_flip = false;
92 /* Mesa flips the src_stride for unpack->Invert, but we want our mt to have
93 * a normal src_stride.
94 */
95 if (unpack->Invert) {
96 src_stride = -src_stride;
97 src_flip = true;
98 }
99
100 src_offset = (GLintptr)pixels;
101 src_offset += _mesa_image_offset(2, unpack, width, height,
102 format, type, 0, 0, 0);
103
104 intel_prepare_render(brw);
105
106 src_buffer = intel_bufferobj_buffer(brw, src,
107 src_offset, width * height *
108 irb->mt->cpp);
109
110 struct intel_mipmap_tree *pbo_mt =
111 intel_miptree_create_for_bo(brw,
112 src_buffer,
113 irb->mt->format,
114 src_offset,
115 width, height,
116 src_stride, I915_TILING_NONE);
117 if (!pbo_mt)
118 return false;
119
120 if (!intel_miptree_blit(brw,
121 pbo_mt, 0, 0,
122 0, 0, src_flip,
123 irb->mt, irb->mt_level, irb->mt_layer,
124 x, y, _mesa_is_winsys_fbo(ctx->DrawBuffer),
125 width, height, GL_COPY)) {
126 DBG("%s: blit failed\n", __FUNCTION__);
127 intel_miptree_release(&pbo_mt);
128 return false;
129 }
130
131 intel_miptree_release(&pbo_mt);
132
133 if (ctx->Query.CurrentOcclusionObject)
134 ctx->Query.CurrentOcclusionObject->Result += width * height;
135
136 intel_check_front_buffer_rendering(brw);
137
138 DBG("%s: success\n", __FUNCTION__);
139 return true;
140 }
141
142 void
143 intelDrawPixels(struct gl_context * ctx,
144 GLint x, GLint y,
145 GLsizei width, GLsizei height,
146 GLenum format,
147 GLenum type,
148 const struct gl_pixelstore_attrib *unpack,
149 const GLvoid * pixels)
150 {
151 struct brw_context *brw = brw_context(ctx);
152
153 if (!_mesa_check_conditional_render(ctx))
154 return;
155
156 if (format == GL_STENCIL_INDEX) {
157 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
158 unpack, pixels);
159 return;
160 }
161
162 if (_mesa_is_bufferobj(unpack->BufferObj)) {
163 if (do_blit_drawpixels(ctx, x, y, width, height, format, type, unpack,
164 pixels)) {
165 return;
166 }
167
168 perf_debug("%s: fallback to generic code in PBO case\n", __FUNCTION__);
169 }
170
171 _mesa_meta_DrawPixels(ctx, x, y, width, height, format, type,
172 unpack, pixels);
173 }