Merge remote branch 'origin/7.8'
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_pixel_read.c
1 /*
2 * Copyright (C) 2010 Maciej Cencora <m.cencora@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "stdint.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/state.h"
33 #include "swrast/swrast.h"
34
35 #include "radeon_common_context.h"
36 #include "radeon_debug.h"
37 #include "radeon_mipmap_tree.h"
38
39 static gl_format gl_format_and_type_to_mesa_format(GLenum format, GLenum type)
40 {
41 switch (format)
42 {
43 case GL_RGB:
44 switch (type) {
45 case GL_UNSIGNED_SHORT_5_6_5:
46 return MESA_FORMAT_RGB565;
47 case GL_UNSIGNED_SHORT_5_6_5_REV:
48 return MESA_FORMAT_RGB565_REV;
49 }
50 break;
51 case GL_RGBA:
52 switch (type) {
53 case GL_FLOAT:
54 return MESA_FORMAT_RGBA_FLOAT32;
55 case GL_UNSIGNED_SHORT_5_5_5_1:
56 return MESA_FORMAT_RGBA5551;
57 case GL_UNSIGNED_INT_8_8_8_8:
58 return MESA_FORMAT_RGBA8888;
59 case GL_UNSIGNED_BYTE:
60 case GL_UNSIGNED_INT_8_8_8_8_REV:
61 return MESA_FORMAT_RGBA8888_REV;
62 }
63 break;
64 case GL_BGRA:
65 switch (type) {
66 case GL_UNSIGNED_SHORT_4_4_4_4:
67 return MESA_FORMAT_ARGB4444_REV;
68 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
69 return MESA_FORMAT_ARGB4444;
70 case GL_UNSIGNED_SHORT_5_5_5_1:
71 return MESA_FORMAT_ARGB1555_REV;
72 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
73 return MESA_FORMAT_ARGB1555;
74 case GL_UNSIGNED_INT_8_8_8_8:
75 return MESA_FORMAT_ARGB8888_REV;
76 case GL_UNSIGNED_BYTE:
77 case GL_UNSIGNED_INT_8_8_8_8_REV:
78 return MESA_FORMAT_ARGB8888;
79
80 }
81 break;
82 }
83
84 return MESA_FORMAT_NONE;
85 }
86
87 static GLboolean
88 do_blit_readpixels(GLcontext * ctx,
89 GLint x, GLint y, GLsizei width, GLsizei height,
90 GLenum format, GLenum type,
91 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
92 {
93 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
94 const struct radeon_renderbuffer *rrb = radeon_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
95 const gl_format dst_format = gl_format_and_type_to_mesa_format(format, type);
96 unsigned dst_rowstride, dst_imagesize, aligned_rowstride, flip_y;
97 struct radeon_bo *dst_buffer;
98 GLint dst_x = 0, dst_y = 0;
99
100 /* It's not worth if number of pixels to copy is really small */
101 if (width * height < 100) {
102 return GL_FALSE;
103 }
104
105 if (dst_format == MESA_FORMAT_NONE ||
106 !radeon->vtbl.check_blit(dst_format) || !radeon->vtbl.blit) {
107 return GL_FALSE;
108 }
109
110 if (ctx->_ImageTransferState || ctx->Color._LogicOpEnabled) {
111 return GL_FALSE;
112 }
113
114 if (pack->SwapBytes || pack->LsbFirst) {
115 return GL_FALSE;
116 }
117
118 if (pack->RowLength > 0) {
119 dst_rowstride = pack->RowLength;
120 } else {
121 dst_rowstride = width;
122 }
123
124 if (!_mesa_clip_copytexsubimage(ctx, &dst_x, &dst_y, &x, &y, &width, &height)) {
125 return GL_TRUE;
126 }
127 assert(x >= 0 && y >= 0);
128
129 aligned_rowstride = get_texture_image_row_stride(radeon, dst_format, dst_rowstride, 0);
130 dst_imagesize = get_texture_image_size(dst_format,
131 aligned_rowstride,
132 height, 1, 0);
133 dst_buffer = radeon_bo_open(radeon->radeonScreen->bom, 0, dst_imagesize, 1024, RADEON_GEM_DOMAIN_GTT, 0);
134
135 /* Disable source Y flipping for FBOs */
136 flip_y = (ctx->ReadBuffer->Name == 0);
137 if (pack->Invert) {
138 y = rrb->base.Height - height - y;
139 flip_y = !flip_y;
140 }
141
142 if (radeon->vtbl.blit(ctx,
143 rrb->bo,
144 rrb->draw_offset,
145 rrb->base.Format,
146 rrb->pitch / rrb->cpp,
147 rrb->base.Width,
148 rrb->base.Height,
149 x,
150 y,
151 dst_buffer,
152 0, /* dst_offset */
153 dst_format,
154 aligned_rowstride / _mesa_get_format_bytes(dst_format),
155 width,
156 height,
157 0, /* dst_x */
158 0, /* dst_y */
159 width,
160 height,
161 flip_y))
162 {
163 radeon_bo_map(dst_buffer, 0);
164 dst_rowstride *= _mesa_get_format_bytes(dst_format);
165 copy_rows(pixels, dst_rowstride, dst_buffer->ptr,
166 aligned_rowstride, height, dst_rowstride);
167 radeon_bo_unmap(dst_buffer);
168 radeon_bo_unref(dst_buffer);
169 return GL_TRUE;
170 } else {
171 radeon_bo_unref(dst_buffer);
172 return GL_FALSE;
173 }
174 }
175
176 void
177 radeonReadPixels(GLcontext * ctx,
178 GLint x, GLint y, GLsizei width, GLsizei height,
179 GLenum format, GLenum type,
180 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
181 {
182 if (do_blit_readpixels(ctx, x, y, width, height, format, type, pack, pixels))
183 return;
184
185 /* Update Mesa state before calling down into _swrast_ReadPixels, as
186 * the spans code requires the computed buffer states to be up to date,
187 * but _swrast_ReadPixels only updates Mesa state after setting up
188 * the spans code.
189 */
190
191 radeon_print(RADEON_FALLBACKS, RADEON_NORMAL,
192 "Falling back to sw for ReadPixels (format %s, type %s)\n",
193 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type));
194
195 if (ctx->NewState)
196 _mesa_update_state(ctx);
197
198 _swrast_ReadPixels(ctx, x, y, width, height, format, type, pack, pixels);
199 }