Merge branch '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_UNSIGNED_BYTE:
54 return MESA_FORMAT_RGBA8888_REV;
55 case GL_FLOAT:
56 return MESA_FORMAT_RGBA_FLOAT32;
57 case GL_UNSIGNED_SHORT_4_4_4_4:
58 return MESA_FORMAT_ARGB4444;
59 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
60 return MESA_FORMAT_ARGB4444;
61 case GL_UNSIGNED_SHORT_5_5_5_1:
62 return MESA_FORMAT_RGBA5551;
63 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
64 return MESA_FORMAT_ARGB1555_REV;
65 case GL_UNSIGNED_INT_8_8_8_8:
66 return MESA_FORMAT_ARGB8888;
67 case GL_UNSIGNED_INT_8_8_8_8_REV:
68 return MESA_FORMAT_ARGB8888_REV;
69 }
70 break;
71 }
72
73 return MESA_FORMAT_NONE;
74 }
75
76 static GLboolean
77 do_blit_readpixels(GLcontext * ctx,
78 GLint x, GLint y, GLsizei width, GLsizei height,
79 GLenum format, GLenum type,
80 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
81 {
82 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
83 const struct radeon_renderbuffer *rrb = radeon_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
84 const gl_format dst_format = gl_format_and_type_to_mesa_format(format, type);
85 unsigned dst_rowstride, dst_imagesize, aligned_rowstride, flip_y;
86 struct radeon_bo *dst_buffer;
87 GLint dst_x = 0, dst_y = 0;
88
89 /* It's not worth if number of pixels to copy is really small */
90 if (width * height < 100) {
91 return GL_FALSE;
92 }
93
94 if (dst_format == MESA_FORMAT_NONE ||
95 !radeon->vtbl.check_blit(dst_format) || !radeon->vtbl.blit) {
96 return GL_FALSE;
97 }
98
99 if (ctx->_ImageTransferState) {
100 return GL_FALSE;
101 }
102
103 if (pack->SwapBytes || pack->LsbFirst) {
104 return GL_FALSE;
105 }
106
107 if (pack->RowLength > 0) {
108 dst_rowstride = pack->RowLength;
109 } else {
110 dst_rowstride = width;
111 }
112
113 if (!_mesa_clip_copytexsubimage(ctx, &dst_x, &dst_y, &x, &y, &width, &height)) {
114 return GL_TRUE;
115 }
116 assert(x >= 0 && y >= 0);
117
118 aligned_rowstride = get_texture_image_row_stride(radeon, dst_format, dst_rowstride, 0);
119 dst_imagesize = get_texture_image_size(dst_format,
120 aligned_rowstride,
121 height, 1, 0);
122 dst_buffer = radeon_bo_open(radeon->radeonScreen->bom, 0, dst_imagesize, 1024, RADEON_GEM_DOMAIN_GTT, 0);
123
124 /* Disable source Y flipping for FBOs */
125 flip_y = (ctx->ReadBuffer->Name == 0);
126 if (pack->Invert) {
127 y = rrb->base.Height - height - y;
128 flip_y = !flip_y;
129 }
130
131 if (radeon->vtbl.blit(ctx,
132 rrb->bo,
133 rrb->draw_offset,
134 rrb->base.Format,
135 rrb->pitch / rrb->cpp,
136 rrb->base.Width,
137 rrb->base.Height,
138 x,
139 y,
140 dst_buffer,
141 0, /* dst_offset */
142 dst_format,
143 aligned_rowstride / _mesa_get_format_bytes(dst_format),
144 width,
145 height,
146 0, /* dst_x */
147 0, /* dst_y */
148 width,
149 height,
150 flip_y))
151 {
152 radeon_bo_map(dst_buffer, 0);
153 dst_rowstride *= _mesa_get_format_bytes(dst_format);
154 copy_rows(pixels, dst_rowstride, dst_buffer->ptr,
155 aligned_rowstride, height, dst_rowstride);
156 radeon_bo_unmap(dst_buffer);
157 radeon_bo_unref(dst_buffer);
158 return GL_TRUE;
159 } else {
160 radeon_bo_unref(dst_buffer);
161 return GL_FALSE;
162 }
163 }
164
165 void
166 radeonReadPixels(GLcontext * ctx,
167 GLint x, GLint y, GLsizei width, GLsizei height,
168 GLenum format, GLenum type,
169 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
170 {
171 if (do_blit_readpixels(ctx, x, y, width, height, format, type, pack, pixels))
172 return;
173
174 /* Update Mesa state before calling down into _swrast_ReadPixels, as
175 * the spans code requires the computed buffer states to be up to date,
176 * but _swrast_ReadPixels only updates Mesa state after setting up
177 * the spans code.
178 */
179
180 radeon_print(RADEON_FALLBACKS, RADEON_NORMAL,
181 "Falling back to sw for ReadPixels (format %s, type %s)\n",
182 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type));
183
184 if (ctx->NewState)
185 _mesa_update_state(ctx);
186
187 _swrast_ReadPixels(ctx, x, y, width, height, format, type, pack, pixels);
188 }