Threaded object update and EvaluationContext
[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         /* without this colors can flicker from previous opengl state */
969         glColor4ub(255, 255, 255, 255);
970
971         UI_view2d_totRect_set(v2d, viewrectx + 0.5f, viewrecty + 0.5f);
972         UI_view2d_curRect_validate(v2d);
973
974         /* only initialize the preview if a render is in progress */
975         if (G.is_rendering)
976                 return;
977
978         ibuf = sequencer_ibuf_get(bmain, scene, sseq, cfra, frame_ofs);
979         
980         if (ibuf == NULL)
981                 return;
982
983         if (ibuf->rect == NULL && ibuf->rect_float == NULL)
984                 return;
985
986         if (sseq->mainb != SEQ_DRAW_IMG_IMBUF || sseq->zebra != 0) {
987                 SequencerScopes *scopes = &sseq->scopes;
988
989                 sequencer_check_scopes(scopes, ibuf);
990
991                 switch (sseq->mainb) {
992                         case SEQ_DRAW_IMG_IMBUF:
993                                 if (!scopes->zebra_ibuf) {
994                                         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
995
996                                         if (display_ibuf->rect_float) {
997                                                 IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
998                                                                                              &scene->display_settings);
999                                         }
1000                                         scopes->zebra_ibuf = make_zebra_view_from_ibuf(display_ibuf, sseq->zebra);
1001                                         IMB_freeImBuf(display_ibuf);
1002                                 }
1003                                 scope = scopes->zebra_ibuf;
1004                                 break;
1005                         case SEQ_DRAW_IMG_WAVEFORM:
1006                                 if ((sseq->flag & SEQ_DRAW_COLOR_SEPARATED) != 0) {
1007                                         if (!scopes->sep_waveform_ibuf)
1008                                                 scopes->sep_waveform_ibuf = sequencer_make_scope(scene, ibuf, make_sep_waveform_view_from_ibuf);
1009                                         scope = scopes->sep_waveform_ibuf;
1010                                 }
1011                                 else {
1012                                         if (!scopes->waveform_ibuf)
1013                                                 scopes->waveform_ibuf = sequencer_make_scope(scene, ibuf, make_waveform_view_from_ibuf);
1014                                         scope = scopes->waveform_ibuf;
1015                                 }
1016                                 break;
1017                         case SEQ_DRAW_IMG_VECTORSCOPE:
1018                                 if (!scopes->vector_ibuf)
1019                                         scopes->vector_ibuf = sequencer_make_scope(scene, ibuf, make_vectorscope_view_from_ibuf);
1020                                 scope = scopes->vector_ibuf;
1021                                 break;
1022                         case SEQ_DRAW_IMG_HISTOGRAM:
1023                                 if (!scopes->histogram_ibuf)
1024                                         scopes->histogram_ibuf = sequencer_make_scope(scene, ibuf, make_histogram_view_from_ibuf);
1025                                 scope = scopes->histogram_ibuf;
1026                                 break;
1027                 }
1028
1029                 scopes->reference_ibuf = ibuf;
1030         }
1031
1032         /* setting up the view - actual drawing starts here */
1033         UI_view2d_view_ortho(v2d);
1034
1035         /* only draw alpha for main buffer */
1036         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1037                 if (sseq->flag & SEQ_USE_ALPHA) {
1038                         glEnable(GL_BLEND);
1039                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1040
1041                         fdrawcheckerboard(v2d->tot.xmin, v2d->tot.ymin, v2d->tot.xmax, v2d->tot.ymax);
1042                         glColor4f(1.0, 1.0, 1.0, 1.0);
1043                 }
1044         }
1045
1046         if (scope) {
1047                 IMB_freeImBuf(ibuf);
1048                 ibuf = scope;
1049
1050                 if (ibuf->rect_float && ibuf->rect == NULL) {
1051                         IMB_rect_from_float(ibuf);
1052                 }
1053
1054                 display_buffer = (unsigned char *)ibuf->rect;
1055                 format = GL_RGBA;
1056                 type = GL_UNSIGNED_BYTE;
1057         }
1058         else {
1059                 bool force_fallback = false;
1060
1061                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1062                 force_fallback |= (ibuf->dither != 0.0f);
1063
1064                 if (force_fallback) {
1065                         /* Fallback to CPU based color space conversion */
1066                         glsl_used = false;
1067                         format = GL_RGBA;
1068                         type = GL_UNSIGNED_BYTE;
1069                         display_buffer = NULL;
1070                 }
1071                 else if (ibuf->rect_float) {
1072                         display_buffer = ibuf->rect_float;
1073
1074                         if (ibuf->channels == 4) {
1075                                 format = GL_RGBA;
1076                         }
1077                         else if (ibuf->channels == 3) {
1078                                 format = GL_RGB;
1079                         }
1080                         else {
1081                                 BLI_assert(!"Incompatible number of channels for float buffer in sequencer");
1082                                 format = GL_RGBA;
1083                                 display_buffer = NULL;
1084                         }
1085
1086                         type = GL_FLOAT;
1087
1088                         if (ibuf->float_colorspace) {
1089                                 glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->float_colorspace, ibuf->dither, true);
1090                         }
1091                         else {
1092                                 glsl_used = IMB_colormanagement_setup_glsl_draw_ctx(C, ibuf->dither, true);
1093                         }
1094                 }
1095                 else if (ibuf->rect) {
1096                         display_buffer = ibuf->rect;
1097                         format = GL_RGBA;
1098                         type = GL_UNSIGNED_BYTE;
1099
1100                         glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, ibuf->dither, false);
1101                 }
1102                 else {
1103                         format = GL_RGBA;
1104                         type = GL_UNSIGNED_BYTE;
1105                         display_buffer = NULL;
1106                 }
1107
1108                 /* there's a data to be displayed, but GLSL is not initialized
1109                  * properly, in this case we fallback to CPU-based display transform
1110                  */
1111                 if ((ibuf->rect || ibuf->rect_float) && !glsl_used) {
1112                         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
1113                         format = GL_RGBA;
1114                         type = GL_UNSIGNED_BYTE;
1115                 }
1116         }
1117
1118         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1119         glColor4f(1.0, 1.0, 1.0, 1.0);
1120
1121         last_texid = glaGetOneInteger(GL_TEXTURE_2D);
1122         glEnable(GL_TEXTURE_2D);
1123         glGenTextures(1, (GLuint *)&texid);
1124
1125         glBindTexture(GL_TEXTURE_2D, texid);
1126
1127         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1128         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1129
1130         if (type == GL_FLOAT)
1131                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1132         else
1133                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1134
1135         glBegin(GL_QUADS);
1136
1137         if (draw_overlay) {
1138                 if (sseq->overlay_type == SEQ_DRAW_OVERLAY_RECT) {
1139                         rctf tot_clip;
1140                         tot_clip.xmin = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmin);
1141                         tot_clip.ymin = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymin);
1142                         tot_clip.xmax = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmax);
1143                         tot_clip.ymax = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymax);
1144
1145                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmin, tot_clip.ymin);
1146                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmin, tot_clip.ymax);
1147                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmax, tot_clip.ymax);
1148                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmax, tot_clip.ymin);
1149                 }
1150                 else if (sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) {
1151                         glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1152                         glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1153                         glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1154                         glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1155                 }
1156         }
1157         else {
1158                 glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1159                 glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1160                 glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1161                 glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1162         }
1163         glEnd();
1164         glBindTexture(GL_TEXTURE_2D, last_texid);
1165         glDisable(GL_TEXTURE_2D);
1166         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF && sseq->flag & SEQ_USE_ALPHA)
1167                 glDisable(GL_BLEND);
1168         glDeleteTextures(1, &texid);
1169
1170         if (glsl_used)
1171                 IMB_colormanagement_finish_glsl_draw();
1172
1173         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1174
1175                 float x1 = v2d->tot.xmin;
1176                 float y1 = v2d->tot.ymin;
1177                 float x2 = v2d->tot.xmax;
1178                 float y2 = v2d->tot.ymax;
1179
1180                 /* border */
1181                 setlinestyle(3);
1182
1183                 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 1.0, 0);
1184
1185                 glBegin(GL_LINE_LOOP);
1186                 glVertex2f(x1 - 0.5f, y1 - 0.5f);
1187                 glVertex2f(x1 - 0.5f, y2 + 0.5f);
1188                 glVertex2f(x2 + 0.5f, y2 + 0.5f);
1189                 glVertex2f(x2 + 0.5f, y1 - 0.5f);
1190                 glEnd();
1191
1192                 /* safety border */
1193                 if ((sseq->flag & SEQ_DRAW_SAFE_MARGINS) != 0) {
1194                         float fac = 0.1;
1195
1196                         float a = fac * (x2 - x1);
1197                         x1 += a;
1198                         x2 -= a;
1199
1200                         a = fac * (y2 - y1);
1201                         y1 += a;
1202                         y2 -= a;
1203
1204                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1205
1206                         uiSetRoundBox(UI_CNR_ALL);
1207                         uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1208
1209                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1210
1211                 }
1212
1213                 setlinestyle(0);
1214         }
1215         
1216         if (sseq->flag & SEQ_SHOW_GPENCIL) {
1217                 if (is_imbuf) {
1218                         /* draw grease-pencil (image aligned) */
1219                         draw_gpencil_2dimage(C);
1220                 }
1221         }
1222
1223         if (!scope)
1224                 IMB_freeImBuf(ibuf);
1225         
1226         /* ortho at pixel level */
1227         UI_view2d_view_restore(C);
1228         
1229         if (sseq->flag & SEQ_SHOW_GPENCIL) {
1230                 if (is_imbuf) {
1231                         /* draw grease-pencil (screen aligned) */
1232                         draw_gpencil_view2d(C, 0);
1233                 }
1234         }
1235
1236
1237         /* NOTE: sequencer mask editing isnt finished, the draw code is working but editing not,
1238          * for now just disable drawing since the strip frame will likely be offset */
1239
1240         //if (sc->mode == SC_MODE_MASKEDIT) {
1241         if (0 && sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1242                 Mask *mask = BKE_sequencer_mask_get(scene);
1243
1244                 if (mask) {
1245                         int width, height;
1246                         float aspx = 1.0f, aspy = 1.0f;
1247                         // ED_mask_get_size(C, &width, &height);
1248
1249                         //Scene *scene = CTX_data_scene(C);
1250                         width = (scene->r.size * scene->r.xsch) / 100;
1251                         height = (scene->r.size * scene->r.ysch) / 100;
1252
1253                         ED_mask_draw_region(mask, ar,
1254                                             0, 0, 0,  /* TODO */
1255                                             width, height,
1256                                             aspx, aspy,
1257                                             FALSE, TRUE,
1258                                             NULL, C);
1259                 }
1260         }
1261
1262         if (cache_handle)
1263                 IMB_display_buffer_release(cache_handle);
1264 }
1265
1266 #if 0
1267 void drawprefetchseqspace(Scene *scene, ARegion *UNUSED(ar), SpaceSeq *sseq)
1268 {
1269         int rectx, recty;
1270         int render_size = sseq->render_size;
1271         int proxy_size = 100.0; 
1272         if (render_size == 0) {
1273                 render_size = scene->r.size;
1274         }
1275         else {
1276                 proxy_size = render_size;
1277         }
1278         if (render_size < 0) {
1279                 return;
1280         }
1281
1282         rectx = (render_size * scene->r.xsch) / 100;
1283         recty = (render_size * scene->r.ysch) / 100;
1284
1285         if (sseq->mainb != SEQ_DRAW_SEQUENCE) {
1286                 give_ibuf_prefetch_request(
1287                     rectx, recty, (scene->r.cfra), sseq->chanshown,
1288                     proxy_size);
1289         }
1290 }
1291 #endif
1292
1293 /* draw backdrop of the sequencer strips view */
1294 static void draw_seq_backdrop(View2D *v2d)
1295 {
1296         int i;
1297         
1298         /* darker gray overlay over the view backdrop */
1299         UI_ThemeColorShade(TH_BACK, -20);
1300         glRectf(v2d->cur.xmin,  -1.0,  v2d->cur.xmax,  1.0);
1301
1302         /* Alternating horizontal stripes */
1303         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1304
1305         glBegin(GL_QUADS);
1306         while (i < v2d->cur.ymax) {
1307                 if (((int)i) & 1)
1308                         UI_ThemeColorShade(TH_BACK, -15);
1309                 else
1310                         UI_ThemeColorShade(TH_BACK, -25);
1311                         
1312                 glVertex2f(v2d->cur.xmax, i);
1313                 glVertex2f(v2d->cur.xmin, i);
1314                 glVertex2f(v2d->cur.xmin, i + 1);
1315                 glVertex2f(v2d->cur.xmax, i + 1);
1316
1317                 i += 1.0;
1318         }
1319         glEnd();
1320         
1321         /* Darker lines separating the horizontal bands */
1322         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1323         UI_ThemeColor(TH_GRID);
1324         
1325         glBegin(GL_LINES);
1326         while (i < v2d->cur.ymax) {
1327                 glVertex2f(v2d->cur.xmax, i);
1328                 glVertex2f(v2d->cur.xmin, i);
1329                         
1330                 i += 1.0;
1331         }
1332         glEnd();
1333 }
1334
1335 /* draw the contents of the sequencer strips view */
1336 static void draw_seq_strips(const bContext *C, Editing *ed, ARegion *ar)
1337 {
1338         Scene *scene = CTX_data_scene(C);
1339         View2D *v2d = &ar->v2d;
1340         Sequence *last_seq = BKE_sequencer_active_get(scene);
1341         int sel = 0, j;
1342         float pixelx = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
1343         
1344         /* loop through twice, first unselected, then selected */
1345         for (j = 0; j < 2; j++) {
1346                 Sequence *seq;
1347                 int outline_tint = (j) ? -60 : -150; /* highlighting around strip edges indicating selection */
1348                 
1349                 /* loop through strips, checking for those that are visible */
1350                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1351                         /* boundbox and selection tests for NOT drawing the strip... */
1352                         if ((seq->flag & SELECT) != sel) continue;
1353                         else if (seq == last_seq) continue;
1354                         else if (min_ii(seq->startdisp, seq->start) > v2d->cur.xmax) continue;
1355                         else if (max_ii(seq->enddisp, seq->start + seq->len) < v2d->cur.xmin) continue;
1356                         else if (seq->machine + 1.0f < v2d->cur.ymin) continue;
1357                         else if (seq->machine > v2d->cur.ymax) continue;
1358                         
1359                         /* strip passed all tests unscathed... so draw it now */
1360                         draw_seq_strip(scene, ar, seq, outline_tint, pixelx);
1361                 }
1362                 
1363                 /* draw selected next time round */
1364                 sel = SELECT;
1365         }
1366         
1367         /* draw the last selected last (i.e. 'active' in other parts of Blender), removes some overlapping error */
1368         if (last_seq)
1369                 draw_seq_strip(scene, ar, last_seq, 120, pixelx);
1370 }
1371
1372 static void seq_draw_sfra_efra(Scene *scene, View2D *v2d)
1373 {       
1374         glEnable(GL_BLEND);
1375         
1376         /* draw darkened area outside of active timeline 
1377          * frame range used is preview range or scene range */
1378         UI_ThemeColorShadeAlpha(TH_BACK, -25, -100);
1379
1380         if (PSFRA < PEFRA + 1) {
1381                 glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
1382                 glRectf((float)(PEFRA + 1), v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1383         }
1384         else {
1385                 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1386         }
1387
1388         UI_ThemeColorShade(TH_BACK, -60);
1389         /* thin lines where the actual frames are */
1390         fdrawline((float)PSFRA, v2d->cur.ymin, (float)PSFRA, v2d->cur.ymax);
1391         fdrawline((float)(PEFRA + 1), v2d->cur.ymin, (float)(PEFRA + 1), v2d->cur.ymax);
1392         
1393         glDisable(GL_BLEND);
1394 }
1395
1396 /* Draw Timeline/Strip Editor Mode for Sequencer */
1397 void draw_timeline_seq(const bContext *C, ARegion *ar)
1398 {
1399         Scene *scene = CTX_data_scene(C);
1400         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1401         SpaceSeq *sseq = CTX_wm_space_seq(C);
1402         View2D *v2d = &ar->v2d;
1403         View2DScrollers *scrollers;
1404         short unit = 0, flag = 0;
1405         float col[3];
1406         
1407         /* clear and setup matrix */
1408         UI_GetThemeColor3fv(TH_BACK, col);
1409         if (ed && ed->metastack.first) 
1410                 glClearColor(col[0], col[1], col[2] - 0.1f, 0.0f);
1411         else 
1412                 glClearColor(col[0], col[1], col[2], 0.0f);
1413         glClear(GL_COLOR_BUFFER_BIT);
1414
1415         UI_view2d_view_ortho(v2d);
1416         
1417         
1418         /* calculate extents of sequencer strips/data 
1419          * NOTE: needed for the scrollers later
1420          */
1421         boundbox_seq(scene, &v2d->tot);
1422         
1423         
1424         /* draw backdrop */
1425         draw_seq_backdrop(v2d);
1426         
1427         /* regular grid-pattern over the rest of the view (i.e. 25-frame grid lines) */
1428         // NOTE: the gridlines are currently spaced every 25 frames, which is only fine for 25 fps, but maybe not for 30...
1429         UI_view2d_constant_grid_draw(v2d);
1430
1431         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
1432         
1433         seq_draw_sfra_efra(scene, v2d);
1434
1435         /* sequence strips (if there is data available to be drawn) */
1436         if (ed) {
1437                 /* draw the data */
1438                 draw_seq_strips(C, ed, ar);
1439                 
1440                 /* text draw cached (for sequence names), in pixelspace now */
1441                 UI_view2d_text_cache_draw(ar);
1442         }
1443         
1444         /* current frame */
1445         UI_view2d_view_ortho(v2d);
1446         if ((sseq->flag & SEQ_DRAWFRAMES) == 0)      flag |= DRAWCFRA_UNIT_SECONDS;
1447         if ((sseq->flag & SEQ_NO_DRAW_CFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
1448         ANIM_draw_cfra(C, v2d, flag);
1449         
1450         /* markers */
1451         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1452         draw_markers_time(C, DRAW_MARKERS_LINES);
1453         
1454         /* preview range */
1455         UI_view2d_view_ortho(v2d);
1456         ANIM_draw_previewrange(C, v2d, 1);
1457
1458         /* overlap playhead */
1459         if (scene->ed && scene->ed->over_flag & SEQ_EDIT_OVERLAY_SHOW) {
1460                 int cfra_over = (scene->ed->over_flag & SEQ_EDIT_OVERLAY_ABS) ? scene->ed->over_cfra : scene->r.cfra + scene->ed->over_ofs;
1461                 glColor3f(0.2, 0.2, 0.2);
1462                 // glRectf(cfra_over, v2d->cur.ymin, scene->ed->over_ofs + scene->r.cfra + 1, v2d->cur.ymax);
1463
1464                 glBegin(GL_LINES);
1465                 glVertex2f(cfra_over, v2d->cur.ymin);
1466                 glVertex2f(cfra_over, v2d->cur.ymax);
1467                 glEnd();
1468
1469         }
1470         
1471         /* callback */
1472         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1473
1474         /* reset view matrix */
1475         UI_view2d_view_restore(C);
1476
1477         /* scrollers */
1478         unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDSSEQ;
1479         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_UNIT_VALUES, V2D_GRID_CLAMP);
1480         UI_view2d_scrollers_draw(C, v2d, scrollers);
1481         UI_view2d_scrollers_free(scrollers);
1482 }
1483
1484