gl_common: minor cleanup/refactor
[mplayer.git] / libvo / vo_gl.c
blob161f9869ec517723b2840e66342487d0be547c46
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * You can alternatively redistribute this file and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <stdbool.h>
30 #include "config.h"
31 #include "talloc.h"
32 #include "mp_msg.h"
33 #include "subopt-helper.h"
34 #include "video_out.h"
35 #include "libmpcodecs/vfcap.h"
36 #include "libmpcodecs/mp_image.h"
37 #include "geometry.h"
38 #include "osd.h"
39 #include "sub/sub.h"
40 #include "bitmap_packer.h"
42 #include "gl_common.h"
43 #include "aspect.h"
44 #include "fastmemcpy.h"
45 #include "sub/ass_mp.h"
47 static int preinit_nosw(struct vo *vo, const char *arg);
49 //! How many parts the OSD may consist of at most
50 #define MAX_OSD_PARTS 20
52 //for gl_priv.use_yuv
53 #define MASK_ALL_YUV (~(1 << YUV_CONVERSION_NONE))
54 #define MASK_NOT_COMBINERS (~((1 << YUV_CONVERSION_NONE) | (1 << YUV_CONVERSION_COMBINERS)))
55 #define MASK_GAMMA_SUPPORT (MASK_NOT_COMBINERS & ~(1 << YUV_CONVERSION_FRAGMENT))
57 struct vertex_eosd {
58 float x, y;
59 uint8_t color[4];
60 float u, v;
63 struct gl_priv {
64 MPGLContext *glctx;
65 GL *gl;
67 int use_osd;
68 int scaled_osd;
69 //! Textures for OSD
70 GLuint osdtex[MAX_OSD_PARTS];
71 //! Alpha textures for OSD
72 GLuint osdatex[MAX_OSD_PARTS];
73 GLuint eosd_texture;
74 int eosd_texture_width, eosd_texture_height;
75 struct bitmap_packer *eosd;
76 struct vertex_eosd *eosd_va;
77 int eosd_render_count;
78 unsigned int bitmap_id;
79 unsigned int bitmap_pos_id;
80 //! Display lists that draw the OSD parts
81 GLuint osdDispList[MAX_OSD_PARTS];
82 GLuint osdaDispList[MAX_OSD_PARTS];
83 //! How many parts the OSD currently consists of
84 int osdtexCnt;
85 int osd_color;
87 int use_ycbcr;
88 int use_yuv;
89 struct mp_csp_details colorspace;
90 int is_yuv;
91 int lscale;
92 int cscale;
93 float filter_strength;
94 float noise_strength;
95 int yuvconvtype;
96 int use_rectangle;
97 int err_shown;
98 uint32_t image_width;
99 uint32_t image_height;
100 uint32_t image_format;
101 uint32_t image_d_width;
102 uint32_t image_d_height;
103 int many_fmts;
104 int have_texture_rg;
105 int ati_hack;
106 int force_pbo;
107 int use_glFinish;
108 int swap_interval;
109 GLenum target;
110 GLint texfmt;
111 GLenum gl_format;
112 GLenum gl_type;
113 GLuint buffer;
114 GLuint buffer_uv[2];
115 int buffersize;
116 int buffersize_uv;
117 void *bufferptr;
118 void *bufferptr_uv[2];
119 GLuint fragprog;
120 GLuint default_texs[22];
121 char *custom_prog;
122 char *custom_tex;
123 int custom_tlin;
124 int custom_trect;
125 int mipmap_gen;
126 int stereo_mode;
128 struct mp_csp_equalizer video_eq;
130 int texture_width;
131 int texture_height;
132 int mpi_flipped;
133 int vo_flipped;
134 int ass_border_x, ass_border_y;
136 unsigned int slice_height;
139 static void resize(struct vo *vo, int x, int y)
141 struct gl_priv *p = vo->priv;
142 GL *gl = p->gl;
144 mp_msg(MSGT_VO, MSGL_V, "[gl] Resize: %dx%d\n", x, y);
145 if (WinID >= 0) {
146 int left = 0, top = 0, w = x, h = y;
147 geometry(&left, &top, &w, &h, vo->dwidth, vo->dheight);
148 top = y - h - top;
149 gl->Viewport(left, top, w, h);
150 } else
151 gl->Viewport(0, 0, x, y);
153 gl->MatrixMode(GL_PROJECTION);
154 gl->LoadIdentity();
155 p->ass_border_x = p->ass_border_y = 0;
156 if (aspect_scaling()) {
157 int new_w, new_h;
158 GLdouble scale_x, scale_y;
159 aspect(vo, &new_w, &new_h, A_WINZOOM);
160 panscan_calc_windowed(vo);
161 new_w += vo->panscan_x;
162 new_h += vo->panscan_y;
163 scale_x = (GLdouble)new_w / (GLdouble)x;
164 scale_y = (GLdouble)new_h / (GLdouble)y;
165 gl->Scaled(scale_x, scale_y, 1);
166 p->ass_border_x = (vo->dwidth - new_w) / 2;
167 p->ass_border_y = (vo->dheight - new_h) / 2;
169 gl->Ortho(0, p->image_width, p->image_height, 0, -1, 1);
171 gl->MatrixMode(GL_MODELVIEW);
172 gl->LoadIdentity();
174 if (!p->scaled_osd)
175 vo_osd_changed(OSDTYPE_OSD);
177 gl->Clear(GL_COLOR_BUFFER_BIT);
178 vo->want_redraw = true;
181 static void texSize(struct vo *vo, int w, int h, int *texw, int *texh)
183 struct gl_priv *p = vo->priv;
185 if (p->use_rectangle) {
186 *texw = w;
187 *texh = h;
188 } else {
189 *texw = 32;
190 while (*texw < w)
191 *texw *= 2;
192 *texh = 32;
193 while (*texh < h)
194 *texh *= 2;
196 if (p->ati_hack)
197 *texw = (*texw + 511) & ~511;
200 //! maximum size of custom fragment program
201 #define MAX_CUSTOM_PROG_SIZE (1024 * 1024)
202 static void update_yuvconv(struct vo *vo)
204 struct gl_priv *p = vo->priv;
205 GL *gl = p->gl;
207 int xs, ys, depth;
208 struct mp_csp_params cparams = { .colorspace = p->colorspace };
209 mp_csp_copy_equalizer_values(&cparams, &p->video_eq);
210 gl_conversion_params_t params = {
211 p->target, p->yuvconvtype, cparams,
212 p->texture_width, p->texture_height, 0, 0, p->filter_strength,
213 p->noise_strength
215 mp_get_chroma_shift(p->image_format, &xs, &ys, &depth);
216 params.chrom_texw = params.texw >> xs;
217 params.chrom_texh = params.texh >> ys;
218 params.csp_params.input_bits = depth;
219 params.csp_params.texture_bits = depth+7 & ~7;
220 glSetupYUVConversion(gl, &params);
221 if (p->custom_prog) {
222 FILE *f = fopen(p->custom_prog, "rb");
223 if (!f) {
224 mp_msg(MSGT_VO, MSGL_WARN,
225 "[gl] Could not read customprog %s\n", p->custom_prog);
226 } else {
227 char *prog = calloc(1, MAX_CUSTOM_PROG_SIZE + 1);
228 fread(prog, 1, MAX_CUSTOM_PROG_SIZE, f);
229 fclose(f);
230 loadGPUProgram(gl, GL_FRAGMENT_PROGRAM, prog);
231 free(prog);
233 gl->ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 0,
234 1.0 / p->texture_width,
235 1.0 / p->texture_height,
236 p->texture_width, p->texture_height);
238 if (p->custom_tex) {
239 FILE *f = fopen(p->custom_tex, "rb");
240 if (!f) {
241 mp_msg(MSGT_VO, MSGL_WARN,
242 "[gl] Could not read customtex %s\n", p->custom_tex);
243 } else {
244 int width, height, maxval;
245 gl->ActiveTexture(GL_TEXTURE3);
246 if (glCreatePPMTex(gl, p->custom_trect ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D,
247 0, p->custom_tlin ? GL_LINEAR : GL_NEAREST,
248 f, &width, &height, &maxval)) {
249 gl->ProgramEnvParameter4f(GL_FRAGMENT_PROGRAM, 1,
250 1.0 / width, 1.0 / height,
251 width, height);
252 } else
253 mp_msg(MSGT_VO, MSGL_WARN,
254 "[gl] Error parsing customtex %s\n", p->custom_tex);
255 fclose(f);
256 gl->ActiveTexture(GL_TEXTURE0);
262 * \brief remove all OSD textures and display-lists, thus clearing it.
264 static void clearOSD(struct vo *vo)
266 struct gl_priv *p = vo->priv;
267 GL *gl = p->gl;
269 int i;
270 if (!p->osdtexCnt)
271 return;
272 gl->DeleteTextures(p->osdtexCnt, p->osdtex);
273 gl->DeleteTextures(p->osdtexCnt, p->osdatex);
274 for (i = 0; i < p->osdtexCnt; i++)
275 gl->DeleteLists(p->osdaDispList[i], 1);
276 for (i = 0; i < p->osdtexCnt; i++)
277 gl->DeleteLists(p->osdDispList[i], 1);
278 p->osdtexCnt = 0;
282 * \brief construct display list from ass image list
283 * \param img image list to create OSD from.
285 static void genEOSD(struct vo *vo, mp_eosd_images_t *imgs)
287 struct gl_priv *p = vo->priv;
288 GL *gl = p->gl;
290 if (imgs->bitmap_pos_id == p->bitmap_pos_id)
291 return;
293 if (!p->eosd_texture)
294 gl->GenTextures(1, &p->eosd_texture);
296 gl->BindTexture(p->target, p->eosd_texture);
298 p->eosd_render_count = 0;
299 bool need_upload = false;
301 if (imgs->bitmap_id != p->bitmap_id) {
302 need_upload = true;
303 int res = packer_pack_from_subbitmaps(p->eosd, imgs, 0);
304 if (res < 0) {
305 mp_msg(MSGT_VO, MSGL_ERR,
306 "[gl] subtitle bitmaps do not fit in maximum texture\n");
307 return;
309 if (res == 1) {
310 mp_msg(MSGT_VO, MSGL_V, "[gl] Allocating a %dx%d texture for "
311 "subtitle bitmaps.\n", p->eosd->w, p->eosd->h);
312 texSize(vo, p->eosd->w, p->eosd->h,
313 &p->eosd_texture_width, &p->eosd_texture_height);
314 // xxx it doesn't need to be cleared, that's a waste of time
315 glCreateClearTex(gl, p->target, GL_ALPHA, GL_ALPHA,
316 GL_UNSIGNED_BYTE, GL_NEAREST,
317 p->eosd_texture_width,
318 p->eosd_texture_height, 0);
322 // 2 triangles primitives per quad = 6 vertices per quad
323 // not using GL_QUADS, as it is deprecated in OpenGL 3.x and later
324 p->eosd_va = talloc_realloc_size(p->eosd, p->eosd_va,
325 p->eosd->count *
326 sizeof(struct vertex_eosd) * 6);
328 float eosd_w = p->eosd_texture_width;
329 float eosd_h = p->eosd_texture_height;
331 if (p->use_rectangle == 1)
332 eosd_w = eosd_h = 1.0f;
334 ASS_Image *i = imgs->imgs;
335 struct pos *spos = p->eosd->result;
336 for (int n = 0; n < p->eosd->count; n++, i = i->next) {
337 if (i->w == 0 || i->h == 0)
338 continue;
340 if (need_upload)
341 glUploadTex(gl, p->target, GL_ALPHA, GL_UNSIGNED_BYTE, i->bitmap,
342 i->stride, spos[n].x, spos[n].y, i->w, i->h, 0);
344 uint8_t color[4] = { i->color >> 24, (i->color >> 16) & 0xff,
345 (i->color >> 8) & 0xff, 255 - (i->color & 0xff) };
347 float x0 = i->dst_x;
348 float y0 = i->dst_y;
349 float x1 = i->dst_x + i->w;
350 float y1 = i->dst_y + i->h;
351 float tx0 = spos[n].x / eosd_w;
352 float ty0 = spos[n].y / eosd_h;
353 float tx1 = (spos[n].x + i->w) / eosd_w;
354 float ty1 = (spos[n].y + i->h) / eosd_h;
356 #define COLOR_INIT {color[0], color[1], color[2], color[3]}
357 struct vertex_eosd *va = &p->eosd_va[p->eosd_render_count * 6];
358 va[0] = (struct vertex_eosd) { x0, y0, COLOR_INIT, tx0, ty0 };
359 va[1] = (struct vertex_eosd) { x0, y1, COLOR_INIT, tx0, ty1 };
360 va[2] = (struct vertex_eosd) { x1, y0, COLOR_INIT, tx1, ty0 };
361 va[3] = (struct vertex_eosd) { x1, y1, COLOR_INIT, tx1, ty1 };
362 va[4] = va[2];
363 va[5] = va[1];
364 #undef COLOR_INIT
365 p->eosd_render_count++;
368 gl->BindTexture(p->target, 0);
369 p->bitmap_id = imgs->bitmap_id;
370 p->bitmap_pos_id = imgs->bitmap_pos_id;
373 // Note: relies on state being setup, like projection matrix and blending
374 static void drawEOSD(struct vo *vo)
376 struct gl_priv *p = vo->priv;
377 GL *gl = p->gl;
379 if (p->eosd_render_count == 0)
380 return;
382 gl->BindTexture(p->target, p->eosd_texture);
384 struct vertex_eosd *va = p->eosd_va;
385 size_t stride = sizeof(struct vertex_eosd);
387 gl->VertexPointer(2, GL_FLOAT, stride, &va[0].x);
388 gl->ColorPointer(4, GL_UNSIGNED_BYTE, stride, &va[0].color[0]);
389 gl->TexCoordPointer(2, GL_FLOAT, stride, &va[0].u);
391 gl->EnableClientState(GL_VERTEX_ARRAY);
392 gl->EnableClientState(GL_TEXTURE_COORD_ARRAY);
393 gl->EnableClientState(GL_COLOR_ARRAY);
395 gl->DrawArrays(GL_TRIANGLES, 0, p->eosd_render_count * 6);
397 gl->DisableClientState(GL_VERTEX_ARRAY);
398 gl->DisableClientState(GL_TEXTURE_COORD_ARRAY);
399 gl->DisableClientState(GL_COLOR_ARRAY);
401 gl->BindTexture(p->target, 0);
405 * \brief uninitialize OpenGL context, freeing textures, buffers etc.
407 static void uninitGl(struct vo *vo)
409 struct gl_priv *p = vo->priv;
410 GL *gl = p->gl;
412 int i = 0;
413 if (gl->DeletePrograms && p->fragprog)
414 gl->DeletePrograms(1, &p->fragprog);
415 p->fragprog = 0;
416 while (p->default_texs[i] != 0)
417 i++;
418 if (i)
419 gl->DeleteTextures(i, p->default_texs);
420 p->default_texs[0] = 0;
421 clearOSD(vo);
422 if (p->eosd_texture)
423 gl->DeleteTextures(1, &p->eosd_texture);
424 p->bitmap_id = p->bitmap_pos_id = 0;
425 p->eosd->w = p->eosd->h = 0;
426 p->eosd_texture = 0;
427 if (gl->DeleteBuffers && p->buffer)
428 gl->DeleteBuffers(1, &p->buffer);
429 p->buffer = 0;
430 p->buffersize = 0;
431 p->bufferptr = NULL;
432 if (gl->DeleteBuffers && p->buffer_uv[0])
433 gl->DeleteBuffers(2, p->buffer_uv);
434 p->buffer_uv[0] = p->buffer_uv[1] = 0;
435 p->buffersize_uv = 0;
436 p->bufferptr_uv[0] = p->bufferptr_uv[1] = 0;
437 p->err_shown = 0;
440 static int isSoftwareGl(struct vo *vo)
442 struct gl_priv *p = vo->priv;
443 const char *renderer = p->gl->GetString(GL_RENDERER);
444 return !renderer || strcmp(renderer, "Software Rasterizer") == 0 ||
445 strstr(renderer, "llvmpipe");
448 static void autodetectGlExtensions(struct vo *vo)
450 struct gl_priv *p = vo->priv;
451 GL *gl = p->gl;
453 const char *extensions = gl->GetString(GL_EXTENSIONS);
454 const char *vendor = gl->GetString(GL_VENDOR);
455 const char *version = gl->GetString(GL_VERSION);
456 const char *renderer = gl->GetString(GL_RENDERER);
457 int is_ati = vendor && strstr(vendor, "ATI") != NULL;
458 int ati_broken_pbo = 0;
459 mp_msg(MSGT_VO, MSGL_V, "[gl] Running on OpenGL '%s' by '%s', version '%s'\n",
460 renderer, vendor, version);
461 if (is_ati && strncmp(version, "2.1.", 4) == 0) {
462 int ver = atoi(version + 4);
463 mp_msg(MSGT_VO, MSGL_V, "[gl] Detected ATI driver version: %i\n", ver);
464 ati_broken_pbo = ver && ver < 8395;
466 if (p->ati_hack == -1)
467 p->ati_hack = ati_broken_pbo;
468 if (p->force_pbo == -1) {
469 p->force_pbo = 0;
470 if (extensions && strstr(extensions, "_pixel_buffer_object"))
471 p->force_pbo = is_ati;
473 p->have_texture_rg = extensions && strstr(extensions, "GL_ARB_texture_rg");
474 if (p->use_rectangle == -1) {
475 p->use_rectangle = 0;
476 if (extensions) {
477 // if (strstr(extensions, "_texture_non_power_of_two"))
478 if (strstr(extensions, "_texture_rectangle"))
479 p->use_rectangle = renderer
480 && strstr(renderer, "Mesa DRI R200") ? 1 : 0;
483 if (p->use_osd == -1)
484 p->use_osd = gl->BindTexture != NULL;
485 if (p->use_yuv == -1)
486 p->use_yuv = glAutodetectYUVConversion(gl);
488 int eq_caps = 0;
489 int yuv_mask = (1 << p->use_yuv);
490 if (!(yuv_mask & MASK_NOT_COMBINERS)) {
491 // combiners
492 eq_caps = (1 << MP_CSP_EQ_HUE) | (1 << MP_CSP_EQ_SATURATION);
493 } else if (yuv_mask & MASK_ALL_YUV) {
494 eq_caps = MP_CSP_EQ_CAPS_COLORMATRIX;
495 if (yuv_mask & MASK_GAMMA_SUPPORT)
496 eq_caps |= MP_CSP_EQ_CAPS_GAMMA;
498 p->video_eq.capabilities = eq_caps;
500 if (is_ati && (p->lscale == 1 || p->lscale == 2 || p->cscale == 1 || p->cscale == 2))
501 mp_msg(MSGT_VO, MSGL_WARN, "[gl] Selected scaling mode may be broken on"
502 " ATI cards.\n"
503 "Tell _them_ to fix GL_REPEAT if you have issues.\n");
504 mp_msg(MSGT_VO, MSGL_V, "[gl] Settings after autodetection: ati-hack = %i, "
505 "force-pbo = %i, rectangle = %i, yuv = %i\n",
506 p->ati_hack, p->force_pbo, p->use_rectangle, p->use_yuv);
509 static GLint get_scale_type(struct vo *vo, int chroma)
511 struct gl_priv *p = vo->priv;
513 int nearest = (chroma ? p->cscale : p->lscale) & 64;
514 if (nearest)
515 return p->mipmap_gen ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST;
516 return p->mipmap_gen ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR;
519 // Return the high byte of the value that represents white in chroma (U/V)
520 static int get_chroma_clear_val(int bit_depth)
522 return 1 << (bit_depth - 1 & 7);
526 * \brief Initialize a (new or reused) OpenGL context.
527 * set global gl-related variables to their default values
529 static int initGl(struct vo *vo, uint32_t d_width, uint32_t d_height)
531 struct gl_priv *p = vo->priv;
532 GL *gl = p->gl;
534 GLint scale_type = get_scale_type(vo, 0);
535 autodetectGlExtensions(vo);
536 p->target = p->use_rectangle == 1 ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
537 p->yuvconvtype = SET_YUV_CONVERSION(p->use_yuv) |
538 SET_YUV_LUM_SCALER(p->lscale) |
539 SET_YUV_CHROM_SCALER(p->cscale);
541 texSize(vo, p->image_width, p->image_height,
542 &p->texture_width, &p->texture_height);
544 gl->Disable(GL_BLEND);
545 gl->Disable(GL_DEPTH_TEST);
546 gl->DepthMask(GL_FALSE);
547 gl->Disable(GL_CULL_FACE);
548 gl->Enable(p->target);
549 gl->DrawBuffer(vo_doublebuffering ? GL_BACK : GL_FRONT);
550 gl->TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
552 mp_msg(MSGT_VO, MSGL_V, "[gl] Creating %dx%d texture...\n",
553 p->texture_width, p->texture_height);
555 glCreateClearTex(gl, p->target, p->texfmt, p->gl_format,
556 p->gl_type, scale_type,
557 p->texture_width, p->texture_height, 0);
559 if (p->mipmap_gen)
560 gl->TexParameteri(p->target, GL_GENERATE_MIPMAP, GL_TRUE);
562 if (p->is_yuv) {
563 int i;
564 int xs, ys, depth;
565 scale_type = get_scale_type(vo, 1);
566 mp_get_chroma_shift(p->image_format, &xs, &ys, &depth);
567 int clear = get_chroma_clear_val(depth);
568 gl->GenTextures(21, p->default_texs);
569 p->default_texs[21] = 0;
570 for (i = 0; i < 7; i++) {
571 gl->ActiveTexture(GL_TEXTURE1 + i);
572 gl->BindTexture(GL_TEXTURE_2D, p->default_texs[i]);
573 gl->BindTexture(GL_TEXTURE_RECTANGLE, p->default_texs[i + 7]);
574 gl->BindTexture(GL_TEXTURE_3D, p->default_texs[i + 14]);
576 gl->ActiveTexture(GL_TEXTURE1);
577 glCreateClearTex(gl, p->target, p->texfmt, p->gl_format,
578 p->gl_type, scale_type,
579 p->texture_width >> xs, p->texture_height >> ys,
580 clear);
581 if (p->mipmap_gen)
582 gl->TexParameteri(p->target, GL_GENERATE_MIPMAP, GL_TRUE);
583 gl->ActiveTexture(GL_TEXTURE2);
584 glCreateClearTex(gl, p->target, p->texfmt, p->gl_format,
585 p->gl_type, scale_type,
586 p->texture_width >> xs, p->texture_height >> ys,
587 clear);
588 if (p->mipmap_gen)
589 gl->TexParameteri(p->target, GL_GENERATE_MIPMAP, GL_TRUE);
590 gl->ActiveTexture(GL_TEXTURE0);
591 gl->BindTexture(p->target, 0);
593 if (p->is_yuv || p->custom_prog) {
594 if ((MASK_NOT_COMBINERS & (1 << p->use_yuv)) || p->custom_prog) {
595 if (!gl->GenPrograms || !gl->BindProgram)
596 mp_msg(MSGT_VO, MSGL_ERR,
597 "[gl] fragment program functions missing!\n");
598 else {
599 gl->GenPrograms(1, &p->fragprog);
600 gl->BindProgram(GL_FRAGMENT_PROGRAM, p->fragprog);
603 update_yuvconv(vo);
606 GLint max_texture_size;
607 gl->GetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
608 p->eosd->w_max = p->eosd->h_max = max_texture_size;
610 resize(vo, d_width, d_height);
612 gl->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
613 gl->Clear(GL_COLOR_BUFFER_BIT);
614 if (gl->SwapInterval && p->swap_interval >= 0)
615 gl->SwapInterval(p->swap_interval);
616 return 1;
619 static int create_window(struct vo *vo, uint32_t d_width, uint32_t d_height,
620 uint32_t flags)
622 struct gl_priv *p = vo->priv;
624 if (p->stereo_mode == GL_3D_QUADBUFFER)
625 flags |= VOFLAG_STEREO;
627 return p->glctx->create_window(p->glctx, d_width, d_height, flags);
630 static int config(struct vo *vo, uint32_t width, uint32_t height,
631 uint32_t d_width, uint32_t d_height, uint32_t flags,
632 uint32_t format)
634 struct gl_priv *p = vo->priv;
636 int xs, ys;
637 p->image_height = height;
638 p->image_width = width;
639 p->image_format = format;
640 p->image_d_width = d_width;
641 p->image_d_height = d_height;
642 p->is_yuv = mp_get_chroma_shift(p->image_format, &xs, &ys, NULL) > 0;
643 p->is_yuv |= (xs << 8) | (ys << 16);
644 glFindFormat(format, p->have_texture_rg, NULL, &p->texfmt, &p->gl_format,
645 &p->gl_type);
647 p->vo_flipped = !!(flags & VOFLAG_FLIPPING);
649 if (create_window(vo, d_width, d_height, flags) < 0)
650 return -1;
652 if (vo->config_count)
653 uninitGl(vo);
654 if (p->glctx->setGlWindow(p->glctx) == SET_WINDOW_FAILED)
655 return -1;
656 initGl(vo, vo->dwidth, vo->dheight);
658 return 0;
661 static void check_events(struct vo *vo)
663 struct gl_priv *p = vo->priv;
665 int e = p->glctx->check_events(vo);
666 if (e & VO_EVENT_REINIT) {
667 uninitGl(vo);
668 initGl(vo, vo->dwidth, vo->dheight);
670 if (e & VO_EVENT_RESIZE)
671 resize(vo, vo->dwidth, vo->dheight);
672 if (e & VO_EVENT_EXPOSE)
673 vo->want_redraw = true;
677 * Creates the textures and the display list needed for displaying
678 * an OSD part.
679 * Callback function for osd_draw_text_ext().
681 static void create_osd_texture(void *ctx, int x0, int y0, int w, int h,
682 unsigned char *src, unsigned char *srca,
683 int stride)
685 struct vo *vo = ctx;
686 struct gl_priv *p = vo->priv;
687 GL *gl = p->gl;
689 // initialize to 8 to avoid special-casing on alignment
690 int sx = 8, sy = 8;
691 GLint scale_type = p->scaled_osd ? GL_LINEAR : GL_NEAREST;
693 if (w <= 0 || h <= 0 || stride < w) {
694 mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n");
695 return;
697 texSize(vo, w, h, &sx, &sy);
699 if (p->osdtexCnt >= MAX_OSD_PARTS) {
700 mp_msg(MSGT_VO, MSGL_ERR, "Too many OSD parts, contact the developers!\n");
701 return;
704 // create Textures for OSD part
705 gl->GenTextures(1, &p->osdtex[p->osdtexCnt]);
706 gl->BindTexture(p->target, p->osdtex[p->osdtexCnt]);
707 glCreateClearTex(gl, p->target, GL_LUMINANCE, GL_LUMINANCE,
708 GL_UNSIGNED_BYTE, scale_type, sx, sy, 0);
709 glUploadTex(gl, p->target, GL_LUMINANCE, GL_UNSIGNED_BYTE, src, stride,
710 0, 0, w, h, 0);
712 gl->GenTextures(1, &p->osdatex[p->osdtexCnt]);
713 gl->BindTexture(p->target, p->osdatex[p->osdtexCnt]);
714 glCreateClearTex(gl, p->target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE,
715 scale_type, sx, sy, 0);
717 int i;
718 char *tmp = malloc(stride * h);
719 // convert alpha from weird MPlayer scale.
720 // in-place is not possible since it is reused for future OSDs
721 for (i = h * stride - 1; i >= 0; i--)
722 tmp[i] = -srca[i];
723 glUploadTex(gl, p->target, GL_ALPHA, GL_UNSIGNED_BYTE, tmp, stride,
724 0, 0, w, h, 0);
725 free(tmp);
728 gl->BindTexture(p->target, 0);
730 // Create a list for rendering this OSD part
731 p->osdaDispList[p->osdtexCnt] = gl->GenLists(1);
732 gl->NewList(p->osdaDispList[p->osdtexCnt], GL_COMPILE);
733 // render alpha
734 gl->BindTexture(p->target, p->osdatex[p->osdtexCnt]);
735 glDrawTex(gl, x0, y0, w, h, 0, 0, w, h, sx, sy, p->use_rectangle == 1, 0, 0);
736 gl->EndList();
737 p->osdDispList[p->osdtexCnt] = gl->GenLists(1);
738 gl->NewList(p->osdDispList[p->osdtexCnt], GL_COMPILE);
739 // render OSD
740 gl->BindTexture(p->target, p->osdtex[p->osdtexCnt]);
741 glDrawTex(gl, x0, y0, w, h, 0, 0, w, h, sx, sy, p->use_rectangle == 1, 0, 0);
742 gl->EndList();
744 p->osdtexCnt++;
747 #define RENDER_OSD 1
748 #define RENDER_EOSD 2
751 * \param type bit 0: render OSD, bit 1: render EOSD
753 static void do_render_osd(struct vo *vo, int type)
755 struct gl_priv *p = vo->priv;
756 GL *gl = p->gl;
758 int draw_osd = (type & RENDER_OSD) && p->osdtexCnt > 0;
759 int draw_eosd = (type & RENDER_EOSD);
760 if (!draw_osd && !draw_eosd)
761 return;
762 // set special rendering parameters
763 if (!p->scaled_osd) {
764 gl->MatrixMode(GL_PROJECTION);
765 gl->PushMatrix();
766 gl->LoadIdentity();
767 gl->Ortho(0, vo->dwidth, vo->dheight, 0, -1, 1);
769 gl->Enable(GL_BLEND);
770 if (draw_eosd) {
771 gl->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
772 drawEOSD(vo);
774 if (draw_osd) {
775 gl->Color4ub((p->osd_color >> 16) & 0xff, (p->osd_color >> 8) & 0xff,
776 p->osd_color & 0xff, 0xff - (p->osd_color >> 24));
777 // draw OSD
778 gl->BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
779 gl->CallLists(p->osdtexCnt, GL_UNSIGNED_INT, p->osdaDispList);
780 gl->BlendFunc(GL_SRC_ALPHA, GL_ONE);
781 gl->CallLists(p->osdtexCnt, GL_UNSIGNED_INT, p->osdDispList);
783 // set rendering parameters back to defaults
784 gl->Disable(GL_BLEND);
785 if (!p->scaled_osd)
786 gl->PopMatrix();
787 gl->BindTexture(p->target, 0);
790 static void draw_osd(struct vo *vo, struct osd_state *osd)
792 struct gl_priv *p = vo->priv;
794 if (!p->use_osd)
795 return;
796 if (vo_osd_changed(0)) {
797 int osd_h, osd_w;
798 clearOSD(vo);
799 osd_w = p->scaled_osd ? p->image_width : vo->dwidth;
800 osd_h = p->scaled_osd ? p->image_height : vo->dheight;
801 osd_draw_text_ext(osd, osd_w, osd_h, p->ass_border_x,
802 p->ass_border_y, p->ass_border_x,
803 p->ass_border_y, p->image_width,
804 p->image_height, create_osd_texture, vo);
806 if (vo_doublebuffering)
807 do_render_osd(vo, RENDER_OSD);
810 static void do_render(struct vo *vo)
812 struct gl_priv *p = vo->priv;
813 GL *gl = p->gl;
815 // Enable(GL_TEXTURE_2D);
816 // BindTexture(GL_TEXTURE_2D, texture_id);
818 gl->Color4f(1, 1, 1, 1);
819 if (p->is_yuv || p->custom_prog)
820 glEnableYUVConversion(gl, p->target, p->yuvconvtype);
821 if (p->stereo_mode) {
822 glEnable3DLeft(gl, p->stereo_mode);
823 glDrawTex(gl, 0, 0, p->image_width, p->image_height,
824 0, 0, p->image_width >> 1, p->image_height,
825 p->texture_width, p->texture_height,
826 p->use_rectangle == 1, p->is_yuv,
827 p->mpi_flipped ^ p->vo_flipped);
828 glEnable3DRight(gl, p->stereo_mode);
829 glDrawTex(gl, 0, 0, p->image_width, p->image_height,
830 p->image_width >> 1, 0, p->image_width >> 1,
831 p->image_height, p->texture_width, p->texture_height,
832 p->use_rectangle == 1, p->is_yuv,
833 p->mpi_flipped ^ p->vo_flipped);
834 glDisable3D(gl, p->stereo_mode);
835 } else {
836 glDrawTex(gl, 0, 0, p->image_width, p->image_height,
837 0, 0, p->image_width, p->image_height,
838 p->texture_width, p->texture_height,
839 p->use_rectangle == 1, p->is_yuv,
840 p->mpi_flipped ^ p->vo_flipped);
842 if (p->is_yuv || p->custom_prog)
843 glDisableYUVConversion(gl, p->target, p->yuvconvtype);
846 static void flip_page(struct vo *vo)
848 struct gl_priv *p = vo->priv;
849 GL *gl = p->gl;
851 if (vo_doublebuffering) {
852 if (p->use_glFinish)
853 gl->Finish();
854 p->glctx->swapGlBuffers(p->glctx);
855 if (aspect_scaling())
856 gl->Clear(GL_COLOR_BUFFER_BIT);
857 } else {
858 do_render(vo);
859 do_render_osd(vo, RENDER_OSD | RENDER_EOSD);
860 if (p->use_glFinish)
861 gl->Finish();
862 else
863 gl->Flush();
867 static int draw_slice(struct vo *vo, uint8_t *src[], int stride[], int w, int h,
868 int x, int y)
870 struct gl_priv *p = vo->priv;
871 GL *gl = p->gl;
873 p->mpi_flipped = stride[0] < 0;
874 glUploadTex(gl, p->target, p->gl_format, p->gl_type, src[0], stride[0],
875 x, y, w, h, p->slice_height);
876 if (p->is_yuv) {
877 int xs, ys;
878 mp_get_chroma_shift(p->image_format, &xs, &ys, NULL);
879 gl->ActiveTexture(GL_TEXTURE1);
880 glUploadTex(gl, p->target, p->gl_format, p->gl_type, src[1], stride[1],
881 x >> xs, y >> ys, w >> xs, h >> ys, p->slice_height);
882 gl->ActiveTexture(GL_TEXTURE2);
883 glUploadTex(gl, p->target, p->gl_format, p->gl_type, src[2], stride[2],
884 x >> xs, y >> ys, w >> xs, h >> ys, p->slice_height);
885 gl->ActiveTexture(GL_TEXTURE0);
887 return 0;
890 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
892 struct gl_priv *p = vo->priv;
893 GL *gl = p->gl;
895 int needed_size;
896 if (!gl->GenBuffers || !gl->BindBuffer || !gl->BufferData || !gl->MapBuffer) {
897 if (!p->err_shown)
898 mp_msg(MSGT_VO, MSGL_ERR, "[gl] extensions missing for dr\n"
899 "Expect a _major_ speed penalty\n");
900 p->err_shown = 1;
901 return VO_FALSE;
903 if (mpi->flags & MP_IMGFLAG_READABLE)
904 return VO_FALSE;
905 if (mpi->type != MP_IMGTYPE_STATIC && mpi->type != MP_IMGTYPE_TEMP &&
906 (mpi->type != MP_IMGTYPE_NUMBERED || mpi->number))
907 return VO_FALSE;
908 if (p->ati_hack) {
909 mpi->width = p->texture_width;
910 mpi->height = p->texture_height;
912 mpi->stride[0] = mpi->width * mpi->bpp / 8;
913 needed_size = mpi->stride[0] * mpi->height;
914 if (!p->buffer)
915 gl->GenBuffers(1, &p->buffer);
916 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer);
917 if (needed_size > p->buffersize) {
918 p->buffersize = needed_size;
919 gl->BufferData(GL_PIXEL_UNPACK_BUFFER, p->buffersize,
920 NULL, GL_DYNAMIC_DRAW);
922 if (!p->bufferptr)
923 p->bufferptr = gl->MapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
924 mpi->planes[0] = p->bufferptr;
925 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
926 if (!mpi->planes[0]) {
927 if (!p->err_shown)
928 mp_msg(MSGT_VO, MSGL_ERR, "[gl] could not acquire buffer for dr\n"
929 "Expect a _major_ speed penalty\n");
930 p->err_shown = 1;
931 return VO_FALSE;
933 if (p->is_yuv) {
934 // planar YUV
935 int xs, ys, component_bits;
936 mp_get_chroma_shift(p->image_format, &xs, &ys, &component_bits);
937 int bp = (component_bits + 7) / 8;
938 mpi->flags |= MP_IMGFLAG_COMMON_STRIDE | MP_IMGFLAG_COMMON_PLANE;
939 mpi->stride[0] = mpi->width * bp;
940 mpi->planes[1] = mpi->planes[0] + mpi->stride[0] * mpi->height;
941 mpi->stride[1] = (mpi->width >> xs) * bp;
942 mpi->planes[2] = mpi->planes[1] + mpi->stride[1] * (mpi->height >> ys);
943 mpi->stride[2] = (mpi->width >> xs) * bp;
944 if (p->ati_hack) {
945 mpi->flags &= ~MP_IMGFLAG_COMMON_PLANE;
946 if (!p->buffer_uv[0])
947 gl->GenBuffers(2, p->buffer_uv);
948 int buffer_size = mpi->stride[1] * mpi->height;
949 if (buffer_size > p->buffersize_uv) {
950 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[0]);
951 gl->BufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, NULL,
952 GL_DYNAMIC_DRAW);
953 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[1]);
954 gl->BufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, NULL,
955 GL_DYNAMIC_DRAW);
956 p->buffersize_uv = buffer_size;
958 if (!p->bufferptr_uv[0]) {
959 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[0]);
960 p->bufferptr_uv[0] = gl->MapBuffer(GL_PIXEL_UNPACK_BUFFER,
961 GL_WRITE_ONLY);
962 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[1]);
963 p->bufferptr_uv[1] = gl->MapBuffer(GL_PIXEL_UNPACK_BUFFER,
964 GL_WRITE_ONLY);
966 mpi->planes[1] = p->bufferptr_uv[0];
967 mpi->planes[2] = p->bufferptr_uv[1];
970 mpi->flags |= MP_IMGFLAG_DIRECT;
971 return VO_TRUE;
974 static void clear_border(struct vo *vo, uint8_t *dst, int start, int stride,
975 int height, int full_height, int value)
977 int right_border = stride - start;
978 int bottom_border = full_height - height;
979 while (height > 0) {
980 if (right_border > 0)
981 memset(dst + start, value, right_border);
982 dst += stride;
983 height--;
985 if (bottom_border > 0)
986 memset(dst, value, stride * bottom_border);
989 static uint32_t draw_image(struct vo *vo, mp_image_t *mpi)
991 struct gl_priv *p = vo->priv;
992 GL *gl = p->gl;
994 int slice = p->slice_height;
995 int stride[3];
996 unsigned char *planes[3];
997 mp_image_t mpi2 = *mpi;
998 int w = mpi->w, h = mpi->h;
999 if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)
1000 goto skip_upload;
1001 mpi2.flags = 0;
1002 mpi2.type = MP_IMGTYPE_TEMP;
1003 mpi2.width = mpi2.w;
1004 mpi2.height = mpi2.h;
1005 if (p->force_pbo && !(mpi->flags & MP_IMGFLAG_DIRECT) && !p->bufferptr
1006 && get_image(vo, &mpi2) == VO_TRUE)
1008 int bp = mpi->bpp / 8;
1009 int xs, ys, component_bits;
1010 mp_get_chroma_shift(p->image_format, &xs, &ys, &component_bits);
1011 if (p->is_yuv)
1012 bp = (component_bits + 7) / 8;
1013 memcpy_pic(mpi2.planes[0], mpi->planes[0], mpi->w * bp, mpi->h,
1014 mpi2.stride[0], mpi->stride[0]);
1015 int uv_bytes = (mpi->w >> xs) * bp;
1016 if (p->is_yuv) {
1017 memcpy_pic(mpi2.planes[1], mpi->planes[1], uv_bytes, mpi->h >> ys,
1018 mpi2.stride[1], mpi->stride[1]);
1019 memcpy_pic(mpi2.planes[2], mpi->planes[2], uv_bytes, mpi->h >> ys,
1020 mpi2.stride[2], mpi->stride[2]);
1022 if (p->ati_hack) {
1023 // since we have to do a full upload we need to clear the borders
1024 clear_border(vo, mpi2.planes[0], mpi->w * bp, mpi2.stride[0],
1025 mpi->h, mpi2.height, 0);
1026 if (p->is_yuv) {
1027 int clear = get_chroma_clear_val(component_bits);
1028 clear_border(vo, mpi2.planes[1], uv_bytes, mpi2.stride[1],
1029 mpi->h >> ys, mpi2.height >> ys, clear);
1030 clear_border(vo, mpi2.planes[2], uv_bytes, mpi2.stride[2],
1031 mpi->h >> ys, mpi2.height >> ys, clear);
1034 mpi = &mpi2;
1036 stride[0] = mpi->stride[0];
1037 stride[1] = mpi->stride[1];
1038 stride[2] = mpi->stride[2];
1039 planes[0] = mpi->planes[0];
1040 planes[1] = mpi->planes[1];
1041 planes[2] = mpi->planes[2];
1042 p->mpi_flipped = stride[0] < 0;
1043 if (mpi->flags & MP_IMGFLAG_DIRECT) {
1044 intptr_t base = (intptr_t)planes[0];
1045 if (p->ati_hack) {
1046 w = p->texture_width;
1047 h = p->texture_height;
1049 if (p->mpi_flipped)
1050 base += (mpi->h - 1) * stride[0];
1051 planes[0] -= base;
1052 planes[1] -= base;
1053 planes[2] -= base;
1054 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer);
1055 gl->UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1056 p->bufferptr = NULL;
1057 if (!(mpi->flags & MP_IMGFLAG_COMMON_PLANE))
1058 planes[0] = planes[1] = planes[2] = NULL;
1059 slice = 0; // always "upload" full texture
1061 glUploadTex(gl, p->target, p->gl_format, p->gl_type, planes[0],
1062 stride[0], 0, 0, w, h, slice);
1063 if (p->is_yuv) {
1064 int xs, ys;
1065 mp_get_chroma_shift(p->image_format, &xs, &ys, NULL);
1066 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
1067 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[0]);
1068 gl->UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1069 p->bufferptr_uv[0] = NULL;
1071 gl->ActiveTexture(GL_TEXTURE1);
1072 glUploadTex(gl, p->target, p->gl_format, p->gl_type, planes[1],
1073 stride[1], 0, 0, w >> xs, h >> ys, slice);
1074 if ((mpi->flags & MP_IMGFLAG_DIRECT) && !(mpi->flags & MP_IMGFLAG_COMMON_PLANE)) {
1075 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, p->buffer_uv[1]);
1076 gl->UnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1077 p->bufferptr_uv[1] = NULL;
1079 gl->ActiveTexture(GL_TEXTURE2);
1080 glUploadTex(gl, p->target, p->gl_format, p->gl_type, planes[2],
1081 stride[2], 0, 0, w >> xs, h >> ys, slice);
1082 gl->ActiveTexture(GL_TEXTURE0);
1084 if (mpi->flags & MP_IMGFLAG_DIRECT) {
1085 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1087 skip_upload:
1088 if (vo_doublebuffering)
1089 do_render(vo);
1090 return VO_TRUE;
1093 static mp_image_t *get_screenshot(struct vo *vo)
1095 struct gl_priv *p = vo->priv;
1096 GL *gl = p->gl;
1098 mp_image_t *image = alloc_mpi(p->texture_width, p->texture_height,
1099 p->image_format);
1101 glDownloadTex(gl, p->target, p->gl_format, p->gl_type, image->planes[0],
1102 image->stride[0]);
1104 if (p->is_yuv) {
1105 gl->ActiveTexture(GL_TEXTURE1);
1106 glDownloadTex(gl, p->target, p->gl_format, p->gl_type, image->planes[1],
1107 image->stride[1]);
1108 gl->ActiveTexture(GL_TEXTURE2);
1109 glDownloadTex(gl, p->target, p->gl_format, p->gl_type, image->planes[2],
1110 image->stride[2]);
1111 gl->ActiveTexture(GL_TEXTURE0);
1114 image->width = p->image_width;
1115 image->height = p->image_height;
1117 image->w = p->image_d_width;
1118 image->h = p->image_d_height;
1120 return image;
1123 static mp_image_t *get_window_screenshot(struct vo *vo)
1125 struct gl_priv *p = vo->priv;
1126 GL *gl = p->gl;
1128 GLint vp[4]; //x, y, w, h
1129 gl->GetIntegerv(GL_VIEWPORT, vp);
1130 mp_image_t *image = alloc_mpi(vp[2], vp[3], IMGFMT_RGB24);
1131 gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1132 gl->PixelStorei(GL_PACK_ALIGNMENT, 0);
1133 gl->PixelStorei(GL_PACK_ROW_LENGTH, 0);
1134 gl->ReadBuffer(GL_FRONT);
1135 //flip image while reading
1136 for (int y = 0; y < vp[3]; y++) {
1137 gl->ReadPixels(vp[0], vp[1] + vp[3] - y - 1, vp[2], 1,
1138 GL_RGB, GL_UNSIGNED_BYTE,
1139 image->planes[0] + y * image->stride[0]);
1141 return image;
1144 static int query_format(struct vo *vo, uint32_t format)
1146 struct gl_priv *p = vo->priv;
1148 int depth;
1149 int caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_FLIP |
1150 VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_ACCEPT_STRIDE;
1151 if (p->use_osd)
1152 caps |= VFCAP_OSD | VFCAP_EOSD | (p->scaled_osd ? 0 : VFCAP_EOSD_UNSCALED);
1153 if (format == IMGFMT_RGB24 || format == IMGFMT_RGBA)
1154 return caps;
1155 if (p->use_yuv && mp_get_chroma_shift(format, NULL, NULL, &depth) &&
1156 (depth == 8 || depth == 16 || glYUVLargeRange(p->use_yuv)) &&
1157 (IMGFMT_IS_YUVP16_NE(format) || !IMGFMT_IS_YUVP16(format)))
1158 return caps;
1159 // HACK, otherwise we get only b&w with some filters (e.g. -vf eq)
1160 // ideally MPlayer should be fixed instead not to use Y800 when it has the choice
1161 if (!p->use_yuv && (format == IMGFMT_Y8 || format == IMGFMT_Y800))
1162 return 0;
1163 if (!p->use_ycbcr && (format == IMGFMT_UYVY || format == IMGFMT_YVYU))
1164 return 0;
1165 if (p->many_fmts &&
1166 glFindFormat(format, p->have_texture_rg, NULL, NULL, NULL, NULL))
1167 return caps;
1168 return 0;
1171 static void uninit(struct vo *vo)
1173 struct gl_priv *p = vo->priv;
1175 if (p->glctx)
1176 uninitGl(vo);
1177 free(p->custom_prog);
1178 p->custom_prog = NULL;
1179 free(p->custom_tex);
1180 p->custom_tex = NULL;
1181 uninit_mpglcontext(p->glctx);
1182 p->glctx = NULL;
1183 p->gl = NULL;
1186 static int backend_valid(void *arg)
1188 return mpgl_find_backend(*(const char **)arg) >= 0;
1191 static int preinit_internal(struct vo *vo, const char *arg, int allow_sw)
1193 struct gl_priv *p = talloc_zero(vo, struct gl_priv);
1194 vo->priv = p;
1196 *p = (struct gl_priv) {
1197 .many_fmts = 1,
1198 .use_osd = -1,
1199 .use_yuv = -1,
1200 .colorspace = MP_CSP_DETAILS_DEFAULTS,
1201 .filter_strength = 0.5,
1202 .use_rectangle = -1,
1203 .ati_hack = -1,
1204 .force_pbo = -1,
1205 .swap_interval = 1,
1206 .custom_prog = NULL,
1207 .custom_tex = NULL,
1208 .custom_tlin = 1,
1209 .osd_color = 0xffffff,
1212 p->eosd = talloc_zero(vo, struct bitmap_packer);
1214 //essentially unused; for legacy warnings only
1215 int user_colorspace = 0;
1216 int levelconv = -1;
1217 int aspect = -1;
1218 char *backend_arg = NULL;
1220 const opt_t subopts[] = {
1221 {"manyfmts", OPT_ARG_BOOL, &p->many_fmts, NULL},
1222 {"osd", OPT_ARG_BOOL, &p->use_osd, NULL},
1223 {"scaled-osd", OPT_ARG_BOOL, &p->scaled_osd, NULL},
1224 {"ycbcr", OPT_ARG_BOOL, &p->use_ycbcr, NULL},
1225 {"slice-height", OPT_ARG_INT, &p->slice_height, int_non_neg},
1226 {"rectangle", OPT_ARG_INT, &p->use_rectangle,int_non_neg},
1227 {"yuv", OPT_ARG_INT, &p->use_yuv, int_non_neg},
1228 {"lscale", OPT_ARG_INT, &p->lscale, int_non_neg},
1229 {"cscale", OPT_ARG_INT, &p->cscale, int_non_neg},
1230 {"filter-strength", OPT_ARG_FLOAT, &p->filter_strength, NULL},
1231 {"noise-strength", OPT_ARG_FLOAT, &p->noise_strength, NULL},
1232 {"ati-hack", OPT_ARG_BOOL, &p->ati_hack, NULL},
1233 {"force-pbo", OPT_ARG_BOOL, &p->force_pbo, NULL},
1234 {"glfinish", OPT_ARG_BOOL, &p->use_glFinish, NULL},
1235 {"swapinterval", OPT_ARG_INT, &p->swap_interval,NULL},
1236 {"customprog", OPT_ARG_MSTRZ,&p->custom_prog, NULL},
1237 {"customtex", OPT_ARG_MSTRZ,&p->custom_tex, NULL},
1238 {"customtlin", OPT_ARG_BOOL, &p->custom_tlin, NULL},
1239 {"customtrect", OPT_ARG_BOOL, &p->custom_trect, NULL},
1240 {"mipmapgen", OPT_ARG_BOOL, &p->mipmap_gen, NULL},
1241 {"osdcolor", OPT_ARG_INT, &p->osd_color, NULL},
1242 {"stereo", OPT_ARG_INT, &p->stereo_mode, NULL},
1243 {"backend", OPT_ARG_MSTRZ,&backend_arg, backend_valid},
1244 // Removed options.
1245 // They are only parsed to notify the user about the replacements.
1246 {"aspect", OPT_ARG_BOOL, &aspect, NULL},
1247 {"colorspace", OPT_ARG_INT, &user_colorspace, NULL},
1248 {"levelconv", OPT_ARG_INT, &levelconv, NULL},
1249 {NULL}
1252 if (subopt_parse(arg, subopts) != 0) {
1253 mp_msg(MSGT_VO, MSGL_FATAL,
1254 "\n-vo gl command line help:\n"
1255 "Example: mplayer -vo gl:slice-height=4\n"
1256 "\nOptions:\n"
1257 " nomanyfmts\n"
1258 " Disable extended color formats for OpenGL 1.2 and later\n"
1259 " slice-height=<0-...>\n"
1260 " Slice size for texture transfer, 0 for whole image\n"
1261 " noosd\n"
1262 " Do not use OpenGL OSD code\n"
1263 " scaled-osd\n"
1264 " Render OSD at movie resolution and scale it\n"
1265 " rectangle=<0,1,2>\n"
1266 " 0: use power-of-two textures\n"
1267 " 1: use texture_rectangle\n"
1268 " 2: use texture_non_power_of_two\n"
1269 " ati-hack\n"
1270 " Workaround ATI bug with PBOs\n"
1271 " force-pbo\n"
1272 " Force use of PBO even if this involves an extra memcpy\n"
1273 " glfinish\n"
1274 " Call glFinish() before swapping buffers\n"
1275 " swapinterval=<n>\n"
1276 " Interval in displayed frames between to buffer swaps.\n"
1277 " 1 is equivalent to enable VSYNC, 0 to disable VSYNC.\n"
1278 " Requires GLX_SGI_swap_control support to work.\n"
1279 " ycbcr\n"
1280 " also try to use the GL_MESA_ycbcr_texture extension\n"
1281 " yuv=<n>\n"
1282 " 0: use software YUV to RGB conversion.\n"
1283 " 1: deprecated, will use yuv=2 (used to be nVidia register combiners).\n"
1284 " 2: use fragment program.\n"
1285 " 3: use fragment program with gamma correction.\n"
1286 " 4: use fragment program with gamma correction via lookup.\n"
1287 " 5: use ATI-specific method (for older cards).\n"
1288 " 6: use lookup via 3D texture.\n"
1289 " lscale=<n>\n"
1290 " 0: use standard bilinear scaling for luma.\n"
1291 " 1: use improved bicubic scaling for luma.\n"
1292 " 2: use cubic in X, linear in Y direction scaling for luma.\n"
1293 " 3: as 1 but without using a lookup texture.\n"
1294 " 4: experimental unsharp masking (sharpening).\n"
1295 " 5: experimental unsharp masking (sharpening) with larger radius.\n"
1296 " cscale=<n>\n"
1297 " as lscale but for chroma (2x slower with little visible effect).\n"
1298 " filter-strength=<value>\n"
1299 " set the effect strength for some lscale/cscale filters\n"
1300 " noise-strength=<value>\n"
1301 " set how much noise to add. 1.0 is suitable for dithering to 6 bit.\n"
1302 " customprog=<filename>\n"
1303 " use a custom YUV conversion program\n"
1304 " customtex=<filename>\n"
1305 " use a custom YUV conversion lookup texture\n"
1306 " nocustomtlin\n"
1307 " use GL_NEAREST scaling for customtex texture\n"
1308 " customtrect\n"
1309 " use texture_rectangle for customtex texture\n"
1310 " mipmapgen\n"
1311 " generate mipmaps for the video image (use with TXB in customprog)\n"
1312 " osdcolor=<0xAARRGGBB>\n"
1313 " use the given color for the OSD\n"
1314 " stereo=<n>\n"
1315 " 0: normal display\n"
1316 " 1: side-by-side to red-cyan stereo\n"
1317 " 2: side-by-side to green-magenta stereo\n"
1318 " 3: side-by-side to quadbuffer stereo\n"
1319 " backend=<sys>\n"
1320 " auto: auto-select (default)\n"
1321 " cocoa: Cocoa/OSX\n"
1322 " win: Win32/WGL\n"
1323 " x11: X11/GLX\n"
1324 " sdl: SDL\n"
1325 "\n");
1326 return -1;
1328 if (user_colorspace != 0 || levelconv != -1) {
1329 mp_msg(MSGT_VO, MSGL_ERR, "[gl] \"colorspace\" and \"levelconv\" "
1330 "suboptions have been removed. Use options --colormatrix and"
1331 " --colormatrix-input-range/--colormatrix-output-range instead.\n");
1332 return -1;
1334 if (aspect != -1) {
1335 mp_msg(MSGT_VO, MSGL_ERR, "[gl] \"noaspect\" suboption has been "
1336 "removed. Use --noaspect instead.\n");
1337 return -1;
1339 if (p->use_yuv == 1) {
1340 mp_msg(MSGT_VO, MSGL_WARN, "[gl] yuv=1 (nVidia register combiners) have"
1341 " been removed, using yuv=2 instead.\n");
1342 p->use_yuv = 2;
1345 int backend = backend_arg ? mpgl_find_backend(backend_arg) : GLTYPE_AUTO;
1346 free(backend_arg);
1348 p->glctx = init_mpglcontext(backend, vo);
1349 if (!p->glctx)
1350 goto err_out;
1351 p->gl = p->glctx->gl;
1353 if (p->glctx->type == GLTYPE_SDL && p->use_yuv == -1) {
1354 // Apparently it's not possible to implement VOFLAG_HIDDEN on SDL 1.2,
1355 // so don't do autodetection. Use a sufficiently useful and safe YUV
1356 // conversion mode.
1357 p->use_yuv = YUV_CONVERSION_FRAGMENT;
1360 if (p->use_yuv == -1 || !allow_sw) {
1361 if (create_window(vo, 320, 200, VOFLAG_HIDDEN) < 0)
1362 goto err_out;
1363 if (p->glctx->setGlWindow(p->glctx) == SET_WINDOW_FAILED)
1364 goto err_out;
1365 if (!allow_sw && isSoftwareGl(vo))
1366 goto err_out;
1367 autodetectGlExtensions(vo);
1368 // We created a window to test whether the GL context supports hardware
1369 // acceleration and so on. Destroy that window to make sure all state
1370 // associated with it is lost.
1371 uninit(vo);
1372 p->glctx = init_mpglcontext(backend, vo);
1373 if (!p->glctx)
1374 goto err_out;
1375 p->gl = p->glctx->gl;
1377 if (p->many_fmts)
1378 mp_msg(MSGT_VO, MSGL_INFO, "[gl] using extended formats. "
1379 "Use -vo gl:nomanyfmts if playback fails.\n");
1380 mp_msg(MSGT_VO, MSGL_V, "[gl] Using %d as slice height "
1381 "(0 means image height).\n", p->slice_height);
1383 return 0;
1385 err_out:
1386 uninit(vo);
1387 return -1;
1390 static int preinit(struct vo *vo, const char *arg)
1392 return preinit_internal(vo, arg, 1);
1395 static int control(struct vo *vo, uint32_t request, void *data)
1397 struct gl_priv *p = vo->priv;
1399 switch (request) {
1400 case VOCTRL_QUERY_FORMAT:
1401 return query_format(vo, *(uint32_t *)data);
1402 case VOCTRL_GET_IMAGE:
1403 return get_image(vo, data);
1404 case VOCTRL_DRAW_IMAGE:
1405 return draw_image(vo, data);
1406 case VOCTRL_DRAW_EOSD:
1407 if (!data)
1408 return VO_FALSE;
1409 genEOSD(vo, data);
1410 if (vo_doublebuffering)
1411 do_render_osd(vo, RENDER_EOSD);
1412 return VO_TRUE;
1413 case VOCTRL_GET_EOSD_RES: {
1414 mp_eosd_res_t *r = data;
1415 r->w = vo->dwidth;
1416 r->h = vo->dheight;
1417 r->mt = r->mb = r->ml = r->mr = 0;
1418 if (p->scaled_osd) {
1419 r->w = p->image_width;
1420 r->h = p->image_height;
1421 } else if (aspect_scaling()) {
1422 r->ml = r->mr = p->ass_border_x;
1423 r->mt = r->mb = p->ass_border_y;
1425 return VO_TRUE;
1427 case VOCTRL_ONTOP:
1428 if (!p->glctx->ontop)
1429 break;
1430 p->glctx->ontop(vo);
1431 return VO_TRUE;
1432 case VOCTRL_FULLSCREEN:
1433 p->glctx->fullscreen(vo);
1434 resize(vo, vo->dwidth, vo->dheight);
1435 return VO_TRUE;
1436 case VOCTRL_BORDER:
1437 if (!p->glctx->border)
1438 break;
1439 p->glctx->border(vo);
1440 resize(vo, vo->dwidth, vo->dheight);
1441 return VO_TRUE;
1442 case VOCTRL_GET_PANSCAN:
1443 return VO_TRUE;
1444 case VOCTRL_SET_PANSCAN:
1445 resize(vo, vo->dwidth, vo->dheight);
1446 return VO_TRUE;
1447 case VOCTRL_GET_EQUALIZER:
1448 if (p->is_yuv) {
1449 struct voctrl_get_equalizer_args *args = data;
1450 return mp_csp_equalizer_get(&p->video_eq, args->name, args->valueptr)
1451 >= 0 ? VO_TRUE : VO_NOTIMPL;
1453 break;
1454 case VOCTRL_SET_EQUALIZER:
1455 if (p->is_yuv) {
1456 struct voctrl_set_equalizer_args *args = data;
1457 if (mp_csp_equalizer_set(&p->video_eq, args->name, args->value) < 0)
1458 return VO_NOTIMPL;
1459 update_yuvconv(vo);
1460 vo->want_redraw = true;
1461 return VO_TRUE;
1463 break;
1464 case VOCTRL_SET_YUV_COLORSPACE: {
1465 bool supports_csp = (1 << p->use_yuv) & MASK_NOT_COMBINERS;
1466 if (vo->config_count && supports_csp) {
1467 p->colorspace = *(struct mp_csp_details *)data;
1468 update_yuvconv(vo);
1469 vo->want_redraw = true;
1471 return VO_TRUE;
1473 case VOCTRL_GET_YUV_COLORSPACE:
1474 *(struct mp_csp_details *)data = p->colorspace;
1475 return VO_TRUE;
1476 case VOCTRL_UPDATE_SCREENINFO:
1477 if (!p->glctx->update_xinerama_info)
1478 break;
1479 p->glctx->update_xinerama_info(vo);
1480 return VO_TRUE;
1481 case VOCTRL_REDRAW_FRAME:
1482 if (vo_doublebuffering)
1483 do_render(vo);
1484 return true;
1485 case VOCTRL_SCREENSHOT: {
1486 struct voctrl_screenshot_args *args = data;
1487 if (args->full_window)
1488 args->out_image = get_window_screenshot(vo);
1489 else
1490 args->out_image = get_screenshot(vo);
1491 return true;
1494 return VO_NOTIMPL;
1497 const struct vo_driver video_out_gl = {
1498 .is_new = true,
1499 .info = &(const vo_info_t) {
1500 "OpenGL",
1501 "gl",
1502 "Reimar Doeffinger <Reimar.Doeffinger@gmx.de>",
1505 .preinit = preinit,
1506 .config = config,
1507 .control = control,
1508 .draw_slice = draw_slice,
1509 .draw_osd = draw_osd,
1510 .flip_page = flip_page,
1511 .check_events = check_events,
1512 .uninit = uninit,
1515 static int preinit_nosw(struct vo *vo, const char *arg)
1517 return preinit_internal(vo, arg, 0);
1520 const struct vo_driver video_out_gl_nosw =
1522 .is_new = true,
1523 .info = &(const vo_info_t) {
1524 "OpenGL no software rendering",
1525 "gl_nosw",
1526 "Reimar Doeffinger <Reimar.Doeffinger@gmx.de>",
1529 .preinit = preinit_nosw,
1530 .config = config,
1531 .control = control,
1532 .draw_slice = draw_slice,
1533 .draw_osd = draw_osd,
1534 .flip_page = flip_page,
1535 .check_events = check_events,
1536 .uninit = uninit,