UI: Refactor timecode functions into BLI_timecode
[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 "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39
40 #include "IMB_imbuf_types.h"
41
42 #include "DNA_scene_types.h"
43 #include "DNA_mask_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_userdef_types.h"
47 #include "DNA_sound_types.h"
48
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 #include "BKE_sequencer.h"
53 #include "BKE_sound.h"
54
55 #include "IMB_colormanagement.h"
56 #include "IMB_imbuf.h"
57
58 #include "BIF_gl.h"
59 #include "BIF_glutil.h"
60
61 #include "ED_anim_api.h"
62 #include "ED_gpencil.h"
63 #include "ED_markers.h"
64 #include "ED_mask.h"
65 #include "ED_sequencer.h"
66 #include "ED_types.h"
67 #include "ED_space_api.h"
68
69 #include "UI_interface.h"
70 #include "UI_resources.h"
71 #include "UI_view2d.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 /* own include */
77 #include "sequencer_intern.h"
78
79
80 #define SEQ_LEFTHANDLE   1
81 #define SEQ_RIGHTHANDLE  2
82
83 #define SEQ_HANDLE_SIZE_MIN  7.0f
84 #define SEQ_HANDLE_SIZE_MAX 40.0f
85
86
87 /* Note, Don't use SEQ_BEGIN/SEQ_END while drawing!
88  * it messes up transform, - Campbell */
89 static void draw_shadedstrip(Sequence *seq, unsigned char col[3], float x1, float y1, float x2, float y2);
90
91 static void get_seq_color3ubv(Scene *curscene, Sequence *seq, unsigned char col[3])
92 {
93         unsigned char blendcol[3];
94         SolidColorVars *colvars = (SolidColorVars *)seq->effectdata;
95
96         switch (seq->type) {
97                 case SEQ_TYPE_IMAGE:
98                         UI_GetThemeColor3ubv(TH_SEQ_IMAGE, col);
99                         break;
100
101                 case SEQ_TYPE_META:
102                         UI_GetThemeColor3ubv(TH_SEQ_META, col);
103                         break;
104
105                 case SEQ_TYPE_MOVIE:
106                         UI_GetThemeColor3ubv(TH_SEQ_MOVIE, col);
107                         break;
108
109                 case SEQ_TYPE_MOVIECLIP:
110                         UI_GetThemeColor3ubv(TH_SEQ_MOVIECLIP, col);
111                         break;
112
113                 case SEQ_TYPE_MASK:
114                         UI_GetThemeColor3ubv(TH_SEQ_MASK, col); /* TODO */
115                         break;
116
117                 case SEQ_TYPE_SCENE:
118                         UI_GetThemeColor3ubv(TH_SEQ_SCENE, col);
119                 
120                         if (seq->scene == curscene) {
121                                 UI_GetColorPtrShade3ubv(col, col, 20);
122                         }
123                         break;
124                 
125                 /* transitions */
126                 case SEQ_TYPE_CROSS:
127                 case SEQ_TYPE_GAMCROSS:
128                 case SEQ_TYPE_WIPE:
129                         UI_GetThemeColor3ubv(TH_SEQ_TRANSITION, col);
130
131                         /* slightly offset hue to distinguish different effects */
132                         if (seq->type == SEQ_TYPE_CROSS)    rgb_byte_set_hue_float_offset(col, 0.04);
133                         if (seq->type == SEQ_TYPE_GAMCROSS) rgb_byte_set_hue_float_offset(col, 0.08);
134                         if (seq->type == SEQ_TYPE_WIPE)     rgb_byte_set_hue_float_offset(col, 0.12);
135                         break;
136
137                 /* effects */
138                 case SEQ_TYPE_TRANSFORM:
139                 case SEQ_TYPE_SPEED:
140                 case SEQ_TYPE_ADD:
141                 case SEQ_TYPE_SUB:
142                 case SEQ_TYPE_MUL:
143                 case SEQ_TYPE_ALPHAOVER:
144                 case SEQ_TYPE_ALPHAUNDER:
145                 case SEQ_TYPE_OVERDROP:
146                 case SEQ_TYPE_GLOW:
147                 case SEQ_TYPE_MULTICAM:
148                 case SEQ_TYPE_ADJUSTMENT:
149                         UI_GetThemeColor3ubv(TH_SEQ_EFFECT, col);
150
151                         /* slightly offset hue to distinguish different effects */
152                         if      (seq->type == SEQ_TYPE_ADD)        rgb_byte_set_hue_float_offset(col, 0.04);
153                         else if (seq->type == SEQ_TYPE_SUB)        rgb_byte_set_hue_float_offset(col, 0.08);
154                         else if (seq->type == SEQ_TYPE_MUL)        rgb_byte_set_hue_float_offset(col, 0.12);
155                         else if (seq->type == SEQ_TYPE_ALPHAOVER)  rgb_byte_set_hue_float_offset(col, 0.16);
156                         else if (seq->type == SEQ_TYPE_ALPHAUNDER) rgb_byte_set_hue_float_offset(col, 0.20);
157                         else if (seq->type == SEQ_TYPE_OVERDROP)   rgb_byte_set_hue_float_offset(col, 0.24);
158                         else if (seq->type == SEQ_TYPE_GLOW)       rgb_byte_set_hue_float_offset(col, 0.28);
159                         else if (seq->type == SEQ_TYPE_TRANSFORM)  rgb_byte_set_hue_float_offset(col, 0.36);
160                         else if (seq->type == SEQ_TYPE_MULTICAM)   rgb_byte_set_hue_float_offset(col, 0.32);
161                         else if (seq->type == SEQ_TYPE_ADJUSTMENT) rgb_byte_set_hue_float_offset(col, 0.40);
162                         break;
163
164                 case SEQ_TYPE_COLOR:
165                         rgb_float_to_uchar(col, colvars->col);
166                         break;
167
168                 case SEQ_TYPE_SOUND_RAM:
169                         UI_GetThemeColor3ubv(TH_SEQ_AUDIO, col);
170                         blendcol[0] = blendcol[1] = blendcol[2] = 128;
171                         if (seq->flag & SEQ_MUTE) UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.5, 20);
172                         break;
173                 
174                 default:
175                         col[0] = 10; col[1] = 255; col[2] = 40;
176                         break;
177         }
178 }
179
180 static void drawseqwave(Scene *scene, Sequence *seq, float x1, float y1, float x2, float y2, float stepsize)
181 {
182         /*
183          * x1 is the starting x value to draw the wave,
184          * x2 the end x value, same for y1 and y2
185          * stepsize is width of a pixel.
186          */
187         if (seq->flag & SEQ_AUDIO_DRAW_WAVEFORM) {
188                 int i, j, pos;
189                 int length = floor((x2 - x1) / stepsize) + 1;
190                 float ymid = (y1 + y2) / 2;
191                 float yscale = (y2 - y1) / 2;
192                 float samplestep;
193                 float startsample, endsample;
194                 float value;
195
196                 SoundWaveform *waveform;
197
198                 if (!seq->sound->waveform)
199                         sound_read_waveform(seq->sound);
200
201                 if (!seq->sound->waveform)
202                         return;  /* zero length sound */
203
204                 waveform = seq->sound->waveform;
205
206                 if (!waveform)
207                         return;
208
209                 startsample = floor((seq->startofs + seq->anim_startofs) / FPS * SOUND_WAVE_SAMPLES_PER_SECOND);
210                 endsample = ceil((seq->startofs + seq->anim_startofs + seq->enddisp - seq->startdisp) / FPS * SOUND_WAVE_SAMPLES_PER_SECOND);
211                 samplestep = (endsample - startsample) * stepsize / (x2 - x1);
212
213                 if (length > floor((waveform->length - startsample) / samplestep))
214                         length = floor((waveform->length - startsample) / samplestep);
215
216                 glBegin(GL_LINE_STRIP);
217                 for (i = 0; i < length; i++) {
218                         pos = startsample + i * samplestep;
219
220                         value = waveform->data[pos * 3];
221
222                         for (j = pos + 1; (j < waveform->length) && (j < pos + samplestep); j++) {
223                                 if (value > waveform->data[j * 3])
224                                         value = waveform->data[j * 3];
225                         }
226
227                         glVertex2f(x1 + i * stepsize, ymid + value * yscale);
228                 }
229                 glEnd();
230
231                 glBegin(GL_LINE_STRIP);
232                 for (i = 0; i < length; i++) {
233                         pos = startsample + i * samplestep;
234
235                         value = waveform->data[pos * 3 + 1];
236
237                         for (j = pos + 1; (j < waveform->length) && (j < pos + samplestep); j++) {
238                                 if (value < waveform->data[j * 3 + 1])
239                                         value = waveform->data[j * 3 + 1];
240                         }
241
242                         glVertex2f(x1 + i * stepsize, ymid + value * yscale);
243                 }
244                 glEnd();
245         }
246 }
247
248 static void drawmeta_stipple(int value)
249 {
250         if (value) {
251                 glEnable(GL_POLYGON_STIPPLE);
252                 glPolygonStipple(stipple_halftone);
253                 
254                 glEnable(GL_LINE_STIPPLE);
255                 glLineStipple(1, 0x8888);
256         }
257         else {
258                 glDisable(GL_POLYGON_STIPPLE);
259                 glDisable(GL_LINE_STIPPLE);
260         }
261 }
262
263 static void drawmeta_contents(Scene *scene, Sequence *seqm, float x1, float y1, float x2, float y2)
264 {
265         /* note: this used to use SEQ_BEGIN/SEQ_END, but it messes up the
266          * seq->depth value, (needed by transform when doing overlap checks)
267          * so for now, just use the meta's immediate children, could be fixed but
268          * its only drawing - campbell */
269         Sequence *seq;
270         unsigned char col[4];
271
272         int chan_min = MAXSEQ;
273         int chan_max = 0;
274         int chan_range = 0;
275         float draw_range = y2 - y1;
276         float draw_height;
277
278         glEnable(GL_BLEND);
279         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
280
281         if (seqm->flag & SEQ_MUTE)
282                 drawmeta_stipple(1);
283
284         for (seq = seqm->seqbase.first; seq; seq = seq->next) {
285                 chan_min = min_ii(chan_min, seq->machine);
286                 chan_max = max_ii(chan_max, seq->machine);
287         }
288
289         chan_range = (chan_max - chan_min) + 1;
290         draw_height = draw_range / chan_range;
291
292         col[3] = 196; /* alpha, used for all meta children */
293
294         for (seq = seqm->seqbase.first; seq; seq = seq->next) {
295                 if ((seq->startdisp > x2 || seq->enddisp < x1) == 0) {
296                         float y_chan = (seq->machine - chan_min) / (float)(chan_range) * draw_range;
297                         float x1_chan = seq->startdisp;
298                         float x2_chan = seq->enddisp;
299                         float y1_chan, y2_chan;
300
301                         if ((seqm->flag & SEQ_MUTE) == 0 && (seq->flag & SEQ_MUTE))
302                                 drawmeta_stipple(1);
303
304                         get_seq_color3ubv(scene, seq, col);
305
306                         glColor4ubv(col);
307                         
308                         /* clamp within parent sequence strip bounds */
309                         if (x1_chan < x1) x1_chan = x1;
310                         if (x2_chan > x2) x2_chan = x2;
311
312                         y1_chan = y1 + y_chan + (draw_height * SEQ_STRIP_OFSBOTTOM);
313                         y2_chan = y1 + y_chan + (draw_height * SEQ_STRIP_OFSTOP);
314
315                         glRectf(x1_chan,  y1_chan, x2_chan,  y2_chan);
316
317                         UI_GetColorPtrShade3ubv(col, col, -30);
318                         glColor4ubv(col);
319                         fdrawbox(x1_chan,  y1_chan, x2_chan,  y2_chan);
320
321                         if ((seqm->flag & SEQ_MUTE) == 0 && (seq->flag & SEQ_MUTE))
322                                 drawmeta_stipple(0);
323                 }
324         }
325
326         if (seqm->flag & SEQ_MUTE)
327                 drawmeta_stipple(0);
328         
329         glDisable(GL_BLEND);
330 }
331
332 /* clamp handles to defined size in pixel space */
333 static float draw_seq_handle_size_get_clamped(Sequence *seq, const float pixelx)
334 {
335         const float minhandle = pixelx * SEQ_HANDLE_SIZE_MIN;
336         const float maxhandle = pixelx * SEQ_HANDLE_SIZE_MAX;
337         return CLAMPIS(seq->handsize, minhandle, maxhandle);
338 }
339
340 /* draw a handle, for each end of a sequence strip */
341 static void draw_seq_handle(View2D *v2d, Sequence *seq, const float handsize_clamped, const short direction)
342 {
343         float v1[2], v2[2], v3[2], rx1 = 0, rx2 = 0; //for triangles and rect
344         float x1, x2, y1, y2;
345         char numstr[32];
346         unsigned int whichsel = 0;
347         
348         x1 = seq->startdisp;
349         x2 = seq->enddisp;
350         
351         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
352         y2 = seq->machine + SEQ_STRIP_OFSTOP;
353
354         /* set up co-ordinates/dimensions for either left or right handle */
355         if (direction == SEQ_LEFTHANDLE) {
356                 rx1 = x1;
357                 rx2 = x1 + handsize_clamped * 0.75f;
358                 
359                 v1[0] = x1 + handsize_clamped / 4; v1[1] = y1 + ( ((y1 + y2) / 2.0f - y1) / 2);
360                 v2[0] = x1 + handsize_clamped / 4; v2[1] = y2 - ( ((y1 + y2) / 2.0f - y1) / 2);
361                 v3[0] = v2[0] + handsize_clamped / 4; v3[1] = (y1 + y2) / 2.0f;
362                 
363                 whichsel = SEQ_LEFTSEL;
364         }
365         else if (direction == SEQ_RIGHTHANDLE) {
366                 rx1 = x2 - handsize_clamped * 0.75f;
367                 rx2 = x2;
368                 
369                 v1[0] = x2 - handsize_clamped / 4; v1[1] = y1 + ( ((y1 + y2) / 2.0f - y1) / 2);
370                 v2[0] = x2 - handsize_clamped / 4; v2[1] = y2 - ( ((y1 + y2) / 2.0f - y1) / 2);
371                 v3[0] = v2[0] - handsize_clamped / 4; v3[1] = (y1 + y2) / 2.0f;
372                 
373                 whichsel = SEQ_RIGHTSEL;
374         }
375         
376         /* draw! */
377         if (seq->type < SEQ_TYPE_EFFECT || 
378             BKE_sequence_effect_get_num_inputs(seq->type) == 0)
379         {
380                 glEnable(GL_BLEND);
381                 
382                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
383                 
384                 if (seq->flag & whichsel) glColor4ub(0, 0, 0, 80);
385                 else if (seq->flag & SELECT) glColor4ub(255, 255, 255, 30);
386                 else glColor4ub(0, 0, 0, 22);
387                 
388                 glRectf(rx1, y1, rx2, y2);
389                 
390                 if (seq->flag & whichsel) glColor4ub(255, 255, 255, 200);
391                 else glColor4ub(0, 0, 0, 50);
392                 
393                 glEnable(GL_POLYGON_SMOOTH);
394                 glBegin(GL_TRIANGLES);
395                 glVertex2fv(v1); glVertex2fv(v2); glVertex2fv(v3);
396                 glEnd();
397                 
398                 glDisable(GL_POLYGON_SMOOTH);
399                 glDisable(GL_BLEND);
400         }
401         
402         if ((G.moving & G_TRANSFORM_SEQ) || (seq->flag & whichsel)) {
403                 const char col[4] = {255, 255, 255, 255};
404                 if (direction == SEQ_LEFTHANDLE) {
405                         BLI_snprintf(numstr, sizeof(numstr), "%d", seq->startdisp);
406                         x1 = rx1;
407                         y1 -= 0.45f;
408                 }
409                 else {
410                         BLI_snprintf(numstr, sizeof(numstr), "%d", seq->enddisp - 1);
411                         x1 = x2 - handsize_clamped * 0.75f;
412                         y1 = y2 + 0.05f;
413                 }
414                 UI_view2d_text_cache_add(v2d, x1, y1, numstr, col);
415         }
416 }
417
418 static void draw_seq_extensions(Scene *scene, ARegion *ar, Sequence *seq)
419 {
420         float x1, x2, y1, y2, pixely, a;
421         unsigned char col[3], blendcol[3];
422         View2D *v2d = &ar->v2d;
423         
424         if (seq->type >= SEQ_TYPE_EFFECT) return;
425
426         x1 = seq->startdisp;
427         x2 = seq->enddisp;
428         
429         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
430         y2 = seq->machine + SEQ_STRIP_OFSTOP;
431
432         pixely = BLI_rctf_size_y(&v2d->cur) / BLI_rcti_size_y(&v2d->mask);
433         
434         if (pixely <= 0) return;  /* can happen when the view is split/resized */
435         
436         blendcol[0] = blendcol[1] = blendcol[2] = 120;
437
438         if (seq->startofs) {
439                 glEnable(GL_BLEND);
440                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
441                 
442                 get_seq_color3ubv(scene, seq, col);
443                 
444                 if (seq->flag & SELECT) {
445                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.3, -40);
446                         glColor4ub(col[0], col[1], col[2], 170);
447                 }
448                 else {
449                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.6, 0);
450                         glColor4ub(col[0], col[1], col[2], 110);
451                 }
452                 
453                 glRectf((float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);
454                 
455                 if (seq->flag & SELECT) glColor4ub(col[0], col[1], col[2], 255);
456                 else glColor4ub(col[0], col[1], col[2], 160);
457
458                 fdrawbox((float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);  //outline
459                 
460                 glDisable(GL_BLEND);
461         }
462         if (seq->endofs) {
463                 glEnable(GL_BLEND);
464                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
465                 
466                 get_seq_color3ubv(scene, seq, col);
467                 
468                 if (seq->flag & SELECT) {
469                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.3, -40);
470                         glColor4ub(col[0], col[1], col[2], 170);
471                 }
472                 else {
473                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.6, 0);
474                         glColor4ub(col[0], col[1], col[2], 110);
475                 }
476                 
477                 glRectf(x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM);
478                 
479                 if (seq->flag & SELECT) glColor4ub(col[0], col[1], col[2], 255);
480                 else glColor4ub(col[0], col[1], col[2], 160);
481
482                 fdrawbox(x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM); //outline
483                 
484                 glDisable(GL_BLEND);
485         }
486         if (seq->startstill) {
487                 get_seq_color3ubv(scene, seq, col);
488                 UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.75, 40);
489                 glColor3ubv((GLubyte *)col);
490                 
491                 draw_shadedstrip(seq, col, x1, y1, (float)(seq->start), y2);
492                 
493                 /* feint pinstripes, helps see exactly which is extended and which isn't,
494                  * especially when the extension is very small */ 
495                 if (seq->flag & SELECT) UI_GetColorPtrBlendShade3ubv(col, col, col, 0.0, 24);
496                 else UI_GetColorPtrShade3ubv(col, col, -16);
497                 
498                 glColor3ubv((GLubyte *)col);
499                 
500                 for (a = y1; a < y2; a += pixely * 2.0f) {
501                         fdrawline(x1,  a,  (float)(seq->start),  a);
502                 }
503         }
504         if (seq->endstill) {
505                 get_seq_color3ubv(scene, seq, col);
506                 UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.75, 40);
507                 glColor3ubv((GLubyte *)col);
508                 
509                 draw_shadedstrip(seq, col, (float)(seq->start + seq->len), y1, x2, y2);
510                 
511                 /* feint pinstripes, helps see exactly which is extended and which isn't,
512                  * especially when the extension is very small */ 
513                 if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, 24);
514                 else UI_GetColorPtrShade3ubv(col, col, -16);
515                 
516                 glColor3ubv((GLubyte *)col);
517                 
518                 for (a = y1; a < y2; a += pixely * 2.0f) {
519                         fdrawline((float)(seq->start + seq->len),  a,  x2,  a);
520                 }
521         }
522 }
523
524 /* draw info text on a sequence strip */
525 static void draw_seq_text(View2D *v2d, Sequence *seq, float x1, float x2, float y1, float y2, const unsigned char background_col[3])
526 {
527         rctf rect;
528         char str[32 + FILE_MAX];
529         const char *name = seq->name + 2;
530         char col[4];
531
532         /* note, all strings should include 'name' */
533         if (name[0] == '\0')
534                 name = BKE_sequence_give_name(seq);
535
536         if (seq->type == SEQ_TYPE_META || seq->type == SEQ_TYPE_ADJUSTMENT) {
537                 BLI_snprintf(str, sizeof(str), "%s | %d", name, seq->len);
538         }
539         else if (seq->type == SEQ_TYPE_SCENE) {
540                 if (seq->scene) {
541                         if (seq->scene_camera) {
542                                 BLI_snprintf(str, sizeof(str), "%s: %s (%s) | %d",
543                                              name, seq->scene->id.name + 2, ((ID *)seq->scene_camera)->name + 2, seq->len);
544                         }
545                         else {
546                                 BLI_snprintf(str, sizeof(str), "%s: %s | %d",
547                                              name, seq->scene->id.name + 2, seq->len);
548                         }
549                 }
550                 else {
551                         BLI_snprintf(str, sizeof(str), "%s | %d",
552                                      name, seq->len);
553                 }
554         }
555         else if (seq->type == SEQ_TYPE_MOVIECLIP) {
556                 if (seq->clip && strcmp(name, seq->clip->id.name + 2) != 0) {
557                         BLI_snprintf(str, sizeof(str), "%s: %s | %d",
558                                      name, seq->clip->id.name + 2, seq->len);
559                 }
560                 else {
561                         BLI_snprintf(str, sizeof(str), "%s | %d",
562                                      name, seq->len);
563                 }
564         }
565         else if (seq->type == SEQ_TYPE_MASK) {
566                 if (seq->mask && strcmp(name, seq->mask->id.name + 2) != 0) {
567                         BLI_snprintf(str, sizeof(str), "%s: %s | %d",
568                                      name, seq->mask->id.name + 2, seq->len);
569                 }
570                 else {
571                         BLI_snprintf(str, sizeof(str), "%s | %d",
572                                      name, seq->len);
573                 }
574         }
575         else if (seq->type == SEQ_TYPE_MULTICAM) {
576                 BLI_snprintf(str, sizeof(str), "Cam %s: %d",
577                              name, seq->multicam_source);
578         }
579         else if (seq->type == SEQ_TYPE_IMAGE) {
580                 BLI_snprintf(str, sizeof(str), "%s: %s%s | %d",
581                              name, seq->strip->dir, seq->strip->stripdata->name, seq->len);
582         }
583         else if (seq->type & SEQ_TYPE_EFFECT) {
584                 BLI_snprintf(str, sizeof(str), "%s | %d",
585                                      name, seq->len);
586         }
587         else if (seq->type == SEQ_TYPE_SOUND_RAM) {
588                 if (seq->sound)
589                         BLI_snprintf(str, sizeof(str), "%s: %s | %d",
590                                      name, seq->sound->name, seq->len);
591                 else
592                         BLI_snprintf(str, sizeof(str), "%s | %d",
593                                      name, seq->len);
594         }
595         else if (seq->type == SEQ_TYPE_MOVIE) {
596                 BLI_snprintf(str, sizeof(str), "%s: %s%s | %d",
597                              name, seq->strip->dir, seq->strip->stripdata->name, seq->len);
598         }
599         
600         if (seq->flag & SELECT) {
601                 col[0] = col[1] = col[2] = 255;
602         }
603         else if ((((int)background_col[0] + (int)background_col[1] + (int)background_col[2]) / 3) < 50) {
604                 col[0] = col[1] = col[2] = 80; /* use lighter text color for dark background */
605         }
606         else {
607                 col[0] = col[1] = col[2] = 0;
608         }
609         col[3] = 255;
610
611         rect.xmin = x1;
612         rect.ymin = y1;
613         rect.xmax = x2;
614         rect.ymax = y2;
615         UI_view2d_text_cache_rectf(v2d, &rect, str, col);
616 }
617
618 /* draws a shaded strip, made from gradient + flat color + gradient */
619 static void draw_shadedstrip(Sequence *seq, unsigned char col[3], float x1, float y1, float x2, float y2)
620 {
621         float ymid1, ymid2;
622         
623         if (seq->flag & SEQ_MUTE) {
624                 glEnable(GL_POLYGON_STIPPLE);
625                 glPolygonStipple(stipple_halftone);
626         }
627         
628         ymid1 = (y2 - y1) * 0.25f + y1;
629         ymid2 = (y2 - y1) * 0.65f + y1;
630         
631         glShadeModel(GL_SMOOTH);
632         glBegin(GL_QUADS);
633         
634         if (seq->flag & SEQ_INVALID_EFFECT) { col[0] = 255; col[1] = 0; col[2] = 255; }
635         else if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, -50);
636         /* else UI_GetColorPtrShade3ubv(col, col, 0); */ /* DO NOTHING */
637         
638         glColor3ubv(col);
639         
640         glVertex2f(x1, y1);
641         glVertex2f(x2, y1);
642
643         if (seq->flag & SEQ_INVALID_EFFECT) { col[0] = 255; col[1] = 0; col[2] = 255; }
644         else if (seq->flag & SELECT) UI_GetColorPtrBlendShade3ubv(col, col, col, 0.0, 5);
645         else UI_GetColorPtrShade3ubv(col, col, -5);
646
647         glColor3ubv((GLubyte *)col);
648         
649         glVertex2f(x2, ymid1);
650         glVertex2f(x1, ymid1);
651         
652         glEnd();
653         
654         glRectf(x1,  ymid1,  x2,  ymid2);
655         
656         glBegin(GL_QUADS);
657         
658         glVertex2f(x1, ymid2);
659         glVertex2f(x2, ymid2);
660         
661         if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, -15);
662         else UI_GetColorPtrShade3ubv(col, col, 25);
663         
664         glColor3ubv((GLubyte *)col);
665         
666         glVertex2f(x2, y2);
667         glVertex2f(x1, y2);
668         
669         glEnd();
670         
671         if (seq->flag & SEQ_MUTE) {
672                 glDisable(GL_POLYGON_STIPPLE);
673         }
674 }
675
676 /*
677  * Draw a sequence strip, bounds check already made
678  * ARegion is currently only used to get the windows width in pixels
679  * so wave file sample drawing precision is zoom adjusted
680  */
681 static void draw_seq_strip(Scene *scene, ARegion *ar, Sequence *seq, int outline_tint, float pixelx)
682 {
683         View2D *v2d = &ar->v2d;
684         float x1, x2, y1, y2;
685         unsigned char col[3], background_col[3], is_single_image;
686         const float handsize_clamped = draw_seq_handle_size_get_clamped(seq, pixelx);
687
688         /* we need to know if this is a single image/color or not for drawing */
689         is_single_image = (char)BKE_sequence_single_check(seq);
690         
691         /* body */
692         x1 = (seq->startstill) ? seq->start : seq->startdisp;
693         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
694         x2 = (seq->endstill) ? (seq->start + seq->len) : seq->enddisp;
695         y2 = seq->machine + SEQ_STRIP_OFSTOP;
696
697
698         /* get the correct color per strip type*/
699         //get_seq_color3ubv(scene, seq, col);
700         get_seq_color3ubv(scene, seq, background_col);
701         
702         /* draw the main strip body */
703         if (is_single_image) {  /* single image */
704                 draw_shadedstrip(seq, background_col,
705                                  BKE_sequence_tx_get_final_left(seq, 0), y1,
706                                  BKE_sequence_tx_get_final_right(seq, 0), y2);
707         }
708         else {  /* normal operation */
709                 draw_shadedstrip(seq, background_col, x1, y1, x2, y2);
710         }
711         
712         /* draw additional info and controls */
713         if (!is_single_image)
714                 draw_seq_extensions(scene, ar, seq);
715         
716         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_LEFTHANDLE);
717         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_RIGHTHANDLE);
718         
719         /* draw the strip outline */
720         x1 = seq->startdisp;
721         x2 = seq->enddisp;
722         
723         /* draw sound wave */
724         if (seq->type == SEQ_TYPE_SOUND_RAM) {
725                 drawseqwave(scene, seq, x1, y1, x2, y2, BLI_rctf_size_x(&ar->v2d.cur) / ar->winx);
726         }
727
728         /* draw lock */
729         if (seq->flag & SEQ_LOCK) {
730                 glEnable(GL_POLYGON_STIPPLE);
731                 glEnable(GL_BLEND);
732
733                 /* light stripes */
734                 glColor4ub(255, 255, 255, 32);
735                 glPolygonStipple(stipple_diag_stripes_pos);
736                 glRectf(x1, y1, x2, y2);
737
738                 /* dark stripes */
739                 glColor4ub(0, 0, 0, 32);
740                 glPolygonStipple(stipple_diag_stripes_neg);
741                 glRectf(x1, y1, x2, y2);
742
743                 glDisable(GL_POLYGON_STIPPLE);
744                 glDisable(GL_BLEND);
745         }
746
747         if (!BKE_sequence_is_valid_check(seq)) {
748                 glEnable(GL_POLYGON_STIPPLE);
749
750                 /* panic! */
751                 glColor4ub(255, 0, 0, 255);
752                 glPolygonStipple(stipple_diag_stripes_pos);
753                 glRectf(x1, y1, x2, y2);
754
755                 glDisable(GL_POLYGON_STIPPLE);
756         }
757
758         get_seq_color3ubv(scene, seq, col);
759         if ((G.moving & G_TRANSFORM_SEQ) && (seq->flag & SELECT)) {
760                 if (seq->flag & SEQ_OVERLAP) {
761                         col[0] = 255; col[1] = col[2] = 40;
762                 }
763                 else
764                         UI_GetColorPtrShade3ubv(col, col, 120 + outline_tint);
765         }
766         else
767                 UI_GetColorPtrShade3ubv(col, col, outline_tint);
768         
769         glColor3ubv((GLubyte *)col);
770         
771         if (seq->flag & SEQ_MUTE) {
772                 glEnable(GL_LINE_STIPPLE);
773                 glLineStipple(1, 0x8888);
774         }
775         
776         uiDrawBoxShade(GL_LINE_LOOP, x1, y1, x2, y2, 0.0, 0.1, 0.0);
777         
778         if (seq->flag & SEQ_MUTE) {
779                 glDisable(GL_LINE_STIPPLE);
780         }
781         
782         if (seq->type == SEQ_TYPE_META) {
783                 drawmeta_contents(scene, seq, x1, y1, x2, y2);
784         }
785         
786         /* calculate if seq is long enough to print a name */
787         x1 = seq->startdisp + handsize_clamped;
788         x2 = seq->enddisp   - handsize_clamped;
789
790         /* info text on the strip */
791         if (x1 < v2d->cur.xmin) x1 = v2d->cur.xmin;
792         else if (x1 > v2d->cur.xmax) x1 = v2d->cur.xmax;
793         if (x2 < v2d->cur.xmin) x2 = v2d->cur.xmin;
794         else if (x2 > v2d->cur.xmax) x2 = v2d->cur.xmax;
795
796         /* nice text here would require changing the view matrix for texture text */
797         if ((x2 - x1) / pixelx > 32) {
798                 draw_seq_text(v2d, seq, x1, x2, y1, y2, background_col);
799         }
800 }
801
802 static Sequence *special_seq_update = NULL;
803
804 static void UNUSED_FUNCTION(set_special_seq_update) (int val)
805 {
806 //      int x;
807
808         /* if mouse over a sequence && LEFTMOUSE */
809         if (val) {
810 // XXX          special_seq_update = find_nearest_seq(&x);
811         }
812         else {
813                 special_seq_update = NULL;
814         }
815 }
816
817 ImBuf *sequencer_ibuf_get(struct Main *bmain, Scene *scene, SpaceSeq *sseq, int cfra, int frame_ofs)
818 {
819         SeqRenderData context;
820         ImBuf *ibuf;
821         int rectx, recty;
822         float render_size;
823         float proxy_size = 100.0;
824         short is_break = G.is_break;
825
826         render_size = sseq->render_size;
827         if (render_size == 0) {
828                 render_size = scene->r.size;
829         }
830         else {
831                 proxy_size = render_size;
832         }
833
834         if (render_size < 0) {
835                 return NULL;
836         }
837
838         rectx = (render_size * (float)scene->r.xsch) / 100.0f + 0.5f;
839         recty = (render_size * (float)scene->r.ysch) / 100.0f + 0.5f;
840
841         context = BKE_sequencer_new_render_data(bmain->eval_ctx, bmain, scene, rectx, recty, proxy_size);
842
843         /* sequencer could start rendering, in this case we need to be sure it wouldn't be canceled
844          * by Esc pressed somewhere in the past
845          */
846         G.is_break = FALSE;
847
848         if (special_seq_update)
849                 ibuf = BKE_sequencer_give_ibuf_direct(&context, cfra + frame_ofs, special_seq_update);
850         else if (!U.prefetchframes) // XXX || (G.f & G_PLAYANIM) == 0) {
851                 ibuf = BKE_sequencer_give_ibuf(&context, cfra + frame_ofs, sseq->chanshown);
852         else
853                 ibuf = BKE_sequencer_give_ibuf_threaded(&context, cfra + frame_ofs, sseq->chanshown);
854
855         /* restore state so real rendering would be canceled (if needed) */
856         G.is_break = is_break;
857
858         return ibuf;
859 }
860
861 static void sequencer_check_scopes(SequencerScopes *scopes, ImBuf *ibuf)
862 {
863         if (scopes->reference_ibuf != ibuf) {
864                 if (scopes->zebra_ibuf) {
865                         IMB_freeImBuf(scopes->zebra_ibuf);
866                         scopes->zebra_ibuf = NULL;
867                 }
868
869                 if (scopes->waveform_ibuf) {
870                         IMB_freeImBuf(scopes->waveform_ibuf);
871                         scopes->waveform_ibuf = NULL;
872                 }
873
874                 if (scopes->sep_waveform_ibuf) {
875                         IMB_freeImBuf(scopes->sep_waveform_ibuf);
876                         scopes->sep_waveform_ibuf = NULL;
877                 }
878
879                 if (scopes->vector_ibuf) {
880                         IMB_freeImBuf(scopes->vector_ibuf);
881                         scopes->vector_ibuf = NULL;
882                 }
883
884                 if (scopes->histogram_ibuf) {
885                         IMB_freeImBuf(scopes->histogram_ibuf);
886                         scopes->histogram_ibuf = NULL;
887                 }
888         }
889 }
890
891 static ImBuf *sequencer_make_scope(Scene *scene, ImBuf *ibuf, ImBuf *(*make_scope_cb) (ImBuf *ibuf))
892 {
893         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
894         ImBuf *scope;
895         
896         IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
897                                                              &scene->display_settings);
898
899         scope = make_scope_cb(display_ibuf);
900
901         IMB_freeImBuf(display_ibuf);
902
903         return scope;
904 }
905
906 void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq, int cfra, int frame_ofs, int draw_overlay)
907 {
908         struct Main *bmain = CTX_data_main(C);
909         struct ImBuf *ibuf = NULL;
910         struct ImBuf *scope = NULL;
911         struct View2D *v2d = &ar->v2d;
912         /* int rectx, recty; */ /* UNUSED */
913         float viewrectx, viewrecty;
914         float render_size = 0.0;
915         float proxy_size = 100.0;
916         float col[3];
917         GLuint texid;
918         GLuint last_texid;
919         void *display_buffer;
920         void *cache_handle = NULL;
921         const int is_imbuf = ED_space_sequencer_check_show_imbuf(sseq);
922         int format, type;
923         bool glsl_used = false;
924
925         if (G.is_rendering == FALSE && (scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
926                 /* stop all running jobs, except screen one. currently previews frustrate Render
927                  * needed to make so sequencer's rendering doesn't conflict with compositor
928                  */
929                 WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_COMPOSITE);
930
931                 if ((scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
932                         /* in case of final rendering used for preview, kill all previews,
933                          * otherwise threading conflict will happen in rendering module
934                          */
935                         WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_RENDER_PREVIEW);
936                 }
937         }
938
939         render_size = sseq->render_size;
940         if (render_size == 0) {
941                 render_size = scene->r.size;
942         }
943         else {
944                 proxy_size = render_size;
945         }
946         if (render_size < 0) {
947                 return;
948         }
949
950         viewrectx = (render_size * (float)scene->r.xsch) / 100.0f;
951         viewrecty = (render_size * (float)scene->r.ysch) / 100.0f;
952
953         /* rectx = viewrectx + 0.5f; */ /* UNUSED */
954         /* recty = viewrecty + 0.5f; */ /* UNUSED */
955
956         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
957                 viewrectx *= scene->r.xasp / scene->r.yasp;
958                 viewrectx /= proxy_size / 100.0f;
959                 viewrecty /= proxy_size / 100.0f;
960         }
961
962         if (!draw_overlay || sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) {
963                 UI_GetThemeColor3fv(TH_SEQ_PREVIEW, col);
964                 glClearColor(col[0], col[1], col[2], 0.0);
965                 glClear(GL_COLOR_BUFFER_BIT);
966         }
967
968         /* only initialize the preview if a render is in progress */
969         if (G.is_rendering)
970                 return;
971
972         ibuf = sequencer_ibuf_get(bmain, scene, sseq, cfra, frame_ofs);
973         
974         if (ibuf == NULL)
975                 return;
976
977         if (ibuf->rect == NULL && ibuf->rect_float == NULL)
978                 return;
979
980         if (sseq->mainb != SEQ_DRAW_IMG_IMBUF || sseq->zebra != 0) {
981                 SequencerScopes *scopes = &sseq->scopes;
982
983                 sequencer_check_scopes(scopes, ibuf);
984
985                 switch (sseq->mainb) {
986                         case SEQ_DRAW_IMG_IMBUF:
987                                 if (!scopes->zebra_ibuf) {
988                                         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
989
990                                         if (display_ibuf->rect_float) {
991                                                 IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
992                                                                                              &scene->display_settings);
993                                         }
994                                         scopes->zebra_ibuf = make_zebra_view_from_ibuf(display_ibuf, sseq->zebra);
995                                         IMB_freeImBuf(display_ibuf);
996                                 }
997                                 scope = scopes->zebra_ibuf;
998                                 break;
999                         case SEQ_DRAW_IMG_WAVEFORM:
1000                                 if ((sseq->flag & SEQ_DRAW_COLOR_SEPARATED) != 0) {
1001                                         if (!scopes->sep_waveform_ibuf)
1002                                                 scopes->sep_waveform_ibuf = sequencer_make_scope(scene, ibuf, make_sep_waveform_view_from_ibuf);
1003                                         scope = scopes->sep_waveform_ibuf;
1004                                 }
1005                                 else {
1006                                         if (!scopes->waveform_ibuf)
1007                                                 scopes->waveform_ibuf = sequencer_make_scope(scene, ibuf, make_waveform_view_from_ibuf);
1008                                         scope = scopes->waveform_ibuf;
1009                                 }
1010                                 break;
1011                         case SEQ_DRAW_IMG_VECTORSCOPE:
1012                                 if (!scopes->vector_ibuf)
1013                                         scopes->vector_ibuf = sequencer_make_scope(scene, ibuf, make_vectorscope_view_from_ibuf);
1014                                 scope = scopes->vector_ibuf;
1015                                 break;
1016                         case SEQ_DRAW_IMG_HISTOGRAM:
1017                                 if (!scopes->histogram_ibuf)
1018                                         scopes->histogram_ibuf = sequencer_make_scope(scene, ibuf, make_histogram_view_from_ibuf);
1019                                 scope = scopes->histogram_ibuf;
1020                                 break;
1021                 }
1022
1023                 /* future files may have new scopes we don't catch above */
1024                 if (scope) {
1025                         scopes->reference_ibuf = ibuf;
1026                         viewrectx = scope->x;
1027                         viewrecty = scope->y;
1028                 }
1029                 else {
1030                         scopes->reference_ibuf = NULL;
1031                 }
1032         }
1033
1034         /* without this colors can flicker from previous opengl state */
1035         glColor4ub(255, 255, 255, 255);
1036
1037         UI_view2d_totRect_set(v2d, viewrectx + 0.5f, viewrecty + 0.5f);
1038         UI_view2d_curRect_validate(v2d);
1039
1040         /* setting up the view - actual drawing starts here */
1041         UI_view2d_view_ortho(v2d);
1042
1043         /* only draw alpha for main buffer */
1044         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1045                 if (sseq->flag & SEQ_USE_ALPHA) {
1046                         glEnable(GL_BLEND);
1047                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1048
1049                         fdrawcheckerboard(v2d->tot.xmin, v2d->tot.ymin, v2d->tot.xmax, v2d->tot.ymax);
1050                         glColor4f(1.0, 1.0, 1.0, 1.0);
1051                 }
1052         }
1053
1054         if (scope) {
1055                 IMB_freeImBuf(ibuf);
1056                 ibuf = scope;
1057
1058                 if (ibuf->rect_float && ibuf->rect == NULL) {
1059                         IMB_rect_from_float(ibuf);
1060                 }
1061
1062                 display_buffer = (unsigned char *)ibuf->rect;
1063                 format = GL_RGBA;
1064                 type = GL_UNSIGNED_BYTE;
1065         }
1066         else {
1067                 bool force_fallback = false;
1068
1069                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1070                 force_fallback |= (ibuf->dither != 0.0f);
1071
1072                 if (force_fallback) {
1073                         /* Fallback to CPU based color space conversion */
1074                         glsl_used = false;
1075                         format = GL_RGBA;
1076                         type = GL_UNSIGNED_BYTE;
1077                         display_buffer = NULL;
1078                 }
1079                 else if (ibuf->rect_float) {
1080                         display_buffer = ibuf->rect_float;
1081
1082                         if (ibuf->channels == 4) {
1083                                 format = GL_RGBA;
1084                         }
1085                         else if (ibuf->channels == 3) {
1086                                 format = GL_RGB;
1087                         }
1088                         else {
1089                                 BLI_assert(!"Incompatible number of channels for float buffer in sequencer");
1090                                 format = GL_RGBA;
1091                                 display_buffer = NULL;
1092                         }
1093
1094                         type = GL_FLOAT;
1095
1096                         if (ibuf->float_colorspace) {
1097                                 glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->float_colorspace, ibuf->dither, true);
1098                         }
1099                         else {
1100                                 glsl_used = IMB_colormanagement_setup_glsl_draw_ctx(C, ibuf->dither, true);
1101                         }
1102                 }
1103                 else if (ibuf->rect) {
1104                         display_buffer = ibuf->rect;
1105                         format = GL_RGBA;
1106                         type = GL_UNSIGNED_BYTE;
1107
1108                         glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, ibuf->dither, false);
1109                 }
1110                 else {
1111                         format = GL_RGBA;
1112                         type = GL_UNSIGNED_BYTE;
1113                         display_buffer = NULL;
1114                 }
1115
1116                 /* there's a data to be displayed, but GLSL is not initialized
1117                  * properly, in this case we fallback to CPU-based display transform
1118                  */
1119                 if ((ibuf->rect || ibuf->rect_float) && !glsl_used) {
1120                         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
1121                         format = GL_RGBA;
1122                         type = GL_UNSIGNED_BYTE;
1123                 }
1124         }
1125
1126         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1127         glColor4f(1.0, 1.0, 1.0, 1.0);
1128
1129         last_texid = glaGetOneInteger(GL_TEXTURE_2D);
1130         glEnable(GL_TEXTURE_2D);
1131         glGenTextures(1, (GLuint *)&texid);
1132
1133         glBindTexture(GL_TEXTURE_2D, texid);
1134
1135         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1136         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1137
1138         if (type == GL_FLOAT)
1139                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1140         else
1141                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1142
1143         glBegin(GL_QUADS);
1144
1145         if (draw_overlay) {
1146                 if (sseq->overlay_type == SEQ_DRAW_OVERLAY_RECT) {
1147                         rctf tot_clip;
1148                         tot_clip.xmin = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmin);
1149                         tot_clip.ymin = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymin);
1150                         tot_clip.xmax = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmax);
1151                         tot_clip.ymax = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymax);
1152
1153                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmin, tot_clip.ymin);
1154                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmin, tot_clip.ymax);
1155                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmax, tot_clip.ymax);
1156                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmax, tot_clip.ymin);
1157                 }
1158                 else if (sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) {
1159                         glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1160                         glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1161                         glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1162                         glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1163                 }
1164         }
1165         else {
1166                 glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1167                 glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1168                 glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1169                 glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1170         }
1171         glEnd();
1172         glBindTexture(GL_TEXTURE_2D, last_texid);
1173         glDisable(GL_TEXTURE_2D);
1174         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF && sseq->flag & SEQ_USE_ALPHA)
1175                 glDisable(GL_BLEND);
1176         glDeleteTextures(1, &texid);
1177
1178         if (glsl_used)
1179                 IMB_colormanagement_finish_glsl_draw();
1180
1181         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1182
1183                 float x1 = v2d->tot.xmin;
1184                 float y1 = v2d->tot.ymin;
1185                 float x2 = v2d->tot.xmax;
1186                 float y2 = v2d->tot.ymax;
1187
1188                 /* border */
1189                 setlinestyle(3);
1190
1191                 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 1.0, 0);
1192
1193                 glBegin(GL_LINE_LOOP);
1194                 glVertex2f(x1 - 0.5f, y1 - 0.5f);
1195                 glVertex2f(x1 - 0.5f, y2 + 0.5f);
1196                 glVertex2f(x2 + 0.5f, y2 + 0.5f);
1197                 glVertex2f(x2 + 0.5f, y1 - 0.5f);
1198                 glEnd();
1199
1200                 /* safety border */
1201                 if ((sseq->flag & SEQ_DRAW_SAFE_MARGINS) != 0) {
1202                         float fac = 0.1;
1203
1204                         float a = fac * (x2 - x1);
1205                         x1 += a;
1206                         x2 -= a;
1207
1208                         a = fac * (y2 - y1);
1209                         y1 += a;
1210                         y2 -= a;
1211
1212                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1213
1214                         uiSetRoundBox(UI_CNR_ALL);
1215                         uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1216
1217                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1218
1219                 }
1220
1221                 setlinestyle(0);
1222         }
1223         
1224         if (sseq->flag & SEQ_SHOW_GPENCIL) {
1225                 if (is_imbuf) {
1226                         /* draw grease-pencil (image aligned) */
1227                         draw_gpencil_2dimage(C);
1228                 }
1229         }
1230
1231         if (!scope)
1232                 IMB_freeImBuf(ibuf);
1233         
1234         /* ortho at pixel level */
1235         UI_view2d_view_restore(C);
1236         
1237         if (sseq->flag & SEQ_SHOW_GPENCIL) {
1238                 if (is_imbuf) {
1239                         /* draw grease-pencil (screen aligned) */
1240                         draw_gpencil_view2d(C, 0);
1241                 }
1242         }
1243
1244
1245         /* NOTE: sequencer mask editing isnt finished, the draw code is working but editing not,
1246          * for now just disable drawing since the strip frame will likely be offset */
1247
1248         //if (sc->mode == SC_MODE_MASKEDIT) {
1249         if (0 && sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1250                 Mask *mask = BKE_sequencer_mask_get(scene);
1251
1252                 if (mask) {
1253                         int width, height;
1254                         float aspx = 1.0f, aspy = 1.0f;
1255                         // ED_mask_get_size(C, &width, &height);
1256
1257                         //Scene *scene = CTX_data_scene(C);
1258                         width = (scene->r.size * scene->r.xsch) / 100;
1259                         height = (scene->r.size * scene->r.ysch) / 100;
1260
1261                         ED_mask_draw_region(mask, ar,
1262                                             0, 0, 0,  /* TODO */
1263                                             width, height,
1264                                             aspx, aspy,
1265                                             FALSE, TRUE,
1266                                             NULL, C);
1267                 }
1268         }
1269
1270         if (cache_handle)
1271                 IMB_display_buffer_release(cache_handle);
1272 }
1273
1274 #if 0
1275 void drawprefetchseqspace(Scene *scene, ARegion *UNUSED(ar), SpaceSeq *sseq)
1276 {
1277         int rectx, recty;
1278         int render_size = sseq->render_size;
1279         int proxy_size = 100.0; 
1280         if (render_size == 0) {
1281                 render_size = scene->r.size;
1282         }
1283         else {
1284                 proxy_size = render_size;
1285         }
1286         if (render_size < 0) {
1287                 return;
1288         }
1289
1290         rectx = (render_size * scene->r.xsch) / 100;
1291         recty = (render_size * scene->r.ysch) / 100;
1292
1293         if (sseq->mainb != SEQ_DRAW_SEQUENCE) {
1294                 give_ibuf_prefetch_request(
1295                     rectx, recty, (scene->r.cfra), sseq->chanshown,
1296                     proxy_size);
1297         }
1298 }
1299 #endif
1300
1301 /* draw backdrop of the sequencer strips view */
1302 static void draw_seq_backdrop(View2D *v2d)
1303 {
1304         int i;
1305         
1306         /* darker gray overlay over the view backdrop */
1307         UI_ThemeColorShade(TH_BACK, -20);
1308         glRectf(v2d->cur.xmin,  -1.0,  v2d->cur.xmax,  1.0);
1309
1310         /* Alternating horizontal stripes */
1311         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1312
1313         glBegin(GL_QUADS);
1314         while (i < v2d->cur.ymax) {
1315                 if (((int)i) & 1)
1316                         UI_ThemeColorShade(TH_BACK, -15);
1317                 else
1318                         UI_ThemeColorShade(TH_BACK, -25);
1319                         
1320                 glVertex2f(v2d->cur.xmax, i);
1321                 glVertex2f(v2d->cur.xmin, i);
1322                 glVertex2f(v2d->cur.xmin, i + 1);
1323                 glVertex2f(v2d->cur.xmax, i + 1);
1324
1325                 i += 1.0;
1326         }
1327         glEnd();
1328         
1329         /* Darker lines separating the horizontal bands */
1330         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1331         UI_ThemeColor(TH_GRID);
1332         
1333         glBegin(GL_LINES);
1334         while (i < v2d->cur.ymax) {
1335                 glVertex2f(v2d->cur.xmax, i);
1336                 glVertex2f(v2d->cur.xmin, i);
1337                         
1338                 i += 1.0;
1339         }
1340         glEnd();
1341 }
1342
1343 /* draw the contents of the sequencer strips view */
1344 static void draw_seq_strips(const bContext *C, Editing *ed, ARegion *ar)
1345 {
1346         Scene *scene = CTX_data_scene(C);
1347         View2D *v2d = &ar->v2d;
1348         Sequence *last_seq = BKE_sequencer_active_get(scene);
1349         int sel = 0, j;
1350         float pixelx = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
1351         
1352         /* loop through twice, first unselected, then selected */
1353         for (j = 0; j < 2; j++) {
1354                 Sequence *seq;
1355                 int outline_tint = (j) ? -60 : -150; /* highlighting around strip edges indicating selection */
1356                 
1357                 /* loop through strips, checking for those that are visible */
1358                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1359                         /* boundbox and selection tests for NOT drawing the strip... */
1360                         if ((seq->flag & SELECT) != sel) continue;
1361                         else if (seq == last_seq) continue;
1362                         else if (min_ii(seq->startdisp, seq->start) > v2d->cur.xmax) continue;
1363                         else if (max_ii(seq->enddisp, seq->start + seq->len) < v2d->cur.xmin) continue;
1364                         else if (seq->machine + 1.0f < v2d->cur.ymin) continue;
1365                         else if (seq->machine > v2d->cur.ymax) continue;
1366                         
1367                         /* strip passed all tests unscathed... so draw it now */
1368                         draw_seq_strip(scene, ar, seq, outline_tint, pixelx);
1369                 }
1370                 
1371                 /* draw selected next time round */
1372                 sel = SELECT;
1373         }
1374         
1375         /* draw the last selected last (i.e. 'active' in other parts of Blender), removes some overlapping error */
1376         if (last_seq)
1377                 draw_seq_strip(scene, ar, last_seq, 120, pixelx);
1378 }
1379
1380 static void seq_draw_sfra_efra(Scene *scene, View2D *v2d)
1381 {       
1382         glEnable(GL_BLEND);
1383         
1384         /* draw darkened area outside of active timeline 
1385          * frame range used is preview range or scene range */
1386         UI_ThemeColorShadeAlpha(TH_BACK, -25, -100);
1387
1388         if (PSFRA < PEFRA + 1) {
1389                 glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
1390                 glRectf((float)(PEFRA + 1), v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1391         }
1392         else {
1393                 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1394         }
1395
1396         UI_ThemeColorShade(TH_BACK, -60);
1397         /* thin lines where the actual frames are */
1398         fdrawline((float)PSFRA, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
1399         fdrawline((float)(PEFRA + 1), v2d->cur.ymin, (float)(PEFRA + 1), v2d->cur.ymax);
1400         
1401         glDisable(GL_BLEND);
1402 }
1403
1404 /* Draw Timeline/Strip Editor Mode for Sequencer */
1405 void draw_timeline_seq(const bContext *C, ARegion *ar)
1406 {
1407         Scene *scene = CTX_data_scene(C);
1408         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1409         SpaceSeq *sseq = CTX_wm_space_seq(C);
1410         View2D *v2d = &ar->v2d;
1411         View2DScrollers *scrollers;
1412         short unit = 0, flag = 0;
1413         float col[3];
1414         
1415         /* clear and setup matrix */
1416         UI_GetThemeColor3fv(TH_BACK, col);
1417         if (ed && ed->metastack.first) 
1418                 glClearColor(col[0], col[1], col[2] - 0.1f, 0.0f);
1419         else 
1420                 glClearColor(col[0], col[1], col[2], 0.0f);
1421         glClear(GL_COLOR_BUFFER_BIT);
1422
1423         UI_view2d_view_ortho(v2d);
1424         
1425         
1426         /* calculate extents of sequencer strips/data 
1427          * NOTE: needed for the scrollers later
1428          */
1429         boundbox_seq(scene, &v2d->tot);
1430         
1431         
1432         /* draw backdrop */
1433         draw_seq_backdrop(v2d);
1434         
1435         /* regular grid-pattern over the rest of the view (i.e. 25-frame grid lines) */
1436         // NOTE: the gridlines are currently spaced every 25 frames, which is only fine for 25 fps, but maybe not for 30...
1437         UI_view2d_constant_grid_draw(v2d);
1438
1439         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
1440         
1441         seq_draw_sfra_efra(scene, v2d);
1442
1443         /* sequence strips (if there is data available to be drawn) */
1444         if (ed) {
1445                 /* draw the data */
1446                 draw_seq_strips(C, ed, ar);
1447                 
1448                 /* text draw cached (for sequence names), in pixelspace now */
1449                 UI_view2d_text_cache_draw(ar);
1450         }
1451         
1452         /* current frame */
1453         UI_view2d_view_ortho(v2d);
1454         if ((sseq->flag & SEQ_DRAWFRAMES) == 0)      flag |= DRAWCFRA_UNIT_SECONDS;
1455         if ((sseq->flag & SEQ_NO_DRAW_CFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
1456         ANIM_draw_cfra(C, v2d, flag);
1457         
1458         /* markers */
1459         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1460         draw_markers_time(C, DRAW_MARKERS_LINES);
1461         
1462         /* preview range */
1463         UI_view2d_view_ortho(v2d);
1464         ANIM_draw_previewrange(C, v2d, 1);
1465
1466         /* overlap playhead */
1467         if (scene->ed && scene->ed->over_flag & SEQ_EDIT_OVERLAY_SHOW) {
1468                 int cfra_over = (scene->ed->over_flag & SEQ_EDIT_OVERLAY_ABS) ? scene->ed->over_cfra : scene->r.cfra + scene->ed->over_ofs;
1469                 glColor3f(0.2, 0.2, 0.2);
1470                 // glRectf(cfra_over, v2d->cur.ymin, scene->ed->over_ofs + scene->r.cfra + 1, v2d->cur.ymax);
1471
1472                 glBegin(GL_LINES);
1473                 glVertex2f(cfra_over, v2d->cur.ymin);
1474                 glVertex2f(cfra_over, v2d->cur.ymax);
1475                 glEnd();
1476
1477         }
1478         
1479         /* callback */
1480         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1481
1482         /* reset view matrix */
1483         UI_view2d_view_restore(C);
1484
1485         /* scrollers */
1486         unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDS;
1487         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_UNIT_VALUES, V2D_GRID_CLAMP);
1488         UI_view2d_scrollers_draw(C, v2d, scrollers);
1489         UI_view2d_scrollers_free(scrollers);
1490 }
1491
1492