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