Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_sequencer / sequencer_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_draw.c
27  *  \ingroup spseq
28  */
29
30
31 #include <string.h>
32 #include <math.h>
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_threads.h"
38
39 #include "IMB_imbuf_types.h"
40
41 #include "DNA_scene_types.h"
42 #include "DNA_mask_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_userdef_types.h"
46 #include "DNA_sound_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_sequencer.h"
52 #include "BKE_sound.h"
53 #include "BKE_scene.h"
54
55 #include "IMB_colormanagement.h"
56 #include "IMB_imbuf.h"
57
58 #include "BIF_glutil.h"
59
60 #include "GPU_immediate.h"
61 #include "GPU_immediate_util.h"
62 #include "GPU_matrix.h"
63
64 #include "ED_anim_api.h"
65 #include "ED_gpencil.h"
66 #include "ED_markers.h"
67 #include "ED_mask.h"
68 #include "ED_sequencer.h"
69 #include "ED_screen.h"
70 #include "ED_space_api.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "WM_api.h"
77
78 #include "MEM_guardedalloc.h"
79
80 /* own include */
81 #include "sequencer_intern.h"
82
83
84 #define SEQ_LEFTHANDLE   1
85 #define SEQ_RIGHTHANDLE  2
86
87 #define SEQ_HANDLE_SIZE_MIN  7.0f
88 #define SEQ_HANDLE_SIZE_MAX 40.0f
89
90
91 /* Note, Don't use SEQ_BEGIN/SEQ_END while drawing!
92  * it messes up transform, - Campbell */
93 #undef SEQ_BEGIN
94 #undef SEQP_BEGIN
95 #undef SEQ_END
96
97 static Sequence *special_seq_update = NULL;
98
99 void color3ubv_from_seq(Scene *curscene, Sequence *seq, unsigned char col[3])
100 {
101         unsigned char blendcol[3];
102         SolidColorVars *colvars = (SolidColorVars *)seq->effectdata;
103
104         switch (seq->type) {
105                 case SEQ_TYPE_IMAGE:
106                         UI_GetThemeColor3ubv(TH_SEQ_IMAGE, col);
107                         break;
108
109                 case SEQ_TYPE_META:
110                         UI_GetThemeColor3ubv(TH_SEQ_META, col);
111                         break;
112
113                 case SEQ_TYPE_MOVIE:
114                         UI_GetThemeColor3ubv(TH_SEQ_MOVIE, col);
115                         break;
116
117                 case SEQ_TYPE_MOVIECLIP:
118                         UI_GetThemeColor3ubv(TH_SEQ_MOVIECLIP, col);
119                         break;
120
121                 case SEQ_TYPE_MASK:
122                         UI_GetThemeColor3ubv(TH_SEQ_MASK, col); /* TODO */
123                         break;
124
125                 case SEQ_TYPE_SCENE:
126                         UI_GetThemeColor3ubv(TH_SEQ_SCENE, col);
127                 
128                         if (seq->scene == curscene) {
129                                 UI_GetColorPtrShade3ubv(col, col, 20);
130                         }
131                         break;
132                 
133                 /* transitions */
134                 case SEQ_TYPE_CROSS:
135                 case SEQ_TYPE_GAMCROSS:
136                 case SEQ_TYPE_WIPE:
137                         UI_GetThemeColor3ubv(TH_SEQ_TRANSITION, col);
138
139                         /* slightly offset hue to distinguish different effects */
140                         if (seq->type == SEQ_TYPE_CROSS)    rgb_byte_set_hue_float_offset(col, 0.04);
141                         if (seq->type == SEQ_TYPE_GAMCROSS) rgb_byte_set_hue_float_offset(col, 0.08);
142                         if (seq->type == SEQ_TYPE_WIPE)     rgb_byte_set_hue_float_offset(col, 0.12);
143                         break;
144
145                 /* effects */
146                 case SEQ_TYPE_TRANSFORM:
147                 case SEQ_TYPE_SPEED:
148                 case SEQ_TYPE_ADD:
149                 case SEQ_TYPE_SUB:
150                 case SEQ_TYPE_MUL:
151                 case SEQ_TYPE_ALPHAOVER:
152                 case SEQ_TYPE_ALPHAUNDER:
153                 case SEQ_TYPE_OVERDROP:
154                 case SEQ_TYPE_GLOW:
155                 case SEQ_TYPE_MULTICAM:
156                 case SEQ_TYPE_ADJUSTMENT:
157                 case SEQ_TYPE_GAUSSIAN_BLUR:
158                         UI_GetThemeColor3ubv(TH_SEQ_EFFECT, col);
159
160                         /* slightly offset hue to distinguish different effects */
161                         if      (seq->type == SEQ_TYPE_ADD)           rgb_byte_set_hue_float_offset(col, 0.04);
162                         else if (seq->type == SEQ_TYPE_SUB)           rgb_byte_set_hue_float_offset(col, 0.08);
163                         else if (seq->type == SEQ_TYPE_MUL)           rgb_byte_set_hue_float_offset(col, 0.12);
164                         else if (seq->type == SEQ_TYPE_ALPHAOVER)     rgb_byte_set_hue_float_offset(col, 0.16);
165                         else if (seq->type == SEQ_TYPE_ALPHAUNDER)    rgb_byte_set_hue_float_offset(col, 0.20);
166                         else if (seq->type == SEQ_TYPE_OVERDROP)      rgb_byte_set_hue_float_offset(col, 0.24);
167                         else if (seq->type == SEQ_TYPE_GLOW)          rgb_byte_set_hue_float_offset(col, 0.28);
168                         else if (seq->type == SEQ_TYPE_TRANSFORM)     rgb_byte_set_hue_float_offset(col, 0.36);
169                         else if (seq->type == SEQ_TYPE_MULTICAM)      rgb_byte_set_hue_float_offset(col, 0.32);
170                         else if (seq->type == SEQ_TYPE_ADJUSTMENT)    rgb_byte_set_hue_float_offset(col, 0.40);
171                         else if (seq->type == SEQ_TYPE_GAUSSIAN_BLUR) rgb_byte_set_hue_float_offset(col, 0.42);
172                         break;
173
174                 case SEQ_TYPE_COLOR:
175                         rgb_float_to_uchar(col, colvars->col);
176                         break;
177
178                 case SEQ_TYPE_SOUND_RAM:
179                         UI_GetThemeColor3ubv(TH_SEQ_AUDIO, col);
180                         blendcol[0] = blendcol[1] = blendcol[2] = 128;
181                         if (seq->flag & SEQ_MUTE) UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.5, 20);
182                         break;
183
184                 case SEQ_TYPE_TEXT:
185                         UI_GetThemeColor3ubv(TH_SEQ_TEXT, col);
186                         break;
187
188                 default:
189                         col[0] = 10; col[1] = 255; col[2] = 40;
190                         break;
191         }
192 }
193
194 static void drawseqwave(View2D *v2d, const bContext *C, SpaceSeq *sseq, Scene *scene, Sequence *seq,
195                         float x1, float y1, float x2, float y2, float stepsize, unsigned int pos)
196 {
197         /*
198          * x1 is the starting x value to draw the wave,
199          * x2 the end x value, same for y1 and y2
200          * stepsize is width of a pixel.
201          */
202
203         /* offset x1 and x2 values, to match view min/max, if strip is out of bounds */
204         int x1_offset = max_ff(v2d->cur.xmin, x1);
205         int x2_offset = min_ff(v2d->cur.xmax + 1.0f, x2);
206
207         if (seq->sound && ((sseq->flag & SEQ_ALL_WAVEFORMS) || (seq->flag & SEQ_AUDIO_DRAW_WAVEFORM))) {
208                 int i, j, p;
209                 int length = floor((x2_offset - x1_offset) / stepsize) + 1;
210                 float ymid = (y1 + y2) / 2.0f;
211                 float yscale = (y2 - y1) / 2.0f;
212                 float samplestep;
213                 float startsample, endsample;
214                 float value1, value2;
215                 bSound *sound = seq->sound;
216                 
217                 SoundWaveform *waveform;
218
219                 if (length < 2) {
220                         return;
221                 }
222
223                 if (!sound->spinlock) {
224                         sound->spinlock = MEM_mallocN(sizeof(SpinLock), "sound_spinlock");
225                         BLI_spin_init(sound->spinlock);
226                 }
227                 
228                 BLI_spin_lock(sound->spinlock);
229                 if (!sound->waveform) {
230                         if (!(sound->flags & SOUND_FLAGS_WAVEFORM_LOADING)) {
231                                 /* prevent sounds from reloading */
232                                 sound->flags |= SOUND_FLAGS_WAVEFORM_LOADING;
233                                 BLI_spin_unlock(sound->spinlock);
234                                 sequencer_preview_add_sound(C, seq);
235                         }
236                         else {
237                                 BLI_spin_unlock(sound->spinlock);
238                         }
239                         return;  /* nothing to draw */
240                 }
241                 BLI_spin_unlock(sound->spinlock);
242                 
243                 waveform = sound->waveform;
244
245                 if (waveform->length == 0) {
246                         /* BKE_sound_read_waveform() set an empty SoundWaveform data in case it cannot generate a valid one...
247                          * See T45726. */
248                         return;
249                 }
250
251                 startsample = floor((seq->startofs + seq->anim_startofs) / FPS * SOUND_WAVE_SAMPLES_PER_SECOND);
252                 endsample = ceil((seq->startofs + seq->anim_startofs + seq->enddisp - seq->startdisp) / FPS * SOUND_WAVE_SAMPLES_PER_SECOND);
253                 samplestep = (endsample - startsample) * stepsize / (x2 - x1);
254
255                 length = min_ii(floor((waveform->length - startsample) / samplestep), length);
256
257                 if (length < 2) {
258                         return;
259                 }
260
261                 immUniformColor4f(1.0f, 1.0f, 1.0f, 0.5f);
262
263                 glEnable(GL_BLEND);
264
265                 immBegin(GWN_PRIM_TRI_STRIP, length * 2);
266
267                 for (i = 0; i < length; i++) {
268                         float sampleoffset = startsample + ((x1_offset - x1) / stepsize + i) * samplestep;
269                         p = sampleoffset;
270
271                         value1 = waveform->data[p * 3];
272                         value2 = waveform->data[p * 3 + 1];
273
274                         if (samplestep > 1.0f) {
275                                 for (j = p + 1; (j < waveform->length) && (j < p + samplestep); j++) {
276                                         if (value1 > waveform->data[j * 3])
277                                                 value1 = waveform->data[j * 3];
278
279                                         if (value2 < waveform->data[j * 3 + 1])
280                                                 value2 = waveform->data[j * 3 + 1];
281                                 }
282                         }
283                         else {
284                                 /* use simple linear interpolation */
285                                 float f = sampleoffset - p;
286                                 value1 = (1.0f - f) * value1 + f * waveform->data[p * 3 + 3];
287                                 value2 = (1.0f - f) * value2 + f * waveform->data[p * 3 + 4];
288                         }
289
290                         immVertex2f(pos, x1_offset + i * stepsize, ymid + value1 * yscale);
291                         immVertex2f(pos, x1_offset + i * stepsize, ymid + value2 * yscale);
292                 }
293
294                 immEnd();
295
296                 glDisable(GL_BLEND);
297         }
298 }
299 static void drawmeta_contents(Scene *scene, Sequence *seqm, float x1, float y1, float x2, float y2)
300 {
301         /* note: this used to use SEQ_BEGIN/SEQ_END, but it messes up the
302          * seq->depth value, (needed by transform when doing overlap checks)
303          * so for now, just use the meta's immediate children, could be fixed but
304          * its only drawing - campbell */
305         Sequence *seq;
306         unsigned char col[4];
307
308         int chan_min = MAXSEQ;
309         int chan_max = 0;
310         int chan_range = 0;
311         float draw_range = y2 - y1;
312         float draw_height;
313         ListBase *seqbase;
314         int offset;
315
316         seqbase = BKE_sequence_seqbase_get(seqm, &offset);
317         if (!seqbase || BLI_listbase_is_empty(seqbase)) {
318                 return;
319         }
320
321         if (seqm->type == SEQ_TYPE_SCENE) {
322                 offset  = seqm->start - offset;
323         }
324         else {
325                 offset = 0;
326         }
327
328         glEnable(GL_BLEND);
329         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
330
331         for (seq = seqbase->first; seq; seq = seq->next) {
332                 chan_min = min_ii(chan_min, seq->machine);
333                 chan_max = max_ii(chan_max, seq->machine);
334         }
335
336         chan_range = (chan_max - chan_min) + 1;
337         draw_height = draw_range / chan_range;
338
339         col[3] = 196; /* alpha, used for all meta children */
340
341         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
342
343         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
344
345         for (seq = seqbase->first; seq; seq = seq->next) {
346                 const int startdisp = seq->startdisp + offset;
347                 const int enddisp   = seq->enddisp   + offset;
348
349                 if ((startdisp > x2 || enddisp < x1) == 0) {
350                         float y_chan = (seq->machine - chan_min) / (float)(chan_range) * draw_range;
351                         float x1_chan = startdisp;
352                         float x2_chan = enddisp;
353                         float y1_chan, y2_chan;
354
355                         color3ubv_from_seq(scene, seq, col);
356
357                         if ((seqm->flag & SEQ_MUTE) || (seq->flag & SEQ_MUTE)) {
358                                 col[3] = 64;
359                         }
360                         else {
361                                 col[3] = 196;
362                         }
363
364                         immUniformColor4ubv(col);
365                         
366                         /* clamp within parent sequence strip bounds */
367                         if (x1_chan < x1) x1_chan = x1;
368                         if (x2_chan > x2) x2_chan = x2;
369
370                         y1_chan = y1 + y_chan + (draw_height * SEQ_STRIP_OFSBOTTOM);
371                         y2_chan = y1 + y_chan + (draw_height * SEQ_STRIP_OFSTOP);
372
373                         immRectf(pos, x1_chan,  y1_chan, x2_chan,  y2_chan);
374                 }
375         }
376
377         immUnbindProgram();
378
379         glDisable(GL_BLEND);
380 }
381
382 /* clamp handles to defined size in pixel space */
383 static float draw_seq_handle_size_get_clamped(Sequence *seq, const float pixelx)
384 {
385         const float minhandle = pixelx * SEQ_HANDLE_SIZE_MIN;
386         const float maxhandle = pixelx * SEQ_HANDLE_SIZE_MAX;
387         float size = CLAMPIS(seq->handsize, minhandle, maxhandle);
388
389         /* ensure we're not greater than half width */
390         return min_ff(size, ((float)(seq->enddisp - seq->startdisp) / 2.0f) / pixelx);
391 }
392
393 /* draw a handle, for each end of a sequence strip */
394 static void draw_seq_handle(View2D *v2d, Sequence *seq, const float handsize_clamped, const short direction, unsigned int pos)
395 {
396         float v1[2], v2[2], v3[2], rx1 = 0, rx2 = 0; //for triangles and rect
397         float x1, x2, y1, y2;
398         unsigned int whichsel = 0;
399         
400         x1 = seq->startdisp;
401         x2 = seq->enddisp;
402         
403         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
404         y2 = seq->machine + SEQ_STRIP_OFSTOP;
405
406         /* set up co-ordinates/dimensions for either left or right handle */
407         if (direction == SEQ_LEFTHANDLE) {
408                 rx1 = x1;
409                 rx2 = x1 + handsize_clamped * 0.75f;
410                 
411                 v1[0] = x1 + handsize_clamped / 4; v1[1] = y1 + ( ((y1 + y2) / 2.0f - y1) / 2);
412                 v2[0] = x1 + handsize_clamped / 4; v2[1] = y2 - ( ((y1 + y2) / 2.0f - y1) / 2);
413                 v3[0] = v2[0] + handsize_clamped / 4; v3[1] = (y1 + y2) / 2.0f;
414                 
415                 whichsel = SEQ_LEFTSEL;
416         }
417         else if (direction == SEQ_RIGHTHANDLE) {
418                 rx1 = x2 - handsize_clamped * 0.75f;
419                 rx2 = x2;
420                 
421                 v1[0] = x2 - handsize_clamped / 4; v1[1] = y1 + ( ((y1 + y2) / 2.0f - y1) / 2);
422                 v2[0] = x2 - handsize_clamped / 4; v2[1] = y2 - ( ((y1 + y2) / 2.0f - y1) / 2);
423                 v3[0] = v2[0] - handsize_clamped / 4; v3[1] = (y1 + y2) / 2.0f;
424                 
425                 whichsel = SEQ_RIGHTSEL;
426         }
427         
428         /* draw! */
429         if (!(seq->type & SEQ_TYPE_EFFECT) ||
430             BKE_sequence_effect_get_num_inputs(seq->type) == 0)
431         {
432                 glEnable(GL_BLEND);
433                 
434                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
435                 
436                 if (seq->flag & whichsel) {
437                         immUniformColor4ub(0, 0, 0, 80);
438                 }
439                 else if (seq->flag & SELECT) {
440                         immUniformColor4ub(255, 255, 255, 30);
441                 }
442                 else {
443                         immUniformColor4ub(0, 0, 0, 22);
444                 }
445                 
446                 immRectf(pos, rx1, y1, rx2, y2);
447                 
448                 if (seq->flag & whichsel) {
449                         immUniformColor4ub(255, 255, 255, 200);
450                 }
451                 else {
452                         immUniformColor4ub(0, 0, 0, 50);
453                 }
454
455                 immBegin(GWN_PRIM_TRIS, 3);
456                 immVertex2fv(pos, v1);
457                 immVertex2fv(pos, v2);
458                 immVertex2fv(pos, v3);
459                 immEnd();
460
461                 glDisable(GL_BLEND);
462         }
463         
464         if ((G.moving & G_TRANSFORM_SEQ) || (seq->flag & whichsel)) {
465                 const char col[4] = {255, 255, 255, 255};
466                 char numstr[32];
467                 size_t numstr_len;
468
469                 if (direction == SEQ_LEFTHANDLE) {
470                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", seq->startdisp);
471                         x1 = rx1;
472                         y1 -= 0.45f;
473                 }
474                 else {
475                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", seq->enddisp - 1);
476                         x1 = x2 - handsize_clamped * 0.75f;
477                         y1 = y2 + 0.05f;
478                 }
479                 UI_view2d_text_cache_add(v2d, x1, y1, numstr, numstr_len, col);
480         }
481 }
482
483 /* draw info text on a sequence strip */
484 static void draw_seq_text(View2D *v2d, SpaceSeq *sseq, Sequence *seq, float x1, float x2, float y1, float y2, const unsigned char background_col[3])
485 {
486         rctf rect;
487         char str[32 + FILE_MAX];
488         size_t str_len;
489         const char *name = seq->name + 2;
490         char col[4];
491
492         /* note, all strings should include 'name' */
493         if (name[0] == '\0')
494                 name = BKE_sequence_give_name(seq);
495
496         if (seq->type == SEQ_TYPE_META || seq->type == SEQ_TYPE_ADJUSTMENT) {
497                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d", name, seq->len);
498         }
499         else if (seq->type == SEQ_TYPE_SCENE) {
500                 if (seq->scene) {
501                         if (seq->scene_camera) {
502                                 str_len = BLI_snprintf(str, sizeof(str), "%s: %s (%s) | %d",
503                                                        name, seq->scene->id.name + 2, ((ID *)seq->scene_camera)->name + 2, seq->len);
504                         }
505                         else {
506                                 str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d",
507                                                        name, seq->scene->id.name + 2, seq->len);
508                         }
509                 }
510                 else {
511                         str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
512                                                name, seq->len);
513                 }
514         }
515         else if (seq->type == SEQ_TYPE_MOVIECLIP) {
516                 if (seq->clip && !STREQ(name, seq->clip->id.name + 2)) {
517                         str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d",
518                                                name, seq->clip->id.name + 2, seq->len);
519                 }
520                 else {
521                         str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
522                                                name, seq->len);
523                 }
524         }
525         else if (seq->type == SEQ_TYPE_MASK) {
526                 if (seq->mask && !STREQ(name, seq->mask->id.name + 2)) {
527                         str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d",
528                                                name, seq->mask->id.name + 2, seq->len);
529                 }
530                 else {
531                         str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
532                                                name, seq->len);
533                 }
534         }
535         else if (seq->type == SEQ_TYPE_MULTICAM) {
536                 str_len = BLI_snprintf(str, sizeof(str), "Cam %s: %d",
537                                        name, seq->multicam_source);
538         }
539         else if (seq->type == SEQ_TYPE_IMAGE) {
540                 str_len = BLI_snprintf(str, sizeof(str), "%s: %s%s | %d",
541                                        name, seq->strip->dir, seq->strip->stripdata->name, seq->len);
542         }
543         else if (seq->type == SEQ_TYPE_TEXT) {
544                 TextVars *textdata = seq->effectdata;
545                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
546                                        textdata->text, seq->startdisp);
547         }
548         else if (seq->type & SEQ_TYPE_EFFECT) {
549                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
550                                        name, seq->len);
551         }
552         else if (seq->type == SEQ_TYPE_SOUND_RAM) {
553                 /* If a waveform is drawn, we don't want to overlay it with text,
554                  * as it would make both hard to read. */
555                 if ((sseq->flag & SEQ_ALL_WAVEFORMS) || (seq->flag & SEQ_AUDIO_DRAW_WAVEFORM)) {
556                         str[0] = 0;
557                         str_len = 0;
558                 }
559                 else if (seq->sound) {
560                         str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d",
561                                                name, seq->sound->name, seq->len);
562                 }
563                 else {
564                         str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
565                                                name, seq->len);
566                 }
567         }
568         else if (seq->type == SEQ_TYPE_MOVIE) {
569                 str_len = BLI_snprintf(str, sizeof(str), "%s: %s%s | %d",
570                                        name, seq->strip->dir, seq->strip->stripdata->name, seq->len);
571         }
572         else {
573                 /* should never get here!, but might with files from future */
574                 BLI_assert(0);
575
576                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
577                                        name, seq->len);
578         }
579         
580         if (seq->flag & SELECT) {
581                 col[0] = col[1] = col[2] = 255;
582         }
583         else if ((((int)background_col[0] + (int)background_col[1] + (int)background_col[2]) / 3) < 50) {
584                 col[0] = col[1] = col[2] = 80; /* use lighter text color for dark background */
585         }
586         else {
587                 col[0] = col[1] = col[2] = 0;
588         }
589         col[3] = 255;
590
591         rect.xmin = x1;
592         rect.ymin = y1;
593         rect.xmax = x2;
594         rect.ymax = y2;
595
596         UI_view2d_text_cache_add_rectf(v2d, &rect, str, str_len, col);
597 }
598
599 static void draw_sequence_extensions(Scene *scene, ARegion *ar, Sequence *seq, unsigned int pos)
600 {
601         float x1, x2, y1, y2, pixely;
602         unsigned char col[4], blendcol[3];
603         View2D *v2d = &ar->v2d;
604         
605         x1 = seq->startdisp;
606         x2 = seq->enddisp;
607         
608         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
609         y2 = seq->machine + SEQ_STRIP_OFSTOP;
610         
611         pixely = BLI_rctf_size_y(&v2d->cur) / BLI_rcti_size_y(&v2d->mask);
612         
613         if (pixely <= 0) return;  /* can happen when the view is split/resized */
614         
615         blendcol[0] = blendcol[1] = blendcol[2] = 120;
616
617         if (seq->startofs || seq->endofs) {
618                 glEnable(GL_BLEND);
619                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
620                 
621                 color3ubv_from_seq(scene, seq, col);
622
623                 if (seq->flag & SELECT) {
624                         UI_GetColorPtrShade3ubv(col, col, -50);
625                 }
626
627                 if (seq->flag & SEQ_MUTE) {
628                         col[3] = 64;
629                 }
630                 else {
631                         if (seq->flag & SELECT) {
632                                 col[3] = 170;
633                         }
634                         else {
635                                 col[3] = 80;
636                         }
637                 }
638         }
639
640         if (seq->startofs) {
641                 immUniformColor4ubv(col);
642                 immRectf(pos, (float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);
643
644                 immUniformColor3ubvAlpha(col, col[3] + 50);
645
646                 imm_draw_line_box(pos, (float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);  /* outline */
647         }
648         if (seq->endofs) {
649                 immUniformColor4ubv(col);
650                 immRectf(pos, x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM);
651
652                 immUniformColor3ubvAlpha(col, col[3] + 50);
653
654                 imm_draw_line_box(pos, x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM); /* outline */
655         }
656
657         if (seq->startofs || seq->endofs) {
658                 glDisable(GL_BLEND);
659         }
660
661         if (seq->startstill || seq->endstill) {
662                 glEnable(GL_BLEND);
663                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
664
665                 color3ubv_from_seq(scene, seq, col);
666                 UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.5f, 60);
667
668                 if (seq->flag & SEQ_MUTE) {
669                         col[3] = 96;
670                 }
671                 else {
672                         if (seq->flag & SELECT) {
673                                 col[3] = 255;
674                         }
675                         else {
676                                 col[3] = 170;
677                         }
678                 }
679
680                 immUniformColor4ubv(col);
681         }
682
683         if (seq->startstill) {
684                 immRectf(pos, x1, y1, (float)(seq->start), y2);
685         }
686         if (seq->endstill) {
687                 immRectf(pos, (float)(seq->start + seq->len), y1, x2, y2);
688         }
689
690         if (seq->startstill || seq->endstill) {
691                 glDisable(GL_BLEND);
692         }
693 }
694
695
696 /*
697  * Draw a sequence strip, bounds check already made
698  * ARegion is currently only used to get the windows width in pixels
699  * so wave file sample drawing precision is zoom adjusted
700  */
701 static void draw_seq_strip(const bContext *C, SpaceSeq *sseq, Scene *scene, ARegion *ar, Sequence *seq, int outline_tint, float pixelx)
702 {
703         View2D *v2d = &ar->v2d;
704         float x1, x2, y1, y2;
705         unsigned char col[4], background_col[4], is_single_image;
706         const float handsize_clamped = draw_seq_handle_size_get_clamped(seq, pixelx);
707
708         /* we need to know if this is a single image/color or not for drawing */
709         is_single_image = (char)BKE_sequence_single_check(seq);
710         
711         /* body */
712         x1 = (seq->startstill) ? seq->start : seq->startdisp;
713         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
714         x2 = (seq->endstill) ? (seq->start + seq->len) : seq->enddisp;
715         y2 = seq->machine + SEQ_STRIP_OFSTOP;
716
717         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
718
719         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
720
721         /* get the correct color per strip type*/
722         //color3ubv_from_seq(scene, seq, col);
723         color3ubv_from_seq(scene, seq, background_col);
724
725         if (seq->flag & SEQ_MUTE) {
726                 background_col[3] = 128;
727
728                 glEnable(GL_BLEND);
729                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
730         }
731         else {
732                 background_col[3] = 255;
733         }
734
735         if (seq->flag & SELECT) {
736                 UI_GetColorPtrShade3ubv(background_col, background_col, -50);
737         }
738
739         immUniformColor4ubv(background_col);
740
741         /* draw the main strip body */
742         if (is_single_image) {  /* single image */
743                 immRectf(pos, BKE_sequence_tx_get_final_left(seq, false),  y1,
744                               BKE_sequence_tx_get_final_right(seq, false),  y2);
745         }
746         else {  /* normal operation */
747                 immRectf(pos, x1, y1, x2, y2);
748         }
749
750         if (seq->flag & SEQ_MUTE) {
751                 glDisable(GL_BLEND);
752         }
753
754         if (!is_single_image) {
755                 if ((sseq->draw_flag & SEQ_DRAW_OFFSET_EXT) || (seq == special_seq_update)) {
756                         draw_sequence_extensions(scene, ar, seq, pos);
757                 }
758         }
759
760         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_LEFTHANDLE, pos);
761         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_RIGHTHANDLE, pos);
762
763         x1 = seq->startdisp;
764         x2 = seq->enddisp;
765         
766         /* draw sound wave */
767         if (seq->type == SEQ_TYPE_SOUND_RAM) {
768                 if (!(sseq->flag & SEQ_NO_WAVEFORMS)) {
769                         drawseqwave(v2d, C, sseq, scene, seq, x1, y1, x2, y2, BLI_rctf_size_x(&ar->v2d.cur) / ar->winx, pos);
770                 }
771         }
772
773         immUnbindProgram();
774
775         /* draw lock */
776         if (seq->flag & SEQ_LOCK) {
777                 glEnable(GL_BLEND);
778
779                 pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
780                 immBindBuiltinProgram(GPU_SHADER_2D_DIAG_STRIPES);
781
782                 immUniform4f("color1", 1.0f, 1.0f, 1.0f, 0.125f);
783                 immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.125f);
784                 immUniform1i("size1", 8);
785                 immUniform1i("size2", 8);
786
787                 immRectf(pos, x1, y1, x2, y2);
788
789                 immUnbindProgram();
790
791                 glDisable(GL_BLEND);
792         }
793
794         if (!BKE_sequence_is_valid_check(seq)) {
795                 glEnable(GL_BLEND);
796
797                 pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
798                 immBindBuiltinProgram(GPU_SHADER_2D_DIAG_STRIPES);
799
800                 immUniform4f("color1", 1.0f, 0.0f, 0.0f, 1.0f);
801                 immUniform4f("color2", 0.0f, 0.0f, 0.0f, 0.0f);
802                 immUniform1i("size1", 8);
803                 immUniform1i("size2", 8);
804
805                 immRectf(pos, x1, y1, x2, y2);
806
807                 immUnbindProgram();
808
809                 glDisable(GL_BLEND);
810         }
811
812         color3ubv_from_seq(scene, seq, col);
813
814         /* draw the strip outline */
815         color3ubv_from_seq(scene, seq, col);
816         if ((G.moving & G_TRANSFORM_SEQ) && (seq->flag & SELECT)) {
817                 if (seq->flag & SEQ_OVERLAP) {
818                         col[0] = 255; col[1] = col[2] = 40;
819                 }
820                 else
821                         UI_GetColorPtrShade3ubv(col, col, 120 + outline_tint);
822         }
823         else
824                 UI_GetColorPtrShade3ubv(col, col, outline_tint);
825         
826         if ((seq->type == SEQ_TYPE_META) ||
827             ((seq->type == SEQ_TYPE_SCENE) && (seq->flag & SEQ_SCENE_STRIPS)))
828         {
829                 drawmeta_contents(scene, seq, x1, y1, x2, y2);
830         }
831
832         pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
833
834         /* TODO: add back stippled line for muted strips? */
835         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
836
837         if (seq->flag & SEQ_MUTE) {
838                 col[3] = 96;
839
840                 glEnable(GL_BLEND);
841                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
842
843                 immUniformColor4ubv(col);
844         }
845         else {
846                 immUniformColor3ubv(col);
847         }
848
849         imm_draw_line_box(pos, x1, y1, x2, y2); /* outline */
850
851         immUnbindProgram();
852
853         /* calculate if seq is long enough to print a name */
854         x1 = seq->startdisp + handsize_clamped;
855         x2 = seq->enddisp   - handsize_clamped;
856
857         /* info text on the strip */
858         if (x1 < v2d->cur.xmin) x1 = v2d->cur.xmin;
859         else if (x1 > v2d->cur.xmax) x1 = v2d->cur.xmax;
860         if (x2 < v2d->cur.xmin) x2 = v2d->cur.xmin;
861         else if (x2 > v2d->cur.xmax) x2 = v2d->cur.xmax;
862
863         /* nice text here would require changing the view matrix for texture text */
864         if ((x2 - x1) / pixelx > 32) {
865                 draw_seq_text(v2d, sseq, seq, x1, x2, y1, y2, background_col);
866         }
867 }
868
869 void sequencer_special_update_set(Sequence *seq)
870 {
871         special_seq_update = seq;
872 }
873
874 Sequence *ED_sequencer_special_preview_get(void)
875 {
876         return special_seq_update;
877 }
878
879 void ED_sequencer_special_preview_set(bContext *C, const int mval[2])
880 {
881         Scene *scene = CTX_data_scene(C);
882         ARegion *ar = CTX_wm_region(C);
883         int hand;
884         Sequence *seq;
885         seq = find_nearest_seq(scene, &ar->v2d, &hand, mval);
886         sequencer_special_update_set(seq);
887 }
888
889 void ED_sequencer_special_preview_clear(void)
890 {
891         sequencer_special_update_set(NULL);
892 }
893
894 ImBuf *sequencer_ibuf_get(struct Main *bmain, Scene *scene, SpaceSeq *sseq, int cfra, int frame_ofs, const char *viewname)
895 {
896         SeqRenderData context;
897         ImBuf *ibuf;
898         int rectx, recty;
899         float render_size;
900         float proxy_size = 100.0;
901         short is_break = G.is_break;
902
903         render_size = sseq->render_size;
904         if (render_size == 0) {
905                 render_size = scene->r.size;
906         }
907         else {
908                 proxy_size = render_size;
909         }
910
911         if (render_size < 0) {
912                 return NULL;
913         }
914
915         rectx = (render_size * (float)scene->r.xsch) / 100.0f + 0.5f;
916         recty = (render_size * (float)scene->r.ysch) / 100.0f + 0.5f;
917
918         BKE_sequencer_new_render_data(
919                 bmain->eval_ctx, bmain, scene,
920                 rectx, recty, proxy_size,
921                 &context);
922         context.view_id = BKE_scene_multiview_view_id_get(&scene->r, viewname);
923
924         /* sequencer could start rendering, in this case we need to be sure it wouldn't be canceled
925          * by Esc pressed somewhere in the past
926          */
927         G.is_break = false;
928
929         if (special_seq_update)
930                 ibuf = BKE_sequencer_give_ibuf_direct(&context, cfra + frame_ofs, special_seq_update);
931         else if (!U.prefetchframes) // XXX || (G.f & G_PLAYANIM) == 0) {
932                 ibuf = BKE_sequencer_give_ibuf(&context, cfra + frame_ofs, sseq->chanshown);
933         else
934                 ibuf = BKE_sequencer_give_ibuf_threaded(&context, cfra + frame_ofs, sseq->chanshown);
935
936         /* restore state so real rendering would be canceled (if needed) */
937         G.is_break = is_break;
938
939         return ibuf;
940 }
941
942 static void sequencer_check_scopes(SequencerScopes *scopes, ImBuf *ibuf)
943 {
944         if (scopes->reference_ibuf != ibuf) {
945                 if (scopes->zebra_ibuf) {
946                         IMB_freeImBuf(scopes->zebra_ibuf);
947                         scopes->zebra_ibuf = NULL;
948                 }
949
950                 if (scopes->waveform_ibuf) {
951                         IMB_freeImBuf(scopes->waveform_ibuf);
952                         scopes->waveform_ibuf = NULL;
953                 }
954
955                 if (scopes->sep_waveform_ibuf) {
956                         IMB_freeImBuf(scopes->sep_waveform_ibuf);
957                         scopes->sep_waveform_ibuf = NULL;
958                 }
959
960                 if (scopes->vector_ibuf) {
961                         IMB_freeImBuf(scopes->vector_ibuf);
962                         scopes->vector_ibuf = NULL;
963                 }
964
965                 if (scopes->histogram_ibuf) {
966                         IMB_freeImBuf(scopes->histogram_ibuf);
967                         scopes->histogram_ibuf = NULL;
968                 }
969         }
970 }
971
972 static ImBuf *sequencer_make_scope(Scene *scene, ImBuf *ibuf, ImBuf *(*make_scope_cb) (ImBuf *ibuf))
973 {
974         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
975         ImBuf *scope;
976         
977         IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
978                                                              &scene->display_settings);
979
980         scope = make_scope_cb(display_ibuf);
981
982         IMB_freeImBuf(display_ibuf);
983
984         return scope;
985 }
986
987 static void sequencer_display_size(Scene *scene, SpaceSeq *sseq, float r_viewrect[2])
988 {
989         float render_size, proxy_size;
990
991         if (sseq->render_size == SEQ_PROXY_RENDER_SIZE_SCENE) {
992                 render_size = (float)scene->r.size / 100.0f;
993                 proxy_size = 1.0f;
994         }
995         else {
996                 render_size = (float)sseq->render_size / 100.0f;
997                 proxy_size = render_size;
998         }
999
1000         r_viewrect[0] = (render_size * (float)scene->r.xsch);
1001         r_viewrect[1] = (render_size * (float)scene->r.ysch);
1002
1003         /* rectx = viewrectx + 0.5f; */ /* UNUSED */
1004         /* recty = viewrecty + 0.5f; */ /* UNUSED */
1005
1006         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1007                 r_viewrect[0] *= scene->r.xasp / scene->r.yasp;
1008                 r_viewrect[0] /= proxy_size;
1009                 r_viewrect[1] /= proxy_size;
1010         }
1011 }
1012
1013 static void sequencer_draw_gpencil(const bContext *C)
1014 {
1015         /* draw grease-pencil (image aligned) */
1016         ED_gpencil_draw_2dimage(C);
1017
1018         /* ortho at pixel level */
1019         UI_view2d_view_restore(C);
1020
1021         /* draw grease-pencil (screen aligned) */
1022         ED_gpencil_draw_view2d(C, 0);
1023 }
1024
1025 /* draws content borders plus safety borders if needed */
1026 static void sequencer_draw_borders(const SpaceSeq *sseq, const View2D *v2d, const Scene *scene)
1027 {
1028         float x1 = v2d->tot.xmin;
1029         float y1 = v2d->tot.ymin;
1030         float x2 = v2d->tot.xmax;
1031         float y2 = v2d->tot.ymax;
1032
1033         glLineWidth(1.0f);
1034
1035         /* border */
1036         const uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1037
1038         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_COLOR);
1039
1040         float viewport_size[4];
1041         glGetFloatv(GL_VIEWPORT, viewport_size);
1042         immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC);
1043
1044         immUniformThemeColor(TH_BACK);
1045         immUniform1i("num_colors", 0);  /* Simple dashes. */
1046         immUniform1f("dash_width", 6.0f);
1047         immUniform1f("dash_factor", 0.5f);
1048
1049         imm_draw_line_box(shdr_pos, x1 - 0.5f, y1 - 0.5f, x2 + 0.5f, y2 + 0.5f);
1050
1051         /* safety border */
1052         if (sseq->flag & SEQ_SHOW_SAFE_MARGINS) {
1053                 immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
1054
1055                 UI_draw_safe_areas(
1056                         shdr_pos, x1, x2, y1, y2,
1057                         scene->safe_areas.title,
1058                         scene->safe_areas.action);
1059
1060                 if (sseq->flag & SEQ_SHOW_SAFE_CENTER) {
1061                         UI_draw_safe_areas(
1062                                 shdr_pos, x1, x2, y1, y2,
1063                                 scene->safe_areas.title_center,
1064                                 scene->safe_areas.action_center);
1065                 }
1066         }
1067
1068         immUnbindProgram();
1069 }
1070
1071 /* draws checkerboard background for transparent content */
1072 static void sequencer_draw_background(
1073         const SpaceSeq *sseq, View2D *v2d, const float viewrect[2], const bool draw_overlay)
1074 {
1075         /* setting up the view */
1076         UI_view2d_totRect_set(v2d, viewrect[0] + 0.5f, viewrect[1] + 0.5f);
1077         UI_view2d_curRect_validate(v2d);
1078         UI_view2d_view_ortho(v2d);
1079
1080         /* only draw alpha for main buffer */
1081         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1082                 if ((sseq->flag & SEQ_USE_ALPHA) && !draw_overlay) {
1083                         imm_draw_checker_box(v2d->tot.xmin, v2d->tot.ymin, v2d->tot.xmax, v2d->tot.ymax);
1084                 }
1085         }
1086 }
1087
1088 void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq, int cfra, int frame_ofs, bool draw_overlay, bool draw_backdrop)
1089 {
1090         struct Main *bmain = CTX_data_main(C);
1091         struct ImBuf *ibuf = NULL;
1092         struct ImBuf *scope = NULL;
1093         struct View2D *v2d = &ar->v2d;
1094         /* int rectx, recty; */ /* UNUSED */
1095         float viewrect[2];
1096         float col[3];
1097         GLuint texid;
1098         void *display_buffer;
1099         void *cache_handle = NULL;
1100         const bool is_imbuf = ED_space_sequencer_check_show_imbuf(sseq);
1101         int format, type;
1102         bool glsl_used = false;
1103         const bool draw_gpencil = ((sseq->flag & SEQ_SHOW_GPENCIL) && sseq->gpd);
1104         const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
1105         bool draw_metadata = false;
1106
1107         if (G.is_rendering == false && (scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
1108                 /* stop all running jobs, except screen one. currently previews frustrate Render
1109                  * needed to make so sequencer's rendering doesn't conflict with compositor
1110                  */
1111                 WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_COMPOSITE);
1112
1113                 if ((scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
1114                         /* in case of final rendering used for preview, kill all previews,
1115                          * otherwise threading conflict will happen in rendering module
1116                          */
1117                         WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_RENDER_PREVIEW);
1118                 }
1119         }
1120
1121         if ((!draw_overlay || sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) && !draw_backdrop) {
1122                 UI_GetThemeColor3fv(TH_SEQ_PREVIEW, col);
1123                 glClearColor(col[0], col[1], col[2], 0.0);
1124                 glClear(GL_COLOR_BUFFER_BIT);
1125         }
1126
1127         /* only initialize the preview if a render is in progress */
1128         if (G.is_rendering)
1129                 return;
1130
1131         if (sseq->render_size == SEQ_PROXY_RENDER_SIZE_NONE) {
1132                 return;
1133         }
1134
1135         /* for now we only support Left/Right */
1136         ibuf = sequencer_ibuf_get(bmain, scene, sseq, cfra, frame_ofs, names[sseq->multiview_eye]);
1137
1138         if ((ibuf == NULL) ||
1139             (ibuf->rect == NULL && ibuf->rect_float == NULL))
1140         {
1141                 sequencer_display_size(scene, sseq, viewrect);
1142
1143                 sequencer_draw_background(sseq, v2d, viewrect, false);
1144                 sequencer_draw_borders(sseq, v2d, scene);
1145
1146                 /* gpencil can also be drawn without a valid imbuf */
1147                 if ((draw_gpencil && is_imbuf) && !draw_overlay) {
1148                         sequencer_draw_gpencil(C);
1149                 }
1150
1151                 return;
1152         }
1153
1154         sequencer_display_size(scene, sseq, viewrect);
1155
1156         if (!draw_backdrop && (sseq->mainb != SEQ_DRAW_IMG_IMBUF || sseq->zebra != 0)) {
1157                 SequencerScopes *scopes = &sseq->scopes;
1158
1159                 sequencer_check_scopes(scopes, ibuf);
1160
1161                 switch (sseq->mainb) {
1162                         case SEQ_DRAW_IMG_IMBUF:
1163                                 if (!scopes->zebra_ibuf) {
1164                                         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
1165
1166                                         if (display_ibuf->rect_float) {
1167                                                 IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
1168                                                                                              &scene->display_settings);
1169                                         }
1170                                         scopes->zebra_ibuf = make_zebra_view_from_ibuf(display_ibuf, sseq->zebra);
1171                                         IMB_freeImBuf(display_ibuf);
1172                                 }
1173                                 scope = scopes->zebra_ibuf;
1174                                 break;
1175                         case SEQ_DRAW_IMG_WAVEFORM:
1176                                 if ((sseq->flag & SEQ_DRAW_COLOR_SEPARATED) != 0) {
1177                                         if (!scopes->sep_waveform_ibuf)
1178                                                 scopes->sep_waveform_ibuf = sequencer_make_scope(scene, ibuf, make_sep_waveform_view_from_ibuf);
1179                                         scope = scopes->sep_waveform_ibuf;
1180                                 }
1181                                 else {
1182                                         if (!scopes->waveform_ibuf)
1183                                                 scopes->waveform_ibuf = sequencer_make_scope(scene, ibuf, make_waveform_view_from_ibuf);
1184                                         scope = scopes->waveform_ibuf;
1185                                 }
1186                                 break;
1187                         case SEQ_DRAW_IMG_VECTORSCOPE:
1188                                 if (!scopes->vector_ibuf)
1189                                         scopes->vector_ibuf = sequencer_make_scope(scene, ibuf, make_vectorscope_view_from_ibuf);
1190                                 scope = scopes->vector_ibuf;
1191                                 break;
1192                         case SEQ_DRAW_IMG_HISTOGRAM:
1193                                 if (!scopes->histogram_ibuf)
1194                                         scopes->histogram_ibuf = sequencer_make_scope(scene, ibuf, make_histogram_view_from_ibuf);
1195                                 scope = scopes->histogram_ibuf;
1196                                 break;
1197                 }
1198
1199                 /* future files may have new scopes we don't catch above */
1200                 if (scope) {
1201                         scopes->reference_ibuf = ibuf;
1202                         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1203                                 /* scopes drawn in image preview use viewrect from orig ibuf - currently that's only zebra */
1204                         }
1205                         else {
1206                                 viewrect[0] = scope->x;
1207                                 viewrect[1] = scope->y;
1208                         }
1209                 }
1210                 else {
1211                         scopes->reference_ibuf = NULL;
1212                 }
1213         }
1214
1215         if (!draw_backdrop) {
1216                 sequencer_draw_background(sseq, v2d, viewrect, draw_overlay);
1217         }
1218
1219         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF && sseq->flag & SEQ_USE_ALPHA) {
1220                 glEnable(GL_BLEND);
1221                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1222         }
1223
1224         /* Format needs to be created prior to any immBindProgram call.
1225          * Do it here because OCIO binds it's own shader.
1226          */
1227         Gwn_VertFormat *imm_format = immVertexFormat();
1228         unsigned int pos = GWN_vertformat_attr_add(imm_format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1229         unsigned int texCoord = GWN_vertformat_attr_add(imm_format, "texCoord", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1230
1231         if (scope) {
1232                 IMB_freeImBuf(ibuf);
1233                 ibuf = scope;
1234
1235                 if (ibuf->rect_float && ibuf->rect == NULL) {
1236                         IMB_rect_from_float(ibuf);
1237                 }
1238
1239                 display_buffer = (unsigned char *)ibuf->rect;
1240                 format = GL_RGBA;
1241                 type = GL_UNSIGNED_BYTE;
1242         }
1243         else {
1244                 bool force_fallback = false;
1245
1246                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1247                 force_fallback |= (ibuf->dither != 0.0f);
1248
1249                 if (force_fallback) {
1250                         /* Fallback to CPU based color space conversion */
1251                         glsl_used = false;
1252                         format = GL_RGBA;
1253                         type = GL_UNSIGNED_BYTE;
1254                         display_buffer = NULL;
1255                 }
1256                 else if (ibuf->rect_float) {
1257                         display_buffer = ibuf->rect_float;
1258
1259                         if (ibuf->channels == 4) {
1260                                 format = GL_RGBA;
1261                         }
1262                         else if (ibuf->channels == 3) {
1263                                 format = GL_RGB;
1264                         }
1265                         else {
1266                                 BLI_assert(!"Incompatible number of channels for float buffer in sequencer");
1267                                 format = GL_RGBA;
1268                                 display_buffer = NULL;
1269                         }
1270
1271                         type = GL_FLOAT;
1272
1273                         if (ibuf->float_colorspace) {
1274                                 glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->float_colorspace, ibuf->dither, true);
1275                         }
1276                         else {
1277                                 glsl_used = IMB_colormanagement_setup_glsl_draw_ctx(C, ibuf->dither, true);
1278                         }
1279                 }
1280                 else if (ibuf->rect) {
1281                         display_buffer = ibuf->rect;
1282                         format = GL_RGBA;
1283                         type = GL_UNSIGNED_BYTE;
1284
1285                         glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, ibuf->dither, false);
1286                 }
1287                 else {
1288                         format = GL_RGBA;
1289                         type = GL_UNSIGNED_BYTE;
1290                         display_buffer = NULL;
1291                 }
1292
1293                 /* there's a data to be displayed, but GLSL is not initialized
1294                  * properly, in this case we fallback to CPU-based display transform
1295                  */
1296                 if ((ibuf->rect || ibuf->rect_float) && !glsl_used) {
1297                         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
1298                         format = GL_RGBA;
1299                         type = GL_UNSIGNED_BYTE;
1300                 }
1301         }
1302
1303         if (draw_backdrop) {
1304                 /* XXX: need to load identity projection too? */
1305                 gpuPushMatrix();
1306                 gpuLoadIdentity();
1307         }
1308
1309         glGenTextures(1, (GLuint *)&texid);
1310
1311         glBindTexture(GL_TEXTURE_2D, texid);
1312
1313         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1314         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1315
1316         if (type == GL_FLOAT)
1317                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1318         else
1319                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1320
1321         if (!glsl_used) {
1322                 immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_COLOR);
1323                 immUniformColor3f(1.0f, 1.0f, 1.0f);
1324                 immUniform1i("image", GL_TEXTURE0);
1325         }
1326
1327         immBegin(GWN_PRIM_TRI_FAN, 4);
1328
1329         if (draw_overlay) {
1330                 if (sseq->overlay_type == SEQ_DRAW_OVERLAY_RECT) {
1331                         rctf tot_clip;
1332                         tot_clip.xmin = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmin);
1333                         tot_clip.ymin = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymin);
1334                         tot_clip.xmax = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmax);
1335                         tot_clip.ymax = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymax);
1336
1337                         immAttrib2f(texCoord, scene->ed->over_border.xmin, scene->ed->over_border.ymin);
1338                         immVertex2f(pos, tot_clip.xmin, tot_clip.ymin);
1339
1340                         immAttrib2f(texCoord, scene->ed->over_border.xmin, scene->ed->over_border.ymax);
1341                         immVertex2f(pos, tot_clip.xmin, tot_clip.ymax);
1342
1343                         immAttrib2f(texCoord, scene->ed->over_border.xmax, scene->ed->over_border.ymax);
1344                         immVertex2f(pos, tot_clip.xmax, tot_clip.ymax);
1345
1346                         immAttrib2f(texCoord, scene->ed->over_border.xmax, scene->ed->over_border.ymin);
1347                         immVertex2f(pos, tot_clip.xmax, tot_clip.ymin);
1348                 }
1349                 else if (sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) {
1350                         immAttrib2f(texCoord, 0.0f, 0.0f);
1351                         immVertex2f(pos, v2d->tot.xmin, v2d->tot.ymin);
1352
1353                         immAttrib2f(texCoord, 0.0f, 1.0f);
1354                         immVertex2f(pos, v2d->tot.xmin, v2d->tot.ymax);
1355
1356                         immAttrib2f(texCoord, 1.0f, 1.0f);
1357                         immVertex2f(pos, v2d->tot.xmax, v2d->tot.ymax);
1358
1359                         immAttrib2f(texCoord, 1.0f, 0.0f);
1360                         immVertex2f(pos, v2d->tot.xmax, v2d->tot.ymin);
1361                 }
1362         }
1363         else if (draw_backdrop) {
1364                 float aspect;
1365                 float image_aspect = viewrect[0] / viewrect[1];
1366                 float imagex, imagey;
1367
1368                 aspect = BLI_rcti_size_x(&ar->winrct) / (float)BLI_rcti_size_y(&ar->winrct);
1369
1370                 if (aspect >= image_aspect) {
1371                         imagex = image_aspect / aspect;
1372                         imagey = 1.0f;
1373                 }
1374                 else {
1375                         imagex = 1.0f;
1376                         imagey = aspect / image_aspect;
1377                 }
1378
1379                 immAttrib2f(texCoord, 0.0f, 0.0f);
1380                 immVertex2f(pos, -imagex, -imagey);
1381
1382                 immAttrib2f(texCoord, 0.0f, 1.0f);
1383                 immVertex2f(pos, -imagex, imagey);
1384
1385                 immAttrib2f(texCoord, 1.0f, 1.0f);
1386                 immVertex2f(pos, imagex, imagey);
1387
1388                 immAttrib2f(texCoord, 1.0f, 0.0f);
1389                 immVertex2f(pos, imagex, -imagey);
1390         }
1391         else {
1392                 draw_metadata = ((sseq->flag & SEQ_SHOW_METADATA) != 0);
1393
1394                 immAttrib2f(texCoord, 0.0f, 0.0f);
1395                 immVertex2f(pos, v2d->tot.xmin, v2d->tot.ymin);
1396
1397                 immAttrib2f(texCoord, 0.0f, 1.0f);
1398                 immVertex2f(pos, v2d->tot.xmin, v2d->tot.ymax);
1399
1400                 immAttrib2f(texCoord, 1.0f, 1.0f);
1401                 immVertex2f(pos, v2d->tot.xmax, v2d->tot.ymax);
1402
1403                 immAttrib2f(texCoord, 1.0f, 0.0f);
1404                 immVertex2f(pos, v2d->tot.xmax, v2d->tot.ymin);
1405         }
1406
1407         immEnd();
1408
1409         glBindTexture(GL_TEXTURE_2D, 0);
1410
1411         if (!glsl_used) {
1412                 immUnbindProgram();
1413         }
1414
1415         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF && sseq->flag & SEQ_USE_ALPHA) {
1416                 glDisable(GL_BLEND);
1417         }
1418
1419         glDeleteTextures(1, &texid);
1420
1421         if (glsl_used)
1422                 IMB_colormanagement_finish_glsl_draw();
1423
1424         if (cache_handle)
1425                 IMB_display_buffer_release(cache_handle);
1426
1427         if (!scope)
1428                 IMB_freeImBuf(ibuf);
1429
1430         if (draw_metadata) {
1431                 ED_region_image_metadata_draw(0.0, 0.0, ibuf, &v2d->tot, 1.0, 1.0);
1432         }
1433
1434         if (draw_backdrop) {
1435                 gpuPopMatrix();
1436                 return;
1437         }
1438
1439         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1440                 sequencer_draw_borders(sseq, v2d, scene);
1441         }
1442
1443         if (draw_gpencil && is_imbuf) {
1444                 sequencer_draw_gpencil(C);
1445         }
1446         else {
1447                 /* ortho at pixel level */
1448                 UI_view2d_view_restore(C);
1449         }
1450
1451
1452         /* NOTE: sequencer mask editing isnt finished, the draw code is working but editing not,
1453          * for now just disable drawing since the strip frame will likely be offset */
1454
1455         //if (sc->mode == SC_MODE_MASKEDIT) {
1456         if (0 && sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1457                 Mask *mask = BKE_sequencer_mask_get(scene);
1458
1459                 if (mask) {
1460                         int width, height;
1461                         float aspx = 1.0f, aspy = 1.0f;
1462                         // ED_mask_get_size(C, &width, &height);
1463
1464                         //Scene *scene = CTX_data_scene(C);
1465                         width = (scene->r.size * scene->r.xsch) / 100;
1466                         height = (scene->r.size * scene->r.ysch) / 100;
1467
1468                         ED_mask_draw_region(mask, ar,
1469                                             0, 0, 0,  /* TODO */
1470                                             width, height,
1471                                             aspx, aspy,
1472                                             false, true,
1473                                             NULL, C);
1474                 }
1475         }
1476 }
1477
1478 #if 0
1479 void drawprefetchseqspace(Scene *scene, ARegion *UNUSED(ar), SpaceSeq *sseq)
1480 {
1481         int rectx, recty;
1482         int render_size = sseq->render_size;
1483         int proxy_size = 100.0; 
1484         if (render_size == 0) {
1485                 render_size = scene->r.size;
1486         }
1487         else {
1488                 proxy_size = render_size;
1489         }
1490         if (render_size < 0) {
1491                 return;
1492         }
1493
1494         rectx = (render_size * scene->r.xsch) / 100;
1495         recty = (render_size * scene->r.ysch) / 100;
1496
1497         if (sseq->mainb != SEQ_DRAW_SEQUENCE) {
1498                 give_ibuf_prefetch_request(
1499                     rectx, recty, (scene->r.cfra), sseq->chanshown,
1500                     proxy_size);
1501         }
1502 }
1503 #endif
1504
1505 /* draw backdrop of the sequencer strips view */
1506 static void draw_seq_backdrop(View2D *v2d)
1507 {
1508         int i;
1509
1510         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1511         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1512
1513         /* darker gray overlay over the view backdrop */
1514         immUniformThemeColorShade(TH_BACK, -20);
1515         immRectf(pos, v2d->cur.xmin,  -1.0,  v2d->cur.xmax,  1.0);
1516
1517         /* Alternating horizontal stripes */
1518         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1519
1520         while (i < v2d->cur.ymax) {
1521                 if (i & 1) {
1522                         immUniformThemeColorShade(TH_BACK, -15);
1523                 }
1524                 else {
1525                         immUniformThemeColorShade(TH_BACK, -25);
1526                 }
1527
1528                 immRectf(pos, v2d->cur.xmin,  i,  v2d->cur.xmax,  i + 1);
1529
1530                 i++;
1531         }
1532
1533         /* Darker lines separating the horizontal bands */
1534         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1535         int line_ct = (int)v2d->cur.ymax - i + 1;
1536         immUniformThemeColor(TH_GRID);
1537         immBegin(GWN_PRIM_LINES, line_ct * 2);
1538         while (line_ct--) {
1539                 immVertex2f(pos, v2d->cur.xmax, i);
1540                 immVertex2f(pos, v2d->cur.xmin, i);
1541         }
1542         immEnd();
1543
1544         immUnbindProgram();
1545 }
1546
1547 /* draw the contents of the sequencer strips view */
1548 static void draw_seq_strips(const bContext *C, Editing *ed, ARegion *ar)
1549 {
1550         Scene *scene = CTX_data_scene(C);
1551         View2D *v2d = &ar->v2d;
1552         SpaceSeq *sseq = CTX_wm_space_seq(C);
1553         Sequence *last_seq = BKE_sequencer_active_get(scene);
1554         int sel = 0, j;
1555         float pixelx = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
1556         
1557         /* loop through twice, first unselected, then selected */
1558         for (j = 0; j < 2; j++) {
1559                 Sequence *seq;
1560                 int outline_tint = (j) ? 40 : -40; /* highlighting around strip edges indicating selection */
1561                 
1562                 /* loop through strips, checking for those that are visible */
1563                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1564                         /* boundbox and selection tests for NOT drawing the strip... */
1565                         if ((seq->flag & SELECT) != sel) continue;
1566                         else if (seq == last_seq) continue;
1567                         else if (min_ii(seq->startdisp, seq->start) > v2d->cur.xmax) continue;
1568                         else if (max_ii(seq->enddisp, seq->start + seq->len) < v2d->cur.xmin) continue;
1569                         else if (seq->machine + 1.0f < v2d->cur.ymin) continue;
1570                         else if (seq->machine > v2d->cur.ymax) continue;
1571                         
1572                         /* strip passed all tests unscathed... so draw it now */
1573                         draw_seq_strip(C, sseq, scene, ar, seq, outline_tint, pixelx);
1574                 }
1575                 
1576                 /* draw selected next time round */
1577                 sel = SELECT;
1578         }
1579         
1580         /* draw the last selected last (i.e. 'active' in other parts of Blender), removes some overlapping error */
1581         if (last_seq)
1582                 draw_seq_strip(C, sseq, scene, ar, last_seq, 120, pixelx);
1583
1584         /* draw highlight when previewing a single strip */
1585         if (special_seq_update) {
1586                 const Sequence *seq = special_seq_update;
1587                 glEnable(GL_BLEND);
1588
1589                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1590                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1591
1592                 immUniformColor4ub(255, 255, 255, 48);
1593                 immRectf(pos, seq->startdisp, seq->machine + SEQ_STRIP_OFSBOTTOM, seq->enddisp, seq->machine + SEQ_STRIP_OFSTOP);
1594
1595                 immUnbindProgram();
1596
1597                 glDisable(GL_BLEND);
1598         }
1599 }
1600
1601 static void seq_draw_sfra_efra(Scene *scene, View2D *v2d)
1602 {
1603         const Editing *ed = BKE_sequencer_editing_get(scene, false);
1604         const int frame_sta = PSFRA;
1605         const int frame_end = PEFRA + 1;
1606
1607         glEnable(GL_BLEND);
1608
1609         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1610         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1611
1612         /* draw darkened area outside of active timeline 
1613          * frame range used is preview range or scene range */
1614         immUniformThemeColorShadeAlpha(TH_BACK, -25, -100);
1615
1616         if (frame_sta < frame_end) {
1617                 immRectf(pos, v2d->cur.xmin, v2d->cur.ymin, (float)frame_sta, v2d->cur.ymax);
1618                 immRectf(pos, (float)frame_end, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1619         }
1620         else {
1621                 immRectf(pos, v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1622         }
1623
1624         immUniformThemeColorShade(TH_BACK, -60);
1625
1626         /* thin lines where the actual frames are */
1627         immBegin(GWN_PRIM_LINES, 4);
1628
1629         immVertex2f(pos, frame_sta, v2d->cur.ymin);
1630         immVertex2f(pos, frame_sta, v2d->cur.ymax);
1631
1632         immVertex2f(pos, frame_end, v2d->cur.ymin);
1633         immVertex2f(pos, frame_end, v2d->cur.ymax);
1634
1635         immEnd();
1636
1637         if (ed && !BLI_listbase_is_empty(&ed->metastack)) {
1638                 MetaStack *ms = ed->metastack.last;
1639
1640                 immUniformColor4ub(255, 255, 255, 8);
1641                 immRectf(pos, ms->disp_range[0], v2d->cur.ymin, ms->disp_range[1], v2d->cur.ymax);
1642
1643                 immUniformThemeColorShade(TH_BACK, -40);
1644
1645                 immBegin(GWN_PRIM_LINES, 4);
1646
1647                 immVertex2f(pos, ms->disp_range[0], v2d->cur.ymin);
1648                 immVertex2f(pos, ms->disp_range[0], v2d->cur.ymax);
1649
1650                 immVertex2f(pos, ms->disp_range[1], v2d->cur.ymin);
1651                 immVertex2f(pos, ms->disp_range[1], v2d->cur.ymax);
1652
1653                 immEnd();
1654         }
1655
1656         immUnbindProgram();
1657
1658         glDisable(GL_BLEND);
1659 }
1660
1661 /* Draw Timeline/Strip Editor Mode for Sequencer */
1662 void draw_timeline_seq(const bContext *C, ARegion *ar)
1663 {
1664         Scene *scene = CTX_data_scene(C);
1665         Editing *ed = BKE_sequencer_editing_get(scene, false);
1666         SpaceSeq *sseq = CTX_wm_space_seq(C);
1667         View2D *v2d = &ar->v2d;
1668         View2DScrollers *scrollers;
1669         short unit = 0, flag = 0;
1670         float col[3];
1671         
1672         /* clear and setup matrix */
1673         UI_GetThemeColor3fv(TH_BACK, col);
1674         if (ed && ed->metastack.first) 
1675                 glClearColor(col[0], col[1], col[2] - 0.1f, 0.0f);
1676         else 
1677                 glClearColor(col[0], col[1], col[2], 0.0f);
1678         glClear(GL_COLOR_BUFFER_BIT);
1679
1680         UI_view2d_view_ortho(v2d);
1681         
1682         
1683         /* calculate extents of sequencer strips/data 
1684          * NOTE: needed for the scrollers later
1685          */
1686         boundbox_seq(scene, &v2d->tot);
1687         
1688         
1689         /* draw backdrop */
1690         draw_seq_backdrop(v2d);
1691         
1692         /* regular grid-pattern over the rest of the view (i.e. 25-frame grid lines) */
1693         // NOTE: the gridlines are currently spaced every 25 frames, which is only fine for 25 fps, but maybe not for 30...
1694         UI_view2d_constant_grid_draw(v2d);
1695
1696         /* Only draw backdrop in pure sequence view. */
1697         if (sseq->view == SEQ_VIEW_SEQUENCE && sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1698                 draw_image_seq(C, scene, ar, sseq, scene->r.cfra, 0, false, true);
1699                 UI_view2d_view_ortho(v2d);
1700         }
1701                 
1702         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
1703         
1704         seq_draw_sfra_efra(scene, v2d);
1705
1706         /* sequence strips (if there is data available to be drawn) */
1707         if (ed) {
1708                 /* draw the data */
1709                 draw_seq_strips(C, ed, ar);
1710                 
1711                 /* text draw cached (for sequence names), in pixelspace now */
1712                 UI_view2d_text_cache_draw(ar);
1713         }
1714         
1715         /* current frame */
1716         UI_view2d_view_ortho(v2d);
1717         if ((sseq->flag & SEQ_DRAWFRAMES) == 0)      flag |= DRAWCFRA_UNIT_SECONDS;
1718         if ((sseq->flag & SEQ_NO_DRAW_CFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
1719         ANIM_draw_cfra(C, v2d, flag);
1720         
1721         /* markers */
1722         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1723         ED_markers_draw(C, DRAW_MARKERS_LINES | DRAW_MARKERS_MARGIN);
1724         
1725         /* preview range */
1726         UI_view2d_view_ortho(v2d);
1727         ANIM_draw_previewrange(C, v2d, 1);
1728
1729         /* overlap playhead */
1730         if (scene->ed && scene->ed->over_flag & SEQ_EDIT_OVERLAY_SHOW) {
1731                 int cfra_over = (scene->ed->over_flag & SEQ_EDIT_OVERLAY_ABS) ? scene->ed->over_cfra : scene->r.cfra + scene->ed->over_ofs;
1732
1733                 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
1734                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1735
1736                 immUniformColor3f(0.2f, 0.2f, 0.2f);
1737
1738                 immBegin(GWN_PRIM_LINES, 2);
1739                 immVertex2f(pos, cfra_over, v2d->cur.ymin);
1740                 immVertex2f(pos, cfra_over, v2d->cur.ymax);
1741                 immEnd();
1742
1743                 immUnbindProgram();
1744         }
1745
1746         /* callback */
1747         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1748
1749         /* reset view matrix */
1750         UI_view2d_view_restore(C);
1751
1752         /* scrollers */
1753         unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDS;
1754         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_UNIT_VALUES, V2D_GRID_CLAMP);
1755         UI_view2d_scrollers_draw(C, v2d, scrollers);
1756         UI_view2d_scrollers_free(scrollers);
1757 }
1758
1759