vk: Add four unit tests for our lock-free data-structures
[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/glformats.h"
32 #include "main/mtypes.h"
33 #include "main/condrender.h"
34 #include "main/fbobject.h"
35 #include "main/teximage.h"
36 #include "main/texobj.h"
37 #include "main/texstate.h"
38 #include "main/bufferobj.h"
39 #include "swrast/swrast.h"
40 #include "drivers/common/meta.h"
41
42 #include "brw_context.h"
43 #include "intel_screen.h"
44 #include "intel_blit.h"
45 #include "intel_buffers.h"
46 #include "intel_fbo.h"
47 #include "intel_mipmap_tree.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", __func__);
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", __func__);
72 return false;
73 }
74
75 intel_prepare_render(brw);
76
77 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
78 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
79
80 mesa_format src_format = _mesa_format_from_format_and_type(format, type);
81 if (_mesa_format_is_mesa_array_format(src_format))
82 src_format = _mesa_format_from_array_format(src_format);
83 mesa_format dst_format = irb->mt->format;
84
85 /* We can safely discard sRGB encode/decode for the DrawPixels interface */
86 src_format = _mesa_get_srgb_format_linear(src_format);
87 dst_format = _mesa_get_srgb_format_linear(dst_format);
88
89 if (!intel_miptree_blit_compatible_formats(src_format, dst_format)) {
90 DBG("%s: bad format for blit\n", __func__);
91 return false;
92 }
93
94 if (unpack->SwapBytes || unpack->LsbFirst ||
95 unpack->SkipPixels || unpack->SkipRows) {
96 DBG("%s: bad packing params\n", __func__);
97 return false;
98 }
99
100 int src_stride = _mesa_image_row_stride(unpack, width, format, type);
101 bool src_flip = false;
102 /* Mesa flips the src_stride for unpack->Invert, but we want our mt to have
103 * a normal src_stride.
104 */
105 if (unpack->Invert) {
106 src_stride = -src_stride;
107 src_flip = true;
108 }
109
110 src_offset = (GLintptr)pixels;
111 src_offset += _mesa_image_offset(2, unpack, width, height,
112 format, type, 0, 0, 0);
113
114 src_buffer = intel_bufferobj_buffer(brw, src, src_offset,
115 height * src_stride);
116
117 struct intel_mipmap_tree *pbo_mt =
118 intel_miptree_create_for_bo(brw,
119 src_buffer,
120 irb->mt->format,
121 src_offset,
122 width, height, 1,
123 src_stride,
124 0);
125 if (!pbo_mt)
126 return false;
127
128 if (!intel_miptree_blit(brw,
129 pbo_mt, 0, 0,
130 0, 0, src_flip,
131 irb->mt, irb->mt_level, irb->mt_layer,
132 x, y, _mesa_is_winsys_fbo(ctx->DrawBuffer),
133 width, height, GL_COPY)) {
134 DBG("%s: blit failed\n", __func__);
135 intel_miptree_release(&pbo_mt);
136 return false;
137 }
138
139 intel_miptree_release(&pbo_mt);
140
141 if (ctx->Query.CurrentOcclusionObject)
142 ctx->Query.CurrentOcclusionObject->Result += width * height;
143
144 DBG("%s: success\n", __func__);
145 return true;
146 }
147
148 void
149 intelDrawPixels(struct gl_context * ctx,
150 GLint x, GLint y,
151 GLsizei width, GLsizei height,
152 GLenum format,
153 GLenum type,
154 const struct gl_pixelstore_attrib *unpack,
155 const GLvoid * pixels)
156 {
157 struct brw_context *brw = brw_context(ctx);
158
159 if (!_mesa_check_conditional_render(ctx))
160 return;
161
162 if (format == GL_STENCIL_INDEX) {
163 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
164 unpack, pixels);
165 return;
166 }
167
168 if (_mesa_is_bufferobj(unpack->BufferObj)) {
169 if (do_blit_drawpixels(ctx, x, y, width, height, format, type, unpack,
170 pixels)) {
171 return;
172 }
173
174 perf_debug("%s: fallback to generic code in PBO case\n", __func__);
175 }
176
177 _mesa_meta_DrawPixels(ctx, x, y, width, height, format, type,
178 unpack, pixels);
179 }