Support displaying metadata for images in sequencer preview windows (not
[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_EFFECT) {
503                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
504                                        name, seq->len);
505         }
506         else if (seq->type == SEQ_TYPE_SOUND_RAM) {
507                 if (seq->sound) {
508                         str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d",
509                                                name, seq->sound->name, seq->len);
510                 }
511                 else {
512                         str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
513                                                name, seq->len);
514                 }
515         }
516         else if (seq->type == SEQ_TYPE_MOVIE) {
517                 str_len = BLI_snprintf(str, sizeof(str), "%s: %s%s | %d",
518                                        name, seq->strip->dir, seq->strip->stripdata->name, seq->len);
519         }
520         else {
521                 /* should never get here!, but might with files from future */
522                 BLI_assert(0);
523
524                 str_len = BLI_snprintf(str, sizeof(str), "%s | %d",
525                                        name, seq->len);
526         }
527         
528         if (seq->flag & SELECT) {
529                 col[0] = col[1] = col[2] = 255;
530         }
531         else if ((((int)background_col[0] + (int)background_col[1] + (int)background_col[2]) / 3) < 50) {
532                 col[0] = col[1] = col[2] = 80; /* use lighter text color for dark background */
533         }
534         else {
535                 col[0] = col[1] = col[2] = 0;
536         }
537         col[3] = 255;
538
539         rect.xmin = x1;
540         rect.ymin = y1;
541         rect.xmax = x2;
542         rect.ymax = y2;
543
544         UI_view2d_text_cache_add_rectf(v2d, &rect, str, str_len, col);
545 }
546
547 /* draws a shaded strip, made from gradient + flat color + gradient */
548 void draw_shadedstrip(Sequence *seq, unsigned char col[3], float x1, float y1, float x2, float y2)
549 {
550         float ymid1, ymid2;
551         
552         if (seq->flag & SEQ_MUTE) {
553                 glEnable(GL_POLYGON_STIPPLE);
554                 glPolygonStipple(stipple_halftone);
555         }
556         
557         ymid1 = (y2 - y1) * 0.25f + y1;
558         ymid2 = (y2 - y1) * 0.65f + y1;
559         
560         glShadeModel(GL_SMOOTH);
561         glBegin(GL_QUADS);
562         
563         if (seq->flag & SEQ_INVALID_EFFECT) { col[0] = 255; col[1] = 0; col[2] = 255; }
564         else if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, -50);
565         /* else UI_GetColorPtrShade3ubv(col, col, 0); */ /* DO NOTHING */
566         
567         glColor3ubv(col);
568         
569         glVertex2f(x1, y1);
570         glVertex2f(x2, y1);
571
572         if (seq->flag & SEQ_INVALID_EFFECT) { col[0] = 255; col[1] = 0; col[2] = 255; }
573         else if (seq->flag & SELECT) UI_GetColorPtrBlendShade3ubv(col, col, col, 0.0, 5);
574         else UI_GetColorPtrShade3ubv(col, col, -5);
575
576         glColor3ubv((GLubyte *)col);
577         
578         glVertex2f(x2, ymid1);
579         glVertex2f(x1, ymid1);
580         
581         glEnd();
582         
583         glRectf(x1,  ymid1,  x2,  ymid2);
584         
585         glBegin(GL_QUADS);
586         
587         glVertex2f(x1, ymid2);
588         glVertex2f(x2, ymid2);
589         
590         if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, -15);
591         else UI_GetColorPtrShade3ubv(col, col, 25);
592         
593         glColor3ubv((GLubyte *)col);
594         
595         glVertex2f(x2, y2);
596         glVertex2f(x1, y2);
597         
598         glEnd();
599         
600         if (seq->flag & SEQ_MUTE) {
601                 glDisable(GL_POLYGON_STIPPLE);
602         }
603 }
604
605 void draw_sequence_extensions(Scene *scene, ARegion *ar, Sequence *seq)
606 {
607         float x1, x2, y1, y2, pixely, a;
608         unsigned char col[3], blendcol[3];
609         View2D *v2d = &ar->v2d;
610         
611         x1 = seq->startdisp;
612         x2 = seq->enddisp;
613         
614         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
615         y2 = seq->machine + SEQ_STRIP_OFSTOP;
616         
617         pixely = BLI_rctf_size_y(&v2d->cur) / BLI_rcti_size_y(&v2d->mask);
618         
619         if (pixely <= 0) return;  /* can happen when the view is split/resized */
620         
621         blendcol[0] = blendcol[1] = blendcol[2] = 120;
622         
623         if (seq->startofs) {
624                 glEnable(GL_BLEND);
625                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
626                 
627                 color3ubv_from_seq(scene, seq, col);
628                 
629                 if (seq->flag & SELECT) {
630                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.3, -40);
631                         glColor4ub(col[0], col[1], col[2], 170);
632                 }
633                 else {
634                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.6, 0);
635                         glColor4ub(col[0], col[1], col[2], 110);
636                 }
637                 
638                 glRectf((float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);
639                 
640                 if (seq->flag & SELECT) glColor4ub(col[0], col[1], col[2], 255);
641                 else glColor4ub(col[0], col[1], col[2], 160);
642                 
643                 fdrawbox((float)(seq->start), y1 - SEQ_STRIP_OFSBOTTOM, x1, y1);  //outline
644                 
645                 glDisable(GL_BLEND);
646         }
647         if (seq->endofs) {
648                 glEnable(GL_BLEND);
649                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
650                 
651                 color3ubv_from_seq(scene, seq, col);
652                 
653                 if (seq->flag & SELECT) {
654                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.3, -40);
655                         glColor4ub(col[0], col[1], col[2], 170);
656                 }
657                 else {
658                         UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.6, 0);
659                         glColor4ub(col[0], col[1], col[2], 110);
660                 }
661                 
662                 glRectf(x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM);
663                 
664                 if (seq->flag & SELECT) glColor4ub(col[0], col[1], col[2], 255);
665                 else glColor4ub(col[0], col[1], col[2], 160);
666                 
667                 fdrawbox(x2, y2, (float)(seq->start + seq->len), y2 + SEQ_STRIP_OFSBOTTOM); //outline
668                 
669                 glDisable(GL_BLEND);
670         }
671         if (seq->startstill) {
672                 color3ubv_from_seq(scene, seq, col);
673                 UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.75, 40);
674                 glColor3ubv((GLubyte *)col);
675                 
676                 draw_shadedstrip(seq, col, x1, y1, (float)(seq->start), y2);
677                 
678                 /* feint pinstripes, helps see exactly which is extended and which isn't,
679                  * especially when the extension is very small */
680                 if (seq->flag & SELECT) UI_GetColorPtrBlendShade3ubv(col, col, col, 0.0, 24);
681                 else UI_GetColorPtrShade3ubv(col, col, -16);
682                 
683                 glColor3ubv((GLubyte *)col);
684                 
685                 for (a = y1; a < y2; a += pixely * 2.0f) {
686                         fdrawline(x1,  a,  (float)(seq->start),  a);
687                 }
688         }
689         if (seq->endstill) {
690                 color3ubv_from_seq(scene, seq, col);
691                 UI_GetColorPtrBlendShade3ubv(col, blendcol, col, 0.75, 40);
692                 glColor3ubv((GLubyte *)col);
693                 
694                 draw_shadedstrip(seq, col, (float)(seq->start + seq->len), y1, x2, y2);
695                 
696                 /* feint pinstripes, helps see exactly which is extended and which isn't,
697                  * especially when the extension is very small */
698                 if (seq->flag & SELECT) UI_GetColorPtrShade3ubv(col, col, 24);
699                 else UI_GetColorPtrShade3ubv(col, col, -16);
700                 
701                 glColor3ubv((GLubyte *)col);
702                 
703                 for (a = y1; a < y2; a += pixely * 2.0f) {
704                         fdrawline((float)(seq->start + seq->len),  a,  x2,  a);
705                 }
706         }
707 }
708
709
710 /*
711  * Draw a sequence strip, bounds check already made
712  * ARegion is currently only used to get the windows width in pixels
713  * so wave file sample drawing precision is zoom adjusted
714  */
715 static void draw_seq_strip(const bContext *C, SpaceSeq *sseq, Scene *scene, ARegion *ar, Sequence *seq, int outline_tint, float pixelx)
716 {
717         View2D *v2d = &ar->v2d;
718         float x1, x2, y1, y2;
719         unsigned char col[3], background_col[3], is_single_image;
720         const float handsize_clamped = draw_seq_handle_size_get_clamped(seq, pixelx);
721
722         /* we need to know if this is a single image/color or not for drawing */
723         is_single_image = (char)BKE_sequence_single_check(seq);
724         
725         /* body */
726         x1 = (seq->startstill) ? seq->start : seq->startdisp;
727         y1 = seq->machine + SEQ_STRIP_OFSBOTTOM;
728         x2 = (seq->endstill) ? (seq->start + seq->len) : seq->enddisp;
729         y2 = seq->machine + SEQ_STRIP_OFSTOP;
730
731
732         /* get the correct color per strip type*/
733         //color3ubv_from_seq(scene, seq, col);
734         color3ubv_from_seq(scene, seq, background_col);
735         
736         /* draw the main strip body */
737         if (is_single_image) {  /* single image */
738                 draw_shadedstrip(seq, background_col,
739                                  BKE_sequence_tx_get_final_left(seq, false), y1,
740                                  BKE_sequence_tx_get_final_right(seq, false), y2);
741         }
742         else {  /* normal operation */
743                 draw_shadedstrip(seq, background_col, x1, y1, x2, y2);
744         }
745
746         if (!is_single_image) {
747                 if ((sseq->draw_flag & SEQ_DRAW_OFFSET_EXT) || (seq == special_seq_update)) {
748                         draw_sequence_extensions(scene, ar, seq);
749                 }
750         }
751
752         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_LEFTHANDLE);
753         draw_seq_handle(v2d, seq, handsize_clamped, SEQ_RIGHTHANDLE);
754         
755         /* draw the strip outline */
756         x1 = seq->startdisp;
757         x2 = seq->enddisp;
758         
759         /* draw sound wave */
760         if (seq->type == SEQ_TYPE_SOUND_RAM) {
761                 if (!(sseq->flag & SEQ_NO_WAVEFORMS)) {
762                         drawseqwave(C, sseq, scene, seq, x1, y1, x2, y2, BLI_rctf_size_x(&ar->v2d.cur) / ar->winx);
763                 }
764         }
765
766         /* draw lock */
767         if (seq->flag & SEQ_LOCK) {
768                 glEnable(GL_POLYGON_STIPPLE);
769                 glEnable(GL_BLEND);
770
771                 /* light stripes */
772                 glColor4ub(255, 255, 255, 32);
773                 glPolygonStipple(stipple_diag_stripes_pos);
774                 glRectf(x1, y1, x2, y2);
775
776                 /* dark stripes */
777                 glColor4ub(0, 0, 0, 32);
778                 glPolygonStipple(stipple_diag_stripes_neg);
779                 glRectf(x1, y1, x2, y2);
780
781                 glDisable(GL_POLYGON_STIPPLE);
782                 glDisable(GL_BLEND);
783         }
784
785         if (!BKE_sequence_is_valid_check(seq)) {
786                 glEnable(GL_POLYGON_STIPPLE);
787
788                 /* panic! */
789                 glColor4ub(255, 0, 0, 255);
790                 glPolygonStipple(stipple_diag_stripes_pos);
791                 glRectf(x1, y1, x2, y2);
792
793                 glDisable(GL_POLYGON_STIPPLE);
794         }
795
796         color3ubv_from_seq(scene, seq, col);
797         if ((G.moving & G_TRANSFORM_SEQ) && (seq->flag & SELECT)) {
798                 if (seq->flag & SEQ_OVERLAP) {
799                         col[0] = 255; col[1] = col[2] = 40;
800                 }
801                 else
802                         UI_GetColorPtrShade3ubv(col, col, 120 + outline_tint);
803         }
804         else
805                 UI_GetColorPtrShade3ubv(col, col, outline_tint);
806         
807         glColor3ubv((GLubyte *)col);
808         
809         if (seq->flag & SEQ_MUTE) {
810                 glEnable(GL_LINE_STIPPLE);
811                 glLineStipple(1, 0x8888);
812         }
813         
814         UI_draw_roundbox_shade_x(GL_LINE_LOOP, x1, y1, x2, y2, 0.0, 0.1, 0.0);
815         
816         if (seq->flag & SEQ_MUTE) {
817                 glDisable(GL_LINE_STIPPLE);
818         }
819         
820         if (seq->type == SEQ_TYPE_META) {
821                 drawmeta_contents(scene, seq, x1, y1, x2, y2);
822         }
823         
824         /* calculate if seq is long enough to print a name */
825         x1 = seq->startdisp + handsize_clamped;
826         x2 = seq->enddisp   - handsize_clamped;
827
828         /* info text on the strip */
829         if (x1 < v2d->cur.xmin) x1 = v2d->cur.xmin;
830         else if (x1 > v2d->cur.xmax) x1 = v2d->cur.xmax;
831         if (x2 < v2d->cur.xmin) x2 = v2d->cur.xmin;
832         else if (x2 > v2d->cur.xmax) x2 = v2d->cur.xmax;
833
834         /* nice text here would require changing the view matrix for texture text */
835         if ((x2 - x1) / pixelx > 32) {
836                 draw_seq_text(v2d, seq, x1, x2, y1, y2, background_col);
837         }
838 }
839
840 void sequencer_special_update_set(Sequence *seq)
841 {
842         special_seq_update = seq;
843 }
844
845 Sequence *ED_sequencer_special_preview_get(void)
846 {
847         return special_seq_update;
848 }
849
850 void ED_sequencer_special_preview_set(bContext *C, const int mval[2])
851 {
852         Scene *scene = CTX_data_scene(C);
853         ARegion *ar = CTX_wm_region(C);
854         int hand;
855         Sequence *seq;
856         seq = find_nearest_seq(scene, &ar->v2d, &hand, mval);
857         sequencer_special_update_set(seq);
858 }
859
860 void ED_sequencer_special_preview_clear(void)
861 {
862         sequencer_special_update_set(NULL);
863 }
864
865 ImBuf *sequencer_ibuf_get(struct Main *bmain, Scene *scene, SpaceSeq *sseq, int cfra, int frame_ofs, const char *viewname)
866 {
867         SeqRenderData context;
868         ImBuf *ibuf;
869         int rectx, recty;
870         float render_size;
871         float proxy_size = 100.0;
872         short is_break = G.is_break;
873
874         render_size = sseq->render_size;
875         if (render_size == 0) {
876                 render_size = scene->r.size;
877         }
878         else {
879                 proxy_size = render_size;
880         }
881
882         if (render_size < 0) {
883                 return NULL;
884         }
885
886         rectx = (render_size * (float)scene->r.xsch) / 100.0f + 0.5f;
887         recty = (render_size * (float)scene->r.ysch) / 100.0f + 0.5f;
888
889         BKE_sequencer_new_render_data(
890                 bmain->eval_ctx, bmain, scene,
891                 rectx, recty, proxy_size,
892                 &context);
893         context.view_id = BKE_scene_multiview_view_id_get(&scene->r, viewname);
894
895         /* sequencer could start rendering, in this case we need to be sure it wouldn't be canceled
896          * by Esc pressed somewhere in the past
897          */
898         G.is_break = false;
899
900         if (special_seq_update)
901                 ibuf = BKE_sequencer_give_ibuf_direct(&context, cfra + frame_ofs, special_seq_update);
902         else if (!U.prefetchframes) // XXX || (G.f & G_PLAYANIM) == 0) {
903                 ibuf = BKE_sequencer_give_ibuf(&context, cfra + frame_ofs, sseq->chanshown);
904         else
905                 ibuf = BKE_sequencer_give_ibuf_threaded(&context, cfra + frame_ofs, sseq->chanshown);
906
907         /* restore state so real rendering would be canceled (if needed) */
908         G.is_break = is_break;
909
910         return ibuf;
911 }
912
913 static void sequencer_check_scopes(SequencerScopes *scopes, ImBuf *ibuf)
914 {
915         if (scopes->reference_ibuf != ibuf) {
916                 if (scopes->zebra_ibuf) {
917                         IMB_freeImBuf(scopes->zebra_ibuf);
918                         scopes->zebra_ibuf = NULL;
919                 }
920
921                 if (scopes->waveform_ibuf) {
922                         IMB_freeImBuf(scopes->waveform_ibuf);
923                         scopes->waveform_ibuf = NULL;
924                 }
925
926                 if (scopes->sep_waveform_ibuf) {
927                         IMB_freeImBuf(scopes->sep_waveform_ibuf);
928                         scopes->sep_waveform_ibuf = NULL;
929                 }
930
931                 if (scopes->vector_ibuf) {
932                         IMB_freeImBuf(scopes->vector_ibuf);
933                         scopes->vector_ibuf = NULL;
934                 }
935
936                 if (scopes->histogram_ibuf) {
937                         IMB_freeImBuf(scopes->histogram_ibuf);
938                         scopes->histogram_ibuf = NULL;
939                 }
940         }
941 }
942
943 static ImBuf *sequencer_make_scope(Scene *scene, ImBuf *ibuf, ImBuf *(*make_scope_cb) (ImBuf *ibuf))
944 {
945         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
946         ImBuf *scope;
947         
948         IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
949                                                              &scene->display_settings);
950
951         scope = make_scope_cb(display_ibuf);
952
953         IMB_freeImBuf(display_ibuf);
954
955         return scope;
956 }
957
958 static void sequencer_display_size(Scene *scene, SpaceSeq *sseq, float r_viewrect[2])
959 {
960         float render_size, proxy_size;
961
962         if (sseq->render_size == SEQ_PROXY_RENDER_SIZE_SCENE) {
963                 render_size = (float)scene->r.size / 100.0f;
964                 proxy_size = 1.0f;
965         }
966         else {
967                 render_size = (float)sseq->render_size / 100.0f;
968                 proxy_size = render_size;
969         }
970
971         r_viewrect[0] = (render_size * (float)scene->r.xsch);
972         r_viewrect[1] = (render_size * (float)scene->r.ysch);
973
974         /* rectx = viewrectx + 0.5f; */ /* UNUSED */
975         /* recty = viewrecty + 0.5f; */ /* UNUSED */
976
977         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
978                 r_viewrect[0] *= scene->r.xasp / scene->r.yasp;
979                 r_viewrect[0] /= proxy_size;
980                 r_viewrect[1] /= proxy_size;
981         }
982 }
983
984 static void sequencer_draw_gpencil(const bContext *C)
985 {
986         /* draw grease-pencil (image aligned) */
987         ED_gpencil_draw_2dimage(C);
988
989         /* ortho at pixel level */
990         UI_view2d_view_restore(C);
991
992         /* draw grease-pencil (screen aligned) */
993         ED_gpencil_draw_view2d(C, 0);
994 }
995
996 /* draws content borders plus safety borders if needed */
997 static void sequencer_draw_borders(const SpaceSeq *sseq, const View2D *v2d, const Scene *scene)
998 {
999         float x1 = v2d->tot.xmin;
1000         float y1 = v2d->tot.ymin;
1001         float x2 = v2d->tot.xmax;
1002         float y2 = v2d->tot.ymax;
1003
1004         /* border */
1005         setlinestyle(3);
1006
1007         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 1.0, 0);
1008
1009         glBegin(GL_LINE_LOOP);
1010         glVertex2f(x1 - 0.5f, y1 - 0.5f);
1011         glVertex2f(x1 - 0.5f, y2 + 0.5f);
1012         glVertex2f(x2 + 0.5f, y2 + 0.5f);
1013         glVertex2f(x2 + 0.5f, y1 - 0.5f);
1014         glEnd();
1015
1016         /* safety border */
1017         if (sseq->flag & SEQ_SHOW_SAFE_MARGINS) {
1018                 UI_draw_safe_areas(
1019                         x1, x2, y1, y2,
1020                         scene->safe_areas.title,
1021                         scene->safe_areas.action);
1022
1023                 if (sseq->flag & SEQ_SHOW_SAFE_CENTER) {
1024                         UI_draw_safe_areas(
1025                                 x1, x2, y1, y2,
1026                                 scene->safe_areas.title_center,
1027                                 scene->safe_areas.action_center);
1028                 }
1029         }
1030
1031         setlinestyle(0);
1032 }
1033
1034 /* draws checkerboard background for transparent content */
1035 static void sequencer_draw_background(const SpaceSeq *sseq, View2D *v2d, const float viewrect[2])
1036 {
1037         /* setting up the view */
1038         UI_view2d_totRect_set(v2d, viewrect[0] + 0.5f, viewrect[1] + 0.5f);
1039         UI_view2d_curRect_validate(v2d);
1040         UI_view2d_view_ortho(v2d);
1041
1042         /* only draw alpha for main buffer */
1043         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1044                 if (sseq->flag & SEQ_USE_ALPHA) {
1045                         glEnable(GL_BLEND);
1046                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1047
1048                         fdrawcheckerboard(v2d->tot.xmin, v2d->tot.ymin, v2d->tot.xmax, v2d->tot.ymax);
1049                         glColor4f(1.0, 1.0, 1.0, 1.0);
1050                 }
1051         }
1052 }
1053
1054 void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq, int cfra, int frame_ofs, bool draw_overlay, bool draw_backdrop)
1055 {
1056         struct Main *bmain = CTX_data_main(C);
1057         struct ImBuf *ibuf = NULL;
1058         struct ImBuf *scope = NULL;
1059         struct View2D *v2d = &ar->v2d;
1060         /* int rectx, recty; */ /* UNUSED */
1061         float viewrect[2];
1062         float col[3];
1063         GLuint texid;
1064         GLuint last_texid;
1065         void *display_buffer;
1066         void *cache_handle = NULL;
1067         const bool is_imbuf = ED_space_sequencer_check_show_imbuf(sseq);
1068         int format, type;
1069         bool glsl_used = false;
1070         const bool draw_gpencil = ((sseq->flag & SEQ_SHOW_GPENCIL) && sseq->gpd);
1071         const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
1072         bool draw_metadata = false;
1073
1074         if (G.is_rendering == false && (scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
1075                 /* stop all running jobs, except screen one. currently previews frustrate Render
1076                  * needed to make so sequencer's rendering doesn't conflict with compositor
1077                  */
1078                 WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_COMPOSITE);
1079
1080                 if ((scene->r.seq_flag & R_SEQ_GL_PREV) == 0) {
1081                         /* in case of final rendering used for preview, kill all previews,
1082                          * otherwise threading conflict will happen in rendering module
1083                          */
1084                         WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_RENDER_PREVIEW);
1085                 }
1086         }
1087
1088         if ((!draw_overlay || sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) && !draw_backdrop) {
1089                 UI_GetThemeColor3fv(TH_SEQ_PREVIEW, col);
1090                 glClearColor(col[0], col[1], col[2], 0.0);
1091                 glClear(GL_COLOR_BUFFER_BIT);
1092         }
1093
1094         /* without this colors can flicker from previous opengl state */
1095         glColor4ub(255, 255, 255, 255);
1096
1097         /* only initialize the preview if a render is in progress */
1098         if (G.is_rendering)
1099                 return;
1100
1101         if (sseq->render_size == SEQ_PROXY_RENDER_SIZE_NONE) {
1102                 return;
1103         }
1104
1105         /* for now we only support Left/Right */
1106         ibuf = sequencer_ibuf_get(bmain, scene, sseq, cfra, frame_ofs, names[sseq->multiview_eye]);
1107
1108         if ((ibuf == NULL) ||
1109             (ibuf->rect == NULL && ibuf->rect_float == NULL))
1110         {
1111                 /* gpencil can also be drawn if no imbuf is invalid */
1112                 if (draw_gpencil && is_imbuf) {
1113                         sequencer_display_size(scene, sseq, viewrect);
1114
1115                         sequencer_draw_background(sseq, v2d, viewrect);
1116                         sequencer_draw_borders(sseq, v2d, scene);
1117
1118                         sequencer_draw_gpencil(C);
1119                 }
1120                 return;
1121         }
1122
1123         sequencer_display_size(scene, sseq, viewrect);
1124
1125         if (!draw_backdrop && (sseq->mainb != SEQ_DRAW_IMG_IMBUF || sseq->zebra != 0)) {
1126                 SequencerScopes *scopes = &sseq->scopes;
1127
1128                 sequencer_check_scopes(scopes, ibuf);
1129
1130                 switch (sseq->mainb) {
1131                         case SEQ_DRAW_IMG_IMBUF:
1132                                 if (!scopes->zebra_ibuf) {
1133                                         ImBuf *display_ibuf = IMB_dupImBuf(ibuf);
1134
1135                                         if (display_ibuf->rect_float) {
1136                                                 IMB_colormanagement_imbuf_make_display_space(display_ibuf, &scene->view_settings,
1137                                                                                              &scene->display_settings);
1138                                         }
1139                                         scopes->zebra_ibuf = make_zebra_view_from_ibuf(display_ibuf, sseq->zebra);
1140                                         IMB_freeImBuf(display_ibuf);
1141                                 }
1142                                 scope = scopes->zebra_ibuf;
1143                                 break;
1144                         case SEQ_DRAW_IMG_WAVEFORM:
1145                                 if ((sseq->flag & SEQ_DRAW_COLOR_SEPARATED) != 0) {
1146                                         if (!scopes->sep_waveform_ibuf)
1147                                                 scopes->sep_waveform_ibuf = sequencer_make_scope(scene, ibuf, make_sep_waveform_view_from_ibuf);
1148                                         scope = scopes->sep_waveform_ibuf;
1149                                 }
1150                                 else {
1151                                         if (!scopes->waveform_ibuf)
1152                                                 scopes->waveform_ibuf = sequencer_make_scope(scene, ibuf, make_waveform_view_from_ibuf);
1153                                         scope = scopes->waveform_ibuf;
1154                                 }
1155                                 break;
1156                         case SEQ_DRAW_IMG_VECTORSCOPE:
1157                                 if (!scopes->vector_ibuf)
1158                                         scopes->vector_ibuf = sequencer_make_scope(scene, ibuf, make_vectorscope_view_from_ibuf);
1159                                 scope = scopes->vector_ibuf;
1160                                 break;
1161                         case SEQ_DRAW_IMG_HISTOGRAM:
1162                                 if (!scopes->histogram_ibuf)
1163                                         scopes->histogram_ibuf = sequencer_make_scope(scene, ibuf, make_histogram_view_from_ibuf);
1164                                 scope = scopes->histogram_ibuf;
1165                                 break;
1166                 }
1167
1168                 /* future files may have new scopes we don't catch above */
1169                 if (scope) {
1170                         scopes->reference_ibuf = ibuf;
1171                         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1172                                 /* scopes drawn in image preview use viewrect from orig ibuf - currently that's only zebra */
1173                         }
1174                         else {
1175                                 viewrect[0] = scope->x;
1176                                 viewrect[1] = scope->y;
1177                         }
1178                 }
1179                 else {
1180                         scopes->reference_ibuf = NULL;
1181                 }
1182         }
1183
1184         if (!draw_backdrop) {
1185                 sequencer_draw_background(sseq, v2d, viewrect);
1186         }
1187
1188         if (scope) {
1189                 IMB_freeImBuf(ibuf);
1190                 ibuf = scope;
1191
1192                 if (ibuf->rect_float && ibuf->rect == NULL) {
1193                         IMB_rect_from_float(ibuf);
1194                 }
1195
1196                 display_buffer = (unsigned char *)ibuf->rect;
1197                 format = GL_RGBA;
1198                 type = GL_UNSIGNED_BYTE;
1199         }
1200         else {
1201                 bool force_fallback = false;
1202
1203                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1204                 force_fallback |= (ibuf->dither != 0.0f);
1205
1206                 if (force_fallback) {
1207                         /* Fallback to CPU based color space conversion */
1208                         glsl_used = false;
1209                         format = GL_RGBA;
1210                         type = GL_UNSIGNED_BYTE;
1211                         display_buffer = NULL;
1212                 }
1213                 else if (ibuf->rect_float) {
1214                         display_buffer = ibuf->rect_float;
1215
1216                         if (ibuf->channels == 4) {
1217                                 format = GL_RGBA;
1218                         }
1219                         else if (ibuf->channels == 3) {
1220                                 format = GL_RGB;
1221                         }
1222                         else {
1223                                 BLI_assert(!"Incompatible number of channels for float buffer in sequencer");
1224                                 format = GL_RGBA;
1225                                 display_buffer = NULL;
1226                         }
1227
1228                         type = GL_FLOAT;
1229
1230                         if (ibuf->float_colorspace) {
1231                                 glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->float_colorspace, ibuf->dither, true);
1232                         }
1233                         else {
1234                                 glsl_used = IMB_colormanagement_setup_glsl_draw_ctx(C, ibuf->dither, true);
1235                         }
1236                 }
1237                 else if (ibuf->rect) {
1238                         display_buffer = ibuf->rect;
1239                         format = GL_RGBA;
1240                         type = GL_UNSIGNED_BYTE;
1241
1242                         glsl_used = IMB_colormanagement_setup_glsl_draw_from_space_ctx(C, ibuf->rect_colorspace, ibuf->dither, false);
1243                 }
1244                 else {
1245                         format = GL_RGBA;
1246                         type = GL_UNSIGNED_BYTE;
1247                         display_buffer = NULL;
1248                 }
1249
1250                 /* there's a data to be displayed, but GLSL is not initialized
1251                  * properly, in this case we fallback to CPU-based display transform
1252                  */
1253                 if ((ibuf->rect || ibuf->rect_float) && !glsl_used) {
1254                         display_buffer = IMB_display_buffer_acquire_ctx(C, ibuf, &cache_handle);
1255                         format = GL_RGBA;
1256                         type = GL_UNSIGNED_BYTE;
1257                 }
1258         }
1259
1260         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1261         glColor4f(1.0, 1.0, 1.0, 1.0);
1262
1263         last_texid = glaGetOneInteger(GL_TEXTURE_2D);
1264         glEnable(GL_TEXTURE_2D);
1265         glGenTextures(1, (GLuint *)&texid);
1266
1267         glBindTexture(GL_TEXTURE_2D, texid);
1268
1269         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1270         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1271
1272         if (type == GL_FLOAT)
1273                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1274         else
1275                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ibuf->x, ibuf->y, 0, format, type, display_buffer);
1276
1277         if (draw_backdrop) {
1278                 glMatrixMode(GL_PROJECTION);
1279                 glPushMatrix();
1280                 glLoadIdentity();
1281                 glMatrixMode(GL_MODELVIEW);
1282                 glPushMatrix();
1283                 glLoadIdentity();
1284         }
1285         glBegin(GL_QUADS);
1286
1287         if (draw_overlay) {
1288                 if (sseq->overlay_type == SEQ_DRAW_OVERLAY_RECT) {
1289                         rctf tot_clip;
1290                         tot_clip.xmin = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmin);
1291                         tot_clip.ymin = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymin);
1292                         tot_clip.xmax = v2d->tot.xmin + (fabsf(BLI_rctf_size_x(&v2d->tot)) * scene->ed->over_border.xmax);
1293                         tot_clip.ymax = v2d->tot.ymin + (fabsf(BLI_rctf_size_y(&v2d->tot)) * scene->ed->over_border.ymax);
1294
1295                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmin, tot_clip.ymin);
1296                         glTexCoord2f(scene->ed->over_border.xmin, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmin, tot_clip.ymax);
1297                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymax); glVertex2f(tot_clip.xmax, tot_clip.ymax);
1298                         glTexCoord2f(scene->ed->over_border.xmax, scene->ed->over_border.ymin); glVertex2f(tot_clip.xmax, tot_clip.ymin);
1299                 }
1300                 else if (sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) {
1301                         glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1302                         glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1303                         glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1304                         glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1305                 }
1306         }
1307         else if (draw_backdrop) {
1308                 float aspect;
1309                 float image_aspect = viewrect[0] / viewrect[1];
1310                 float imagex, imagey;
1311
1312                 aspect = BLI_rcti_size_x(&ar->winrct) / (float)BLI_rcti_size_y(&ar->winrct);
1313
1314                 if (aspect >= image_aspect) {
1315                         imagex = image_aspect / aspect;
1316                         imagey = 1.0f;
1317                 }
1318                 else {
1319                         imagex = 1.0f;
1320                         imagey = aspect / image_aspect;
1321                 }
1322
1323                 glTexCoord2f(0.0f, 0.0f); glVertex2f(-imagex, -imagey);
1324                 glTexCoord2f(0.0f, 1.0f); glVertex2f(-imagex, imagey);
1325                 glTexCoord2f(1.0f, 1.0f); glVertex2f(imagex, imagey);
1326                 glTexCoord2f(1.0f, 0.0f); glVertex2f(imagex, -imagey);
1327         }
1328         else {
1329                 draw_metadata = ((sseq->flag & SEQ_SHOW_METADATA) != 0);
1330
1331                 glTexCoord2f(0.0f, 0.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymin);
1332                 glTexCoord2f(0.0f, 1.0f); glVertex2f(v2d->tot.xmin, v2d->tot.ymax);
1333                 glTexCoord2f(1.0f, 1.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymax);
1334                 glTexCoord2f(1.0f, 0.0f); glVertex2f(v2d->tot.xmax, v2d->tot.ymin);
1335         }
1336         glEnd();
1337
1338         glBindTexture(GL_TEXTURE_2D, last_texid);
1339         glDisable(GL_TEXTURE_2D);
1340         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF && sseq->flag & SEQ_USE_ALPHA)
1341                 glDisable(GL_BLEND);
1342         glDeleteTextures(1, &texid);
1343
1344         if (glsl_used)
1345                 IMB_colormanagement_finish_glsl_draw();
1346
1347         if (cache_handle)
1348                 IMB_display_buffer_release(cache_handle);
1349
1350         if (!scope)
1351                 IMB_freeImBuf(ibuf);
1352
1353         if (draw_metadata) {
1354                 ED_region_image_metadata_draw(0.0, 0.0, ibuf, v2d->tot, 1.0, 1.0);
1355         }
1356
1357         if (draw_backdrop) {
1358                 glPopMatrix();
1359                 glMatrixMode(GL_PROJECTION);
1360                 glPopMatrix();
1361                 glMatrixMode(GL_MODELVIEW);
1362                 return;
1363         }
1364
1365         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1366                 sequencer_draw_borders(sseq, v2d, scene);
1367         }
1368
1369         if (draw_gpencil && is_imbuf) {
1370                 sequencer_draw_gpencil(C);
1371         }
1372         else {
1373                 /* ortho at pixel level */
1374                 UI_view2d_view_restore(C);
1375         }
1376
1377
1378         /* NOTE: sequencer mask editing isnt finished, the draw code is working but editing not,
1379          * for now just disable drawing since the strip frame will likely be offset */
1380
1381         //if (sc->mode == SC_MODE_MASKEDIT) {
1382         if (0 && sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
1383                 Mask *mask = BKE_sequencer_mask_get(scene);
1384
1385                 if (mask) {
1386                         int width, height;
1387                         float aspx = 1.0f, aspy = 1.0f;
1388                         // ED_mask_get_size(C, &width, &height);
1389
1390                         //Scene *scene = CTX_data_scene(C);
1391                         width = (scene->r.size * scene->r.xsch) / 100;
1392                         height = (scene->r.size * scene->r.ysch) / 100;
1393
1394                         ED_mask_draw_region(mask, ar,
1395                                             0, 0, 0,  /* TODO */
1396                                             width, height,
1397                                             aspx, aspy,
1398                                             false, true,
1399                                             NULL, C);
1400                 }
1401         }
1402 }
1403
1404 #if 0
1405 void drawprefetchseqspace(Scene *scene, ARegion *UNUSED(ar), SpaceSeq *sseq)
1406 {
1407         int rectx, recty;
1408         int render_size = sseq->render_size;
1409         int proxy_size = 100.0; 
1410         if (render_size == 0) {
1411                 render_size = scene->r.size;
1412         }
1413         else {
1414                 proxy_size = render_size;
1415         }
1416         if (render_size < 0) {
1417                 return;
1418         }
1419
1420         rectx = (render_size * scene->r.xsch) / 100;
1421         recty = (render_size * scene->r.ysch) / 100;
1422
1423         if (sseq->mainb != SEQ_DRAW_SEQUENCE) {
1424                 give_ibuf_prefetch_request(
1425                     rectx, recty, (scene->r.cfra), sseq->chanshown,
1426                     proxy_size);
1427         }
1428 }
1429 #endif
1430
1431 /* draw backdrop of the sequencer strips view */
1432 static void draw_seq_backdrop(View2D *v2d)
1433 {
1434         int i;
1435         
1436         /* darker gray overlay over the view backdrop */
1437         UI_ThemeColorShade(TH_BACK, -20);
1438         glRectf(v2d->cur.xmin,  -1.0,  v2d->cur.xmax,  1.0);
1439
1440         /* Alternating horizontal stripes */
1441         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1442
1443         glBegin(GL_QUADS);
1444         while (i < v2d->cur.ymax) {
1445                 if (((int)i) & 1)
1446                         UI_ThemeColorShade(TH_BACK, -15);
1447                 else
1448                         UI_ThemeColorShade(TH_BACK, -25);
1449                         
1450                 glVertex2f(v2d->cur.xmax, i);
1451                 glVertex2f(v2d->cur.xmin, i);
1452                 glVertex2f(v2d->cur.xmin, i + 1);
1453                 glVertex2f(v2d->cur.xmax, i + 1);
1454
1455                 i += 1.0;
1456         }
1457         glEnd();
1458         
1459         /* Darker lines separating the horizontal bands */
1460         i = max_ii(1, ((int)v2d->cur.ymin) - 1);
1461         UI_ThemeColor(TH_GRID);
1462         
1463         glBegin(GL_LINES);
1464         while (i < v2d->cur.ymax) {
1465                 glVertex2f(v2d->cur.xmax, i);
1466                 glVertex2f(v2d->cur.xmin, i);
1467                         
1468                 i += 1.0;
1469         }
1470         glEnd();
1471 }
1472
1473 /* draw the contents of the sequencer strips view */
1474 static void draw_seq_strips(const bContext *C, Editing *ed, ARegion *ar)
1475 {
1476         Scene *scene = CTX_data_scene(C);
1477         View2D *v2d = &ar->v2d;
1478         SpaceSeq *sseq = CTX_wm_space_seq(C);
1479         Sequence *last_seq = BKE_sequencer_active_get(scene);
1480         int sel = 0, j;
1481         float pixelx = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
1482         
1483         /* loop through twice, first unselected, then selected */
1484         for (j = 0; j < 2; j++) {
1485                 Sequence *seq;
1486                 int outline_tint = (j) ? -60 : -150; /* highlighting around strip edges indicating selection */
1487                 
1488                 /* loop through strips, checking for those that are visible */
1489                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1490                         /* boundbox and selection tests for NOT drawing the strip... */
1491                         if ((seq->flag & SELECT) != sel) continue;
1492                         else if (seq == last_seq) continue;
1493                         else if (min_ii(seq->startdisp, seq->start) > v2d->cur.xmax) continue;
1494                         else if (max_ii(seq->enddisp, seq->start + seq->len) < v2d->cur.xmin) continue;
1495                         else if (seq->machine + 1.0f < v2d->cur.ymin) continue;
1496                         else if (seq->machine > v2d->cur.ymax) continue;
1497                         
1498                         /* strip passed all tests unscathed... so draw it now */
1499                         draw_seq_strip(C, sseq, scene, ar, seq, outline_tint, pixelx);
1500                 }
1501                 
1502                 /* draw selected next time round */
1503                 sel = SELECT;
1504         }
1505         
1506         /* draw the last selected last (i.e. 'active' in other parts of Blender), removes some overlapping error */
1507         if (last_seq)
1508                 draw_seq_strip(C, sseq, scene, ar, last_seq, 120, pixelx);
1509
1510         /* draw highlight when previewing a single strip */
1511         if (special_seq_update) {
1512                 const Sequence *seq = special_seq_update;
1513                 glEnable(GL_BLEND);
1514                 glColor4ub(255, 255, 255, 48);
1515                 glRectf(seq->startdisp, seq->machine + SEQ_STRIP_OFSBOTTOM, seq->enddisp, seq->machine + SEQ_STRIP_OFSTOP);
1516                 glDisable(GL_BLEND);
1517         }
1518 }
1519
1520 static void seq_draw_sfra_efra(Scene *scene, View2D *v2d)
1521 {
1522         const Editing *ed = BKE_sequencer_editing_get(scene, false);
1523         const int frame_sta = PSFRA;
1524         const int frame_end = PEFRA + 1;
1525
1526         glEnable(GL_BLEND);
1527         
1528         /* draw darkened area outside of active timeline 
1529          * frame range used is preview range or scene range */
1530         UI_ThemeColorShadeAlpha(TH_BACK, -25, -100);
1531
1532         if (frame_sta < frame_end) {
1533                 glRectf(v2d->cur.xmin, v2d->cur.ymin, (float)frame_sta, v2d->cur.ymax);
1534                 glRectf((float)frame_end, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1535         }
1536         else {
1537                 glRectf(v2d->cur.xmin, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
1538         }
1539
1540         UI_ThemeColorShade(TH_BACK, -60);
1541         /* thin lines where the actual frames are */
1542         fdrawline(frame_sta, v2d->cur.ymin, frame_sta, v2d->cur.ymax);
1543         fdrawline(frame_end, v2d->cur.ymin, frame_end, v2d->cur.ymax);
1544
1545         if (ed && !BLI_listbase_is_empty(&ed->metastack)) {
1546                 MetaStack *ms = ed->metastack.last;
1547
1548                 glColor4ub(255, 255, 255, 8);
1549                 glRectf(ms->disp_range[0], v2d->cur.ymin, ms->disp_range[1], v2d->cur.ymax);
1550
1551                 UI_ThemeColorShade(TH_BACK, -40);
1552
1553                 fdrawline(ms->disp_range[0], v2d->cur.ymin, ms->disp_range[0], v2d->cur.ymax);
1554                 fdrawline(ms->disp_range[1], v2d->cur.ymin, ms->disp_range[1], v2d->cur.ymax);
1555         }
1556
1557         glDisable(GL_BLEND);
1558 }
1559
1560 /* Draw Timeline/Strip Editor Mode for Sequencer */
1561 void draw_timeline_seq(const bContext *C, ARegion *ar)
1562 {
1563         Scene *scene = CTX_data_scene(C);
1564         Editing *ed = BKE_sequencer_editing_get(scene, false);
1565         SpaceSeq *sseq = CTX_wm_space_seq(C);
1566         View2D *v2d = &ar->v2d;
1567         View2DScrollers *scrollers;
1568         short unit = 0, flag = 0;
1569         float col[3];
1570         
1571         /* clear and setup matrix */
1572         UI_GetThemeColor3fv(TH_BACK, col);
1573         if (ed && ed->metastack.first) 
1574                 glClearColor(col[0], col[1], col[2] - 0.1f, 0.0f);
1575         else 
1576                 glClearColor(col[0], col[1], col[2], 0.0f);
1577         glClear(GL_COLOR_BUFFER_BIT);
1578
1579         UI_view2d_view_ortho(v2d);
1580         
1581         
1582         /* calculate extents of sequencer strips/data 
1583          * NOTE: needed for the scrollers later
1584          */
1585         boundbox_seq(scene, &v2d->tot);
1586         
1587         
1588         /* draw backdrop */
1589         draw_seq_backdrop(v2d);
1590         
1591         /* regular grid-pattern over the rest of the view (i.e. 25-frame grid lines) */
1592         // NOTE: the gridlines are currently spaced every 25 frames, which is only fine for 25 fps, but maybe not for 30...
1593         UI_view2d_constant_grid_draw(v2d);
1594
1595         if (sseq->draw_flag & SEQ_DRAW_BACKDROP) {
1596                 draw_image_seq(C, scene, ar, sseq, scene->r.cfra, 0, false, true);
1597                 UI_view2d_view_ortho(v2d);
1598         }
1599                 
1600         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
1601         
1602         seq_draw_sfra_efra(scene, v2d);
1603
1604         /* sequence strips (if there is data available to be drawn) */
1605         if (ed) {
1606                 /* draw the data */
1607                 draw_seq_strips(C, ed, ar);
1608                 
1609                 /* text draw cached (for sequence names), in pixelspace now */
1610                 UI_view2d_text_cache_draw(ar);
1611         }
1612         
1613         /* current frame */
1614         UI_view2d_view_ortho(v2d);
1615         if ((sseq->flag & SEQ_DRAWFRAMES) == 0)      flag |= DRAWCFRA_UNIT_SECONDS;
1616         if ((sseq->flag & SEQ_NO_DRAW_CFRANUM) == 0) flag |= DRAWCFRA_SHOW_NUMBOX;
1617         ANIM_draw_cfra(C, v2d, flag);
1618         
1619         /* markers */
1620         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1621         ED_markers_draw(C, DRAW_MARKERS_LINES | DRAW_MARKERS_MARGIN);
1622         
1623         /* preview range */
1624         UI_view2d_view_ortho(v2d);
1625         ANIM_draw_previewrange(C, v2d, 1);
1626
1627         /* overlap playhead */
1628         if (scene->ed && scene->ed->over_flag & SEQ_EDIT_OVERLAY_SHOW) {
1629                 int cfra_over = (scene->ed->over_flag & SEQ_EDIT_OVERLAY_ABS) ? scene->ed->over_cfra : scene->r.cfra + scene->ed->over_ofs;
1630                 glColor3f(0.2, 0.2, 0.2);
1631                 // glRectf(cfra_over, v2d->cur.ymin, scene->ed->over_ofs + scene->r.cfra + 1, v2d->cur.ymax);
1632
1633                 glBegin(GL_LINES);
1634                 glVertex2f(cfra_over, v2d->cur.ymin);
1635                 glVertex2f(cfra_over, v2d->cur.ymax);
1636                 glEnd();
1637
1638         }
1639         
1640         /* callback */
1641         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1642
1643         /* reset view matrix */
1644         UI_view2d_view_restore(C);
1645
1646         /* scrollers */
1647         unit = (sseq->flag & SEQ_DRAWFRAMES) ? V2D_UNIT_FRAMES : V2D_UNIT_SECONDS;
1648         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_UNIT_VALUES, V2D_GRID_CLAMP);
1649         UI_view2d_scrollers_draw(C, v2d, scrollers);
1650         UI_view2d_scrollers_free(scrollers);
1651 }
1652
1653