Workbench: studiolight
[blender.git] / source / blender / editors / interface / interface_icons.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  * Contributors: Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_icons.c
27  *  \ingroup edinterface
28  */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "GPU_draw.h"
37 #include "GPU_matrix.h"
38 #include "GPU_batch.h"
39 #include "GPU_immediate.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_fileops_types.h"
44 #include "BLI_math_vector.h"
45
46 #include "DNA_brush_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_dynamicpaint_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_workspace_types.h"
53
54 #include "RNA_access.h"
55 #include "RNA_enum_types.h"
56
57 #include "BKE_context.h"
58 #include "BKE_global.h"
59 #include "BKE_icons.h"
60 #include "BKE_appdir.h"
61
62 #include "IMB_imbuf.h"
63 #include "IMB_imbuf_types.h"
64 #include "IMB_thumbs.h"
65
66 #include "BIF_glutil.h"
67
68 #include "DEG_depsgraph.h"
69
70 #include "DRW_engine.h"
71
72 #include "ED_datafiles.h"
73 #include "ED_keyframes_draw.h"
74 #include "ED_render.h"
75
76 #include "UI_interface.h"
77 #include "UI_interface_icons.h"
78
79 #include "interface_intern.h"
80
81 #ifndef WITH_HEADLESS
82 #define ICON_GRID_COLS      26
83 #define ICON_GRID_ROWS      30
84
85 #define ICON_GRID_MARGIN    10
86 #define ICON_GRID_W         32
87 #define ICON_GRID_H         32
88 #endif  /* WITH_HEADLESS */
89
90 typedef struct IconImage {
91         int w;
92         int h;
93         unsigned int *rect;
94         unsigned char *datatoc_rect;
95         int datatoc_size;
96 } IconImage;
97
98 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
99
100 #define ICON_TYPE_PREVIEW   0
101 #define ICON_TYPE_TEXTURE   1
102 #define ICON_TYPE_BUFFER    2
103 #define ICON_TYPE_VECTOR    3
104 #define ICON_TYPE_GEOM      4
105
106 typedef struct DrawInfo {
107         int type;
108
109         union {
110                 /* type specific data */
111                 struct {
112                         VectorDrawFunc func;
113                 } vector;
114                 struct {
115                         ImBuf *image_cache;
116                 } geom;
117                 struct {
118                         IconImage *image;
119                 } buffer;
120                 struct {
121                         int x, y, w, h;
122                 } texture;
123         } data;
124 } DrawInfo;
125
126 typedef struct IconTexture {
127         GLuint id;
128         int w;
129         int h;
130         float invw;
131         float invh;
132 } IconTexture;
133
134 /* ******************* STATIC LOCAL VARS ******************* */
135 /* static here to cache results of icon directory scan, so it's not 
136  * scanning the filesystem each time the menu is drawn */
137 static struct ListBase iconfilelist = {NULL, NULL};
138 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
139
140 /* **************************************************** */
141
142 #ifndef WITH_HEADLESS
143
144 static DrawInfo *def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
145 {
146         Icon *new_icon = NULL;
147         IconImage *iimg = NULL;
148         DrawInfo *di;
149
150         new_icon = MEM_callocN(sizeof(Icon), "texicon");
151
152         new_icon->obj = NULL; /* icon is not for library object */
153         new_icon->id_type = 0;
154
155         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
156         di->type = type;
157
158         if (type == ICON_TYPE_TEXTURE) {
159                 di->data.texture.x = xofs;
160                 di->data.texture.y = yofs;
161                 di->data.texture.w = size;
162                 di->data.texture.h = size;
163         }
164         else if (type == ICON_TYPE_BUFFER) {
165                 iimg = MEM_callocN(sizeof(IconImage), "icon_img");
166                 iimg->w = size;
167                 iimg->h = size;
168
169                 /* icon buffers can get initialized runtime now, via datatoc */
170                 if (bbuf) {
171                         int y, imgsize;
172                         
173                         iimg->rect = MEM_mallocN(size * size * sizeof(unsigned int), "icon_rect");
174                         
175                         /* Here we store the rect in the icon - same as before */
176                         if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0)
177                                 memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
178                         else {
179                                 /* this code assumes square images */
180                                 imgsize = bbuf->x;
181                                 for (y = 0; y < size; y++) {
182                                         memcpy(&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
183                                 }
184                         }
185                 }
186                 di->data.buffer.image = iimg;
187         }
188
189         new_icon->drawinfo_free = UI_icons_free_drawinfo;
190         new_icon->drawinfo = di;
191
192         BKE_icon_set(icon_id, new_icon);
193         
194         return di;
195 }
196
197 static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
198 {
199         Icon *new_icon = NULL;
200         DrawInfo *di;
201
202         new_icon = MEM_callocN(sizeof(Icon), "texicon");
203
204         new_icon->obj = NULL; /* icon is not for library object */
205         new_icon->id_type = 0;
206
207         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
208         di->type = ICON_TYPE_VECTOR;
209         di->data.vector.func = drawFunc;
210
211         new_icon->drawinfo_free = NULL;
212         new_icon->drawinfo = di;
213
214         BKE_icon_set(icon_id, new_icon);
215 }
216
217 /* Vector Icon Drawing Routines */
218
219 /* Utilities */
220
221 static void viconutil_set_point(GLint pt[2], int x, int y)
222 {
223         pt[0] = x;
224         pt[1] = y;
225 }
226
227 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
228 {
229         GLint pts[3][2];
230         int cx = x + w / 2 - 4;
231         int cy = y + w / 2;
232         int d = w / 5, d2 = w / 7;
233
234         viconutil_set_point(pts[0], cx - d2, cy + d);
235         viconutil_set_point(pts[1], cx - d2, cy - d);
236         viconutil_set_point(pts[2], cx + d2, cy);
237
238         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
239         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
240         immUniformColor4f(0.2f, 0.2f, 0.2f, alpha);
241
242         immBegin(GWN_PRIM_TRIS, 3);
243         immVertex2iv(pos, pts[0]);
244         immVertex2iv(pos, pts[1]);
245         immVertex2iv(pos, pts[2]);
246         immEnd();
247
248         immUnbindProgram();
249 }
250
251 static void vicon_keytype_draw_wrapper(int x, int y, int w, int h, float alpha, short key_type)
252 {
253         /* init dummy theme state for Action Editor - where these colors are defined
254          * (since we're doing this offscreen, free from any particular space_id)
255          */
256         struct bThemeState theme_state;
257         
258         UI_Theme_Store(&theme_state);
259         UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
260         
261         /* the "x" and "y" given are the bottom-left coordinates of the icon,
262          * while the draw_keyframe_shape() function needs the midpoint for
263          * the keyframe
264          */
265         int xco = x + w / 2;
266         int yco = y + h / 2;
267
268         Gwn_VertFormat *format = immVertexFormat();
269         unsigned int pos_id = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
270         unsigned int size_id = GWN_vertformat_attr_add(format, "size", GWN_COMP_F32, 1, GWN_FETCH_FLOAT);
271         unsigned int color_id = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
272         unsigned int outline_color_id = GWN_vertformat_attr_add(format, "outlineColor", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
273
274         immBindBuiltinProgram(GPU_SHADER_KEYFRAME_DIAMOND);
275         GPU_enable_program_point_size();
276         immBegin(GWN_PRIM_POINTS, 1);
277
278         /* draw keyframe
279          * - size: 0.6 * h (found out experimentally... dunno why!)
280          * - sel: true (so that "keyframe" state shows the iconic yellow icon)
281          */
282         draw_keyframe_shape(xco, yco, 0.6f * h, true, key_type, KEYFRAME_SHAPE_BOTH, alpha,
283                             pos_id, size_id, color_id, outline_color_id);
284
285         immEnd();
286         GPU_disable_program_point_size();
287         immUnbindProgram();
288
289         UI_Theme_Restore(&theme_state);
290 }
291
292 static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
293 {
294         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME);
295 }
296
297 static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
298 {
299         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN);
300 }
301
302 static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
303 {
304         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME);
305 }
306
307 static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
308 {
309         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER);
310 }
311
312 static void vicon_keytype_moving_hold_draw(int x, int y, int w, int h, float alpha)
313 {
314         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_MOVEHOLD);
315 }
316
317 static void vicon_colorset_draw(int index, int x, int y, int w, int h, float UNUSED(alpha))
318 {
319         bTheme *btheme = UI_GetTheme();
320         ThemeWireColor *cs = &btheme->tarm[index];
321
322         /* Draw three bands of color: One per color
323          *    x-----a-----b-----c
324          *    |  N  |  S  |  A  |
325          *    x-----a-----b-----c
326          */
327         const int a = x + w / 3;
328         const int b = x + w / 3 * 2;
329         const int c = x + w;
330
331         unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
332         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
333
334         /* XXX: Include alpha into this... */
335         /* normal */
336         immUniformColor3ubv((unsigned char *)cs->solid);
337         immRecti(pos, x, y, a, y + h);
338
339         /* selected */
340         immUniformColor3ubv((unsigned char *)cs->select);
341         immRecti(pos, a, y, b, y + h);
342
343         /* active */
344         immUniformColor3ubv((unsigned char *)cs->active);
345         immRecti(pos, b, y, c, y + h);
346
347         immUnbindProgram();
348 }
349
350 #define DEF_VICON_COLORSET_DRAW_NTH(prefix, index)                                    \
351         static void vicon_colorset_draw_##prefix(int x, int y, int w, int h, float alpha) \
352         {                                                                                 \
353                 vicon_colorset_draw(index, x, y, w, h, alpha);                                \
354         }
355         
356 DEF_VICON_COLORSET_DRAW_NTH(01, 0)
357 DEF_VICON_COLORSET_DRAW_NTH(02, 1)
358 DEF_VICON_COLORSET_DRAW_NTH(03, 2)
359 DEF_VICON_COLORSET_DRAW_NTH(04, 3)
360 DEF_VICON_COLORSET_DRAW_NTH(05, 4)
361 DEF_VICON_COLORSET_DRAW_NTH(06, 5)
362 DEF_VICON_COLORSET_DRAW_NTH(07, 6)
363 DEF_VICON_COLORSET_DRAW_NTH(08, 7)
364 DEF_VICON_COLORSET_DRAW_NTH(09, 8)
365 DEF_VICON_COLORSET_DRAW_NTH(10, 9)
366 DEF_VICON_COLORSET_DRAW_NTH(11, 10)
367 DEF_VICON_COLORSET_DRAW_NTH(12, 11)
368 DEF_VICON_COLORSET_DRAW_NTH(13, 12)
369 DEF_VICON_COLORSET_DRAW_NTH(14, 13)
370 DEF_VICON_COLORSET_DRAW_NTH(15, 14)
371 DEF_VICON_COLORSET_DRAW_NTH(16, 15)
372 DEF_VICON_COLORSET_DRAW_NTH(17, 16)
373 DEF_VICON_COLORSET_DRAW_NTH(18, 17)
374 DEF_VICON_COLORSET_DRAW_NTH(19, 18)
375 DEF_VICON_COLORSET_DRAW_NTH(20, 19)
376
377 #undef DEF_VICON_COLORSET_DRAW_NTH
378
379 #ifndef WITH_HEADLESS
380
381 static void init_brush_icons(void)
382 {
383
384 #define INIT_BRUSH_ICON(icon_id, name)                                          \
385         {                                                                           \
386                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _png;          \
387                 int size = datatoc_ ##name## _png_size;                                 \
388                 DrawInfo *di;                                                           \
389                 \
390                 di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER);       \
391                 di->data.buffer.image->datatoc_rect = rect;                             \
392                 di->data.buffer.image->datatoc_size = size;                             \
393         }
394         /* end INIT_BRUSH_ICON */
395
396         const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
397
398         INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
399         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
400         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
401         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
402         INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
403         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
404         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
405         INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
406         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
407         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
408         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
409         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
410         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
411         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
412         INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
413         INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
414         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
415         INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
416         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
417         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
418         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
419         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
420         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
421         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
422         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
423         INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
424         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
425         INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
426         INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
427         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
428         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
429         INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
430
431 #undef INIT_BRUSH_ICON
432 }
433
434 static void icon_verify_datatoc(IconImage *iimg)
435 {
436         /* if it has own rect, things are all OK */
437         if (iimg->rect)
438                 return;
439         
440         if (iimg->datatoc_rect) {
441                 ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
442                                                     iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
443                 /* w and h were set on initialize */
444                 if (bbuf->x != iimg->h && bbuf->y != iimg->w)
445                         IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
446                 
447                 iimg->rect = bbuf->rect;
448                 bbuf->rect = NULL;
449                 IMB_freeImBuf(bbuf);
450         }
451 }
452
453 static void init_matcap_icons(void)
454 {
455         /* dynamic allocation now, tucking datatoc pointers in DrawInfo */
456 #define INIT_MATCAP_ICON(icon_id, name)                                       \
457         {                                                                         \
458                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _jpg;        \
459                 int size = datatoc_ ##name## _jpg_size;                               \
460                 DrawInfo *di;                                                         \
461                                                                                       \
462                 di = def_internal_icon(NULL, icon_id, 0, 0, 96, ICON_TYPE_BUFFER);   \
463                 di->data.buffer.image->datatoc_rect = rect;                           \
464                 di->data.buffer.image->datatoc_size = size;                           \
465         } (void)0
466
467         INIT_MATCAP_ICON(ICON_MATCAP_01, mc01);
468         INIT_MATCAP_ICON(ICON_MATCAP_02, mc02);
469         INIT_MATCAP_ICON(ICON_MATCAP_03, mc03);
470         INIT_MATCAP_ICON(ICON_MATCAP_04, mc04);
471         INIT_MATCAP_ICON(ICON_MATCAP_05, mc05);
472         INIT_MATCAP_ICON(ICON_MATCAP_06, mc06);
473         INIT_MATCAP_ICON(ICON_MATCAP_07, mc07);
474         INIT_MATCAP_ICON(ICON_MATCAP_08, mc08);
475         INIT_MATCAP_ICON(ICON_MATCAP_09, mc09);
476         INIT_MATCAP_ICON(ICON_MATCAP_10, mc10);
477         INIT_MATCAP_ICON(ICON_MATCAP_11, mc11);
478         INIT_MATCAP_ICON(ICON_MATCAP_12, mc12);
479         INIT_MATCAP_ICON(ICON_MATCAP_13, mc13);
480         INIT_MATCAP_ICON(ICON_MATCAP_14, mc14);
481         INIT_MATCAP_ICON(ICON_MATCAP_15, mc15);
482         INIT_MATCAP_ICON(ICON_MATCAP_16, mc16);
483         INIT_MATCAP_ICON(ICON_MATCAP_17, mc17);
484         INIT_MATCAP_ICON(ICON_MATCAP_18, mc18);
485         INIT_MATCAP_ICON(ICON_MATCAP_19, mc19);
486         INIT_MATCAP_ICON(ICON_MATCAP_20, mc20);
487         INIT_MATCAP_ICON(ICON_MATCAP_21, mc21);
488         INIT_MATCAP_ICON(ICON_MATCAP_22, mc22);
489         INIT_MATCAP_ICON(ICON_MATCAP_23, mc23);
490         INIT_MATCAP_ICON(ICON_MATCAP_24, mc24);
491
492 #undef INIT_MATCAP_ICON
493
494 }
495
496 static void init_studio_light_icons(void)
497 {
498         /* dynamic allocation now, tucking datatoc pointers in DrawInfo */
499 #define INIT_STUDIOLIGHT_ICON(icon_id, studiolight_id)                                         \
500         {                                                                                          \
501                 DrawInfo *di;                                                                          \
502                 di = def_internal_icon(NULL, icon_id, 0, 0, 96, ICON_TYPE_BUFFER);                     \
503                 di->data.buffer.image->rect = WORKBENCH_generate_studiolight_preview(studiolight_id, 96); \
504         } (void)0
505
506         INIT_STUDIOLIGHT_ICON(ICON_STUDIOLIGHT_01, 0);
507         INIT_STUDIOLIGHT_ICON(ICON_STUDIOLIGHT_02, 1);
508         INIT_STUDIOLIGHT_ICON(ICON_STUDIOLIGHT_03, 2);
509         INIT_STUDIOLIGHT_ICON(ICON_STUDIOLIGHT_04, 3);
510         INIT_STUDIOLIGHT_ICON(ICON_STUDIOLIGHT_05, 4);
511
512 }
513 static void init_internal_icons(void)
514 {
515 //      bTheme *btheme = UI_GetTheme();
516         ImBuf *b16buf = NULL, *b32buf = NULL;
517         int x, y, icontype;
518
519 #if 0 // temp disabled
520         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
521                 char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
522                 char iconfilestr[FILE_MAX];
523                 
524                 if (icondir) {
525                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
526                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
527                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
528                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
529                                 IMB_freeImBuf(bbuf);
530                                 bbuf = NULL;
531                         }
532                 }
533                 else {
534                         printf("%s: 'icons' data path not found, continuing\n", __func__);
535                 }
536         }
537 #endif
538         if (b16buf == NULL)
539                 b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
540                                                datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
541         if (b16buf)
542                 IMB_premultiply_alpha(b16buf);
543
544         if (b32buf == NULL)
545                 b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
546                                                datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
547         if (b32buf)
548                 IMB_premultiply_alpha(b32buf);
549         
550         if (b16buf && b32buf) {
551                 /* free existing texture if any */
552                 if (icongltex.id) {
553                         glDeleteTextures(1, &icongltex.id);
554                         icongltex.id = 0;
555                 }
556
557 #if 0 /* should be a compile-time check (if needed at all) */
558                 /* we only use a texture for cards with non-power of two */
559                 if (GPU_full_non_power_of_two_support()) {
560 #else
561                 {
562 #endif
563                         glGenTextures(1, &icongltex.id);
564
565                         if (icongltex.id) {
566                                 int level = 2;
567                                 
568                                 icongltex.w = b32buf->x;
569                                 icongltex.h = b32buf->y;
570                                 icongltex.invw = 1.0f / b32buf->x;
571                                 icongltex.invh = 1.0f / b32buf->y;
572
573                                 glBindTexture(GL_TEXTURE_2D, icongltex.id);
574                                 
575                                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
576                                 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
577                                 
578                                 while (b16buf->x > 1) {
579                                         ImBuf *nbuf = IMB_onehalf(b16buf);
580                                         glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
581                                         level++;
582                                         IMB_freeImBuf(b16buf);
583                                         b16buf = nbuf;
584                                 }
585                                 
586                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
587                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
588                                 
589                                 glBindTexture(GL_TEXTURE_2D, 0);
590                         }
591                 }
592         }
593
594         if (icongltex.id)
595                 icontype = ICON_TYPE_TEXTURE;
596         else
597                 icontype = ICON_TYPE_BUFFER;
598         
599         if (b32buf) {
600                 for (y = 0; y < ICON_GRID_ROWS; y++) {
601                         for (x = 0; x < ICON_GRID_COLS; x++) {
602                                 def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
603                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
604                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
605                                                   icontype);
606                         }
607                 }
608         }
609
610         def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
611         
612         def_internal_vicon(VICO_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
613         def_internal_vicon(VICO_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
614         def_internal_vicon(VICO_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
615         def_internal_vicon(VICO_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
616         def_internal_vicon(VICO_KEYTYPE_MOVING_HOLD_VEC, vicon_keytype_moving_hold_draw);
617         
618         def_internal_vicon(VICO_COLORSET_01_VEC, vicon_colorset_draw_01);
619         def_internal_vicon(VICO_COLORSET_02_VEC, vicon_colorset_draw_02);
620         def_internal_vicon(VICO_COLORSET_03_VEC, vicon_colorset_draw_03);
621         def_internal_vicon(VICO_COLORSET_04_VEC, vicon_colorset_draw_04);
622         def_internal_vicon(VICO_COLORSET_05_VEC, vicon_colorset_draw_05);
623         def_internal_vicon(VICO_COLORSET_06_VEC, vicon_colorset_draw_06);
624         def_internal_vicon(VICO_COLORSET_07_VEC, vicon_colorset_draw_07);
625         def_internal_vicon(VICO_COLORSET_08_VEC, vicon_colorset_draw_08);
626         def_internal_vicon(VICO_COLORSET_09_VEC, vicon_colorset_draw_09);
627         def_internal_vicon(VICO_COLORSET_10_VEC, vicon_colorset_draw_10);
628         def_internal_vicon(VICO_COLORSET_11_VEC, vicon_colorset_draw_11);
629         def_internal_vicon(VICO_COLORSET_12_VEC, vicon_colorset_draw_12);
630         def_internal_vicon(VICO_COLORSET_13_VEC, vicon_colorset_draw_13);
631         def_internal_vicon(VICO_COLORSET_14_VEC, vicon_colorset_draw_14);
632         def_internal_vicon(VICO_COLORSET_15_VEC, vicon_colorset_draw_15);
633         def_internal_vicon(VICO_COLORSET_16_VEC, vicon_colorset_draw_16);
634         def_internal_vicon(VICO_COLORSET_17_VEC, vicon_colorset_draw_17);
635         def_internal_vicon(VICO_COLORSET_18_VEC, vicon_colorset_draw_18);
636         def_internal_vicon(VICO_COLORSET_19_VEC, vicon_colorset_draw_19);
637         def_internal_vicon(VICO_COLORSET_20_VEC, vicon_colorset_draw_20);
638
639         IMB_freeImBuf(b16buf);
640         IMB_freeImBuf(b32buf);
641         
642 }
643 #endif  /* WITH_HEADLESS */
644
645 static void init_iconfile_list(struct ListBase *list)
646 {
647         IconFile *ifile;
648         struct direntry *dir;
649         int totfile, i, index = 1;
650         const char *icondir;
651
652         BLI_listbase_clear(list);
653         icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
654
655         if (icondir == NULL)
656                 return;
657         
658         totfile = BLI_filelist_dir_contents(icondir, &dir);
659
660         for (i = 0; i < totfile; i++) {
661                 if ((dir[i].type & S_IFREG)) {
662                         const char *filename = dir[i].relname;
663                         
664                         if (BLI_testextensie(filename, ".png")) {
665                                 /* loading all icons on file start is overkill & slows startup
666                                  * its possible they change size after blender load anyway. */
667 #if 0
668                                 int ifilex, ifiley;
669                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
670                                 ImBuf *bbuf = NULL;
671                                 /* check to see if the image is the right size, continue if not */
672                                 /* copying strings here should go ok, assuming that we never get back
673                                  * a complete path to file longer than 256 chars */
674                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
675                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
676
677                                 if (bbuf) {
678                                         ifilex = bbuf->x;
679                                         ifiley = bbuf->y;
680                                         IMB_freeImBuf(bbuf);
681                                 }
682                                 else {
683                                         ifilex = ifiley = 0;
684                                 }
685                                 
686                                 /* bad size or failed to load */
687                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
688                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
689                                         continue;
690                                 }
691 #endif          /* removed */
692
693                                 /* found a potential icon file, so make an entry for it in the cache list */
694                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
695                                 
696                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
697                                 ifile->index = index;
698
699                                 BLI_addtail(list, ifile);
700                                 
701                                 index++;
702                         }
703                 }
704         }
705
706         BLI_filelist_free(dir, totfile);
707         dir = NULL;
708 }
709
710 static void free_iconfile_list(struct ListBase *list)
711 {
712         IconFile *ifile = NULL, *next_ifile = NULL;
713         
714         for (ifile = list->first; ifile; ifile = next_ifile) {
715                 next_ifile = ifile->next;
716                 BLI_freelinkN(list, ifile);
717         }
718 }
719
720 #endif  /* WITH_HEADLESS */
721
722 int UI_iconfile_get_index(const char *filename)
723 {
724         IconFile *ifile;
725         ListBase *list = &(iconfilelist);
726         
727         for (ifile = list->first; ifile; ifile = ifile->next) {
728                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
729                         return ifile->index;
730                 }
731         }
732         
733         return 0;
734 }
735
736 ListBase *UI_iconfile_list(void)
737 {
738         ListBase *list = &(iconfilelist);
739         
740         return list;
741 }
742
743
744 void UI_icons_free(void)
745 {
746 #ifndef WITH_HEADLESS
747         if (icongltex.id) {
748                 glDeleteTextures(1, &icongltex.id);
749                 icongltex.id = 0;
750         }
751
752         free_iconfile_list(&iconfilelist);
753         BKE_icons_free();
754 #endif
755 }
756
757 void UI_icons_free_drawinfo(void *drawinfo)
758 {
759         DrawInfo *di = drawinfo;
760
761         if (di) {
762                 if (di->type == ICON_TYPE_BUFFER) {
763                         if (di->data.buffer.image) {
764                                 if (di->data.buffer.image->rect)
765                                         MEM_freeN(di->data.buffer.image->rect);
766                                 MEM_freeN(di->data.buffer.image);
767                         }
768                 }
769                 else if (di->type == ICON_TYPE_GEOM) {
770                         if (di->data.geom.image_cache) {
771                                 IMB_freeImBuf(di->data.geom.image_cache);
772                         }
773                 }
774
775                 MEM_freeN(di);
776         }
777 }
778
779 /**
780  * #Icon.data_type and #Icon.obj
781  */
782 static DrawInfo *icon_create_drawinfo(int icon_data_type)
783 {
784         DrawInfo *di = NULL;
785
786         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
787
788         if (ELEM(icon_data_type, ICON_DATA_ID, ICON_DATA_PREVIEW)) {
789                 di->type = ICON_TYPE_PREVIEW;
790         }
791         else if (icon_data_type == ICON_DATA_GEOM) {
792                 di->type = ICON_TYPE_GEOM;
793         }
794         else {
795                 BLI_assert(0);
796         }
797
798         return di;
799 }
800
801 static DrawInfo *icon_ensure_drawinfo(Icon *icon)
802 {
803         if (icon->drawinfo) {
804                 return icon->drawinfo;
805         }
806         DrawInfo *di = icon_create_drawinfo(icon->obj_type);
807         icon->drawinfo = di;
808         icon->drawinfo_free = UI_icons_free_drawinfo;
809         return di;
810 }
811
812 /* note!, returns unscaled by DPI */
813 int UI_icon_get_width(int icon_id)
814 {
815         Icon *icon = NULL;
816         DrawInfo *di = NULL;
817
818         icon = BKE_icon_get(icon_id);
819         
820         if (icon == NULL) {
821                 if (G.debug & G_DEBUG)
822                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
823                 return 0;
824         }
825         
826         di = icon_ensure_drawinfo(icon);
827         if (di) {
828                 return ICON_DEFAULT_WIDTH;
829         }
830
831         return 0;
832 }
833
834 int UI_icon_get_height(int icon_id)
835 {
836         Icon *icon = BKE_icon_get(icon_id);
837         if (icon == NULL) {
838                 if (G.debug & G_DEBUG)
839                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
840                 return 0;
841         }
842
843         DrawInfo *di = icon_ensure_drawinfo(icon);
844         if (di) {
845                 return ICON_DEFAULT_HEIGHT;
846         }
847
848         return 0;
849 }
850
851 void UI_icons_init(int first_dyn_id)
852 {
853         BKE_icons_init(first_dyn_id);
854 #ifndef WITH_HEADLESS
855         init_iconfile_list(&iconfilelist);
856         init_internal_icons();
857         init_brush_icons();
858         init_matcap_icons();
859         init_studio_light_icons();
860 #endif
861 }
862
863 /* Render size for preview images and icons
864  */
865 int UI_preview_render_size(enum eIconSizes size)
866 {
867         switch (size) {
868                 case ICON_SIZE_ICON:
869                         return ICON_RENDER_DEFAULT_HEIGHT;
870                 case ICON_SIZE_PREVIEW:
871                         return PREVIEW_RENDER_DEFAULT_HEIGHT;
872                 default:
873                         return 0;
874         }
875 }
876
877 /* Create rect for the icon
878  */
879 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
880 {
881         unsigned int render_size = UI_preview_render_size(size);
882
883         if (!prv_img) {
884                 if (G.debug & G_DEBUG)
885                         printf("%s, error: requested preview image does not exist", __func__);
886         }
887         else if (!prv_img->rect[size]) {
888                 prv_img->w[size] = render_size;
889                 prv_img->h[size] = render_size;
890                 prv_img->flag[size] |= PRV_CHANGED;
891                 prv_img->changed_timestamp[size] = 0;
892                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
893         }
894 }
895
896 static void ui_id_preview_image_render_size(
897         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job);
898
899 void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
900 {
901         Icon *icon = BKE_icon_get(icon_id);
902
903         if (icon) {
904                 DrawInfo *di = icon_ensure_drawinfo(icon);
905
906                 if (di) {
907                         switch (di->type) {
908                                 case ICON_TYPE_PREVIEW:
909                                 {
910                                         ID *id = (icon->id_type != 0) ? icon->obj : NULL;
911                                         PreviewImage *prv = id ? BKE_previewimg_id_ensure(id) : icon->obj;
912                                         /* Using jobs for screen previews crashes due to offscreen rendering.
913                                          * XXX would be nicer if PreviewImage could store if it supports jobs */
914                                         const bool use_jobs = !id || (GS(id->name) != ID_SCR);
915
916                                         if (prv) {
917                                                 const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
918
919                                                 if (id || (prv->tag & PRV_TAG_DEFFERED) != 0) {
920                                                         ui_id_preview_image_render_size(C, NULL, id, prv, size, use_jobs);
921                                                 }
922                                         }
923                                         break;
924                                 }
925                         }
926                 }
927         }
928 }
929
930 /* only called when icon has changed */
931 /* only call with valid pointer from UI_icon_draw */
932 static void icon_set_image(
933         const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
934 {
935         if (!prv_img) {
936                 if (G.debug & G_DEBUG)
937                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
938                 return;
939         }
940
941         if (prv_img->flag[size] & PRV_USER_EDITED) {
942                 /* user-edited preview, do not auto-update! */
943                 return;
944         }
945
946         icon_create_rect(prv_img, size);
947
948         if (use_job) {
949                 /* Job (background) version */
950                 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
951         }
952         else {
953                 if (!scene) {
954                         scene = CTX_data_scene(C);
955                 }
956                 /* Immediate version */
957                 ED_preview_icon_render(CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
958         }
959 }
960
961 PreviewImage *UI_icon_to_preview(int icon_id)
962 {
963         Icon *icon = BKE_icon_get(icon_id);
964         
965         if (icon) {
966                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
967                 if (di) {
968                         if (di->type == ICON_TYPE_PREVIEW) {
969                                 PreviewImage *prv = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
970
971                                 if (prv) {
972                                         return BKE_previewimg_copy(prv);
973                                 }
974                         }
975                         else if (di->data.buffer.image) {
976                                 ImBuf *bbuf;
977
978                                 bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size,
979                                                              IB_rect, NULL, __func__);
980                                 if (bbuf) {
981                                         PreviewImage *prv = BKE_previewimg_create();
982
983                                         prv->rect[0] = bbuf->rect;
984
985                                         prv->w[0] = bbuf->x;
986                                         prv->h[0] = bbuf->y;
987
988                                         bbuf->rect = NULL;
989                                         IMB_freeImBuf(bbuf);
990
991                                         return prv;
992                                 }
993                         }
994                 }
995         }
996         return NULL;
997 }
998
999 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
1000                            unsigned int *rect, float alpha, const float rgb[3], const float desaturate)
1001 {
1002         ImBuf *ima = NULL;
1003         int draw_w = w;
1004         int draw_h = h;
1005         int draw_x = x;
1006         int draw_y = y;
1007
1008         /* sanity check */
1009         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
1010                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
1011                 BLI_assert(!"invalid icon size");
1012                 return;
1013         }
1014         /* modulate color */
1015         float col[4] = {1.0f, 1.0f, 1.0f, alpha};
1016
1017         if (rgb) {
1018                 col[0] = rgb[0];
1019                 col[1] = rgb[1];
1020                 col[2] = rgb[2];
1021         }
1022
1023         /* rect contains image in 'rendersize', we only scale if needed */
1024         if (rw != w || rh != h) {
1025                 /* preserve aspect ratio and center */
1026                 if (rw > rh) {
1027                         draw_w = w;
1028                         draw_h = (int)(((float)rh / (float)rw) * (float)w);
1029                         draw_y += (h - draw_h) / 2;
1030                 }
1031                 else if (rw < rh) {
1032                         draw_w = (int)(((float)rw / (float)rh) * (float)h);
1033                         draw_h = h;
1034                         draw_x += (w - draw_w) / 2;
1035                 }
1036                 /* if the image is squared, the draw_ initialization values are good */
1037
1038                 /* first allocate imbuf for scaling and copy preview into it */
1039                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1040                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1041                 IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
1042                 rect = ima->rect;
1043         }
1044
1045         /* We need to flush widget base first to ensure correct ordering. */
1046         UI_widgetbase_draw_cache_flush();
1047
1048         /* draw */
1049         GPUBuiltinShader shader;
1050         if (desaturate != 0.0f) {
1051                 shader = GPU_SHADER_2D_IMAGE_DESATURATE_COLOR;
1052         }
1053         else {
1054                 shader = GPU_SHADER_2D_IMAGE_COLOR;
1055         }
1056         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(shader);
1057
1058         if (shader == GPU_SHADER_2D_IMAGE_DESATURATE_COLOR) {
1059                 immUniform1f("factor", desaturate);
1060         }
1061
1062         immDrawPixelsTex(&state, draw_x, draw_y, draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect,
1063                          1.0f, 1.0f, col);
1064
1065         if (ima)
1066                 IMB_freeImBuf(ima);
1067 }
1068
1069 /* High enough to make a difference, low enough so that
1070  * small draws are still efficient with the use of glUniform.
1071  * NOTE TODO: We could use UBO but we would need some triple
1072  * buffer system + persistent mapping for this to be more
1073  * efficient than simple glUniform calls. */
1074 #define ICON_DRAW_CACHE_SIZE 16
1075
1076 typedef struct IconDrawCall {
1077         rctf pos;
1078         rctf tex;
1079         float color[4];
1080 } IconDrawCall;
1081
1082 static struct {
1083         IconDrawCall drawcall_cache[ICON_DRAW_CACHE_SIZE];
1084         int calls; /* Number of calls batched together */
1085         bool enabled;
1086         float mat[4][4];
1087 } g_icon_draw_cache = {{{{0}}}};
1088
1089 void UI_icon_draw_cache_begin(void)
1090 {
1091         BLI_assert(g_icon_draw_cache.enabled == false);
1092         g_icon_draw_cache.enabled = true;
1093 }
1094
1095 static void icon_draw_cache_flush_ex(void)
1096 {
1097         if (g_icon_draw_cache.calls == 0)
1098                 return;
1099
1100         /* We need to flush widget base first to ensure correct ordering. */
1101         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1102         UI_widgetbase_draw_cache_flush();
1103
1104         glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1105
1106         glActiveTexture(GL_TEXTURE0);
1107         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1108
1109         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR);
1110         GPU_shader_bind(shader);
1111
1112         int img_loc = GPU_shader_get_uniform(shader, "image");
1113         int data_loc = GPU_shader_get_uniform(shader, "calls_data[0]");
1114
1115         glUniform1i(img_loc, 0);
1116         glUniform4fv(data_loc, ICON_DRAW_CACHE_SIZE * 3, (float *)g_icon_draw_cache.drawcall_cache);
1117
1118         GWN_draw_primitive(GWN_PRIM_TRIS, 6 * g_icon_draw_cache.calls);
1119
1120         glBindTexture(GL_TEXTURE_2D, 0);
1121
1122         g_icon_draw_cache.calls = 0;
1123 }
1124
1125 void UI_icon_draw_cache_end(void)
1126 {
1127         BLI_assert(g_icon_draw_cache.enabled == true);
1128         g_icon_draw_cache.enabled = false;
1129
1130         /* Don't change blend state if it's not needed. */
1131         if (g_icon_draw_cache.calls == 0)
1132                 return;
1133
1134         glEnable(GL_BLEND);
1135
1136         icon_draw_cache_flush_ex();
1137
1138         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1139         glDisable(GL_BLEND);
1140 }
1141
1142 static void icon_draw_texture_cached(
1143         float x, float y, float w, float h, int ix, int iy,
1144         int UNUSED(iw), int ih, float alpha, const float rgb[3])
1145 {
1146
1147         float mvp[4][4];
1148         gpuGetModelViewProjectionMatrix(mvp);
1149
1150         IconDrawCall *call = &g_icon_draw_cache.drawcall_cache[g_icon_draw_cache.calls];
1151         g_icon_draw_cache.calls++;
1152
1153         /* Manual mat4*vec2 */
1154         call->pos.xmin = x * mvp[0][0] + y * mvp[1][0] + mvp[3][0];
1155         call->pos.ymin = x * mvp[0][1] + y * mvp[1][1] + mvp[3][1];
1156         call->pos.xmax = call->pos.xmin + w * mvp[0][0] + h * mvp[1][0];
1157         call->pos.ymax = call->pos.ymin + w * mvp[0][1] + h * mvp[1][1];
1158
1159         call->tex.xmin = ix * icongltex.invw;
1160         call->tex.xmax = (ix + ih) * icongltex.invw;
1161         call->tex.ymin = iy * icongltex.invh;
1162         call->tex.ymax = (iy + ih) * icongltex.invh;
1163
1164         if (rgb) copy_v4_fl4(call->color, rgb[0], rgb[1], rgb[2], alpha);
1165         else     copy_v4_fl(call->color, alpha);
1166
1167         if (g_icon_draw_cache.calls == ICON_DRAW_CACHE_SIZE) {
1168                 icon_draw_cache_flush_ex();
1169         }
1170 }
1171
1172 static void icon_draw_texture(
1173         float x, float y, float w, float h, int ix, int iy,
1174         int iw, int ih, float alpha, const float rgb[3])
1175 {
1176         if (g_icon_draw_cache.enabled) {
1177                 icon_draw_texture_cached(x, y, w, h, ix, iy, iw, ih, alpha, rgb);
1178                 return;
1179         }
1180
1181         /* We need to flush widget base first to ensure correct ordering. */
1182         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1183         UI_widgetbase_draw_cache_flush();
1184
1185         float x1, x2, y1, y2;
1186
1187         x1 = ix * icongltex.invw;
1188         x2 = (ix + ih) * icongltex.invw;
1189         y1 = iy * icongltex.invh;
1190         y2 = (iy + ih) * icongltex.invh;
1191
1192         glActiveTexture(GL_TEXTURE0);
1193         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1194
1195         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_RECT_COLOR);
1196         GPU_shader_bind(shader);
1197
1198         if (rgb) glUniform4f(GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_COLOR), rgb[0], rgb[1], rgb[2], alpha);
1199         else     glUniform4f(GPU_shader_get_builtin_uniform(shader, GWN_UNIFORM_COLOR), alpha, alpha, alpha, alpha);
1200
1201         glUniform1i(GPU_shader_get_uniform(shader, "image"), 0);
1202         glUniform4f(GPU_shader_get_uniform(shader, "rect_icon"), x1, y1, x2, y2);
1203         glUniform4f(GPU_shader_get_uniform(shader, "rect_geom"), x, y, x + w, y + h);
1204
1205         GWN_draw_primitive(GWN_PRIM_TRI_STRIP, 4);
1206
1207         glBindTexture(GL_TEXTURE_2D, 0);
1208 }
1209
1210 /* Drawing size for preview images */
1211 static int get_draw_size(enum eIconSizes size)
1212 {
1213         switch (size) {
1214                 case ICON_SIZE_ICON:
1215                         return ICON_DEFAULT_HEIGHT;
1216                 case ICON_SIZE_PREVIEW:
1217                         return PREVIEW_DEFAULT_HEIGHT;
1218                 default:
1219                         return 0;
1220         }
1221 }
1222
1223
1224
1225 static void icon_draw_size(
1226         float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1227         enum eIconSizes size, int draw_size, const float desaturate)
1228 {
1229         bTheme *btheme = UI_GetTheme();
1230         Icon *icon = NULL;
1231         IconImage *iimg;
1232         const float fdraw_size = (float)draw_size;
1233         int w, h;
1234         
1235         icon = BKE_icon_get(icon_id);
1236         alpha *= btheme->tui.icon_alpha;
1237         
1238         if (icon == NULL) {
1239                 if (G.debug & G_DEBUG)
1240                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1241                 return;
1242         }
1243
1244         /* scale width and height according to aspect */
1245         w = (int)(fdraw_size / aspect + 0.5f);
1246         h = (int)(fdraw_size / aspect + 0.5f);
1247
1248         DrawInfo *di = icon_ensure_drawinfo(icon);
1249
1250         if (di->type == ICON_TYPE_VECTOR) {
1251                 /* We need to flush widget base first to ensure correct ordering. */
1252                 UI_widgetbase_draw_cache_flush();
1253                 /* vector icons use the uiBlock transformation, they are not drawn
1254                  * with untransformed coordinates like the other icons */
1255                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1256         }
1257         else if (di->type == ICON_TYPE_GEOM) {
1258                 /* We need to flush widget base first to ensure correct ordering. */
1259                 UI_widgetbase_draw_cache_flush();
1260
1261 #ifdef USE_TOOLBAR_HACK
1262                 /* TODO(campbell): scale icons up for toolbar, we need a way to detect larger buttons and do this automatic. */
1263                 {
1264                         float scale = (float)ICON_DEFAULT_HEIGHT_TOOLBAR / (float)ICON_DEFAULT_HEIGHT;
1265                         y = (y + (h / 2)) - ((h * scale) / 2);
1266                         w *= scale;
1267                         h *= scale;
1268                 }
1269 #endif
1270
1271                 /* This could re-generate often if rendered at different sizes in the one interface.
1272                  * TODO(campbell): support caching multiple sizes. */
1273                 ImBuf *ibuf = di->data.geom.image_cache;
1274                 if ((ibuf == NULL) ||
1275                     (ibuf->x != w) ||
1276                     (ibuf->y != h))
1277                 {
1278                         if (ibuf) {
1279                                 IMB_freeImBuf(ibuf);
1280                         }
1281                         ibuf = BKE_icon_geom_rasterize(icon->obj, w, h);
1282                         di->data.geom.image_cache = ibuf;
1283                 }
1284                 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1285                 icon_draw_rect(x, y, w, h, aspect, w, h, ibuf->rect, alpha, rgb, desaturate);
1286                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1287         }
1288         else if (di->type == ICON_TYPE_TEXTURE) {
1289                 /* texture image use premul alpha for correct scaling */
1290                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1291                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1292                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1293                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1294         }
1295         else if (di->type == ICON_TYPE_BUFFER) {
1296                 /* it is a builtin icon */
1297                 iimg = di->data.buffer.image;
1298 #ifndef WITH_HEADLESS
1299                 icon_verify_datatoc(iimg);
1300 #endif
1301                 if (!iimg->rect) return;  /* something has gone wrong! */
1302
1303                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1304                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, desaturate);
1305                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1306         }
1307         else if (di->type == ICON_TYPE_PREVIEW) {
1308                 PreviewImage *pi = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1309
1310                 if (pi) {
1311                         /* no create icon on this level in code */
1312                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1313                         
1314                         /* preview images use premul alpha ... */
1315                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1316
1317                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, rgb, desaturate);
1318                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1319                 }
1320         }
1321 }
1322
1323 static void ui_id_preview_image_render_size(
1324         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1325 {
1326         if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1327                 /* create the rect if necessary */
1328                 icon_set_image(C, scene, id, pi, size, use_job);
1329
1330                 pi->flag[size] &= ~PRV_CHANGED;
1331         }
1332 }
1333
1334 void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1335 {
1336         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1337
1338         if (pi) {
1339                 if (big)
1340                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);  /* bigger preview size */
1341                 else
1342                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);     /* icon size */
1343         }
1344 }
1345
1346 static void ui_id_icon_render(const bContext *C, ID *id, bool use_jobs)
1347 {
1348         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1349         enum eIconSizes i;
1350         
1351         if (!pi)
1352                 return;
1353         
1354         for (i = 0; i < NUM_ICON_SIZES; i++) {
1355                 /* check if rect needs to be created; changed
1356                  * only set by dynamic icons */
1357                 if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
1358                         icon_set_image(C, NULL, id, pi, i, use_jobs);
1359                         pi->flag[i] &= ~PRV_CHANGED;
1360                 }
1361         }
1362 }
1363
1364
1365 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1366 {
1367         Brush *br = (Brush *)id;
1368
1369         if (br->flag & BRUSH_CUSTOM_ICON) {
1370                 BKE_icon_id_ensure(id);
1371                 ui_id_icon_render(C, id, true);
1372         }
1373         else {
1374                 Object *ob = CTX_data_active_object(C);
1375                 SpaceImage *sima;
1376                 const EnumPropertyItem *items = NULL;
1377                 int tool = PAINT_TOOL_DRAW, mode = 0;
1378
1379                 /* XXX: this is not nice, should probably make brushes
1380                  * be strictly in one paint mode only to avoid
1381                  * checking various context stuff here */
1382
1383                 if (CTX_wm_view3d(C) && ob) {
1384                         if (ob->mode & OB_MODE_SCULPT)
1385                                 mode = OB_MODE_SCULPT;
1386                         else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
1387                                 mode = OB_MODE_VERTEX_PAINT;
1388                         else if (ob->mode & OB_MODE_TEXTURE_PAINT)
1389                                 mode = OB_MODE_TEXTURE_PAINT;
1390                 }
1391                 else if ((sima = CTX_wm_space_image(C)) &&
1392                          (sima->mode == SI_MODE_PAINT))
1393                 {
1394                         mode = OB_MODE_TEXTURE_PAINT;
1395                 }
1396
1397                 /* reset the icon */
1398                 if (mode == OB_MODE_SCULPT) {
1399                         items = rna_enum_brush_sculpt_tool_items;
1400                         tool = br->sculpt_tool;
1401                 }
1402                 else if (mode == OB_MODE_VERTEX_PAINT) {
1403                         items = rna_enum_brush_vertex_tool_items;
1404                         tool = br->vertexpaint_tool;
1405                 }
1406                 else if (mode == OB_MODE_TEXTURE_PAINT) {
1407                         items = rna_enum_brush_image_tool_items;
1408                         tool = br->imagepaint_tool;
1409                 }
1410
1411                 if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1412                         id->icon_id = 0;
1413         }
1414
1415         return id->icon_id;
1416 }
1417
1418 static int ui_id_screen_get_icon(const bContext *C, ID *id)
1419 {
1420         BKE_icon_id_ensure(id);
1421         /* Don't use jobs here, offscreen rendering doesn't like this and crashes. */
1422         ui_id_icon_render(C, id, false);
1423
1424         return id->icon_id;
1425 }
1426
1427 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
1428 {
1429         int iconid = 0;
1430         
1431         /* icon */
1432         switch (GS(id->name)) {
1433                 case ID_BR:
1434                         iconid = ui_id_brush_get_icon(C, id);
1435                         break;
1436                 case ID_MA: /* fall through */
1437                 case ID_TE: /* fall through */
1438                 case ID_IM: /* fall through */
1439                 case ID_WO: /* fall through */
1440                 case ID_LA: /* fall through */
1441                         iconid = BKE_icon_id_ensure(id);
1442                         /* checks if not exists, or changed */
1443                         UI_id_icon_render(C, NULL, id, big, true);
1444                         break;
1445                 case ID_SCR:
1446                         iconid = ui_id_screen_get_icon(C, id);
1447                         break;
1448                 default:
1449                         break;
1450         }
1451
1452         return iconid;
1453 }
1454
1455 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1456 {
1457         ID *id = NULL;
1458
1459         if (!ptr->data)
1460                 return rnaicon;
1461
1462         /* try ID, material, texture or dynapaint slot */
1463         if (RNA_struct_is_ID(ptr->type)) {
1464                 id = ptr->id.data;
1465         }
1466         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1467                 id = RNA_pointer_get(ptr, "material").data;
1468         }
1469         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1470                 id = RNA_pointer_get(ptr, "texture").data;
1471         }
1472         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1473                 DynamicPaintSurface *surface = (DynamicPaintSurface *)ptr->data;
1474
1475                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1476                         return ICON_TEXTURE_SHADED;
1477                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1478                         return ICON_OUTLINER_DATA_MESH;
1479                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1480                         return ICON_FILE_IMAGE;
1481         }
1482
1483         /* get icon from ID */
1484         if (id) {
1485                 int icon = ui_id_icon_get(C, id, big);
1486
1487                 return icon ? icon : rnaicon;
1488         }
1489
1490         return rnaicon;
1491 }
1492
1493 int UI_idcode_icon_get(const int idcode)
1494 {
1495         switch (idcode) {
1496                 case ID_AC:
1497                         return ICON_ACTION;
1498                 case ID_AR:
1499                         return ICON_ARMATURE_DATA;
1500                 case ID_BR:
1501                         return ICON_BRUSH_DATA;
1502                 case ID_CA:
1503                         return ICON_CAMERA_DATA;
1504                 case ID_CF:
1505                         return ICON_FILE;
1506                 case ID_CU:
1507                         return ICON_CURVE_DATA;
1508                 case ID_GD:
1509                         return ICON_GREASEPENCIL;
1510                 case ID_GR:
1511                         return ICON_GROUP;
1512                 case ID_IM:
1513                         return ICON_IMAGE_DATA;
1514                 case ID_LA:
1515                         return ICON_LAMP_DATA;
1516                 case ID_LS:
1517                         return ICON_LINE_DATA;
1518                 case ID_LT:
1519                         return ICON_LATTICE_DATA;
1520                 case ID_MA:
1521                         return ICON_MATERIAL_DATA;
1522                 case ID_MB:
1523                         return ICON_META_DATA;
1524                 case ID_MC:
1525                         return ICON_CLIP;
1526                 case ID_ME:
1527                         return ICON_MESH_DATA;
1528                 case ID_MSK:
1529                         return ICON_MOD_MASK;  /* TODO! this would need its own icon! */
1530                 case ID_NT:
1531                         return ICON_NODETREE;
1532                 case ID_OB:
1533                         return ICON_OBJECT_DATA;
1534                 case ID_PA:
1535                         return ICON_PARTICLE_DATA;
1536                 case ID_PAL:
1537                         return ICON_COLOR;  /* TODO! this would need its own icon! */
1538                 case ID_PC:
1539                         return ICON_CURVE_BEZCURVE;  /* TODO! this would need its own icon! */
1540                 case ID_LP:
1541                         return ICON_RADIO;
1542                 case ID_SCE:
1543                         return ICON_SCENE_DATA;
1544                 case ID_SPK:
1545                         return ICON_SPEAKER;
1546                 case ID_SO:
1547                         return ICON_SOUND;
1548                 case ID_TE:
1549                         return ICON_TEXTURE_DATA;
1550                 case ID_TXT:
1551                         return ICON_TEXT;
1552                 case ID_VF:
1553                         return ICON_FONT_DATA;
1554                 case ID_WO:
1555                         return ICON_WORLD_DATA;
1556                 default:
1557                         return ICON_NONE;
1558         }
1559 }
1560
1561 static void icon_draw_at_size(
1562         float x, float y, int icon_id, float aspect, float alpha,
1563         enum eIconSizes size, const float desaturate)
1564 {
1565         int draw_size = get_draw_size(size);
1566         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, desaturate);
1567 }
1568
1569 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1570 {
1571         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0.0f);
1572 }
1573
1574 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
1575 {
1576         int draw_size = get_draw_size(ICON_SIZE_ICON);
1577         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false);
1578 }
1579
1580 void UI_icon_draw_desaturate(float x, float y, int icon_id, float aspect, float alpha, float desaturate)
1581 {
1582         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, desaturate);
1583 }
1584
1585 /* draws icon with dpi scale factor */
1586 void UI_icon_draw(float x, float y, int icon_id)
1587 {
1588         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
1589 }
1590
1591 void UI_icon_draw_alpha(float x, float y, int icon_id, float alpha)
1592 {
1593         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, alpha);
1594 }
1595
1596 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1597 {
1598         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, false);
1599 }
1600
1601 void UI_icon_draw_preview(float x, float y, int icon_id)
1602 {
1603         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, false);
1604 }
1605
1606 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1607 {
1608         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, false);
1609 }
1610
1611 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, float alpha, int size)
1612 {
1613         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, ICON_SIZE_PREVIEW, size, false);
1614 }
1615