1/**************************************************************************
2
3Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
4Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
5                     VA Linux Systems Inc., Fremont, California.
6
7The Weather Channel (TM) funded Tungsten Graphics to develop the
8initial release of the Radeon 8500 driver under the XFree86 license.
9This notice must be preserved.
10
11All Rights Reserved.
12
13Permission is hereby granted, free of charge, to any person obtaining
14a copy of this software and associated documentation files (the
15"Software"), to deal in the Software without restriction, including
16without limitation the rights to use, copy, modify, merge, publish,
17distribute, sublicense, and/or sell copies of the Software, and to
18permit persons to whom the Software is furnished to do so, subject to
19the following conditions:
20
21The above copyright notice and this permission notice (including the
22next paragraph) shall be included in all copies or substantial
23portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
29LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
33**************************************************************************/
34
35/*
36 * Authors:
37 *   Kevin E. Martin <martin@valinux.com>
38 *   Gareth Hughes <gareth@valinux.com>
39 *   Keith Whitwell <keithw@vmware.com>
40 *
41 */
42
43#include "main/glheader.h"
44#include "main/texformat.h"
45#include "main/renderbuffer.h"
46#include "main/samplerobj.h"
47#include "main/framebuffer.h"
48#include "swrast/swrast.h"
49#include "swrast/s_renderbuffer.h"
50
51#include "radeon_common.h"
52#include "radeon_span.h"
53
54
55static void
56radeon_renderbuffer_map(struct gl_context *ctx,
57			struct gl_renderbuffer *rb,
58			bool flip_y)
59{
60	struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
61	GLubyte *map;
62	int stride;
63
64	if (!rb || !rrb)
65		return;
66
67	ctx->Driver.MapRenderbuffer(ctx, rb, 0, 0, rb->Width, rb->Height,
68				    GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
69				    &map, &stride, flip_y);
70
71	rrb->base.Map = map;
72	rrb->base.RowStride = stride;
73	/* No floating point color buffers, use GLubytes */
74	rrb->base.ColorType = GL_UNSIGNED_BYTE;
75}
76
77static void
78radeon_renderbuffer_unmap(struct gl_context *ctx, struct gl_renderbuffer *rb)
79{
80	struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
81	if (!rb || !rrb)
82		return;
83
84	ctx->Driver.UnmapRenderbuffer(ctx, rb);
85
86	rrb->base.Map = NULL;
87	rrb->base.RowStride = 0;
88}
89
90static void
91radeon_map_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
92{
93	GLuint i;
94
95	radeon_print(RADEON_MEMORY, RADEON_TRACE,
96		"%s( %p , fb %p )\n",
97		     __func__, ctx, fb);
98
99	/* check for render to textures */
100	for (i = 0; i < BUFFER_COUNT; i++)
101		radeon_renderbuffer_map(ctx, fb->Attachment[i].Renderbuffer,
102			fb->FlipY);
103
104        if (_mesa_is_front_buffer_drawing(fb))
105		RADEON_CONTEXT(ctx)->front_buffer_dirty = true;
106}
107
108static void
109radeon_unmap_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
110{
111	GLuint i;
112
113	radeon_print(RADEON_MEMORY, RADEON_TRACE,
114		"%s( %p , fb %p)\n",
115		     __func__, ctx, fb);
116
117	/* check for render to textures */
118	for (i = 0; i < BUFFER_COUNT; i++)
119		radeon_renderbuffer_unmap(ctx, fb->Attachment[i].Renderbuffer);
120
121        if (_mesa_is_front_buffer_drawing(fb))
122		RADEON_CONTEXT(ctx)->front_buffer_dirty = true;
123}
124
125static void radeonSpanRenderStart(struct gl_context * ctx)
126{
127	radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
128
129	radeon_firevertices(rmesa);
130
131	_swrast_map_textures(ctx);
132
133	radeon_map_framebuffer(ctx, ctx->DrawBuffer);
134	if (ctx->ReadBuffer != ctx->DrawBuffer)
135		radeon_map_framebuffer(ctx, ctx->ReadBuffer);
136}
137
138static void radeonSpanRenderFinish(struct gl_context * ctx)
139{
140	_swrast_flush(ctx);
141	_swrast_unmap_textures(ctx);
142
143	radeon_unmap_framebuffer(ctx, ctx->DrawBuffer);
144	if (ctx->ReadBuffer != ctx->DrawBuffer)
145		radeon_unmap_framebuffer(ctx, ctx->ReadBuffer);
146}
147
148void radeonInitSpanFuncs(struct gl_context * ctx)
149{
150	struct swrast_device_driver *swdd =
151	    _swrast_GetDeviceDriverReference(ctx);
152	swdd->SpanRenderStart = radeonSpanRenderStart;
153	swdd->SpanRenderFinish = radeonSpanRenderFinish;
154}
155
156