Merge commit 'origin/gallium-winsys-handle-rebased'
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_tex_copy.c
1 /*
2 * Copyright (C) 2009 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 "radeon_common.h"
29 #include "radeon_texture.h"
30
31 #include "main/image.h"
32 #include "main/teximage.h"
33 #include "main/texstate.h"
34 #include "drivers/common/meta.h"
35
36 #include "radeon_mipmap_tree.h"
37
38 static GLboolean
39 do_copy_texsubimage(GLcontext *ctx,
40 GLenum target, GLint level,
41 struct radeon_tex_obj *tobj,
42 radeon_texture_image *timg,
43 GLint dstx, GLint dsty,
44 GLint x, GLint y,
45 GLsizei width, GLsizei height)
46 {
47 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
48 struct radeon_renderbuffer *rrb;
49 unsigned src_bpp;
50 unsigned dst_bpp;
51 gl_format src_mesaformat;
52 gl_format dst_mesaformat;
53 unsigned src_width;
54 unsigned dst_width;
55
56 if (!radeon->vtbl.blit) {
57 return GL_FALSE;
58 }
59
60 if (_mesa_get_format_bits(timg->base.TexFormat, GL_DEPTH_BITS) > 0) {
61 rrb = radeon_get_depthbuffer(radeon);
62 } else {
63 rrb = radeon_get_colorbuffer(radeon);
64 }
65
66 if (!timg->mt) {
67 radeon_validate_texture_miptree(ctx, &tobj->base);
68 }
69
70 assert(rrb && rrb->bo);
71 assert(timg->mt);
72 assert(timg->mt->bo);
73 assert(timg->base.Width >= dstx + width);
74 assert(timg->base.Height >= dsty + height);
75
76 intptr_t src_offset = rrb->draw_offset;
77 intptr_t dst_offset = radeon_miptree_image_offset(timg->mt, _mesa_tex_target_to_face(target), level);
78
79 if (0) {
80 fprintf(stderr, "%s: copying to face %d, level %d\n",
81 __FUNCTION__, _mesa_tex_target_to_face(target), level);
82 fprintf(stderr, "to: x %d, y %d, offset %d\n", dstx, dsty, (uint32_t) dst_offset);
83 fprintf(stderr, "from (%dx%d) width %d, height %d, offset %d, pitch %d\n",
84 x, y, rrb->base.Width, rrb->base.Height, (uint32_t) src_offset, rrb->pitch/rrb->cpp);
85 fprintf(stderr, "src size %d, dst size %d\n", rrb->bo->size, timg->mt->bo->size);
86
87 }
88
89 src_mesaformat = rrb->base.Format;
90 dst_mesaformat = timg->base.TexFormat;
91 src_width = rrb->base.Width;
92 dst_width = timg->base.Width;
93 src_bpp = _mesa_get_format_bytes(src_mesaformat);
94 dst_bpp = _mesa_get_format_bytes(dst_mesaformat);
95 if (!radeon->vtbl.check_blit(dst_mesaformat)) {
96 if (src_bpp != dst_bpp)
97 return GL_FALSE;
98
99 switch (dst_bpp) {
100 case 2:
101 src_mesaformat = MESA_FORMAT_RGB565;
102 dst_mesaformat = MESA_FORMAT_RGB565;
103 break;
104 case 4:
105 src_mesaformat = MESA_FORMAT_ARGB8888;
106 dst_mesaformat = MESA_FORMAT_ARGB8888;
107 break;
108 case 1:
109 src_mesaformat = MESA_FORMAT_A8;
110 dst_mesaformat = MESA_FORMAT_A8;
111 break;
112 default:
113 return GL_FALSE;
114 }
115 }
116
117 /* blit from src buffer to texture */
118 return radeon->vtbl.blit(ctx, rrb->bo, src_offset, src_mesaformat, rrb->pitch/rrb->cpp,
119 src_width, rrb->base.Height, x, y,
120 timg->mt->bo, dst_offset, dst_mesaformat,
121 timg->mt->levels[level].rowstride / dst_bpp,
122 dst_width, timg->base.Height,
123 dstx, dsty, width, height, 1);
124 }
125
126 void
127 radeonCopyTexImage2D(GLcontext *ctx, GLenum target, GLint level,
128 GLenum internalFormat,
129 GLint x, GLint y, GLsizei width, GLsizei height,
130 GLint border)
131 {
132 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
133 struct gl_texture_object *texObj =
134 _mesa_select_tex_object(ctx, texUnit, target);
135 struct gl_texture_image *texImage =
136 _mesa_select_tex_image(ctx, texObj, target, level);
137 int srcx, srcy, dstx, dsty;
138
139 if (border)
140 goto fail;
141
142 /* Setup or redefine the texture object, mipmap tree and texture
143 * image. Don't populate yet.
144 */
145 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
146 width, height, border,
147 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
148 &ctx->DefaultPacking, texObj, texImage);
149
150 srcx = x;
151 srcy = y;
152 dstx = 0;
153 dsty = 0;
154 if (!_mesa_clip_copytexsubimage(ctx,
155 &dstx, &dsty,
156 &srcx, &srcy,
157 &width, &height)) {
158 return;
159 }
160
161 if (!do_copy_texsubimage(ctx, target, level,
162 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
163 0, 0, x, y, width, height)) {
164 goto fail;
165 }
166
167 return;
168
169 fail:
170 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
171 width, height, border);
172 }
173
174 void
175 radeonCopyTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
176 GLint xoffset, GLint yoffset,
177 GLint x, GLint y,
178 GLsizei width, GLsizei height)
179 {
180 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
181 struct gl_texture_object *texObj = _mesa_select_tex_object(ctx, texUnit, target);
182 struct gl_texture_image *texImage = _mesa_select_tex_image(ctx, texObj, target, level);
183
184 if (!do_copy_texsubimage(ctx, target, level,
185 radeon_tex_obj(texObj), (radeon_texture_image *)texImage,
186 xoffset, yoffset, x, y, width, height)) {
187
188 //DEBUG_FALLBACKS
189
190 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
191 xoffset, yoffset, x, y, width, height);
192 }
193 }