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