add binfolder path relative filters, fix gbrp color model, vwdw timebar tweaks, title...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / resourcepixmap.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 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
23
24 #include "arender.h"
25 #include "aedit.h"
26 #include "asset.h"
27 #include "asset.inc"
28 #include "automation.h"
29 #include "autos.h"
30 #include "bcsignals.h"
31 #include "cache.h"
32 #include "clip.h"
33 #include "bccmodels.h"
34 #include "datatype.h"
35 #include "edit.h"
36 #include "edits.h"
37 #include "edl.h"
38 #include "edlsession.h"
39 #include "file.h"
40 #include "filesystem.h"
41 #include "floatauto.h"
42 #include "floatautos.h"
43 #include "framecache.h"
44 #include "indexfile.h"
45 #include "language.h"
46 #include "localsession.h"
47 #include "mwindow.h"
48 #include "mwindowgui.h"
49 #include "renderengine.h"
50 #include "resourcethread.h"
51 #include "resourcepixmap.h"
52 #include "samples.h"
53 #include "theme.h"
54 #include "timelinepane.h"
55 #include "track.h"
56 #include "trackcanvas.h"
57 #include "transportque.h"
58 #include "vedit.h"
59 #include "vframe.h"
60 #include "wavecache.h"
61
62
63 ResourcePixmap::ResourcePixmap(MWindow *mwindow,
64         MWindowGUI *gui,
65         Edit *edit,
66         int pane_number,
67         int w,
68         int h)
69  : BC_Pixmap(gui, w, h)
70 {
71         reset();
72
73         this->mwindow = mwindow;
74         this->gui = gui;
75         this->pane_number = pane_number;
76         startsource = edit->startsource;
77         data_type = edit->track->data_type;
78         if( edit->asset ) {
79                 source_framerate = edit->asset->frame_rate;
80                 source_samplerate = edit->asset->sample_rate;
81         }
82         else
83         if( edit->nested_edl ) {
84                 source_framerate = edit->nested_edl->session->frame_rate;
85                 source_samplerate = edit->nested_edl->session->sample_rate;
86         }
87
88         project_framerate = edit->edl->session->frame_rate;
89         project_samplerate = edit->edl->session->sample_rate;
90         edit_id = edit->id;  pixmap_w = w;  pixmap_h = h;
91 }
92
93 ResourcePixmap::~ResourcePixmap()
94 {
95 }
96
97
98 void ResourcePixmap::reset()
99 {
100         edit_x = 0;
101         pixmap_x = 0;
102         pixmap_w = 0;
103         pixmap_h = 0;
104         zoom_sample = 0;
105         zoom_track = 0;
106         zoom_y = 0;
107         visible = 1;
108 }
109
110 void ResourcePixmap::resize(int w, int h)
111 {
112         int new_w = (w > get_w()) ? w : get_w();
113         int new_h = (h > get_h()) ? h : get_h();
114
115         BC_Pixmap::resize(new_w, new_h);
116 }
117
118
119 void ResourcePixmap::draw_data(TrackCanvas *canvas,
120         Edit *edit,
121         int64_t edit_x,
122         int64_t edit_w,
123         int64_t pixmap_x,
124         int64_t pixmap_w,
125         int64_t pixmap_h,
126         int mode,
127         int indexes_only)
128 {
129 // Get new areas to fill in relative to pixmap
130 // Area to redraw relative to pixmap
131         int refresh_x = 0;
132         int refresh_w = 0;
133
134 // Ignore if called by resourcethread.
135 //      if( mode == IGNORE_THREAD ) return;
136
137         int y = 0;
138         if( mwindow->edl->session->show_titles )
139                 y += mwindow->theme->get_image("title_bg_data")->get_h();
140         Track *track = edit->edits->track;
141
142
143 // If want indexes only & index can't be drawn, don't do anything.
144         int need_redraw = 0;
145         int64_t index_zoom = 0;
146         Indexable *indexable = 0;
147         if( edit->asset ) indexable = edit->asset;
148         if( edit->nested_edl ) indexable = edit->nested_edl;
149         if( indexable && indexes_only ) {
150                 IndexFile indexfile(mwindow, indexable);
151                 if( !indexfile.open_index() ) {
152                         index_zoom = indexable->index_state->index_zoom;
153                         indexfile.close_index();
154                 }
155
156                 if( index_zoom ) {
157                         if( data_type == TRACK_AUDIO ) {
158                                 double asset_over_session = (double)indexable->get_sample_rate() /
159                                         mwindow->edl->session->sample_rate;
160                                 if( index_zoom <= mwindow->edl->local_session->zoom_sample *
161                                         asset_over_session )
162                                         need_redraw = 1;
163                         }
164                 }
165
166                 if( !need_redraw )
167                         return;
168         }
169
170
171 // Redraw everything
172 /* Incremental drawing is not possible with resource thread */
173         if( 1 )
174 //              edit->startsource != this->startsource ||
175 //              mwindow->edl->session->sample_rate != project_samplerate ||
176 //              !EQUIV(mwindow->edl->session->frame_rate, project_framerate) ||
177 //              mwindow->edl->local_session->zoom_sample != zoom_sample ||
178 //              mwindow->edl->local_session->zoom_track != zoom_track ||
179 //              this->pixmap_h != pixmap_h ||
180 //              (data_type == TRACK_AUDIO &&
181 //                      mwindow->edl->local_session->zoom_y != zoom_y) ||
182 //              (mode == 2) ||
183 //              need_redraw)
184         {
185 // Redraw the whole thing.
186                 refresh_x = 0;
187                 refresh_w = pixmap_w;
188         }
189         else {
190 // Start translated right
191                 if( pixmap_w == this->pixmap_w && edit_x < this->edit_x && edit_w != pixmap_w ) {
192                         refresh_w = this->edit_x - edit_x;
193                         refresh_x = this->pixmap_w - refresh_w;
194
195 // Moved completely off the pixmap
196                         if( refresh_w > this->pixmap_w ) {
197                                 refresh_w = this->pixmap_w;
198                                 refresh_x = 0;
199                         }
200                         else {
201                                 copy_area(refresh_w,
202                                         y,
203                                         refresh_x,
204                                         mwindow->edl->local_session->zoom_track,
205                                         0,
206                                         y);
207                         }
208                 }
209                 else
210 // Start translated left
211                 if( pixmap_w == this->pixmap_w && edit_x > this->edit_x && edit_w != pixmap_w ) {
212                         refresh_x = 0;
213                         refresh_w = edit_x - this->edit_x;
214
215 // Moved completely off the pixmap
216                         if( refresh_w > this->pixmap_w ) {
217                                 refresh_w = this->pixmap_w;
218                         }
219                         else {
220                                 copy_area(0,
221                                         y,
222                                         this->pixmap_w - refresh_w,
223                                         mwindow->edl->local_session->zoom_track,
224                                         refresh_w,
225                                         y);
226                         }
227                 }
228                 else
229 // Start translated right and pixmap came off of right side
230                 if( pixmap_w < this->pixmap_w && edit_x < this->edit_x &&
231                         this->edit_x + edit_w > this->pixmap_x + this->pixmap_w ) {
232                         refresh_w = (this->edit_x + edit_w) - (this->pixmap_x + this->pixmap_w);
233                         refresh_x = pixmap_w - refresh_w;
234
235                         if( refresh_w >= pixmap_w ) {
236                                 refresh_x = 0;
237                                 refresh_w = pixmap_w;
238                         }
239                         else {
240                                 copy_area(this->edit_x - edit_x,
241                                         y,
242                                         pixmap_w - refresh_w,
243                                         mwindow->edl->local_session->zoom_track,
244                                         0,
245                                         y);
246                         }
247                 }
248                 else
249 // Start translated right and reduced in size on the right.
250                 if( pixmap_w < this->pixmap_w && edit_x < this->edit_x ) {
251                         refresh_x = 0;
252                         refresh_w = 0;
253
254                         copy_area(this->pixmap_w - pixmap_w,
255                                 y,
256                                 pixmap_w,
257                                 mwindow->edl->local_session->zoom_track,
258                                 0,
259                                 y);
260                 }
261                 else
262 // Start translated left and pixmap came off left side
263                 if( edit_x >= 0 && this->edit_x < 0 ) {
264                         refresh_x = 0;
265                         refresh_w = -this->edit_x;
266
267                         if( refresh_w > pixmap_w ) {
268                                 refresh_w = pixmap_w;
269                         }
270                         else {
271                                 copy_area(0,
272                                                 y,
273                                                 this->pixmap_w,
274                                                 mwindow->edl->local_session->zoom_track,
275                                                 refresh_w,
276                                                 y);
277                         }
278                 }
279                 else
280 // Start translated left and reduced in size on the right
281                 if( pixmap_w < this->pixmap_w && edit_x > this->edit_x ) {
282                         refresh_x = 0;
283                         refresh_w = 0;
284                 }
285                 else
286 // Start translated right and left went into left side.
287                 if( pixmap_w > this->pixmap_w && edit_x < 0 && this->edit_x > 0 ) {
288                         refresh_w = pixmap_w - (edit_x + this->pixmap_w);
289                         refresh_x = pixmap_w - refresh_w;
290
291 // Moved completely off new pixmap
292                         if( refresh_w > pixmap_w ) {
293                                 refresh_w = pixmap_w;
294                                 refresh_x = 0;
295                         }
296                         else {
297                                 copy_area(-edit_x,
298                                         y,
299                                         refresh_x,
300                                         mwindow->edl->local_session->zoom_track,
301                                         0,
302                                         y);
303                         }
304                 }
305                 else
306 // Start translated right and increased in size on the right
307                 if( pixmap_w > this->pixmap_w && edit_x <= this->edit_x ) {
308                         refresh_w = pixmap_w - this->pixmap_w;
309                         refresh_x = pixmap_w - refresh_w;
310                 }
311                 else
312 // Start translated left and increased in size on the right
313                 if( pixmap_w > this->pixmap_w && edit_x > this->edit_x ) {
314                         refresh_x = 0;
315                         refresh_w = edit_x - this->edit_x;
316
317 // Moved completely off new pixmap
318                         if( refresh_w > this->pixmap_w ) {
319                                 refresh_w = pixmap_w;
320                                 refresh_x = 0;
321                         }
322 // Shift and insert
323                         else {
324                                 copy_area(0,
325                                         y,
326                                         this->pixmap_w,
327                                         mwindow->edl->local_session->zoom_track,
328                                         refresh_w,
329                                         y);
330                         }
331                 }
332         }
333
334 // Update pixmap settings
335         this->edit_id = edit->id;
336         this->startsource = edit->startsource;
337
338         if( edit->asset )
339                 this->source_framerate = edit->asset->frame_rate;
340         else
341         if( edit->nested_edl )
342                 this->source_framerate = edit->nested_edl->session->frame_rate;
343
344         if( edit->asset )
345                 this->source_samplerate = edit->asset->sample_rate;
346         else
347         if( edit->nested_edl )
348                 this->source_samplerate = edit->nested_edl->session->sample_rate;
349
350         this->project_framerate = edit->edl->session->frame_rate;
351         this->project_samplerate = edit->edl->session->sample_rate;
352         this->edit_x = edit_x;
353         this->pixmap_x = pixmap_x;
354         this->pixmap_w = pixmap_w;
355         this->pixmap_h = pixmap_h;
356         this->zoom_sample = mwindow->edl->local_session->zoom_sample;
357         this->zoom_track = mwindow->edl->local_session->zoom_track;
358         this->zoom_y = mwindow->edl->local_session->zoom_y;
359
360
361
362 // Draw background image
363         if( refresh_w > 0 )
364                 mwindow->theme->draw_resource_bg(canvas,
365                         this,
366                         edit_x,
367                         edit_w,
368                         pixmap_x,
369                         refresh_x,
370                         y,
371                         refresh_x + refresh_w,
372                         mwindow->edl->local_session->zoom_track + y);
373 //printf("ResourcePixmap::draw_data 70\n");
374
375
376 // Draw media which already exists
377         if( track->draw ) {
378                 switch( track->data_type )
379                 {
380                         case TRACK_AUDIO:
381                                 draw_audio_resource(canvas,
382                                         edit,
383                                         refresh_x,
384                                         refresh_w);
385                                 break;
386
387                         case TRACK_VIDEO:
388                                 draw_video_resource(canvas,
389                                         edit,
390                                         edit_x,
391                                         edit_w,
392                                         pixmap_x,
393                                         pixmap_w,
394                                         refresh_x,
395                                         refresh_w,
396                                         mode);
397                                 break;
398
399                         case TRACK_SUBTITLE:
400                                 draw_subttl_resource(canvas,
401                                         edit,
402                                         refresh_x,
403                                         refresh_w);
404                                 break;
405                 }
406         }
407
408 // Draw title
409 SET_TRACE
410         if( mwindow->edl->session->show_titles )
411                 draw_title(canvas,
412                         edit,
413                         edit_x,
414                         edit_w,
415                         pixmap_x,
416                         pixmap_w);
417 SET_TRACE
418 }
419
420 void ResourcePixmap::draw_title(TrackCanvas *canvas,
421         Edit *edit, int64_t edit_x, int64_t edit_w,
422         int64_t pixmap_x, int64_t pixmap_w)
423 {
424 // coords relative to pixmap
425         int64_t total_x = edit_x - pixmap_x, total_w = edit_w;
426         int64_t x = total_x, w = total_w;
427         int left_margin = 10;
428
429         if( x < 0 ) { w -= -x; x = 0; }
430         if( w > pixmap_w ) w -= w - pixmap_w;
431
432         canvas->draw_3segmenth(x, 0, w, total_x, total_w,
433                 mwindow->theme->get_image("title_bg_data"), this);
434
435 //      if( total_x > -BC_INFINITY ) {
436                 char title[BCTEXTLEN];
437                 edit->get_title(title);
438                 canvas->set_color(mwindow->theme->title_color);
439                 canvas->set_font(mwindow->theme->title_font);
440
441 // Justify the text on the left boundary of the edit if it is visible.
442 // Otherwise justify it on the left side of the screen.
443                 int text_x = total_x + left_margin;
444                 text_x = MAX(left_margin, text_x);
445 //printf("ResourcePixmap::draw_title 1 %d\n", text_x);
446                 canvas->draw_text(text_x,
447                         canvas->get_text_ascent(mwindow->theme->title_font) + 2,
448                         title, strlen(title), this);
449 //      }
450 }
451
452
453 // Need to draw one more x
454 void ResourcePixmap::draw_audio_resource(TrackCanvas *canvas, Edit *edit, int x, int w)
455 {
456         if( w <= 0 ) return;
457         if( !edit->asset && !edit->nested_edl ) return;
458         Indexable *indexable = 0;
459         if( edit->asset ) indexable = edit->asset;
460         if( edit->nested_edl ) indexable = edit->nested_edl;
461 // printf("ResourcePixmap::draw_audio_resource %d x=%d w=%d\n", __LINE__, x, w);
462 SET_TRACE
463
464         IndexState *index_state = indexable->index_state;
465         double asset_over_session = (double)indexable->get_sample_rate() /
466                 mwindow->edl->session->sample_rate;
467
468 // Develop strategy for drawing
469 // printf("ResourcePixmap::draw_audio_resource %d %p %d\n",
470 // __LINE__,
471 // index_state,
472 // index_state->index_status);
473         switch( index_state->index_status )
474         {
475                 case INDEX_NOTTESTED:
476                         return;
477                         break;
478 // Disabled.  All files have an index.
479 //              case INDEX_TOOSMALL:
480 //                      draw_audio_source(canvas, edit, x, w);
481 //                      break;
482                 case INDEX_BUILDING:
483                 case INDEX_READY:
484                 {
485                         IndexFile indexfile(mwindow, indexable);
486                         if( !indexfile.open_index() ) {
487                                 if( index_state->index_zoom >
488                                                 mwindow->edl->local_session->zoom_sample *
489                                                 asset_over_session ) {
490 //printf("ResourcePixmap::draw_audio_resource %d\n", __LINE__);
491
492                                         draw_audio_source(canvas, edit, x, w);
493                                 }
494                                 else {
495 //printf("ResourcePixmap::draw_audio_resource %d\n", __LINE__);
496                                         indexfile.draw_index(canvas,
497                                                 this,
498                                                 edit,
499                                                 x,
500                                                 w);
501 SET_TRACE
502                                 }
503
504                                 indexfile.close_index();
505 SET_TRACE
506                         }
507                         break;
508                 }
509         }
510 }
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529 void ResourcePixmap::draw_audio_source(TrackCanvas *canvas, Edit *edit, int x, int w)
530 {
531         w++;
532         Indexable *indexable = edit->get_source();
533         int center_pixel = mwindow->edl->local_session->zoom_track / 2;
534         if( mwindow->edl->session->show_titles )
535                 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
536         int64_t scale_y = mwindow->edl->local_session->zoom_y;
537         int y_max = center_pixel + scale_y / 2 - 1;
538
539         double project_zoom = mwindow->edl->local_session->zoom_sample;
540         FloatAutos *speed_autos = !edit->track->has_speed() ? 0 :
541                 (FloatAutos *)edit->track->automation->autos[AUTOMATION_SPEED];
542         int64_t edit_position = (x + pixmap_x - edit_x) * project_zoom;
543         int64_t start_position = edit->startsource;
544         start_position += !speed_autos ? edit_position :
545                 speed_autos->automation_integral(edit->startproject, edit_position, PLAY_FORWARD);
546         int64_t end_position = edit->startsource;
547         edit_position = (x + w + pixmap_x - edit_x) * project_zoom;
548         end_position += !speed_autos ? edit_position :
549                 speed_autos->automation_integral(edit->startproject, edit_position, PLAY_FORWARD);
550
551         double session_sample_rate = mwindow->edl->session->sample_rate;
552         double asset_over_session = (double)indexable->get_sample_rate() / session_sample_rate;
553         start_position *= asset_over_session;
554         end_position *= asset_over_session;
555         int sample_size = end_position - start_position;
556         if( sample_size < 0 ) sample_size = 0;
557         int source_samples = sample_size + 1;
558
559 // Single sample zoom
560         if( mwindow->edl->local_session->zoom_sample == 1 ) {
561                 Samples *buffer = 0;
562                 int result = 0;
563                 canvas->set_color(mwindow->theme->audio_color);
564
565                 if( indexable->is_asset ) {
566                         mwindow->gui->unlock_window();
567                         File *source = mwindow->audio_cache->check_out(edit->asset, mwindow->edl);
568                         mwindow->gui->lock_window("draw_audio_source");
569
570                         if( !source ) {
571                                 printf(_("ResourcePixmap::draw_audio_source: failed to check out %s for drawing.\n"), edit->asset->path);
572                                 return;
573                         }
574
575                         source->set_audio_position(start_position);
576                         source->set_channel(edit->channel);
577                         buffer = new Samples(source_samples);
578                         result = source->read_samples(buffer, source_samples);
579                         mwindow->audio_cache->check_in(edit->asset);
580                 }
581                 else {
582                         if( mwindow->gui->render_engine &&
583                                 mwindow->gui->render_engine_id != indexable->id ) {
584                                 delete mwindow->gui->render_engine;
585                                 mwindow->gui->render_engine = 0;
586                         }
587
588                         if( !mwindow->gui->render_engine ) {
589                                 TransportCommand command;
590                                 command.command = NORMAL_FWD;
591                                 command.get_edl()->copy_all(edit->nested_edl);
592                                 command.change_type = CHANGE_ALL;
593                                 command.realtime = 0;
594                                 mwindow->gui->render_engine = new RenderEngine(0,
595                                         mwindow->preferences, 0, 0);
596                                 mwindow->gui->render_engine_id = edit->nested_edl->id;
597                                 mwindow->gui->render_engine->set_acache(mwindow->audio_cache);
598                                 mwindow->gui->render_engine->arm_command(&command);
599                         }
600
601                         if( mwindow->gui->render_engine->arender ) {
602                                 Samples *buffers[MAX_CHANNELS];
603                                 memset(buffers, 0, sizeof(buffers));
604                                 int nch = indexable->get_audio_channels(), ch = edit->channel;
605                                 for( int i=0; i<nch; ++i )
606                                         buffers[i] = new Samples(source_samples);
607                                 mwindow->gui->render_engine->arender->process_buffer(
608                                         buffers, source_samples, start_position);
609                                 for( int i=0; i<nch; ++i )
610                                         if( i != ch ) delete buffers[i];
611                                 buffer = buffers[ch];
612                         }
613                 }
614
615                 if( !result ) {
616                         double *samples = buffer->get_data();
617                         int y1 = center_pixel - samples[0] * scale_y / 2;
618                         int y2 = CLIP(y1, 0, y_max);
619
620                         for( int x0=0; x0<w; ++x0 ) {
621                                 int x1 = x0 + x, x2 = x1 + 1;
622                                 edit_position = (x1 + pixmap_x - edit_x) * project_zoom;
623                                 int64_t speed_position = edit->startsource;
624                                 speed_position += !speed_autos ? edit_position :
625                                         speed_autos->automation_integral(
626                                                 edit->startproject, edit_position, PLAY_FORWARD);
627                                 int j = speed_position * asset_over_session - start_position;
628                                 CLAMP(j, 0, sample_size);
629                                 int y0 = y2;
630                                 y1 = center_pixel - samples[j] * scale_y / 2;
631                                 y2 = CLIP(y1, 0, y_max);
632 //printf("ResourcePixmap::draw_audio_source %d %d %d\n", __LINE__, y1, y2);
633                                 canvas->draw_line(x0, y0, x2, y2, this);
634                         }
635                 }
636
637                 delete buffer;
638         }
639         else {
640                 edit_position = (x + pixmap_x - edit_x) * project_zoom;
641                 int64_t speed_position = edit->startsource;
642                 speed_position += !speed_autos ? edit_position :
643                         speed_autos->automation_integral(
644                                 edit->startproject, edit_position, PLAY_FORWARD);
645                 int64_t next_position = asset_over_session * speed_position;
646 // Multiple sample zoom
647                 int first_pixel = 1, prev_y1 = -1, prev_y2 = y_max;
648                 canvas->set_color(mwindow->theme->audio_color);
649                 ++x;
650
651 // Draw each pixel from the cache
652 //printf("ResourcePixmap::draw_audio_source %d x=%d w=%d\n", __LINE__, x, w);
653                 for( int x2=x+w; x<x2; ++x ) {
654                         int64_t prev_position = next_position;
655                         edit_position = (x + pixmap_x - edit_x) * project_zoom;
656                         speed_position = edit->startsource;
657                         speed_position += !speed_autos ? edit_position :
658                                 speed_autos->automation_integral(
659                                         edit->startproject, edit_position, PLAY_FORWARD);
660                         next_position = speed_position * asset_over_session;
661 // Starting sample of pixel relative to asset rate.
662                         WaveCacheItem *item = mwindow->wave_cache->get_wave(indexable->id,
663                                         edit->channel, prev_position, next_position);
664                         if( item ) {
665 //printf("ResourcePixmap::draw_audio_source %d\n", __LINE__);
666                                 int y_lo = (int)(center_pixel - item->low * scale_y / 2);
667                                 int y1 = CLIP(y_lo, 0, y_max);
668                                 int y_hi = (int)(center_pixel - item->high * scale_y / 2);
669                                 int y2 = CLIP(y_hi, 0, y_max);
670                                 if( !first_pixel ) {
671                                         y_lo = MIN(y1,prev_y2);
672                                         y_hi = MAX(y2,prev_y1);
673                                 }
674                                 else {
675                                         first_pixel = 0;
676                                         y_lo = y1;  y_hi = y2;
677                                 }
678                                 prev_y1 = y1;  prev_y2 = y2;
679                                 canvas->draw_line(x, y_lo, x, y_hi, this);
680 //printf("ResourcePixmap::draw_audio_source %d %d %d %d\n", __LINE__, x, y1, y2);
681                                 mwindow->wave_cache->unlock();
682                         }
683                         else {
684 //printf("ResourcePixmap::draw_audio_source %d\n", __LINE__);
685                                 gui->resource_thread->add_wave(this,
686                                         canvas->pane->number, indexable, x,
687                                         edit->channel, prev_position, next_position);
688                                 first_pixel = 1;  prev_y1 = -1;  prev_y2 = y_max;
689                         }
690                 }
691         }
692
693         canvas->test_timer();
694 }
695
696 void ResourcePixmap::draw_wave(TrackCanvas *canvas, int x, double high, double low)
697 {
698         int top_pixel = 0;
699         if( mwindow->edl->session->show_titles )
700                 top_pixel = mwindow->theme->get_image("title_bg_data")->get_h();
701         int center_pixel = mwindow->edl->local_session->zoom_track / 2 + top_pixel;
702         int bottom_pixel = top_pixel + mwindow->edl->local_session->zoom_track;
703         int y1 = (int)(center_pixel -
704                 low * mwindow->edl->local_session->zoom_y / 2);
705         int y2 = (int)(center_pixel -
706                 high * mwindow->edl->local_session->zoom_y / 2);
707         CLAMP(y1, top_pixel, bottom_pixel);
708         CLAMP(y2, top_pixel, bottom_pixel);
709         canvas->set_color(mwindow->theme->audio_color);
710         canvas->draw_line(x, y1, x, y2, this);
711 }
712
713
714 void ResourcePixmap::draw_video_resource(TrackCanvas *canvas,
715         Edit *edit, int64_t edit_x, int64_t edit_w, int64_t pixmap_x, int64_t pixmap_w,
716         int refresh_x, int refresh_w, int mode)
717 {
718 //PRINT_TRACE
719 //BC_Signals::dump_stack();
720
721 // pixels spanned by a picon
722         int64_t picon_w = Units::round(edit->picon_w());
723         int64_t picon_h = edit->picon_h();
724 //      if( picon_w <= 0 || picon_w > edit_w ) return;
725 // Don't draw video if picon is empty, or edit only hairline
726         if( picon_w < 1 || edit_w < 2 ) return;
727 // or bigger than edit and fills at less than 1.5 percent timeline
728         if( picon_w > edit_w && edit_w < canvas->get_w()/64 ) return;
729
730 // Frames spanned by a picon
731         double frame_w = edit->frame_w();
732 // pixels spanned by a frame
733         if( frame_w < picon_w ) frame_w = picon_w;
734 // Current pixel relative to pixmap
735         int y = 0;
736         if( mwindow->edl->session->show_titles )
737                 y += mwindow->theme->get_image("title_bg_data")->get_h();
738
739 // Frame in project touched by current pixel
740         FloatAutos *speed_autos = !edit->track->has_speed() ? 0 :
741                 (FloatAutos *)edit->track->automation->autos[AUTOMATION_SPEED];
742         Indexable *indexable = edit->get_source();
743         double session_sample_rate = mwindow->edl->session->sample_rate;
744         double project_zoom = mwindow->edl->local_session->zoom_sample / session_sample_rate;
745         int skip_frames = Units::to_int64(((int64_t)refresh_x + pixmap_x - edit_x) / frame_w);
746         int x = Units::to_int64(skip_frames * frame_w) + edit_x - pixmap_x;
747
748 // Draw only cached frames
749         while( x < refresh_x + refresh_w ) {
750                 int64_t edit_position =
751                         edit->track->to_units((x + pixmap_x - edit_x) * project_zoom, 0);
752                 int64_t speed_position = edit->startsource;
753                 speed_position += !speed_autos ? edit_position :
754                          speed_autos->automation_integral(
755                                 edit->startproject, edit_position, PLAY_FORWARD);
756                 VFrame *picon_frame = indexable->id < 0 ? 0 :
757                         mwindow->frame_cache->get_frame_ptr(speed_position, edit->channel,
758                                 mwindow->edl->session->frame_rate, BC_RGB888,
759                                 picon_w, picon_h, indexable->id);
760                 if( picon_frame ) {
761                         draw_vframe(picon_frame, x, y, picon_w, picon_h, 0, 0);
762                         mwindow->frame_cache->unlock();
763                 }
764                 else if( mode != IGNORE_THREAD ) {
765 // Set picon thread to draw in background
766 // printf("ResourcePixmap::draw_video_resource %d %d %lld\n",
767 // __LINE__, mwindow->frame_cache->total(), source_frame);
768                         gui->resource_thread->add_picon(this, canvas->pane->number, x, y,
769                                 picon_w, picon_h, mwindow->edl->session->frame_rate,
770                                 speed_position, edit->channel, indexable);
771                 }
772                 x += frame_w;
773                 canvas->test_timer();
774         }
775 }
776
777 #include "strack.h"
778
779 void ResourcePixmap::draw_subttl_resource(TrackCanvas *canvas, Edit *edit, int x, int w)
780 {
781         SEdit *sedit = (SEdit *)edit;
782         char *text = sedit->get_text();
783         if( !*text || w < 10 ) return;
784         int center_pixel = mwindow->edl->local_session->zoom_track / 2;
785         if( mwindow->edl->session->show_titles )
786                 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
787         int64_t scale_y = mwindow->edl->local_session->zoom_y;
788         int x0 = edit_x;
789         if( x0 < 0 ) x0 = -x0;
790         int x1 = (int)(pixmap_x - x0 + x);
791         int y_max = center_pixel + scale_y / 2 - 1;
792         int font = MEDIUMFONT, color = WHITE;
793         canvas->set_font(font);
794         canvas->set_color(color);
795         int ch = canvas->get_text_height(font);
796         int hh = canvas->get_text_height(font,text) + ch/2;
797         int y1 = y_max - hh - 10;
798         if( y1 < 0 ) y1 = 0;
799         canvas->draw_text(x1, y1, text, -1, this);
800 }
801
802 void ResourcePixmap::dump()
803 {
804         printf("ResourcePixmap %p\n", this);
805         printf(" edit %jx edit_x %jd pixmap_x %jd pixmap_w %jd visible %d\n",
806                 edit_id, edit_x, pixmap_x, pixmap_w, visible);
807 }
808