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