modify folder segv fix, opengl pbfr resource conflict fix
[goodguy/history.git] / cinelerra-5.1 / cinelerra / playback3d.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #define GL_GLEXT_PROTOTYPES
23
24 #include "bccolors.h"
25 #include "bcsignals.h"
26 #include "bcwindowbase.h"
27 #include "canvas.h"
28 #include "clip.h"
29 #include "condition.h"
30 #include "edl.h"
31 #include "maskautos.h"
32 #include "maskauto.h"
33 #include "mutex.h"
34 #include "mwindow.h"
35 #include "overlayframe.inc"
36 #include "overlayframe.h"
37 #include "playback3d.h"
38 #include "pluginclient.h"
39 #include "pluginvclient.h"
40 #include "edlsession.h"
41 #include "transportque.inc"
42 #include "vframe.h"
43
44 #ifdef HAVE_GL
45 #include <GL/gl.h>
46 #include <GL/glext.h>
47 #include <GL/glu.h>
48 #endif
49
50 #include <string.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53
54 #define QQ(q)#q
55 #define SS(s)QQ(s)
56
57
58 // Shaders
59 // These should be passed to VFrame::make_shader to construct shaders.
60 // Can't hard code sampler2D
61
62
63 #ifdef HAVE_GL
64 static const char *yuv_to_rgb_frag =
65         "uniform sampler2D tex;\n"
66         "uniform mat3 yuv_to_rgb_matrix;\n"
67         "uniform float yminf;\n"
68         "void main()\n"
69         "{\n"
70         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
71         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
72         "       gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
73         "}\n";
74
75 static const char *yuva_to_yuv_frag =
76         "uniform sampler2D tex;\n"
77         "void main()\n"
78         "{\n"
79         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
80         "       float a = yuva.a;\n"
81         "       float anti_a = 1.0 - a;\n"
82         "       yuva.r *= a;\n"
83         "       yuva.g = yuva.g * a + 0.5 * anti_a;\n"
84         "       yuva.b = yuva.b * a + 0.5 * anti_a;\n"
85         "       yuva.a = 1.0;\n"
86         "       gl_FragColor = yuva;\n"
87         "}\n";
88
89 static const char *yuva_to_rgb_frag =
90         "uniform sampler2D tex;\n"
91         "uniform mat3 yuv_to_rgb_matrix;\n"
92         "uniform float yminf;\n"
93         "void main()\n"
94         "{\n"
95         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
96         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
97         "       yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
98         "       yuva.rgb *= yuva.a;\n"
99         "       yuva.a = 1.0;\n"
100         "       gl_FragColor = yuva;\n"
101         "}\n";
102
103 static const char *rgb_to_yuv_frag =
104         "uniform sampler2D tex;\n"
105         "uniform mat3 rgb_to_yuv_matrix;\n"
106         "uniform float yminf;\n"
107         "void main()\n"
108         "{\n"
109         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
110         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
111         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
112         "       gl_FragColor = rgba;\n"
113         "}\n";
114
115
116 static const char *rgba_to_rgb_frag =
117         "uniform sampler2D tex;\n"
118         "void main()\n"
119         "{\n"
120         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
121         "       rgba.rgb *= rgba.a;\n"
122         "       rgba.a = 1.0;\n"
123         "       gl_FragColor = rgba;\n"
124         "}\n";
125
126 static const char *rgba_to_yuv_frag =
127         "uniform sampler2D tex;\n"
128         "uniform mat3 rgb_to_yuv_matrix;\n"
129         "uniform float yminf;\n"
130         "void main()\n"
131         "{\n"
132         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
133         "       rgba.rgb *= rgba.a;\n"
134         "       rgba.a = 1.0;\n"
135         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
136         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
137         "       gl_FragColor = rgba;\n"
138         "}\n";
139
140 //static const char *rgba_to_rgb_flatten =
141 //      "void main() {\n"
142 //      "       gl_FragColor.rgb *= gl_FragColor.a;\n"
143 //      "       gl_FragColor.a = 1.0;\n"
144 //      "}\n";
145
146 #define GL_STD_BLEND(FN) \
147 static const char *blend_##FN##_frag = \
148         "uniform sampler2D tex2;\n" \
149         "uniform vec2 tex2_dimensions;\n" \
150         "uniform float alpha;\n" \
151         "void main() {\n" \
152         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
153         "       vec4 result;\n" \
154         "       result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
155         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
156         "       gl_FragColor = mix(canvas, result, alpha);\n" \
157         "}\n"
158
159 #define GL_VEC_BLEND(FN) \
160 static const char *blend_##FN##_frag = \
161         "uniform sampler2D tex2;\n" \
162         "uniform vec2 tex2_dimensions;\n" \
163         "uniform float alpha;\n" \
164         "void main() {\n" \
165         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
166         "       vec4 result;\n" \
167         "       result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
168         "       result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
169         "       result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
170         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
171         "       result = clamp(result, 0.0, 1.0);\n" \
172         "       gl_FragColor = mix(canvas, result, alpha);\n" \
173         "}\n"
174
175 #undef mabs
176 #define mabs abs
177 #undef mmin
178 #define mmin min
179 #undef mmax
180 #define mmax max
181
182 #undef ZERO
183 #define ZERO 0.0
184 #undef ONE
185 #define ONE 1.0
186 #undef TWO
187 #define TWO 2.0
188
189 // NORMAL
190 static const char *blend_NORMAL_frag =
191         "uniform sampler2D tex2;\n"
192         "uniform vec2 tex2_dimensions;\n"
193         "uniform float alpha;\n"
194         "void main() {\n"
195         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
196         "       vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
197         "       gl_FragColor = mix(canvas, result, alpha);\n"
198         "}\n";
199
200 // REPLACE
201 static const char *blend_REPLACE_frag =
202         "uniform float alpha;\n"
203         "void main() {\n"
204         "}\n";
205
206 // ADDITION
207 static const char *blend_ADDITION_frag =
208         "uniform sampler2D tex2;\n"
209         "uniform vec2 tex2_dimensions;\n"
210         "uniform float alpha;\n"
211         "void main() {\n"
212         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
213         "       vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
214         "       gl_FragColor = mix(canvas, result, alpha);\n"
215         "}\n";
216
217 // SUBTRACT
218 static const char *blend_SUBTRACT_frag =
219         "uniform sampler2D tex2;\n"
220         "uniform vec2 tex2_dimensions;\n"
221         "uniform float alpha;\n"
222         "void main() {\n"
223         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
224         "       vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
225         "       gl_FragColor = mix(canvas, result, alpha);\n"
226         "}\n";
227
228 GL_STD_BLEND(MULTIPLY);
229 GL_VEC_BLEND(DIVIDE);
230 GL_VEC_BLEND(MAX);
231 GL_VEC_BLEND(MIN);
232 GL_VEC_BLEND(DARKEN);
233 GL_VEC_BLEND(LIGHTEN);
234 GL_STD_BLEND(DST);
235 GL_STD_BLEND(DST_ATOP);
236 GL_STD_BLEND(DST_IN);
237 GL_STD_BLEND(DST_OUT);
238 GL_STD_BLEND(DST_OVER);
239 GL_STD_BLEND(SRC);
240 GL_STD_BLEND(SRC_ATOP);
241 GL_STD_BLEND(SRC_IN);
242 GL_STD_BLEND(SRC_OUT);
243 GL_STD_BLEND(SRC_OVER);
244 GL_STD_BLEND(AND);
245 GL_STD_BLEND(OR);
246 GL_STD_BLEND(XOR);
247 GL_VEC_BLEND(OVERLAY);
248 GL_STD_BLEND(SCREEN);
249 GL_VEC_BLEND(BURN);
250 GL_VEC_BLEND(DODGE);
251 GL_VEC_BLEND(HARDLIGHT);
252 GL_VEC_BLEND(SOFTLIGHT);
253 GL_VEC_BLEND(DIFFERENCE);
254
255 static const char *read_texture_frag =
256         "uniform sampler2D tex;\n"
257         "void main()\n"
258         "{\n"
259         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
260         "}\n";
261
262 static const char *multiply_mask4_frag =
263         "uniform sampler2D tex;\n"
264         "uniform sampler2D tex1;\n"
265         "uniform float scale;\n"
266         "void main()\n"
267         "{\n"
268         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
269         "       gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
270         "}\n";
271
272 static const char *multiply_mask3_frag =
273         "uniform sampler2D tex;\n"
274         "uniform sampler2D tex1;\n"
275         "uniform float scale;\n"
276         "uniform bool is_yuv;\n"
277         "void main()\n"
278         "{\n"
279         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
280         "       float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
281         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
282         "}\n";
283
284 static const char *multiply_yuvmask3_frag =
285         "uniform sampler2D tex;\n"
286         "uniform sampler2D tex1;\n"
287         "uniform float scale;\n"
288         "void main()\n"
289         "{\n"
290         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
291         "       float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
292         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
293         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
294         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
295         "}\n";
296
297 static const char *fade_rgba_frag =
298         "uniform sampler2D tex;\n"
299         "uniform float alpha;\n"
300         "void main()\n"
301         "{\n"
302         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
303         "       gl_FragColor.a *= alpha;\n"
304         "}\n";
305
306 static const char *fade_yuv_frag =
307         "uniform sampler2D tex;\n"
308         "uniform float alpha;\n"
309         "void main()\n"
310         "{\n"
311         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
312         "       gl_FragColor.r *= alpha;\n"
313         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
314         "       gl_FragColor.g *= alpha;\n"
315         "       gl_FragColor.b *= alpha;\n"
316         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
317         "}\n";
318
319 #endif
320
321
322 Playback3DCommand::Playback3DCommand()
323  : BC_SynchronousCommand()
324 {
325         canvas = 0;
326         is_nested = 0;
327 }
328
329 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
330 {
331         Playback3DCommand *ptr = (Playback3DCommand*)command;
332         this->canvas = ptr->canvas;
333         this->is_cleared = ptr->is_cleared;
334
335         this->in_x1 = ptr->in_x1;
336         this->in_y1 = ptr->in_y1;
337         this->in_x2 = ptr->in_x2;
338         this->in_y2 = ptr->in_y2;
339         this->out_x1 = ptr->out_x1;
340         this->out_y1 = ptr->out_y1;
341         this->out_x2 = ptr->out_x2;
342         this->out_y2 = ptr->out_y2;
343         this->alpha = ptr->alpha;
344         this->mode = ptr->mode;
345         this->interpolation_type = ptr->interpolation_type;
346
347         this->input = ptr->input;
348         this->start_position_project = ptr->start_position_project;
349         this->keyframe_set = ptr->keyframe_set;
350         this->keyframe = ptr->keyframe;
351         this->default_auto = ptr->default_auto;
352         this->plugin_client = ptr->plugin_client;
353         this->want_texture = ptr->want_texture;
354         this->is_nested = ptr->is_nested;
355         this->dst_cmodel = ptr->dst_cmodel;
356
357         BC_SynchronousCommand::copy_from(command);
358 }
359
360
361 ///static void glDebugCallback(GLenum src, GLenum typ, GLuint id,
362 ///     GLenum svy, GLsizei len, const GLchar* msg, void* dat)
363 //static void glDebugCallback(unsigned int src, unsigned int typ, unsigned int id,
364 //      unsigned int svy, int len, const char* msg, const void* dat)
365 //{
366 //      printf("glDebug: %d:%d; %d/%d %s\n",src,typ,id,svy,msg);
367 //}
368
369
370 Playback3D::Playback3D(MWindow *mwindow)
371  : BC_Synchronous()
372 {
373         this->mwindow = mwindow;
374         temp_texture = 0;
375         //Enabling OpenGL debug output on nVidia drivers
376 //      glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
377 //      glEnable(GL_DEBUG_OUTPUT);
378 //      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
379 //      glDebugMessageCallback(glDebugCallback, 0);
380 }
381
382 Playback3D::~Playback3D()
383 {
384 }
385
386
387
388
389 BC_SynchronousCommand* Playback3D::new_command()
390 {
391         return new Playback3DCommand;
392 }
393
394
395
396 void Playback3D::handle_command(BC_SynchronousCommand *command)
397 {
398 //printf("Playback3D::handle_command 1 %d\n", command->command);
399         switch(command->command)
400         {
401                 case Playback3DCommand::WRITE_BUFFER:
402                         write_buffer_sync((Playback3DCommand*)command);
403                         break;
404
405                 case Playback3DCommand::CLEAR_OUTPUT:
406                         clear_output_sync((Playback3DCommand*)command);
407                         break;
408
409                 case Playback3DCommand::CLEAR_INPUT:
410                         clear_input_sync((Playback3DCommand*)command);
411                         break;
412
413                 case Playback3DCommand::DO_CAMERA:
414                         do_camera_sync((Playback3DCommand*)command);
415                         break;
416
417                 case Playback3DCommand::OVERLAY:
418                         overlay_sync((Playback3DCommand*)command);
419                         break;
420
421                 case Playback3DCommand::DO_FADE:
422                         do_fade_sync((Playback3DCommand*)command);
423                         break;
424
425                 case Playback3DCommand::DO_MASK:
426                         do_mask_sync((Playback3DCommand*)command);
427                         break;
428
429                 case Playback3DCommand::PLUGIN:
430                         run_plugin_sync((Playback3DCommand*)command);
431                         break;
432
433                 case Playback3DCommand::COPY_FROM:
434                         copy_from_sync((Playback3DCommand*)command);
435                         break;
436
437                 case Playback3DCommand::CONVERT_CMODEL:
438                         convert_cmodel_sync((Playback3DCommand*)command);
439                         break;
440
441 //              case Playback3DCommand::DRAW_REFRESH:
442 //                      draw_refresh_sync((Playback3DCommand*)command);
443 //                      break;
444         }
445 //printf("Playback3D::handle_command 10\n");
446 }
447
448
449
450
451 void Playback3D::copy_from(Canvas *canvas,
452         VFrame *dst,
453         VFrame *src,
454         int want_texture)
455 {
456         Playback3DCommand command;
457         command.command = Playback3DCommand::COPY_FROM;
458         command.canvas = canvas;
459         command.frame = dst;
460         command.input = src;
461         command.want_texture = want_texture;
462         send_command(&command);
463 }
464
465 void Playback3D::copy_from_sync(Playback3DCommand *command)
466 {
467 #ifdef HAVE_GL
468         command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
469         BC_WindowBase *window = command->canvas->get_canvas();
470         if(window)
471         {
472                 window->lock_window("Playback3D:draw_refresh_sync");
473                 window->enable_opengl();
474                 int w = command->input->get_w();
475                 int h = command->input->get_h();
476
477                 if(command->input->get_opengl_state() == VFrame::SCREEN &&
478                         w == command->frame->get_w() && h == command->frame->get_h())
479                 {
480 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
481 // command->input->get_w(),
482 // command->input->get_h(),
483 // command->frame->get_w(),
484 // command->frame->get_h(),
485 // command->frame->get_color_model());
486 // With NVidia at least,
487                         if(w % 4)
488                         {
489                                 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
490                         }
491                         else
492 // Copy to texture
493                         if(command->want_texture)
494                         {
495 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
496 // Screen_to_texture requires the source pbuffer enabled.
497                                 command->input->enable_opengl();
498                                 command->frame->screen_to_texture();
499                                 command->frame->set_opengl_state(VFrame::TEXTURE);
500                         }
501                         else
502 // Copy to RAM
503                         {
504                                 command->input->to_texture();
505                                 command->input->bind_texture(0);
506                                 command->frame->enable_opengl();
507                                 command->frame->init_screen();
508                                 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
509                                         VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
510                                 if( shader > 0 ) {
511                                         glUseProgram(shader);
512                                         int variable = glGetUniformLocation(shader, "tex");
513                                         glUniform1i(variable, 0);
514                                         BC_GL_YUV_TO_RGB(shader);
515                                 }
516                                 else
517                                         glUseProgram(0);
518                                 command->input->draw_texture(1);
519                                 command->frame->screen_to_ram();
520                                 glUseProgram(0);
521                         }
522                 }
523                 else
524                 {
525                         printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
526                                 command->input->get_opengl_state(), w, h,
527                                 command->frame->get_w(), command->frame->get_h());
528                 }
529
530                 window->unlock_window();
531         }
532         command->canvas->unlock_canvas();
533 #endif
534 }
535
536
537
538
539 // void Playback3D::draw_refresh(Canvas *canvas,
540 //      VFrame *frame,
541 //      float in_x1,
542 //      float in_y1,
543 //      float in_x2,
544 //      float in_y2,
545 //      float out_x1,
546 //      float out_y1,
547 //      float out_x2,
548 //      float out_y2)
549 // {
550 //      Playback3DCommand command;
551 //      command.command = Playback3DCommand::DRAW_REFRESH;
552 //      command.canvas = canvas;
553 //      command.frame = frame;
554 //      command.in_x1 = in_x1;
555 //      command.in_y1 = in_y1;
556 //      command.in_x2 = in_x2;
557 //      command.in_y2 = in_y2;
558 //      command.out_x1 = out_x1;
559 //      command.out_y1 = out_y1;
560 //      command.out_x2 = out_x2;
561 //      command.out_y2 = out_y2;
562 //      send_command(&command);
563 // }
564 //
565 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
566 // {
567 //      command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
568 //      BC_WindowBase *window = command->canvas->get_canvas();
569 //      if(window)
570 //      {
571 //              window->lock_window("Playback3D:draw_refresh_sync");
572 //              window->enable_opengl();
573 //
574 // // Read output pbuffer back to RAM in project colormodel
575 // // RGB 8bit is fastest for OpenGL to read back.
576 //              command->frame->reallocate(0,
577 //                      0,
578 //                      0,
579 //                      0,
580 //                      command->frame->get_w(),
581 //                      command->frame->get_h(),
582 //                      BC_RGB888,
583 //                      -1);
584 //              command->frame->screen_to_ram();
585 //
586 //              window->clear_box(0,
587 //                                              0,
588 //                                              window->get_w(),
589 //                                              window->get_h());
590 //              window->draw_vframe(command->frame,
591 //                                                      (int)command->out_x1,
592 //                                                      (int)command->out_y1,
593 //                                                      (int)(command->out_x2 - command->out_x1),
594 //                                                      (int)(command->out_y2 - command->out_y1),
595 //                                                      (int)command->in_x1,
596 //                                                      (int)command->in_y1,
597 //                                                      (int)(command->in_x2 - command->in_x1),
598 //                                                      (int)(command->in_y2 - command->in_y1),
599 //                                                      0);
600 //
601 //              window->unlock_window();
602 //      }
603 //      command->canvas->unlock_canvas();
604 // }
605
606
607
608
609
610 void Playback3D::write_buffer(Canvas *canvas,
611         VFrame *frame,
612         float in_x1,
613         float in_y1,
614         float in_x2,
615         float in_y2,
616         float out_x1,
617         float out_y1,
618         float out_x2,
619         float out_y2,
620         int is_cleared)
621 {
622         Playback3DCommand command;
623         command.command = Playback3DCommand::WRITE_BUFFER;
624         command.canvas = canvas;
625         command.frame = frame;
626         command.in_x1 = in_x1;
627         command.in_y1 = in_y1;
628         command.in_x2 = in_x2;
629         command.in_y2 = in_y2;
630         command.out_x1 = out_x1;
631         command.out_y1 = out_y1;
632         command.out_x2 = out_x2;
633         command.out_y2 = out_y2;
634         command.is_cleared = is_cleared;
635         send_command(&command);
636 }
637
638
639 void Playback3D::write_buffer_sync(Playback3DCommand *command)
640 {
641         command->canvas->lock_canvas("Playback3D::write_buffer_sync");
642         if(command->canvas->get_canvas())
643         {
644                 BC_WindowBase *window = command->canvas->get_canvas();
645                 window->lock_window("Playback3D::write_buffer_sync");
646 // Update hidden cursor
647                 window->update_video_cursor();
648 // Make sure OpenGL is enabled first.
649                 window->enable_opengl();
650
651 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
652                 int flip_y = 0, frame_state = command->frame->get_opengl_state();
653                 switch( frame_state ) {
654 // Upload texture and composite to screen
655                         case VFrame::RAM:
656                                 flip_y = 1;
657                         case VFrame::SCREEN:
658                                 command->frame->to_texture();
659                                 window->enable_opengl();
660 // Composite texture to screen and swap buffer
661                         case VFrame::TEXTURE:
662                                 draw_output(command, flip_y);
663                                 break;
664                         default:
665                                 printf("Playback3D::write_buffer_sync unknown state\n");
666                                 break;
667                 }
668                 command->frame->set_opengl_state(frame_state);
669                 window->unlock_window();
670         }
671
672         command->canvas->unlock_canvas();
673 }
674
675
676
677 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
678 {
679 #ifdef HAVE_GL
680         int texture_id = command->frame->get_texture_id();
681         BC_WindowBase *window = command->canvas->get_canvas();
682
683 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
684 // texture_id,
685 // command->canvas->get_canvas());
686
687
688
689
690 // If virtual console is being used, everything in this function has
691 // already been done except the page flip.
692         if(texture_id >= 0)
693         {
694                 canvas_w = window->get_w();
695                 canvas_h = window->get_h();
696                 VFrame::init_screen(canvas_w, canvas_h);
697                 int color_model = command->frame->get_color_model();
698                 int is_yuv = BC_CModels::is_yuv(color_model);
699
700                 if(!command->is_cleared)
701                 {
702 // If we get here, the virtual console was not used.
703                         init_frame(command, 0);
704                 }
705
706 // Texture
707 // Undo any previous shader settings
708                 command->frame->bind_texture(0);
709
710 // Convert colormodel
711                 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
712                 if( shader > 0 ) {
713                         glUseProgram(shader);
714 // Set texture unit of the texture
715                         int variable = glGetUniformLocation(shader, "tex");
716                         glUniform1i(variable, 0);
717                         BC_GL_YUV_TO_RGB(shader);
718                 }
719
720                 if(BC_CModels::components(color_model) == 4)
721                 {
722                         glEnable(GL_BLEND);
723                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
724                 }
725
726                 command->frame->draw_texture(
727                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
728                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
729                         flip_y);
730
731
732 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
733 // command->in_x1,
734 // command->in_y1,
735 // command->in_x2,
736 // command->in_y2,
737 // command->out_x1,
738 // command->out_y1,
739 // command->out_x2,
740 // command->out_y2);
741
742                 glUseProgram(0);
743
744                 command->canvas->get_canvas()->flip_opengl();
745
746         }
747 #endif
748 }
749
750
751 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
752 {
753 #ifdef HAVE_GL
754         float gbuv = is_yuv ? 0.5 : 0.0;
755         glClearColor(0.0, gbuv, gbuv, 0.0);
756         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
757 #endif
758 }
759
760
761 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
762 {
763         Playback3DCommand command;
764         command.command = Playback3DCommand::CLEAR_OUTPUT;
765         command.canvas = canvas;
766         command.frame = output;
767         send_command(&command);
768 }
769
770 void Playback3D::clear_output_sync(Playback3DCommand *command)
771 {
772         command->canvas->lock_canvas("Playback3D::clear_output_sync");
773         if(command->canvas->get_canvas())
774         {
775                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
776 // If we get here, the virtual console is being used.
777                 command->canvas->get_canvas()->enable_opengl();
778                 int is_yuv = 0;
779
780 // Using pbuffer for refresh frame.
781                 if(command->frame)
782                 {
783                         command->frame->enable_opengl();
784                         int color_model = command->canvas->mwindow->edl->session->color_model;
785                         is_yuv = BC_CModels::is_yuv(color_model);
786                 }
787
788                 init_frame(command, is_yuv);
789                 command->canvas->get_canvas()->unlock_window();
790         }
791         command->canvas->unlock_canvas();
792 }
793
794
795 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
796 {
797         Playback3DCommand command;
798         command.command = Playback3DCommand::CLEAR_INPUT;
799         command.canvas = canvas;
800         command.frame = frame;
801         send_command(&command);
802 }
803
804 void Playback3D::clear_input_sync(Playback3DCommand *command)
805 {
806         command->canvas->lock_canvas("Playback3D::clear_output_sync");
807         if(command->canvas->get_canvas())
808         {
809                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
810                 command->canvas->get_canvas()->enable_opengl();
811                 command->frame->enable_opengl();
812                 command->frame->clear_pbuffer();
813                 command->frame->set_opengl_state(VFrame::SCREEN);
814                 command->canvas->get_canvas()->unlock_window();
815         }
816         command->canvas->unlock_canvas();
817 }
818
819 void Playback3D::do_camera(Canvas *canvas,
820         VFrame *output,
821         VFrame *input,
822         float in_x1,
823         float in_y1,
824         float in_x2,
825         float in_y2,
826         float out_x1,
827         float out_y1,
828         float out_x2,
829         float out_y2)
830 {
831         Playback3DCommand command;
832         command.command = Playback3DCommand::DO_CAMERA;
833         command.canvas = canvas;
834         command.input = input;
835         command.frame = output;
836         command.in_x1 = in_x1;
837         command.in_y1 = in_y1;
838         command.in_x2 = in_x2;
839         command.in_y2 = in_y2;
840         command.out_x1 = out_x1;
841         command.out_y1 = out_y1;
842         command.out_x2 = out_x2;
843         command.out_y2 = out_y2;
844         send_command(&command);
845 }
846
847 void Playback3D::do_camera_sync(Playback3DCommand *command)
848 {
849         command->canvas->lock_canvas("Playback3D::do_camera_sync");
850         if(command->canvas->get_canvas())
851         {
852                 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
853                 command->canvas->get_canvas()->enable_opengl();
854
855                 command->input->to_texture();
856                 command->frame->enable_opengl();
857                 command->frame->init_screen();
858                 command->frame->clear_pbuffer();
859
860                 command->input->bind_texture(0);
861 // Must call draw_texture in input frame to get the texture coordinates right.
862
863 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
864 // command->in_x1,
865 // command->in_y2,
866 // command->in_x2,
867 // command->in_y1,
868 // command->out_x1,
869 // (float)command->input->get_h() - command->out_y1,
870 // command->out_x2,
871 // (float)command->input->get_h() - command->out_y2);
872                 command->input->draw_texture(
873                         command->in_x1, command->in_y2,
874                         command->in_x2, command->in_y1,
875                         command->out_x1,
876                         (float)command->frame->get_h() - command->out_y1,
877                         command->out_x2,
878                         (float)command->frame->get_h() - command->out_y2);
879
880
881                 command->frame->set_opengl_state(VFrame::SCREEN);
882                 command->frame->screen_to_ram();
883                 command->canvas->get_canvas()->unlock_window();
884         }
885         command->canvas->unlock_canvas();
886 }
887
888 void Playback3D::overlay(Canvas *canvas, VFrame *input,
889         float in_x1, float in_y1, float in_x2, float in_y2,
890         float out_x1, float out_y1, float out_x2, float out_y2,
891         float alpha, int mode, int interpolation_type,
892         VFrame *output, int is_nested)
893 {
894         Playback3DCommand command;
895         command.command = Playback3DCommand::OVERLAY;
896         command.canvas = canvas;
897         command.frame = output;
898         command.input = input;
899         command.in_x1 = in_x1;
900         command.in_y1 = in_y1;
901         command.in_x2 = in_x2;
902         command.in_y2 = in_y2;
903         command.out_x1 = out_x1;
904         command.out_y1 = out_y1;
905         command.out_x2 = out_x2;
906         command.out_y2 = out_y2;
907         command.alpha = alpha;
908         command.mode = mode;
909         command.interpolation_type = interpolation_type;
910         command.is_nested = is_nested;
911         send_command(&command);
912 }
913
914 void Playback3D::overlay_sync(Playback3DCommand *command)
915 {
916 #ifdef HAVE_GL
917 // To do these operations, we need to copy the input buffer to a texture
918 // and blend 2 textures in a shader
919         static const char * const overlay_shaders[TRANSFER_TYPES] = {
920                 blend_NORMAL_frag,      // TRANSFER_NORMAL
921                 blend_ADDITION_frag,    // TRANSFER_ADDITION
922                 blend_SUBTRACT_frag,    // TRANSFER_SUBTRACT
923                 blend_MULTIPLY_frag,    // TRANSFER_MULTIPLY
924                 blend_DIVIDE_frag,      // TRANSFER_DIVIDE
925                 blend_REPLACE_frag,     // TRANSFER_REPLACE
926                 blend_MAX_frag,         // TRANSFER_MAX
927                 blend_MIN_frag,         // TRANSFER_MIN
928                 blend_DARKEN_frag,      // TRANSFER_DARKEN
929                 blend_LIGHTEN_frag,     // TRANSFER_LIGHTEN
930                 blend_DST_frag,         // TRANSFER_DST
931                 blend_DST_ATOP_frag,    // TRANSFER_DST_ATOP
932                 blend_DST_IN_frag,      // TRANSFER_DST_IN
933                 blend_DST_OUT_frag,     // TRANSFER_DST_OUT
934                 blend_DST_OVER_frag,    // TRANSFER_DST_OVER
935                 blend_SRC_frag,         // TRANSFER_SRC
936                 blend_SRC_ATOP_frag,    // TRANSFER_SRC_ATOP
937                 blend_SRC_IN_frag,      // TRANSFER_SRC_IN
938                 blend_SRC_OUT_frag,     // TRANSFER_SRC_OUT
939                 blend_SRC_OVER_frag,    // TRANSFER_SRC_OVER
940                 blend_AND_frag,         // TRANSFER_AND
941                 blend_OR_frag,          // TRANSFER_OR
942                 blend_XOR_frag,         // TRANSFER_XOR
943                 blend_OVERLAY_frag,     // TRANSFER_OVERLAY
944                 blend_SCREEN_frag,      // TRANSFER_SCREEN
945                 blend_BURN_frag,        // TRANSFER_BURN
946                 blend_DODGE_frag,       // TRANSFER_DODGE
947                 blend_HARDLIGHT_frag,   // TRANSFER_HARDLIGHT
948                 blend_SOFTLIGHT_frag,   // TRANSFER_SOFTLIGHT
949                 blend_DIFFERENCE_frag,  // TRANSFER_DIFFERENCE
950         };
951
952         command->canvas->lock_canvas("Playback3D::overlay_sync");
953         if(command->canvas->get_canvas()) {
954                 BC_WindowBase *window = command->canvas->get_canvas();
955                 window->lock_window("Playback3D::overlay_sync");
956 // Make sure OpenGL is enabled first.
957                 window->enable_opengl();
958                 window->update_video_cursor();
959
960                 glColor4f(1, 1, 1, 1);
961                 glDisable(GL_BLEND);
962
963
964 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
965                 switch( command->input->get_opengl_state() ) {
966 // Upload texture and composite to screen
967                 case VFrame::RAM:
968                         command->input->to_texture();
969                         break;
970 // Just composite texture to screen
971                 case VFrame::TEXTURE:
972                         break;
973 // read from PBuffer to texture, then composite texture to screen
974                 case VFrame::SCREEN:
975                         command->input->enable_opengl();
976                         command->input->screen_to_texture();
977                         break;
978                 default:
979                         printf("Playback3D::overlay_sync unknown state\n");
980                         break;
981                 }
982
983                 if(command->frame) {
984 // Render to PBuffer
985                         command->frame->enable_opengl();
986                         command->frame->set_opengl_state(VFrame::SCREEN);
987                         canvas_w = command->frame->get_w();
988                         canvas_h = command->frame->get_h();
989                 }
990                 else {
991 // Render to canvas
992                         window->enable_opengl();
993                         canvas_w = window->get_w();
994                         canvas_h = window->get_h();
995                 }
996
997
998                 const char *shader_stack[16];
999                 memset(shader_stack,0, sizeof(shader_stack));
1000                 int total_shaders = 0, need_matrix = 0;
1001
1002                 VFrame::init_screen(canvas_w, canvas_h);
1003
1004 // Enable texture
1005                 command->input->bind_texture(0);
1006
1007 // Convert colormodel to RGB if not nested.
1008 // The color model setting in the output frame is ignored.
1009 //              if( command->is_nested <= 0 &&  // not nested
1010 //                  BC_CModels::is_yuv(command->input->get_color_model()) ) {
1011 //                      need_matrix = 1;
1012 //                      shader_stack[total_shaders++] = yuv_to_rgb_frag;
1013 //              }
1014
1015 // get the shaders
1016 #define add_shader(s) \
1017   if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1018   shader_stack[total_shaders++] = s
1019
1020                 switch(command->mode) {
1021                 case TRANSFER_REPLACE:
1022 // This requires overlaying an alpha multiplied image on a black screen.
1023                         if( command->input->get_texture_components() != 4 ) break;
1024                         add_shader(overlay_shaders[command->mode]);
1025                         break;
1026                 default:
1027                         enable_overlay_texture(command);
1028                         add_shader(overlay_shaders[command->mode]);
1029                         break;
1030                 }
1031
1032 // if to flatten alpha
1033 //              if( command->is_nested < 0 ) {
1034 //                      switch(command->input->get_color_model()) {
1035 //// yuv has already been converted to rgb
1036 //                      case BC_YUVA8888:
1037 //                      case BC_RGBA_FLOAT:
1038 //                      case BC_RGBA8888:
1039 //                              add_shader(rgba_to_rgb_flatten);
1040 //                              break;
1041 //                      }
1042 //              }
1043
1044 // run the shaders
1045                 add_shader(0);
1046                 unsigned int shader = !shader_stack[0] ? 0 :
1047                         VFrame::make_shader(shader_stack);
1048                 if( shader > 0 ) {
1049                         glUseProgram(shader);
1050                         if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1051 // Set texture unit of the texture
1052                         glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1053 // Set texture unit of the temp texture
1054                         glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1055 // Set alpha
1056                         int variable = glGetUniformLocation(shader, "alpha");
1057                         glUniform1f(variable, command->alpha);
1058 // Set dimensions of the temp texture
1059                         if(temp_texture)
1060                                 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1061                                         (float)temp_texture->get_texture_w(),
1062                                         (float)temp_texture->get_texture_h());
1063                 }
1064                 else
1065                         glUseProgram(0);
1066
1067
1068 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1069 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1070 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1071
1072                 command->input->draw_texture(
1073                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1074                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1075                         !command->is_nested);
1076                 glUseProgram(0);
1077
1078 // Delete temp texture
1079                 if(temp_texture) {
1080                         delete temp_texture;
1081                         temp_texture = 0;
1082                         glActiveTexture(GL_TEXTURE1);
1083                         glDisable(GL_TEXTURE_2D);
1084                 }
1085                 glActiveTexture(GL_TEXTURE0);
1086                 glDisable(GL_TEXTURE_2D);
1087
1088                 window->unlock_window();
1089         }
1090         command->canvas->unlock_canvas();
1091 #endif
1092 }
1093
1094 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1095 {
1096 #ifdef HAVE_GL
1097         glDisable(GL_BLEND);
1098
1099         glActiveTexture(GL_TEXTURE1);
1100         BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1101                 command->input->get_color_model());
1102         temp_texture->bind(1);
1103
1104 // Read canvas into texture
1105         glReadBuffer(GL_BACK);
1106         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1107 #endif
1108 }
1109
1110
1111 void Playback3D::do_mask(Canvas *canvas,
1112         VFrame *output,
1113         int64_t start_position_project,
1114         MaskAutos *keyframe_set,
1115         MaskAuto *keyframe,
1116         MaskAuto *default_auto)
1117 {
1118         Playback3DCommand command;
1119         command.command = Playback3DCommand::DO_MASK;
1120         command.canvas = canvas;
1121         command.frame = output;
1122         command.start_position_project = start_position_project;
1123         command.keyframe_set = keyframe_set;
1124         command.keyframe = keyframe;
1125         command.default_auto = default_auto;
1126
1127         send_command(&command);
1128 }
1129
1130
1131
1132 #ifdef HAVE_GL
1133 struct Vertex : ListItem<Vertex>
1134 {
1135         GLdouble c[3];
1136 };
1137 // this list is only used from the main thread, no locking needed
1138 // this must be a list so that pointers to allocated entries remain valid
1139 // when new entries are added
1140 static List<Vertex> *vertex_cache = 0;
1141
1142 static void combine_callback(GLdouble coords[3],
1143         GLdouble *vertex_data[4],
1144         GLfloat weight[4],
1145         GLdouble **dataOut)
1146 {
1147 // can't use malloc here; GLU doesn't delete the memory for us!
1148         Vertex* vertex = vertex_cache->append();
1149         vertex->c[0] = coords[0];
1150         vertex->c[1] = coords[1];
1151         vertex->c[2] = coords[2];
1152 // we don't need to interpolate anything
1153
1154         *dataOut = &vertex->c[0];
1155 }
1156 #endif
1157
1158
1159 void Playback3D::do_mask_sync(Playback3DCommand *command)
1160 {
1161 #ifdef HAVE_GL
1162         command->canvas->lock_canvas("Playback3D::do_mask_sync");
1163         if(command->canvas->get_canvas())
1164         {
1165                 BC_WindowBase *window = command->canvas->get_canvas();
1166                 window->lock_window("Playback3D::do_mask_sync");
1167                 window->enable_opengl();
1168
1169                 switch(command->frame->get_opengl_state())
1170                 {
1171                         case VFrame::RAM:
1172 // Time to upload to the texture
1173                                 command->frame->to_texture();
1174                                 break;
1175
1176                         case VFrame::SCREEN:
1177 // Read back from PBuffer
1178 // Bind context to pbuffer
1179                                 command->frame->enable_opengl();
1180                                 command->frame->screen_to_texture();
1181                                 break;
1182                 }
1183
1184
1185
1186 // Create PBuffer and draw the mask on it
1187                 command->frame->enable_opengl();
1188
1189 // Initialize coordinate system
1190                 int w = command->frame->get_w();
1191                 int h = command->frame->get_h();
1192                 command->frame->init_screen();
1193
1194 // Clear screen
1195                 glDisable(GL_TEXTURE_2D);
1196                 if(command->default_auto->mode == MASK_MULTIPLY_ALPHA)
1197                 {
1198                         glClearColor(0.0, 0.0, 0.0, 0.0);
1199                         glColor4f((float)command->keyframe->value / 100,
1200                                 (float)command->keyframe->value / 100,
1201                                 (float)command->keyframe->value / 100,
1202                                 1.0);
1203                 }
1204                 else
1205                 {
1206                         glClearColor(1.0, 1.0, 1.0, 1.0);
1207                         glColor4f((float)1.0 - (float)command->keyframe->value / 100,
1208                                 (float)1.0 - (float)command->keyframe->value / 100,
1209                                 (float)1.0 - (float)command->keyframe->value / 100,
1210                                 1.0);
1211                 }
1212                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1213
1214
1215 // Draw mask with scaling to simulate feathering
1216                 GLUtesselator *tesselator = gluNewTess();
1217                 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
1218                 gluTessCallback(tesselator, GLU_TESS_VERTEX, (GLvoid (*) ( )) &glVertex3dv);
1219                 gluTessCallback(tesselator, GLU_TESS_BEGIN, (GLvoid (*) ( )) &glBegin);
1220                 gluTessCallback(tesselator, GLU_TESS_END, (GLvoid (*) ( )) &glEnd);
1221                 gluTessCallback(tesselator, GLU_TESS_COMBINE, (GLvoid (*) ( ))&combine_callback);
1222
1223                 vertex_cache = new List<Vertex>;
1224
1225
1226 // Draw every submask as a new polygon
1227                 int total_submasks = command->keyframe_set->total_submasks(
1228                         command->start_position_project,
1229                         PLAY_FORWARD);
1230                 float scale = command->keyframe->feather + 1;
1231                 int display_list = glGenLists(1);
1232                 glNewList(display_list, GL_COMPILE);
1233                 for(int k = 0; k < total_submasks; k++)
1234                 {
1235                         gluTessBeginPolygon(tesselator, NULL);
1236                         gluTessBeginContour(tesselator);
1237                         ArrayList<MaskPoint*> *points = new ArrayList<MaskPoint*>;
1238                         command->keyframe_set->get_points(points,
1239                                 k,
1240                                 command->start_position_project,
1241                                 PLAY_FORWARD);
1242
1243                         int first_point = 0;
1244 // Need to tabulate every vertex in persistent memory because
1245 // gluTessVertex doesn't copy them.
1246                         ArrayList<GLdouble*> coords;
1247                         coords.set_array_delete();
1248                         for(int i = 0; i < points->total; i++)
1249                         {
1250                                 MaskPoint *point1 = points->values[i];
1251                                 MaskPoint *point2 = (i >= points->total - 1) ?
1252                                         points->values[0] :
1253                                         points->values[i + 1];
1254
1255                                 float x, y;
1256                                 int segments = 0;
1257                                 if(point1->control_x2 == 0 &&
1258                                         point1->control_y2 == 0 &&
1259                                         point2->control_x1 == 0 &&
1260                                         point2->control_y1 == 0)
1261                                         segments = 1;
1262
1263                                 float x0 = point1->x;
1264                                 float y0 = point1->y;
1265                                 float x1 = point1->x + point1->control_x2;
1266                                 float y1 = point1->y + point1->control_y2;
1267                                 float x2 = point2->x + point2->control_x1;
1268                                 float y2 = point2->y + point2->control_y1;
1269                                 float x3 = point2->x;
1270                                 float y3 = point2->y;
1271
1272                                 // forward differencing bezier curves implementation taken from GPL code at
1273                                 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1274
1275                                 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1276
1277                                 // [-1 +3 -3 +1]
1278                                 // [+3 -6 +3  0]
1279                                 // [-3 +3  0  0]
1280                                 // [+1  0  0  0]
1281
1282                                 cx3 = -  x0 + 3*x1 - 3*x2 + x3;
1283                                 cx2 =  3*x0 - 6*x1 + 3*x2;
1284                                 cx1 = -3*x0 + 3*x1;
1285                                 cx0 =    x0;
1286
1287                                 cy3 = -  y0 + 3*y1 - 3*y2 + y3;
1288                                 cy2 =  3*y0 - 6*y1 + 3*y2;
1289                                 cy1 = -3*y0 + 3*y1;
1290                                 cy0 =    y0;
1291
1292                                 // This equation is from Graphics Gems I.
1293                                 //
1294                                 // The idea is that since we're approximating a cubic curve with lines,
1295                                 // any error we incur is due to the curvature of the line, which we can
1296                                 // estimate by calculating the maximum acceleration of the curve.  For
1297                                 // a cubic, the acceleration (second derivative) is a line, meaning that
1298                                 // the absolute maximum acceleration must occur at either the beginning
1299                                 // (|c2|) or the end (|c2+c3|).  Our bounds here are a little more
1300                                 // conservative than that, but that's okay.
1301                                 if (segments == 0)
1302                                 {
1303                                         float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1304                                         float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1305
1306                                         float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1307                                         float h = 1.0;
1308
1309                                         if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1310                                         segments = int(1/h);
1311                                 }
1312
1313                                 for(int j = 0; j <= segments; j++)
1314                                 {
1315                                         float t = (float)j / segments;
1316                                         x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1317                                         y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1318
1319                                         if(j > 0 || first_point)
1320                                         {
1321                                                 GLdouble *coord = new GLdouble[3];
1322                                                 coord[0] = x / scale;
1323                                                 coord[1] = -h + y / scale;
1324                                                 coord[2] = 0;
1325                                                 coords.append(coord);
1326                                                 first_point = 0;
1327                                         }
1328                                 }
1329                         }
1330
1331 // Now that we know the total vertices, send them to GLU
1332                         for(int i = 0; i < coords.total; i++)
1333                                 gluTessVertex(tesselator, coords.values[i], coords.values[i]);
1334
1335                         gluTessEndContour(tesselator);
1336                         gluTessEndPolygon(tesselator);
1337                         points->remove_all_objects();
1338                         delete points;
1339                         coords.remove_all_objects();
1340                 }
1341                 glEndList();
1342                 glCallList(display_list);
1343                 glDeleteLists(display_list, 1);
1344                 gluDeleteTess(tesselator);
1345
1346                 delete vertex_cache;
1347                 vertex_cache = 0;
1348
1349                 glColor4f(1, 1, 1, 1);
1350
1351
1352 // Read mask into temporary texture.
1353 // For feathering, just read the part of the screen after the downscaling.
1354
1355
1356                 float w_scaled = w / scale;
1357                 float h_scaled = h / scale;
1358 // Don't vary the texture size according to scaling because that
1359 // would waste memory.
1360 // This enables and binds the temporary texture.
1361                 glActiveTexture(GL_TEXTURE1);
1362                 BC_Texture::new_texture(&temp_texture,
1363                         w,
1364                         h,
1365                         command->frame->get_color_model());
1366                 temp_texture->bind(1);
1367                 glReadBuffer(GL_BACK);
1368
1369 // Need to add extra size to fill in the bottom right
1370                 glCopyTexSubImage2D(GL_TEXTURE_2D,
1371                         0,
1372                         0,
1373                         0,
1374                         0,
1375                         0,
1376                         (int)MIN(w_scaled + 2, w),
1377                         (int)MIN(h_scaled + 2, h));
1378
1379                 command->frame->bind_texture(0);
1380
1381
1382 // For feathered masks, use a shader to multiply.
1383 // For unfeathered masks, we could use a stencil buffer
1384 // for further optimization but we also need a YUV algorithm.
1385                 unsigned int frag_shader = 0;
1386                 switch(temp_texture->get_texture_components()) {
1387                 case 3:
1388                         frag_shader = VFrame::make_shader(0,
1389                                 command->frame->get_color_model() == BC_YUV888 ?
1390                                         multiply_yuvmask3_frag : multiply_mask3_frag,
1391                                 0);
1392                         break;
1393                 case 4:
1394                         frag_shader = VFrame::make_shader(0, multiply_mask4_frag, 0);
1395                         break;
1396                 }
1397
1398                 if( frag_shader ) {
1399                         int variable;
1400                         glUseProgram(frag_shader);
1401                         if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1402                                 glUniform1i(variable, 0);
1403                         if((variable = glGetUniformLocation(frag_shader, "tex1")) >= 0)
1404                                 glUniform1i(variable, 1);
1405                         if((variable = glGetUniformLocation(frag_shader, "scale")) >= 0)
1406                                 glUniform1f(variable, scale);
1407                 }
1408
1409
1410
1411 // Write texture to PBuffer with multiply and scaling for feather.
1412
1413
1414                 command->frame->draw_texture(0, 0, w, h, 0, 0, w, h);
1415                 command->frame->set_opengl_state(VFrame::SCREEN);
1416
1417
1418 // Disable temp texture
1419                 glUseProgram(0);
1420
1421                 glActiveTexture(GL_TEXTURE1);
1422                 glDisable(GL_TEXTURE_2D);
1423                 delete temp_texture;
1424                 temp_texture = 0;
1425
1426                 glActiveTexture(GL_TEXTURE0);
1427                 glDisable(GL_TEXTURE_2D);
1428
1429 // Default drawable
1430                 window->enable_opengl();
1431                 window->unlock_window();
1432         }
1433         command->canvas->unlock_canvas();
1434 #endif
1435 }
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446 void Playback3D::convert_cmodel(Canvas *canvas,
1447         VFrame *output,
1448         int dst_cmodel)
1449 {
1450 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1451         int src_cmodel = output->get_color_model();
1452         if(
1453                 (output->get_opengl_state() == VFrame::TEXTURE ||
1454                 output->get_opengl_state() == VFrame::SCREEN) &&
1455 // OpenGL has no floating point.
1456                 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1457                   (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1458                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1459                   (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1460 // OpenGL sets alpha to 1 on import
1461                   (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1462                   (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1463                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1464                 ) return;
1465
1466
1467
1468         Playback3DCommand command;
1469         command.command = Playback3DCommand::CONVERT_CMODEL;
1470         command.canvas = canvas;
1471         command.frame = output;
1472         command.dst_cmodel = dst_cmodel;
1473         send_command(&command);
1474 }
1475
1476 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1477 {
1478 #ifdef HAVE_GL
1479         command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1480
1481         if( command->canvas->get_canvas() ) {
1482                 BC_WindowBase *window = command->canvas->get_canvas();
1483                 window->lock_window("Playback3D::convert_cmodel_sync");
1484                 window->enable_opengl();
1485
1486 // Import into hardware
1487                 command->frame->enable_opengl();
1488                 command->frame->init_screen();
1489                 command->frame->to_texture();
1490
1491 // Colormodel permutation
1492                 int src_cmodel = command->frame->get_color_model();
1493                 int dst_cmodel = command->dst_cmodel;
1494                 typedef struct {
1495                         int src, dst, typ;
1496                         const char *shader;
1497                 } cmodel_shader_table_t;
1498                 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1499                 int type = -1;
1500                 static cmodel_shader_table_t cmodel_shader_table[]  = {
1501                         { BC_RGB888,    BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1502                         { BC_RGB888,    BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1503                         { BC_RGBA8888,  BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1504                         { BC_RGBA8888,  BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1505                         { BC_RGBA8888,  BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1506                         { BC_RGBA8888,  BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1507                         { BC_RGB_FLOAT, BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1508                         { BC_RGB_FLOAT, BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1509                         { BC_RGBA_FLOAT,BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1510                         { BC_RGBA_FLOAT,BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1511                         { BC_RGBA_FLOAT,BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1512                         { BC_RGBA_FLOAT,BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1513                         { BC_YUV888,    BC_RGB888,      yuv_to_rgb, yuv_to_rgb_frag  },
1514                         { BC_YUV888,    BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1515                         { BC_YUV888,    BC_RGB_FLOAT,   yuv_to_rgb, yuv_to_rgb_frag  },
1516                         { BC_YUV888,    BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1517                         { BC_YUVA8888,  BC_RGB888,      yuv_to_rgb, yuva_to_rgb_frag },
1518                         { BC_YUVA8888,  BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1519                         { BC_YUVA8888,  BC_RGB_FLOAT,   yuv_to_rgb, yuva_to_rgb_frag },
1520                         { BC_YUVA8888,  BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1521                         { BC_YUVA8888,  BC_YUV888,      yuv_to_yuv, yuva_to_yuv_frag },
1522                 };
1523
1524                 const char *shader = 0;
1525                 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1526                 for( int i=0; i<table_size; ++i ) {
1527                         if( cmodel_shader_table[i].src == src_cmodel &&
1528                             cmodel_shader_table[i].dst == dst_cmodel ) {
1529                                 shader = cmodel_shader_table[i].shader;
1530                                 type = cmodel_shader_table[i].typ;
1531                                 break;
1532                         }
1533                 }
1534
1535 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1536 // __LINE__,
1537 // command->frame->get_color_model(),
1538 // command->dst_cmodel,
1539 // shader);
1540
1541                 const char *shader_stack[9];
1542                 memset(shader_stack,0, sizeof(shader_stack));
1543                 int current_shader = 0;
1544
1545                 if( shader ) {
1546 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1547                         shader_stack[current_shader++] = shader;
1548                         shader_stack[current_shader] = 0;
1549                         unsigned int shader_id = VFrame::make_shader(shader_stack);
1550
1551                         command->frame->bind_texture(0);
1552                         glUseProgram(shader_id);
1553
1554                         glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1555                         switch( type ) {
1556                         case rgb_to_yuv:
1557                                 BC_GL_RGB_TO_YUV(shader_id);
1558                                 break;
1559                         case yuv_to_rgb:
1560                                 BC_GL_YUV_TO_RGB(shader_id);
1561                                 break;
1562                         }
1563
1564                         command->frame->draw_texture();
1565                         if(shader) glUseProgram(0);
1566                         command->frame->set_opengl_state(VFrame::SCREEN);
1567                 }
1568
1569                 window->unlock_window();
1570         }
1571
1572         command->canvas->unlock_canvas();
1573 #endif // HAVE_GL
1574 }
1575
1576 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1577 {
1578         Playback3DCommand command;
1579         command.command = Playback3DCommand::DO_FADE;
1580         command.canvas = canvas;
1581         command.frame = frame;
1582         command.alpha = fade;
1583         send_command(&command);
1584 }
1585
1586 void Playback3D::do_fade_sync(Playback3DCommand *command)
1587 {
1588 #ifdef HAVE_GL
1589         command->canvas->lock_canvas("Playback3D::do_mask_sync");
1590         if(command->canvas->get_canvas())
1591         {
1592                 BC_WindowBase *window = command->canvas->get_canvas();
1593                 window->lock_window("Playback3D::do_fade_sync");
1594                 window->enable_opengl();
1595
1596                 switch(command->frame->get_opengl_state())
1597                 {
1598                         case VFrame::RAM:
1599                                 command->frame->to_texture();
1600                                 break;
1601
1602                         case VFrame::SCREEN:
1603 // Read back from PBuffer
1604 // Bind context to pbuffer
1605                                 command->frame->enable_opengl();
1606                                 command->frame->screen_to_texture();
1607                                 break;
1608                 }
1609
1610
1611                 command->frame->enable_opengl();
1612                 command->frame->init_screen();
1613                 command->frame->bind_texture(0);
1614
1615 //              glClearColor(0.0, 0.0, 0.0, 0.0);
1616 //              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1617                 glDisable(GL_BLEND);
1618                 unsigned int frag_shader = 0;
1619                 switch(command->frame->get_color_model())
1620                 {
1621 // For the alpha colormodels, the native function seems to multiply the
1622 // components by the alpha instead of just the alpha.
1623                         case BC_RGBA8888:
1624                         case BC_RGBA_FLOAT:
1625                         case BC_YUVA8888:
1626                                 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1627                                 break;
1628
1629                         case BC_RGB888:
1630                                 glEnable(GL_BLEND);
1631                                 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1632                                 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1633                                 break;
1634
1635
1636                         case BC_YUV888:
1637                                 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1638                                 break;
1639                 }
1640
1641
1642                 if( frag_shader ) {
1643                         glUseProgram(frag_shader);
1644                         int variable;
1645                         if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1646                                 glUniform1i(variable, 0);
1647                         if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1648                                 glUniform1f(variable, command->alpha);
1649                 }
1650
1651                 command->frame->draw_texture();
1652                 command->frame->set_opengl_state(VFrame::SCREEN);
1653
1654                 if(frag_shader)
1655                 {
1656                         glUseProgram(0);
1657                 }
1658
1659                 glColor4f(1, 1, 1, 1);
1660                 glDisable(GL_BLEND);
1661
1662                 window->unlock_window();
1663         }
1664         command->canvas->unlock_canvas();
1665 #endif
1666 }
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1679 {
1680         Playback3DCommand command;
1681         command.command = Playback3DCommand::PLUGIN;
1682         command.canvas = canvas;
1683         command.plugin_client = client;
1684         return send_command(&command);
1685 }
1686
1687 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1688 {
1689         command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1690         if(command->canvas->get_canvas())
1691         {
1692                 BC_WindowBase *window = command->canvas->get_canvas();
1693                 window->lock_window("Playback3D::run_plugin_sync");
1694                 window->enable_opengl();
1695
1696                 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1697
1698                 window->unlock_window();
1699         }
1700         command->canvas->unlock_canvas();
1701 }
1702
1703