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