Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / nouveau / nouveau_span.c
1 /*
2  * Copyright (C) 2009 Francisco Jerez.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_fbo.h"
29 #include "nouveau_context.h"
30 #include "nouveau_bo.h"
31
32 #include "swrast/swrast.h"
33
34 #define LOCAL_VARS                                                      \
35         struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface; \
36         GLuint p;                                                       \
37         (void)p;
38
39 #define LOCAL_DEPTH_VARS LOCAL_VARS
40
41 #define HW_LOCK()
42 #define HW_UNLOCK()
43
44 #define HW_CLIPLOOP() {                                                 \
45         int minx = 0;                                                   \
46         int miny = 0;                                                   \
47         int maxx = rb->Width;                                           \
48         int maxy = rb->Height;
49
50 #define HW_ENDCLIPLOOP() }
51
52 #define Y_FLIP(y) (rb->Name ? (y) : rb->Height - 1 - (y))
53
54 /* RGB565 span functions */
55 #define SPANTMP_PIXEL_FMT GL_RGB
56 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_SHORT_5_6_5
57 #define TAG(x) nouveau_##x##_rgb565
58 #define TAG2(x, y) nouveau_##x##_rgb565##y
59 #define GET_PTR(x, y) (s->bo->map + (y)*s->pitch + (x)*s->cpp)
60
61 #include "spantmp2.h"
62
63 /* RGB888 span functions */
64 #define SPANTMP_PIXEL_FMT GL_BGR
65 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
66 #define TAG(x) nouveau_##x##_rgb888
67 #define TAG2(x, y) nouveau_##x##_rgb888##y
68 #define GET_PTR(x, y) (s->bo->map + (y)*s->pitch + (x)*s->cpp)
69
70 #include "spantmp2.h"
71
72 /* ARGB8888 span functions */
73 #define SPANTMP_PIXEL_FMT GL_BGRA
74 #define SPANTMP_PIXEL_TYPE GL_UNSIGNED_INT_8_8_8_8_REV
75 #define TAG(x) nouveau_##x##_argb8888
76 #define TAG2(x, y) nouveau_##x##_argb8888##y
77 #define GET_PTR(x, y) (s->bo->map + (y)*s->pitch + (x)*s->cpp)
78
79 #include "spantmp2.h"
80
81 /* Z16 span functions */
82 #define VALUE_TYPE uint16_t
83 #define READ_DEPTH(v, x, y)                                             \
84         v = *(uint16_t *)(s->bo->map + (y)*s->pitch + (x)*s->cpp);
85 #define WRITE_DEPTH(x, y, v)                                            \
86         *(uint16_t *)(s->bo->map + (y)*s->pitch + (x)*s->cpp) = v
87 #define TAG(x) nouveau_##x##_z16
88
89 #include "depthtmp.h"
90
91 /* Z24S8 span functions */
92 #define VALUE_TYPE uint32_t
93 #define READ_DEPTH(v, x, y)                                             \
94         v = *(uint32_t *)(s->bo->map + (y)*s->pitch + (x)*s->cpp);
95 #define WRITE_DEPTH(x, y, v)                                            \
96         *(uint32_t *)(s->bo->map + (y)*s->pitch + (x)*s->cpp) = v
97 #define TAG(x) nouveau_##x##_z24s8
98
99 #include "depthtmp.h"
100
101 static void
102 renderbuffer_map_unmap(struct gl_renderbuffer *rb, GLboolean map)
103 {
104         struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
105
106         if (map) {
107                 switch (rb->Format) {
108                 case MESA_FORMAT_RGB565:
109                         nouveau_InitPointers_rgb565(rb);
110                         break;
111                 case MESA_FORMAT_XRGB8888:
112                         nouveau_InitPointers_rgb888(rb);
113                         break;
114                 case MESA_FORMAT_ARGB8888:
115                         nouveau_InitPointers_argb8888(rb);
116                         break;
117                 case MESA_FORMAT_Z16:
118                         nouveau_InitDepthPointers_z16(rb);
119                         break;
120                 case MESA_FORMAT_Z24_S8:
121                         nouveau_InitDepthPointers_z24s8(rb);
122                         break;
123                 default:
124                         assert(0);
125                 }
126
127                 nouveau_bo_map(s->bo, NOUVEAU_BO_RDWR);
128         } else {
129                 nouveau_bo_unmap(s->bo);
130         }
131 }
132
133 static void
134 texture_unit_map_unmap(struct gl_context *ctx, struct gl_texture_unit *u, GLboolean map)
135 {
136         if (!u->_ReallyEnabled)
137                 return;
138
139         if (map)
140                 ctx->Driver.MapTexture(ctx, u->_Current);
141         else
142                 ctx->Driver.UnmapTexture(ctx, u->_Current);
143 }
144
145 static void
146 framebuffer_map_unmap(struct gl_framebuffer *fb, GLboolean map)
147 {
148         int i;
149
150         for (i = 0; i < fb->_NumColorDrawBuffers; i++)
151                 renderbuffer_map_unmap(fb->_ColorDrawBuffers[i], map);
152
153         renderbuffer_map_unmap(fb->_ColorReadBuffer, map);
154
155         if (fb->_DepthBuffer)
156                 renderbuffer_map_unmap(fb->_DepthBuffer->Wrapped, map);
157 }
158
159 static void
160 span_map_unmap(struct gl_context *ctx, GLboolean map)
161 {
162         int i;
163
164         framebuffer_map_unmap(ctx->DrawBuffer, map);
165
166         if (ctx->ReadBuffer != ctx->DrawBuffer)
167                 framebuffer_map_unmap(ctx->ReadBuffer, map);
168
169         for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
170                 texture_unit_map_unmap(ctx, &ctx->Texture.Unit[i], map);
171 }
172
173 static void
174 nouveau_span_start(struct gl_context *ctx)
175 {
176         nouveau_fallback(ctx, SWRAST);
177         span_map_unmap(ctx, GL_TRUE);
178 }
179
180 static void
181 nouveau_span_finish(struct gl_context *ctx)
182 {
183         span_map_unmap(ctx, GL_FALSE);
184         nouveau_fallback(ctx, HWTNL);
185 }
186
187 void
188 nouveau_span_functions_init(struct gl_context *ctx)
189 {
190         struct swrast_device_driver *swdd =
191                 _swrast_GetDeviceDriverReference(ctx);
192
193         swdd->SpanRenderStart = nouveau_span_start;
194         swdd->SpanRenderFinish = nouveau_span_finish;
195 }