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 <stdio.h>
28 #include "main/mtypes.h"
29 #include "main/fbobject.h"
30 
31 #include "nouveau_driver.h"
32 #include "nouveau_context.h"
33 #include "nouveau_fbo.h"
34 #include "nouveau_util.h"
35 
36 #include "drivers/common/meta.h"
37 
38 const char * const nouveau_vendor_string = "Nouveau";
39 
40 const char *
nouveau_get_renderer_string(unsigned chipset)41 nouveau_get_renderer_string(unsigned chipset)
42 {
43 	char hardware_name[32];
44 	static char buffer[128];
45 
46 	snprintf(hardware_name, sizeof(hardware_name), "nv%02X", chipset);
47 	driGetRendererString(buffer, hardware_name, 0);
48 
49 	return buffer;
50 }
51 
52 static const GLubyte *
nouveau_get_string(struct gl_context * ctx,GLenum name)53 nouveau_get_string(struct gl_context *ctx, GLenum name)
54 {
55 	switch (name) {
56 		case GL_VENDOR:
57 			return (GLubyte *)nouveau_vendor_string;
58 
59 		case GL_RENDERER:
60 			return (GLubyte *)nouveau_get_renderer_string(context_chipset(ctx));
61 		default:
62 			return NULL;
63 	}
64 }
65 
66 static void
nouveau_flush(struct gl_context * ctx,unsigned gallium_flush_flags)67 nouveau_flush(struct gl_context *ctx, unsigned gallium_flush_flags)
68 {
69 	struct nouveau_context *nctx = to_nouveau_context(ctx);
70 	struct nouveau_pushbuf *push = context_push(ctx);
71 
72 	PUSH_KICK(push);
73 
74 	if (_mesa_is_winsys_fbo(ctx->DrawBuffer) &&
75 	    ctx->DrawBuffer->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
76 		__DRIscreen *screen = nctx->screen->dri_screen;
77 		const __DRIdri2LoaderExtension *dri2 = screen->dri2.loader;
78 		__DRIdrawable *drawable = nctx->dri_context->driDrawablePriv;
79 
80 		if (drawable && drawable->loaderPrivate)
81 			dri2->flushFrontBuffer(drawable, drawable->loaderPrivate);
82 	}
83 }
84 
85 static void
nouveau_finish(struct gl_context * ctx)86 nouveau_finish(struct gl_context *ctx)
87 {
88 	struct nouveau_context *nctx = to_nouveau_context(ctx);
89 	struct nouveau_pushbuf *push = context_push(ctx);
90 	struct nouveau_pushbuf_refn refn =
91 		{ nctx->fence, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR };
92 
93 	nouveau_flush(ctx, 0);
94 
95 	if (!nouveau_pushbuf_space(push, 16, 0, 0) &&
96 	    !nouveau_pushbuf_refn(push, &refn, 1)) {
97 		PUSH_DATA(push, 0);
98 		PUSH_KICK(push);
99 	}
100 
101 	nouveau_bo_wait(nctx->fence, NOUVEAU_BO_RDWR, context_client(ctx));
102 }
103 
104 void
nouveau_clear(struct gl_context * ctx,GLbitfield buffers)105 nouveau_clear(struct gl_context *ctx, GLbitfield buffers)
106 {
107 	struct gl_framebuffer *fb = ctx->DrawBuffer;
108 	int x, y, w, h;
109 	int i, buf;
110 
111 	nouveau_validate_framebuffer(ctx);
112 	get_scissors(fb, &x, &y, &w, &h);
113 
114 	for (i = 0; i < BUFFER_COUNT; i++) {
115 		struct nouveau_surface *s;
116 		unsigned mask, value;
117 
118 		buf = buffers & (1 << i);
119 		if (!buf)
120 			continue;
121 
122 		s = &to_nouveau_renderbuffer(
123 			fb->Attachment[i].Renderbuffer)->surface;
124 
125 		if (buf & BUFFER_BITS_COLOR) {
126 			const float *color = ctx->Color.ClearColor.f;
127 
128 			if (fb->Attachment[i].Renderbuffer->_BaseFormat ==
129 			    GL_LUMINANCE_ALPHA)
130 				value = pack_la_clamp_f(
131 						s->format, color[0], color[3]);
132 			else
133 				value = pack_rgba_clamp_f(s->format, color);
134 
135 			const uint8_t colormask[4] = {
136 				GET_COLORMASK_BIT(ctx->Color.ColorMask, 0, 0) ? 0xff : 0,
137 				GET_COLORMASK_BIT(ctx->Color.ColorMask, 0, 1) ? 0xff : 0,
138 				GET_COLORMASK_BIT(ctx->Color.ColorMask, 0, 2) ? 0xff : 0,
139 				GET_COLORMASK_BIT(ctx->Color.ColorMask, 0, 3) ? 0xff : 0,
140 			};
141 			mask = pack_rgba_i(s->format, colormask);
142 
143 			if (mask)
144 				context_drv(ctx)->surface_fill(
145 					ctx, s, mask, value, x, y, w, h);
146 
147 			buffers &= ~buf;
148 
149 		} else if (buf & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL)) {
150 			mask = pack_zs_i(s->format,
151 					 (buffers & BUFFER_BIT_DEPTH &&
152 					  ctx->Depth.Mask) ? ~0 : 0,
153 					 (buffers & BUFFER_BIT_STENCIL ?
154 					  ctx->Stencil.WriteMask[0] : 0));
155 			value = pack_zs_f(s->format,
156 					  ctx->Depth.Clear,
157 					  ctx->Stencil.Clear);
158 
159 			if (mask)
160 				context_drv(ctx)->surface_fill(
161 					ctx, s, mask, value, x, y, w, h);
162 
163 			buffers &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
164 		}
165 	}
166 
167 	if (buffers)
168 		_mesa_meta_Clear(ctx, buffers);
169 }
170 
171 void
nouveau_driver_functions_init(struct dd_function_table * functions)172 nouveau_driver_functions_init(struct dd_function_table *functions)
173 {
174 	functions->GetString = nouveau_get_string;
175 	functions->Flush = nouveau_flush;
176 	functions->Finish = nouveau_finish;
177 	functions->Clear = nouveau_clear;
178 	functions->DrawPixels = _mesa_meta_DrawPixels;
179 	functions->CopyPixels = _mesa_meta_CopyPixels;
180 	functions->Bitmap = _mesa_meta_Bitmap;
181 	functions->BlitFramebuffer = _mesa_meta_and_swrast_BlitFramebuffer;
182 }
183