Fix incorrect drawing of material list outline, after recent changes.
[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 #include "GPU_state.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_fileops_types.h"
45 #include "BLI_math_vector.h"
46
47 #include "DNA_brush_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_dynamicpaint_types.h"
50 #include "DNA_gpencil_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_workspace_types.h"
55
56 #include "RNA_access.h"
57 #include "RNA_enum_types.h"
58
59 #include "BKE_context.h"
60 #include "BKE_global.h"
61 #include "BKE_paint.h"
62 #include "BKE_icons.h"
63 #include "BKE_appdir.h"
64 #include "BKE_studiolight.h"
65
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
68 #include "IMB_thumbs.h"
69
70 #include "BIF_glutil.h"
71
72 #include "DEG_depsgraph.h"
73
74 #include "DRW_engine.h"
75
76 #include "ED_datafiles.h"
77 #include "ED_keyframes_draw.h"
78 #include "ED_render.h"
79
80 #include "UI_interface.h"
81 #include "UI_interface_icons.h"
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "interface_intern.h"
87
88 #ifndef WITH_HEADLESS
89 #define ICON_GRID_COLS      26
90 #define ICON_GRID_ROWS      30
91
92 #define ICON_GRID_MARGIN    10
93 #define ICON_GRID_W         32
94 #define ICON_GRID_H         32
95 #endif  /* WITH_HEADLESS */
96
97 typedef struct IconImage {
98         int w;
99         int h;
100         unsigned int *rect;
101         unsigned char *datatoc_rect;
102         int datatoc_size;
103 } IconImage;
104
105 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
106
107 #define ICON_TYPE_PREVIEW        0
108 #define ICON_TYPE_COLOR_TEXTURE  1
109 #define ICON_TYPE_MONO_TEXTURE   2
110 #define ICON_TYPE_BUFFER         3
111 #define ICON_TYPE_VECTOR         4
112 #define ICON_TYPE_GEOM           5
113 #define ICON_TYPE_EVENT          6  /* draw keymap entries using custom renderer. */
114 #define ICON_TYPE_GPLAYER        7
115 #define ICON_TYPE_BLANK          8
116
117 typedef struct DrawInfo {
118         int type;
119
120         union {
121                 /* type specific data */
122                 struct {
123                         VectorDrawFunc func;
124                 } vector;
125                 struct {
126                         ImBuf *image_cache;
127                 } geom;
128                 struct {
129                         IconImage *image;
130                 } buffer;
131                 struct {
132                         int x, y, w, h;
133                         int theme_color;
134                 } texture;
135                 struct {
136                         /* Can be packed into a single int. */
137                         short event_type;
138                         short event_value;
139                         int icon;
140                         /* Allow lookups. */
141                         struct DrawInfo *next;
142                 } input;
143         } data;
144 } DrawInfo;
145
146 typedef struct IconTexture {
147         GLuint id;
148         int w;
149         int h;
150         float invw;
151         float invh;
152 } IconTexture;
153
154 typedef struct IconType {
155         int type;
156         int theme_color;
157 } IconType;
158
159 /* ******************* STATIC LOCAL VARS ******************* */
160 /* static here to cache results of icon directory scan, so it's not
161  * scanning the filesystem each time the menu is drawn */
162 static struct ListBase iconfilelist = {NULL, NULL};
163 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
164
165 #ifndef WITH_HEADLESS
166
167 static const IconType icontypes[] = {
168 #define DEF_ICON(name) {ICON_TYPE_MONO_TEXTURE, 0},
169 #define DEF_ICON_COLLECTION(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_COLLECTION},
170 #define DEF_ICON_OBJECT(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT},
171 #define DEF_ICON_OBJECT_DATA(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT_DATA},
172 #define DEF_ICON_MODIFIER(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_MODIFIER},
173 #define DEF_ICON_SHADING(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_SHADING},
174 #define DEF_ICON_VECTOR(name) {ICON_TYPE_VECTOR, 0},
175 #define DEF_ICON_COLOR(name) {ICON_TYPE_COLOR_TEXTURE, 0},
176 #define DEF_ICON_BLANK(name) {ICON_TYPE_BLANK, 0},
177 #include "UI_icons.h"
178 };
179
180 /* **************************************************** */
181
182 static DrawInfo *def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type, int theme_color)
183 {
184         Icon *new_icon = NULL;
185         IconImage *iimg = NULL;
186         DrawInfo *di;
187
188         new_icon = MEM_callocN(sizeof(Icon), "texicon");
189
190         new_icon->obj = NULL; /* icon is not for library object */
191         new_icon->id_type = 0;
192
193         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
194         di->type = type;
195
196         if (ELEM(type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
197                 di->data.texture.theme_color = theme_color;
198                 di->data.texture.x = xofs;
199                 di->data.texture.y = yofs;
200                 di->data.texture.w = size;
201                 di->data.texture.h = size;
202         }
203         else if (type == ICON_TYPE_BUFFER) {
204                 iimg = MEM_callocN(sizeof(IconImage), "icon_img");
205                 iimg->w = size;
206                 iimg->h = size;
207
208                 /* icon buffers can get initialized runtime now, via datatoc */
209                 if (bbuf) {
210                         int y, imgsize;
211
212                         iimg->rect = MEM_mallocN(size * size * sizeof(unsigned int), "icon_rect");
213
214                         /* Here we store the rect in the icon - same as before */
215                         if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0)
216                                 memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
217                         else {
218                                 /* this code assumes square images */
219                                 imgsize = bbuf->x;
220                                 for (y = 0; y < size; y++) {
221                                         memcpy(&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
222                                 }
223                         }
224                 }
225                 di->data.buffer.image = iimg;
226         }
227
228         new_icon->drawinfo_free = UI_icons_free_drawinfo;
229         new_icon->drawinfo = di;
230
231         BKE_icon_set(icon_id, new_icon);
232
233         return di;
234 }
235
236 static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
237 {
238         Icon *new_icon = NULL;
239         DrawInfo *di;
240
241         new_icon = MEM_callocN(sizeof(Icon), "texicon");
242
243         new_icon->obj = NULL; /* icon is not for library object */
244         new_icon->id_type = 0;
245
246         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
247         di->type = ICON_TYPE_VECTOR;
248         di->data.vector.func = drawFunc;
249
250         new_icon->drawinfo_free = NULL;
251         new_icon->drawinfo = di;
252
253         BKE_icon_set(icon_id, new_icon);
254 }
255
256 /* Vector Icon Drawing Routines */
257
258 /* Utilities */
259
260 static void viconutil_set_point(GLint pt[2], int x, int y)
261 {
262         pt[0] = x;
263         pt[1] = y;
264 }
265
266 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
267 {
268         GLint pts[3][2];
269         int cx = x + w / 2 - 4;
270         int cy = y + w / 2;
271         int d = w / 5, d2 = w / 7;
272
273         viconutil_set_point(pts[0], cx - d2, cy + d);
274         viconutil_set_point(pts[1], cx - d2, cy - d);
275         viconutil_set_point(pts[2], cx + d2, cy);
276
277         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
278         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
279         immUniformColor4f(0.2f, 0.2f, 0.2f, alpha);
280
281         immBegin(GPU_PRIM_TRIS, 3);
282         immVertex2iv(pos, pts[0]);
283         immVertex2iv(pos, pts[1]);
284         immVertex2iv(pos, pts[2]);
285         immEnd();
286
287         immUnbindProgram();
288 }
289
290 static void vicon_keytype_draw_wrapper(int x, int y, int w, int h, float alpha, short key_type, short handle_type)
291 {
292         /* init dummy theme state for Action Editor - where these colors are defined
293          * (since we're doing this offscreen, free from any particular space_id)
294          */
295         struct bThemeState theme_state;
296
297         UI_Theme_Store(&theme_state);
298         UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
299
300         /* the "x" and "y" given are the bottom-left coordinates of the icon,
301          * while the draw_keyframe_shape() function needs the midpoint for
302          * the keyframe
303          */
304         float xco = x + w / 2 + 0.5f;
305         float yco = y + h / 2 + 0.5f;
306
307         GPUVertFormat *format = immVertexFormat();
308         uint pos_id = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
309         uint size_id = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
310         uint color_id = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
311         uint outline_color_id = GPU_vertformat_attr_add(format, "outlineColor", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
312         uint flags_id = GPU_vertformat_attr_add(format, "flags", GPU_COMP_U32, 1, GPU_FETCH_INT);
313
314         immBindBuiltinProgram(GPU_SHADER_KEYFRAME_DIAMOND);
315         GPU_enable_program_point_size();
316         immUniform2f("ViewportSize", -1.0f, -1.0f);
317         immBegin(GPU_PRIM_POINTS, 1);
318
319         /* draw keyframe
320          * - size: (default icon size == 16, default dopesheet icon size == 10)
321          * - sel: true unless in handletype icons (so that "keyframe" state shows the iconic yellow icon)
322          */
323         bool sel = (handle_type == KEYFRAME_HANDLE_NONE);
324
325         draw_keyframe_shape(xco, yco, (10.0f / 16.0f) * h, sel, key_type, KEYFRAME_SHAPE_BOTH, alpha,
326                             pos_id, size_id, color_id, outline_color_id,
327                             flags_id, handle_type, KEYFRAME_EXTREME_NONE);
328
329         immEnd();
330         GPU_disable_program_point_size();
331         immUnbindProgram();
332
333         UI_Theme_Restore(&theme_state);
334 }
335
336 static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
337 {
338         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_NONE);
339 }
340
341 static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
342 {
343         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN, KEYFRAME_HANDLE_NONE);
344 }
345
346 static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
347 {
348         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME, KEYFRAME_HANDLE_NONE);
349 }
350
351 static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
352 {
353         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER, KEYFRAME_HANDLE_NONE);
354 }
355
356 static void vicon_keytype_moving_hold_draw(int x, int y, int w, int h, float alpha)
357 {
358         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_MOVEHOLD, KEYFRAME_HANDLE_NONE);
359 }
360
361 static void vicon_handletype_free_draw(int x, int y, int w, int h, float alpha)
362 {
363         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_FREE);
364 }
365
366 static void vicon_handletype_aligned_draw(int x, int y, int w, int h, float alpha)
367 {
368         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_ALIGNED);
369 }
370
371 static void vicon_handletype_vector_draw(int x, int y, int w, int h, float alpha)
372 {
373         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_VECTOR);
374 }
375
376 static void vicon_handletype_auto_draw(int x, int y, int w, int h, float alpha)
377 {
378         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO);
379 }
380
381 static void vicon_handletype_auto_clamp_draw(int x, int y, int w, int h, float alpha)
382 {
383         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO_CLAMP);
384 }
385
386 static void vicon_colorset_draw(int index, int x, int y, int w, int h, float UNUSED(alpha))
387 {
388         bTheme *btheme = UI_GetTheme();
389         ThemeWireColor *cs = &btheme->tarm[index];
390
391         /* Draw three bands of color: One per color
392          *    x-----a-----b-----c
393          *    |  N  |  S  |  A  |
394          *    x-----a-----b-----c
395          */
396         const int a = x + w / 3;
397         const int b = x + w / 3 * 2;
398         const int c = x + w;
399
400         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
401         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
402
403         /* XXX: Include alpha into this... */
404         /* normal */
405         immUniformColor3ubv((unsigned char *)cs->solid);
406         immRecti(pos, x, y, a, y + h);
407
408         /* selected */
409         immUniformColor3ubv((unsigned char *)cs->select);
410         immRecti(pos, a, y, b, y + h);
411
412         /* active */
413         immUniformColor3ubv((unsigned char *)cs->active);
414         immRecti(pos, b, y, c, y + h);
415
416         immUnbindProgram();
417 }
418
419 #define DEF_ICON_VECTOR_COLORSET_DRAW_NTH(prefix, index)                              \
420         static void vicon_colorset_draw_##prefix(int x, int y, int w, int h, float alpha) \
421         {                                                                                 \
422                 vicon_colorset_draw(index, x, y, w, h, alpha);                                \
423         }
424
425 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(01, 0)
426 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(02, 1)
427 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(03, 2)
428 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(04, 3)
429 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(05, 4)
430 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(06, 5)
431 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(07, 6)
432 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(08, 7)
433 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(09, 8)
434 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(10, 9)
435 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(11, 10)
436 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(12, 11)
437 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(13, 12)
438 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(14, 13)
439 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(15, 14)
440 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(16, 15)
441 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(17, 16)
442 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(18, 17)
443 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(19, 18)
444 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(20, 19)
445
446 #undef DEF_ICON_VECTOR_COLORSET_DRAW_NTH
447
448 /* Dynamically render icon instead of rendering a plain color to a texture/buffer
449  * This is mot strictly a "vicon", as it needs access to icon->obj to get the color info,
450  * but it works in a very similar way.
451  */
452 static void vicon_gplayer_color_draw(Icon *icon, int x, int y, int w, int h)
453 {
454         bGPDlayer *gpl = (bGPDlayer *)icon->obj;
455
456         /* Just draw a colored rect - Like for vicon_colorset_draw() */
457         /* TODO: Make this have rounded corners, and maybe be a bit smaller.
458          * However, UI_draw_roundbox_aa() draws the colors too dark, so can't be used.
459          */
460         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
461         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
462
463         immUniformColor3fv(gpl->color);
464         immRecti(pos, x, y, x + w - 1, y + h - 1);
465
466         immUnbindProgram();
467 }
468
469
470 #ifndef WITH_HEADLESS
471
472 static void init_brush_icons(void)
473 {
474
475 #define INIT_BRUSH_ICON(icon_id, name)                                          \
476         {                                                                           \
477                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _png;          \
478                 int size = datatoc_ ##name## _png_size;                                 \
479                 DrawInfo *di;                                                           \
480                 \
481                 di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER, 0);    \
482                 di->data.buffer.image->datatoc_rect = rect;                             \
483                 di->data.buffer.image->datatoc_size = size;                             \
484         }
485         /* end INIT_BRUSH_ICON */
486
487         const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
488
489         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
490         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
491         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
492         INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
493         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
494         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
495         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
496         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
497         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
498         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
499         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
500         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
501         INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
502         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
503         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
504         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
505         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
506         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
507         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
508         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
509         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
510         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
511         INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
512         INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
513         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
514         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
515
516         /* grease pencil sculpt */
517         INIT_BRUSH_ICON(ICON_GPBRUSH_SMOOTH, gp_brush_smooth);
518         INIT_BRUSH_ICON(ICON_GPBRUSH_THICKNESS, gp_brush_thickness);
519         INIT_BRUSH_ICON(ICON_GPBRUSH_STRENGTH, gp_brush_strength);
520         INIT_BRUSH_ICON(ICON_GPBRUSH_GRAB, gp_brush_grab);
521         INIT_BRUSH_ICON(ICON_GPBRUSH_PUSH, gp_brush_push);
522         INIT_BRUSH_ICON(ICON_GPBRUSH_TWIST, gp_brush_twist);
523         INIT_BRUSH_ICON(ICON_GPBRUSH_PINCH, gp_brush_pinch);
524         INIT_BRUSH_ICON(ICON_GPBRUSH_RANDOMIZE, gp_brush_randomize);
525         INIT_BRUSH_ICON(ICON_GPBRUSH_CLONE, gp_brush_clone);
526         INIT_BRUSH_ICON(ICON_GPBRUSH_WEIGHT, gp_brush_weight);
527
528         /* grease pencil drawing brushes */
529         INIT_BRUSH_ICON(ICON_GPBRUSH_PENCIL, gp_brush_pencil);
530         INIT_BRUSH_ICON(ICON_GPBRUSH_PEN, gp_brush_pen);
531         INIT_BRUSH_ICON(ICON_GPBRUSH_INK, gp_brush_ink);
532         INIT_BRUSH_ICON(ICON_GPBRUSH_INKNOISE, gp_brush_inknoise);
533         INIT_BRUSH_ICON(ICON_GPBRUSH_BLOCK, gp_brush_block);
534         INIT_BRUSH_ICON(ICON_GPBRUSH_MARKER, gp_brush_marker);
535         INIT_BRUSH_ICON(ICON_GPBRUSH_FILL, gp_brush_fill);
536         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_SOFT, gp_brush_erase_soft);
537         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_HARD, gp_brush_erase_hard);
538         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_STROKE, gp_brush_erase_stroke);
539
540 #undef INIT_BRUSH_ICON
541 }
542
543 static DrawInfo *g_di_event_list = NULL;
544
545 int UI_icon_from_event_type(short event_type, short event_value)
546 {
547         if (event_type == RIGHTSHIFTKEY) {
548                 event_type = LEFTSHIFTKEY;
549         }
550         else if (event_type == RIGHTCTRLKEY) {
551                 event_type = LEFTCTRLKEY;
552         }
553         else if (event_type == RIGHTALTKEY) {
554                 event_type = LEFTALTKEY;
555         }
556         else if (event_type == EVT_TWEAK_L) {
557                 event_type = LEFTMOUSE;
558                 event_value = KM_CLICK_DRAG;
559         }
560         else if (event_type == EVT_TWEAK_M) {
561                 event_type = MIDDLEMOUSE;
562                 event_value = KM_CLICK_DRAG;
563         }
564         else if (event_type == EVT_TWEAK_R) {
565                 event_type = RIGHTMOUSE;
566                 event_value = KM_CLICK_DRAG;
567         }
568
569         DrawInfo *di = g_di_event_list;
570         do {
571                 if (di->data.input.event_type == event_type) {
572                         return di->data.input.icon;
573                 }
574         } while ((di = di->data.input.next));
575
576         if (event_type == LEFTMOUSE) {
577                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_LMB : ICON_MOUSE_LMB_DRAG;
578         }
579         else if (event_type == MIDDLEMOUSE) {
580                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_MMB : ICON_MOUSE_MMB_DRAG;
581         }
582         else if (event_type == RIGHTMOUSE) {
583                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_RMB : ICON_MOUSE_RMB_DRAG;
584         }
585
586         return ICON_NONE;
587 }
588
589 int UI_icon_from_keymap_item(const wmKeyMapItem *kmi, int r_icon_mod[4])
590 {
591         if (r_icon_mod) {
592                 memset(r_icon_mod, 0x0, sizeof(int[4]));
593                 int i = 0;
594                 if (!ELEM(kmi->ctrl, KM_NOTHING, KM_ANY)) {
595                         r_icon_mod[i++] = ICON_EVENT_CTRL;
596                 }
597                 if (!ELEM(kmi->alt, KM_NOTHING, KM_ANY)) {
598                         r_icon_mod[i++] = ICON_EVENT_ALT;
599                 }
600                 if (!ELEM(kmi->shift, KM_NOTHING, KM_ANY)) {
601                         r_icon_mod[i++] = ICON_EVENT_SHIFT;
602                 }
603                 if (!ELEM(kmi->oskey, KM_NOTHING, KM_ANY)) {
604                         r_icon_mod[i++] = ICON_EVENT_OS;
605                 }
606         }
607         return UI_icon_from_event_type(kmi->type, kmi->val);
608 }
609
610 static void init_event_icons(void)
611 {
612         DrawInfo *di_next = NULL;
613
614 #define INIT_EVENT_ICON(icon_id, type, value) \
615         { \
616                 DrawInfo *di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_EVENT, 0); \
617                 di->data.input.event_type = type; \
618                 di->data.input.event_value = value; \
619                 di->data.input.icon = icon_id; \
620                 di->data.input.next = di_next; \
621                 di_next = di; \
622         }
623         /* end INIT_EVENT_ICON */
624
625         const int w = 16; /* DUMMY */
626
627         INIT_EVENT_ICON(ICON_EVENT_A, AKEY, KM_ANY);
628         INIT_EVENT_ICON(ICON_EVENT_B, BKEY, KM_ANY);
629         INIT_EVENT_ICON(ICON_EVENT_C, CKEY, KM_ANY);
630         INIT_EVENT_ICON(ICON_EVENT_D, DKEY, KM_ANY);
631         INIT_EVENT_ICON(ICON_EVENT_E, EKEY, KM_ANY);
632         INIT_EVENT_ICON(ICON_EVENT_F, FKEY, KM_ANY);
633         INIT_EVENT_ICON(ICON_EVENT_G, GKEY, KM_ANY);
634         INIT_EVENT_ICON(ICON_EVENT_H, HKEY, KM_ANY);
635         INIT_EVENT_ICON(ICON_EVENT_I, IKEY, KM_ANY);
636         INIT_EVENT_ICON(ICON_EVENT_J, JKEY, KM_ANY);
637         INIT_EVENT_ICON(ICON_EVENT_K, KKEY, KM_ANY);
638         INIT_EVENT_ICON(ICON_EVENT_L, LKEY, KM_ANY);
639         INIT_EVENT_ICON(ICON_EVENT_M, MKEY, KM_ANY);
640         INIT_EVENT_ICON(ICON_EVENT_N, NKEY, KM_ANY);
641         INIT_EVENT_ICON(ICON_EVENT_O, OKEY, KM_ANY);
642         INIT_EVENT_ICON(ICON_EVENT_P, PKEY, KM_ANY);
643         INIT_EVENT_ICON(ICON_EVENT_Q, QKEY, KM_ANY);
644         INIT_EVENT_ICON(ICON_EVENT_R, RKEY, KM_ANY);
645         INIT_EVENT_ICON(ICON_EVENT_S, SKEY, KM_ANY);
646         INIT_EVENT_ICON(ICON_EVENT_T, TKEY, KM_ANY);
647         INIT_EVENT_ICON(ICON_EVENT_U, UKEY, KM_ANY);
648         INIT_EVENT_ICON(ICON_EVENT_V, VKEY, KM_ANY);
649         INIT_EVENT_ICON(ICON_EVENT_W, WKEY, KM_ANY);
650         INIT_EVENT_ICON(ICON_EVENT_X, XKEY, KM_ANY);
651         INIT_EVENT_ICON(ICON_EVENT_Y, YKEY, KM_ANY);
652         INIT_EVENT_ICON(ICON_EVENT_Z, ZKEY, KM_ANY);
653         INIT_EVENT_ICON(ICON_EVENT_SHIFT, LEFTSHIFTKEY, KM_ANY);
654         INIT_EVENT_ICON(ICON_EVENT_CTRL, LEFTCTRLKEY, KM_ANY);
655         INIT_EVENT_ICON(ICON_EVENT_ALT, LEFTALTKEY, KM_ANY);
656         INIT_EVENT_ICON(ICON_EVENT_OS, OSKEY, KM_ANY);
657         INIT_EVENT_ICON(ICON_EVENT_F1,  F1KEY, KM_ANY);
658         INIT_EVENT_ICON(ICON_EVENT_F2,  F2KEY, KM_ANY);
659         INIT_EVENT_ICON(ICON_EVENT_F3,  F3KEY, KM_ANY);
660         INIT_EVENT_ICON(ICON_EVENT_F4,  F4KEY, KM_ANY);
661         INIT_EVENT_ICON(ICON_EVENT_F5,  F5KEY, KM_ANY);
662         INIT_EVENT_ICON(ICON_EVENT_F6,  F6KEY, KM_ANY);
663         INIT_EVENT_ICON(ICON_EVENT_F7,  F7KEY, KM_ANY);
664         INIT_EVENT_ICON(ICON_EVENT_F8,  F8KEY, KM_ANY);
665         INIT_EVENT_ICON(ICON_EVENT_F9,  F9KEY, KM_ANY);
666         INIT_EVENT_ICON(ICON_EVENT_F10, F10KEY, KM_ANY);
667         INIT_EVENT_ICON(ICON_EVENT_F11, F11KEY, KM_ANY);
668         INIT_EVENT_ICON(ICON_EVENT_F12, F12KEY, KM_ANY);
669         INIT_EVENT_ICON(ICON_EVENT_ESC, ESCKEY, KM_ANY);
670         INIT_EVENT_ICON(ICON_EVENT_TAB, TABKEY, KM_ANY);
671         INIT_EVENT_ICON(ICON_EVENT_PAGEUP, PAGEUPKEY, KM_ANY);
672         INIT_EVENT_ICON(ICON_EVENT_PAGEDOWN, PAGEDOWNKEY, KM_ANY);
673         INIT_EVENT_ICON(ICON_EVENT_RETURN, RETKEY, KM_ANY);
674
675         g_di_event_list = di_next;
676
677 #undef INIT_EVENT_ICON
678 }
679
680 static void icon_verify_datatoc(IconImage *iimg)
681 {
682         /* if it has own rect, things are all OK */
683         if (iimg->rect)
684                 return;
685
686         if (iimg->datatoc_rect) {
687                 ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
688                                                     iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
689                 /* w and h were set on initialize */
690                 if (bbuf->x != iimg->h && bbuf->y != iimg->w)
691                         IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
692
693                 iimg->rect = bbuf->rect;
694                 bbuf->rect = NULL;
695                 IMB_freeImBuf(bbuf);
696         }
697 }
698
699 static void init_internal_icons(void)
700 {
701 //      bTheme *btheme = UI_GetTheme();
702         ImBuf *b16buf = NULL, *b32buf = NULL;
703         int x, y;
704
705 #if 0 // temp disabled
706         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
707                 char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
708                 char iconfilestr[FILE_MAX];
709
710                 if (icondir) {
711                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
712                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
713                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
714                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
715                                 IMB_freeImBuf(bbuf);
716                                 bbuf = NULL;
717                         }
718                 }
719                 else {
720                         printf("%s: 'icons' data path not found, continuing\n", __func__);
721                 }
722         }
723 #endif
724         if (b16buf == NULL)
725                 b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
726                                                datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
727         if (b16buf)
728                 IMB_premultiply_alpha(b16buf);
729
730         if (b32buf == NULL)
731                 b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
732                                                datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
733         if (b32buf)
734                 IMB_premultiply_alpha(b32buf);
735
736         if (b16buf && b32buf) {
737                 /* Free existing texture if any. */
738                 if (icongltex.id) {
739                         glDeleteTextures(1, &icongltex.id);
740                         icongltex.id = 0;
741                 }
742
743                 /* Allocate OpenGL texture. */
744                 glGenTextures(1, &icongltex.id);
745
746                 if (icongltex.id) {
747                         int level = 2;
748
749                         icongltex.w = b32buf->x;
750                         icongltex.h = b32buf->y;
751                         icongltex.invw = 1.0f / b32buf->x;
752                         icongltex.invh = 1.0f / b32buf->y;
753
754                         glBindTexture(GL_TEXTURE_2D, icongltex.id);
755
756                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
757                         glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
758
759                         while (b16buf->x > 1) {
760                                 ImBuf *nbuf = IMB_onehalf(b16buf);
761                                 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
762                                 level++;
763                                 IMB_freeImBuf(b16buf);
764                                 b16buf = nbuf;
765                         }
766
767                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
768                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
769
770                         glBindTexture(GL_TEXTURE_2D, 0);
771                 }
772
773                 /* Define icons. */
774                 for (y = 0; y < ICON_GRID_ROWS; y++) {
775                         /* Row W has monochrome icons. */
776                         for (x = 0; x < ICON_GRID_COLS; x++) {
777                                 IconType icontype = icontypes[y * ICON_GRID_COLS + x];
778                                 if (!ELEM(icontype.type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
779                                         continue;
780                                 }
781
782                                 def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
783                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
784                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
785                                                   icontype.type, icontype.theme_color);
786                         }
787                 }
788         }
789
790         def_internal_vicon(ICON_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
791
792         def_internal_vicon(ICON_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
793         def_internal_vicon(ICON_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
794         def_internal_vicon(ICON_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
795         def_internal_vicon(ICON_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
796         def_internal_vicon(ICON_KEYTYPE_MOVING_HOLD_VEC, vicon_keytype_moving_hold_draw);
797
798         def_internal_vicon(ICON_HANDLETYPE_FREE_VEC, vicon_handletype_free_draw);
799         def_internal_vicon(ICON_HANDLETYPE_ALIGNED_VEC, vicon_handletype_aligned_draw);
800         def_internal_vicon(ICON_HANDLETYPE_VECTOR_VEC, vicon_handletype_vector_draw);
801         def_internal_vicon(ICON_HANDLETYPE_AUTO_VEC, vicon_handletype_auto_draw);
802         def_internal_vicon(ICON_HANDLETYPE_AUTO_CLAMP_VEC, vicon_handletype_auto_clamp_draw);
803
804         def_internal_vicon(ICON_COLORSET_01_VEC, vicon_colorset_draw_01);
805         def_internal_vicon(ICON_COLORSET_02_VEC, vicon_colorset_draw_02);
806         def_internal_vicon(ICON_COLORSET_03_VEC, vicon_colorset_draw_03);
807         def_internal_vicon(ICON_COLORSET_04_VEC, vicon_colorset_draw_04);
808         def_internal_vicon(ICON_COLORSET_05_VEC, vicon_colorset_draw_05);
809         def_internal_vicon(ICON_COLORSET_06_VEC, vicon_colorset_draw_06);
810         def_internal_vicon(ICON_COLORSET_07_VEC, vicon_colorset_draw_07);
811         def_internal_vicon(ICON_COLORSET_08_VEC, vicon_colorset_draw_08);
812         def_internal_vicon(ICON_COLORSET_09_VEC, vicon_colorset_draw_09);
813         def_internal_vicon(ICON_COLORSET_10_VEC, vicon_colorset_draw_10);
814         def_internal_vicon(ICON_COLORSET_11_VEC, vicon_colorset_draw_11);
815         def_internal_vicon(ICON_COLORSET_12_VEC, vicon_colorset_draw_12);
816         def_internal_vicon(ICON_COLORSET_13_VEC, vicon_colorset_draw_13);
817         def_internal_vicon(ICON_COLORSET_14_VEC, vicon_colorset_draw_14);
818         def_internal_vicon(ICON_COLORSET_15_VEC, vicon_colorset_draw_15);
819         def_internal_vicon(ICON_COLORSET_16_VEC, vicon_colorset_draw_16);
820         def_internal_vicon(ICON_COLORSET_17_VEC, vicon_colorset_draw_17);
821         def_internal_vicon(ICON_COLORSET_18_VEC, vicon_colorset_draw_18);
822         def_internal_vicon(ICON_COLORSET_19_VEC, vicon_colorset_draw_19);
823         def_internal_vicon(ICON_COLORSET_20_VEC, vicon_colorset_draw_20);
824
825         IMB_freeImBuf(b16buf);
826         IMB_freeImBuf(b32buf);
827
828 }
829 #endif  /* WITH_HEADLESS */
830
831 static void init_iconfile_list(struct ListBase *list)
832 {
833         IconFile *ifile;
834         struct direntry *dir;
835         int totfile, i, index = 1;
836         const char *icondir;
837
838         BLI_listbase_clear(list);
839         icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
840
841         if (icondir == NULL)
842                 return;
843
844         totfile = BLI_filelist_dir_contents(icondir, &dir);
845
846         for (i = 0; i < totfile; i++) {
847                 if ((dir[i].type & S_IFREG)) {
848                         const char *filename = dir[i].relname;
849
850                         if (BLI_path_extension_check(filename, ".png")) {
851                                 /* loading all icons on file start is overkill & slows startup
852                                  * its possible they change size after blender load anyway. */
853 #if 0
854                                 int ifilex, ifiley;
855                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
856                                 ImBuf *bbuf = NULL;
857                                 /* check to see if the image is the right size, continue if not */
858                                 /* copying strings here should go ok, assuming that we never get back
859                                  * a complete path to file longer than 256 chars */
860                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
861                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
862
863                                 if (bbuf) {
864                                         ifilex = bbuf->x;
865                                         ifiley = bbuf->y;
866                                         IMB_freeImBuf(bbuf);
867                                 }
868                                 else {
869                                         ifilex = ifiley = 0;
870                                 }
871
872                                 /* bad size or failed to load */
873                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
874                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
875                                         continue;
876                                 }
877 #endif          /* removed */
878
879                                 /* found a potential icon file, so make an entry for it in the cache list */
880                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
881
882                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
883                                 ifile->index = index;
884
885                                 BLI_addtail(list, ifile);
886
887                                 index++;
888                         }
889                 }
890         }
891
892         BLI_filelist_free(dir, totfile);
893         dir = NULL;
894 }
895
896 static void free_iconfile_list(struct ListBase *list)
897 {
898         IconFile *ifile = NULL, *next_ifile = NULL;
899
900         for (ifile = list->first; ifile; ifile = next_ifile) {
901                 next_ifile = ifile->next;
902                 BLI_freelinkN(list, ifile);
903         }
904 }
905
906 #endif  /* WITH_HEADLESS */
907
908 int UI_iconfile_get_index(const char *filename)
909 {
910         IconFile *ifile;
911         ListBase *list = &(iconfilelist);
912
913         for (ifile = list->first; ifile; ifile = ifile->next) {
914                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
915                         return ifile->index;
916                 }
917         }
918
919         return 0;
920 }
921
922 ListBase *UI_iconfile_list(void)
923 {
924         ListBase *list = &(iconfilelist);
925
926         return list;
927 }
928
929
930 void UI_icons_free(void)
931 {
932 #ifndef WITH_HEADLESS
933         if (icongltex.id) {
934                 glDeleteTextures(1, &icongltex.id);
935                 icongltex.id = 0;
936         }
937
938         free_iconfile_list(&iconfilelist);
939         BKE_icons_free();
940 #endif
941 }
942
943 void UI_icons_free_drawinfo(void *drawinfo)
944 {
945         DrawInfo *di = drawinfo;
946
947         if (di) {
948                 if (di->type == ICON_TYPE_BUFFER) {
949                         if (di->data.buffer.image) {
950                                 if (di->data.buffer.image->rect)
951                                         MEM_freeN(di->data.buffer.image->rect);
952                                 MEM_freeN(di->data.buffer.image);
953                         }
954                 }
955                 else if (di->type == ICON_TYPE_GEOM) {
956                         if (di->data.geom.image_cache) {
957                                 IMB_freeImBuf(di->data.geom.image_cache);
958                         }
959                 }
960
961                 MEM_freeN(di);
962         }
963 }
964
965 /**
966  * #Icon.data_type and #Icon.obj
967  */
968 static DrawInfo *icon_create_drawinfo(Icon *icon)
969 {
970         int  icon_data_type = icon->obj_type;
971         DrawInfo *di = NULL;
972
973         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
974
975         if (ELEM(icon_data_type, ICON_DATA_ID, ICON_DATA_PREVIEW)) {
976                 di->type = ICON_TYPE_PREVIEW;
977         }
978         else if (icon_data_type == ICON_DATA_GEOM) {
979                 di->type = ICON_TYPE_GEOM;
980         }
981         else if (icon_data_type == ICON_DATA_STUDIOLIGHT) {
982                 di->type = ICON_TYPE_BUFFER;
983         }
984         else if (icon_data_type == ICON_DATA_GPLAYER) {
985                 di->type = ICON_TYPE_GPLAYER;
986         }
987         else {
988                 BLI_assert(0);
989         }
990
991         return di;
992 }
993
994 static DrawInfo *icon_ensure_drawinfo(Icon *icon)
995 {
996         if (icon->drawinfo) {
997                 return icon->drawinfo;
998         }
999         DrawInfo *di = icon_create_drawinfo(icon);
1000         icon->drawinfo = di;
1001         icon->drawinfo_free = UI_icons_free_drawinfo;
1002         return di;
1003 }
1004
1005 /* note!, returns unscaled by DPI */
1006 int UI_icon_get_width(int icon_id)
1007 {
1008         Icon *icon = NULL;
1009         DrawInfo *di = NULL;
1010
1011         icon = BKE_icon_get(icon_id);
1012
1013         if (icon == NULL) {
1014                 if (G.debug & G_DEBUG)
1015                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1016                 return 0;
1017         }
1018
1019         di = icon_ensure_drawinfo(icon);
1020         if (di) {
1021                 return ICON_DEFAULT_WIDTH;
1022         }
1023
1024         return 0;
1025 }
1026
1027 int UI_icon_get_height(int icon_id)
1028 {
1029         Icon *icon = BKE_icon_get(icon_id);
1030         if (icon == NULL) {
1031                 if (G.debug & G_DEBUG)
1032                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1033                 return 0;
1034         }
1035
1036         DrawInfo *di = icon_ensure_drawinfo(icon);
1037         if (di) {
1038                 return ICON_DEFAULT_HEIGHT;
1039         }
1040
1041         return 0;
1042 }
1043
1044 void UI_icons_init()
1045 {
1046 #ifndef WITH_HEADLESS
1047         init_iconfile_list(&iconfilelist);
1048         init_internal_icons();
1049         init_brush_icons();
1050         init_event_icons();
1051 #endif
1052 }
1053
1054 /* Render size for preview images and icons
1055  */
1056 int UI_preview_render_size(enum eIconSizes size)
1057 {
1058         switch (size) {
1059                 case ICON_SIZE_ICON:
1060                         return ICON_RENDER_DEFAULT_HEIGHT;
1061                 case ICON_SIZE_PREVIEW:
1062                         return PREVIEW_RENDER_DEFAULT_HEIGHT;
1063                 default:
1064                         return 0;
1065         }
1066 }
1067
1068 /* Create rect for the icon
1069  */
1070 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
1071 {
1072         unsigned int render_size = UI_preview_render_size(size);
1073
1074         if (!prv_img) {
1075                 if (G.debug & G_DEBUG)
1076                         printf("%s, error: requested preview image does not exist", __func__);
1077         }
1078         else if (!prv_img->rect[size]) {
1079                 prv_img->w[size] = render_size;
1080                 prv_img->h[size] = render_size;
1081                 prv_img->flag[size] |= PRV_CHANGED;
1082                 prv_img->changed_timestamp[size] = 0;
1083                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
1084         }
1085 }
1086
1087 static void ui_id_preview_image_render_size(
1088         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job);
1089
1090 static void ui_studiolight_icon_job_exec(void *customdata, short *UNUSED(stop), short *UNUSED(do_update), float *UNUSED(progress))
1091 {
1092         Icon **tmp = (Icon **)customdata;
1093         Icon *icon = *tmp;
1094         DrawInfo *di = icon_ensure_drawinfo(icon);
1095         StudioLight *sl = icon->obj;
1096         BKE_studiolight_preview(di->data.buffer.image->rect, sl, icon->id_type);
1097 }
1098
1099 static void ui_studiolight_kill_icon_preview_job(wmWindowManager *wm, int icon_id)
1100 {
1101         Icon *icon = BKE_icon_get(icon_id);
1102         WM_jobs_kill_type(wm, icon, WM_JOB_TYPE_STUDIOLIGHT);
1103         icon->obj = NULL;
1104 }
1105
1106 static void ui_studiolight_free_function(StudioLight *sl, void *data)
1107 {
1108         wmWindowManager *wm = data;
1109
1110         /* Happens if job was canceled or already finished. */
1111         if (wm == NULL)
1112                 return;
1113
1114         // get icons_id, get icons and kill wm jobs
1115         if (sl->icon_id_radiance) {
1116                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_radiance);
1117         }
1118         if (sl->icon_id_irradiance) {
1119                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_irradiance);
1120         }
1121         if (sl->icon_id_matcap) {
1122                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap);
1123         }
1124         if (sl->icon_id_matcap_flipped) {
1125                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap_flipped);
1126         }
1127 }
1128
1129 static void ui_studiolight_icon_job_end(void *customdata)
1130 {
1131         Icon **tmp = (Icon **)customdata;
1132         Icon *icon = *tmp;
1133         StudioLight *sl = icon->obj;
1134         BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, NULL);
1135 }
1136
1137 void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
1138 {
1139         Icon *icon = BKE_icon_get(icon_id);
1140
1141         if (icon) {
1142                 DrawInfo *di = icon_ensure_drawinfo(icon);
1143
1144                 if (di) {
1145                         switch (di->type) {
1146                                 case ICON_TYPE_PREVIEW:
1147                                 {
1148                                         ID *id = (icon->id_type != 0) ? icon->obj : NULL;
1149                                         PreviewImage *prv = id ? BKE_previewimg_id_ensure(id) : icon->obj;
1150                                         /* Using jobs for screen previews crashes due to offscreen rendering.
1151                                          * XXX would be nicer if PreviewImage could store if it supports jobs */
1152                                         const bool use_jobs = !id || (GS(id->name) != ID_SCR);
1153
1154                                         if (prv) {
1155                                                 const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
1156
1157                                                 if (id || (prv->tag & PRV_TAG_DEFFERED) != 0) {
1158                                                         ui_id_preview_image_render_size(C, NULL, id, prv, size, use_jobs);
1159                                                 }
1160                                         }
1161                                         break;
1162                                 }
1163                                 case ICON_TYPE_BUFFER:
1164                                 {
1165                                         if (icon->obj_type == ICON_DATA_STUDIOLIGHT) {
1166                                                 if (di->data.buffer.image == NULL) {
1167                                                         wmWindowManager *wm = CTX_wm_manager(C);
1168                                                         StudioLight *sl = icon->obj;
1169                                                         BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, wm);
1170                                                         IconImage *img = MEM_mallocN(sizeof(IconImage), __func__);
1171
1172                                                         img->w = STUDIOLIGHT_ICON_SIZE;
1173                                                         img->h = STUDIOLIGHT_ICON_SIZE;
1174                                                         size_t size = STUDIOLIGHT_ICON_SIZE * STUDIOLIGHT_ICON_SIZE * sizeof(uint);
1175                                                         img->rect = MEM_mallocN(size, __func__);
1176                                                         memset(img->rect, 0, size);
1177                                                         di->data.buffer.image = img;
1178
1179                                                         wmJob *wm_job = WM_jobs_get(wm, CTX_wm_window(C), icon, "StudioLight Icon", 0, WM_JOB_TYPE_STUDIOLIGHT);
1180                                                         Icon **tmp = MEM_callocN(sizeof(Icon *), __func__);
1181                                                         *tmp = icon;
1182                                                         WM_jobs_customdata_set(wm_job, tmp, MEM_freeN);
1183                                                         WM_jobs_timer(wm_job, 0.01, 0, NC_WINDOW);
1184                                                         WM_jobs_callbacks(wm_job, ui_studiolight_icon_job_exec, NULL, NULL, ui_studiolight_icon_job_end);
1185                                                         WM_jobs_start(CTX_wm_manager(C), wm_job);
1186                                                 }
1187                                         }
1188                                         break;
1189                                 }
1190                         }
1191                 }
1192         }
1193 }
1194
1195 /* only called when icon has changed */
1196 /* only call with valid pointer from UI_icon_draw */
1197 static void icon_set_image(
1198         const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
1199 {
1200         if (!prv_img) {
1201                 if (G.debug & G_DEBUG)
1202                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
1203                 return;
1204         }
1205
1206         if (prv_img->flag[size] & PRV_USER_EDITED) {
1207                 /* user-edited preview, do not auto-update! */
1208                 return;
1209         }
1210
1211         icon_create_rect(prv_img, size);
1212
1213         if (use_job) {
1214                 /* Job (background) version */
1215                 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1216         }
1217         else {
1218                 if (!scene) {
1219                         scene = CTX_data_scene(C);
1220                 }
1221                 /* Immediate version */
1222                 ED_preview_icon_render(CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1223         }
1224 }
1225
1226 PreviewImage *UI_icon_to_preview(int icon_id)
1227 {
1228         Icon *icon = BKE_icon_get(icon_id);
1229
1230         if (icon) {
1231                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
1232                 if (di) {
1233                         if (di->type == ICON_TYPE_PREVIEW) {
1234                                 PreviewImage *prv = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1235
1236                                 if (prv) {
1237                                         return BKE_previewimg_copy(prv);
1238                                 }
1239                         }
1240                         else if (di->data.buffer.image) {
1241                                 ImBuf *bbuf;
1242
1243                                 bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size,
1244                                                              IB_rect, NULL, __func__);
1245                                 if (bbuf) {
1246                                         PreviewImage *prv = BKE_previewimg_create();
1247
1248                                         prv->rect[0] = bbuf->rect;
1249
1250                                         prv->w[0] = bbuf->x;
1251                                         prv->h[0] = bbuf->y;
1252
1253                                         bbuf->rect = NULL;
1254                                         IMB_freeImBuf(bbuf);
1255
1256                                         return prv;
1257                                 }
1258                         }
1259                 }
1260         }
1261         return NULL;
1262 }
1263
1264 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
1265                            unsigned int *rect, float alpha, const float rgb[3], const float desaturate)
1266 {
1267         ImBuf *ima = NULL;
1268         int draw_w = w;
1269         int draw_h = h;
1270         int draw_x = x;
1271         int draw_y = y;
1272
1273         /* sanity check */
1274         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
1275                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
1276                 BLI_assert(!"invalid icon size");
1277                 return;
1278         }
1279         /* modulate color */
1280         float col[4] = {1.0f, 1.0f, 1.0f, alpha};
1281
1282         if (rgb) {
1283                 col[0] = rgb[0];
1284                 col[1] = rgb[1];
1285                 col[2] = rgb[2];
1286         }
1287
1288         /* rect contains image in 'rendersize', we only scale if needed */
1289         if (rw != w || rh != h) {
1290                 /* preserve aspect ratio and center */
1291                 if (rw > rh) {
1292                         draw_w = w;
1293                         draw_h = (int)(((float)rh / (float)rw) * (float)w);
1294                         draw_y += (h - draw_h) / 2;
1295                 }
1296                 else if (rw < rh) {
1297                         draw_w = (int)(((float)rw / (float)rh) * (float)h);
1298                         draw_h = h;
1299                         draw_x += (w - draw_w) / 2;
1300                 }
1301                 /* if the image is squared, the draw_ initialization values are good */
1302
1303                 /* first allocate imbuf for scaling and copy preview into it */
1304                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1305                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1306                 IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
1307                 rect = ima->rect;
1308         }
1309
1310         /* draw */
1311         GPUBuiltinShader shader;
1312         if (desaturate != 0.0f) {
1313                 shader = GPU_SHADER_2D_IMAGE_DESATURATE_COLOR;
1314         }
1315         else {
1316                 shader = GPU_SHADER_2D_IMAGE_COLOR;
1317         }
1318         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(shader);
1319
1320         if (shader == GPU_SHADER_2D_IMAGE_DESATURATE_COLOR) {
1321                 immUniform1f("factor", desaturate);
1322         }
1323
1324         immDrawPixelsTex(&state, draw_x, draw_y, draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect,
1325                          1.0f, 1.0f, col);
1326
1327         if (ima)
1328                 IMB_freeImBuf(ima);
1329 }
1330
1331 /* High enough to make a difference, low enough so that
1332  * small draws are still efficient with the use of glUniform.
1333  * NOTE TODO: We could use UBO but we would need some triple
1334  * buffer system + persistent mapping for this to be more
1335  * efficient than simple glUniform calls. */
1336 #define ICON_DRAW_CACHE_SIZE 16
1337
1338 typedef struct IconDrawCall {
1339         rctf pos;
1340         rctf tex;
1341         float color[4];
1342 } IconDrawCall;
1343
1344 static struct {
1345         IconDrawCall drawcall_cache[ICON_DRAW_CACHE_SIZE];
1346         int calls; /* Number of calls batched together */
1347         bool enabled;
1348         float mat[4][4];
1349 } g_icon_draw_cache = {{{{0}}}};
1350
1351 void UI_icon_draw_cache_begin(void)
1352 {
1353         BLI_assert(g_icon_draw_cache.enabled == false);
1354         g_icon_draw_cache.enabled = true;
1355 }
1356
1357 static void icon_draw_cache_flush_ex(void)
1358 {
1359         if (g_icon_draw_cache.calls == 0)
1360                 return;
1361
1362         /* We need to flush widget base first to ensure correct ordering. */
1363         UI_widgetbase_draw_cache_flush();
1364
1365         GPU_blend_set_func(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1366
1367         glActiveTexture(GL_TEXTURE0);
1368         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1369
1370         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR);
1371         GPU_shader_bind(shader);
1372
1373         int img_loc = GPU_shader_get_uniform(shader, "image");
1374         int data_loc = GPU_shader_get_uniform(shader, "calls_data[0]");
1375
1376         glUniform1i(img_loc, 0);
1377         glUniform4fv(data_loc, ICON_DRAW_CACHE_SIZE * 3, (float *)g_icon_draw_cache.drawcall_cache);
1378
1379         GPU_draw_primitive(GPU_PRIM_TRIS, 6 * g_icon_draw_cache.calls);
1380
1381         glBindTexture(GL_TEXTURE_2D, 0);
1382
1383         g_icon_draw_cache.calls = 0;
1384
1385         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1386 }
1387
1388 void UI_icon_draw_cache_end(void)
1389 {
1390         BLI_assert(g_icon_draw_cache.enabled == true);
1391         g_icon_draw_cache.enabled = false;
1392
1393         /* Don't change blend state if it's not needed. */
1394         if (g_icon_draw_cache.calls == 0)
1395                 return;
1396
1397         GPU_blend(true);
1398         icon_draw_cache_flush_ex();
1399         GPU_blend(false);
1400 }
1401
1402 static void icon_draw_texture_cached(
1403         float x, float y, float w, float h, int ix, int iy,
1404         int UNUSED(iw), int ih, float alpha, const float rgb[3])
1405 {
1406
1407         float mvp[4][4];
1408         GPU_matrix_model_view_projection_get(mvp);
1409
1410         IconDrawCall *call = &g_icon_draw_cache.drawcall_cache[g_icon_draw_cache.calls];
1411         g_icon_draw_cache.calls++;
1412
1413         /* Manual mat4*vec2 */
1414         call->pos.xmin = x * mvp[0][0] + y * mvp[1][0] + mvp[3][0];
1415         call->pos.ymin = x * mvp[0][1] + y * mvp[1][1] + mvp[3][1];
1416         call->pos.xmax = call->pos.xmin + w * mvp[0][0] + h * mvp[1][0];
1417         call->pos.ymax = call->pos.ymin + w * mvp[0][1] + h * mvp[1][1];
1418
1419         call->tex.xmin = ix * icongltex.invw;
1420         call->tex.xmax = (ix + ih) * icongltex.invw;
1421         call->tex.ymin = iy * icongltex.invh;
1422         call->tex.ymax = (iy + ih) * icongltex.invh;
1423
1424         if (rgb) copy_v4_fl4(call->color, rgb[0], rgb[1], rgb[2], alpha);
1425         else     copy_v4_fl(call->color, alpha);
1426
1427         if (g_icon_draw_cache.calls == ICON_DRAW_CACHE_SIZE) {
1428                 icon_draw_cache_flush_ex();
1429         }
1430 }
1431
1432 static void icon_draw_texture(
1433         float x, float y, float w, float h, int ix, int iy,
1434         int iw, int ih, float alpha, const float rgb[3])
1435 {
1436         if (g_icon_draw_cache.enabled) {
1437                 icon_draw_texture_cached(x, y, w, h, ix, iy, iw, ih, alpha, rgb);
1438                 return;
1439         }
1440
1441         /* We need to flush widget base first to ensure correct ordering. */
1442         UI_widgetbase_draw_cache_flush();
1443
1444         GPU_blend_set_func(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1445
1446         float x1, x2, y1, y2;
1447
1448         x1 = ix * icongltex.invw;
1449         x2 = (ix + ih) * icongltex.invw;
1450         y1 = iy * icongltex.invh;
1451         y2 = (iy + ih) * icongltex.invh;
1452
1453         glActiveTexture(GL_TEXTURE0);
1454         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1455
1456         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_RECT_COLOR);
1457         GPU_shader_bind(shader);
1458
1459         if (rgb) glUniform4f(GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR), rgb[0], rgb[1], rgb[2], alpha);
1460         else     glUniform4f(GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR), alpha, alpha, alpha, alpha);
1461
1462         glUniform1i(GPU_shader_get_uniform(shader, "image"), 0);
1463         glUniform4f(GPU_shader_get_uniform(shader, "rect_icon"), x1, y1, x2, y2);
1464         glUniform4f(GPU_shader_get_uniform(shader, "rect_geom"), x, y, x + w, y + h);
1465
1466         GPU_draw_primitive(GPU_PRIM_TRI_STRIP, 4);
1467
1468         glBindTexture(GL_TEXTURE_2D, 0);
1469
1470         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1471 }
1472
1473 /* Drawing size for preview images */
1474 static int get_draw_size(enum eIconSizes size)
1475 {
1476         switch (size) {
1477                 case ICON_SIZE_ICON:
1478                         return ICON_DEFAULT_HEIGHT;
1479                 case ICON_SIZE_PREVIEW:
1480                         return PREVIEW_DEFAULT_HEIGHT;
1481                 default:
1482                         return 0;
1483         }
1484 }
1485
1486
1487
1488 static void icon_draw_size(
1489         float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1490         enum eIconSizes size, int draw_size, const float desaturate, const char mono_rgba[4])
1491 {
1492         bTheme *btheme = UI_GetTheme();
1493         Icon *icon = NULL;
1494         IconImage *iimg;
1495         const float fdraw_size = (float)draw_size;
1496         int w, h;
1497
1498         icon = BKE_icon_get(icon_id);
1499         alpha *= btheme->tui.icon_alpha;
1500
1501         if (icon == NULL) {
1502                 if (G.debug & G_DEBUG)
1503                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1504                 return;
1505         }
1506
1507         /* scale width and height according to aspect */
1508         w = (int)(fdraw_size / aspect + 0.5f);
1509         h = (int)(fdraw_size / aspect + 0.5f);
1510
1511         DrawInfo *di = icon_ensure_drawinfo(icon);
1512
1513         /* We need to flush widget base first to ensure correct ordering. */
1514         UI_widgetbase_draw_cache_flush();
1515
1516         if (di->type == ICON_TYPE_VECTOR) {
1517                 /* vector icons use the uiBlock transformation, they are not drawn
1518                  * with untransformed coordinates like the other icons */
1519                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1520         }
1521         else if (di->type == ICON_TYPE_GEOM) {
1522 #ifdef USE_UI_TOOLBAR_HACK
1523                 /* TODO(campbell): scale icons up for toolbar, we need a way to detect larger buttons and do this automatic. */
1524                 {
1525                         float scale = (float)ICON_DEFAULT_HEIGHT_TOOLBAR / (float)ICON_DEFAULT_HEIGHT;
1526                         y = (y + (h / 2)) - ((h * scale) / 2);
1527                         w *= scale;
1528                         h *= scale;
1529                 }
1530 #endif
1531
1532                 /* This could re-generate often if rendered at different sizes in the one interface.
1533                  * TODO(campbell): support caching multiple sizes. */
1534                 ImBuf *ibuf = di->data.geom.image_cache;
1535                 if ((ibuf == NULL) ||
1536                     (ibuf->x != w) ||
1537                     (ibuf->y != h))
1538                 {
1539                         if (ibuf) {
1540                                 IMB_freeImBuf(ibuf);
1541                         }
1542                         ibuf = BKE_icon_geom_rasterize(icon->obj, w, h);
1543                         di->data.geom.image_cache = ibuf;
1544                 }
1545
1546                 GPU_blend_set_func_separate(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1547                 icon_draw_rect(x, y, w, h, aspect, w, h, ibuf->rect, alpha, rgb, desaturate);
1548                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1549         }
1550         else if (di->type == ICON_TYPE_EVENT) {
1551                 const short event_type = di->data.input.event_type;
1552                 const short event_value = di->data.input.event_value;
1553                 icon_draw_rect_input(x, y, w, h, alpha, event_type, event_value);
1554         }
1555         else if (di->type == ICON_TYPE_COLOR_TEXTURE) {
1556                 /* texture image use premul alpha for correct scaling */
1557                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1558                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1559         }
1560         else if (di->type == ICON_TYPE_MONO_TEXTURE) {
1561                 /* icon that matches text color, assumed to be white */
1562                 float color[4];
1563                 if (!UI_GetIconThemeColor4fv(di->data.texture.theme_color, color)) {
1564                         if (mono_rgba) {
1565                                 rgba_uchar_to_float(color, (const uchar *)mono_rgba);
1566                         }
1567                         else {
1568                                 UI_GetThemeColor4fv(TH_TEXT, color);
1569                         }
1570                 }
1571
1572                 if (rgb) {
1573                         mul_v3_v3(color, rgb);
1574                 }
1575
1576                 mul_v4_fl(color, alpha);
1577
1578                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1579                                   di->data.texture.w, di->data.texture.h, color[3], color);
1580         }
1581
1582         else if (di->type == ICON_TYPE_BUFFER) {
1583                 /* it is a builtin icon */
1584                 iimg = di->data.buffer.image;
1585 #ifndef WITH_HEADLESS
1586                 icon_verify_datatoc(iimg);
1587 #endif
1588                 if (!iimg->rect) return;  /* something has gone wrong! */
1589
1590                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, desaturate);
1591         }
1592         else if (di->type == ICON_TYPE_PREVIEW) {
1593                 PreviewImage *pi = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1594
1595                 if (pi) {
1596                         /* no create icon on this level in code */
1597                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1598
1599                         /* Preview images use premultiplied alpha. */
1600                         GPU_blend_set_func_separate(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1601                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, rgb, desaturate);
1602                         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1603                 }
1604         }
1605         else if (di->type == ICON_TYPE_GPLAYER) {
1606                 BLI_assert(icon->obj != NULL);
1607
1608                 /* Just draw a colored rect - Like for vicon_colorset_draw() */
1609 #ifndef WITH_HEADLESS
1610                 vicon_gplayer_color_draw(icon, (int)x, (int)y,  w, h);
1611 #endif
1612         }
1613 }
1614
1615 static void ui_id_preview_image_render_size(
1616         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1617 {
1618         if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1619                 /* create the rect if necessary */
1620                 icon_set_image(C, scene, id, pi, size, use_job);
1621
1622                 pi->flag[size] &= ~PRV_CHANGED;
1623         }
1624 }
1625
1626 void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1627 {
1628         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1629
1630         if (pi) {
1631                 if (big)
1632                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);  /* bigger preview size */
1633                 else
1634                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);     /* icon size */
1635         }
1636 }
1637
1638 static void ui_id_icon_render(const bContext *C, ID *id, bool use_jobs)
1639 {
1640         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1641         enum eIconSizes i;
1642
1643         if (!pi)
1644                 return;
1645
1646         for (i = 0; i < NUM_ICON_SIZES; i++) {
1647                 /* check if rect needs to be created; changed
1648                  * only set by dynamic icons */
1649                 if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
1650                         icon_set_image(C, NULL, id, pi, i, use_jobs);
1651                         pi->flag[i] &= ~PRV_CHANGED;
1652                 }
1653         }
1654 }
1655
1656
1657 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1658 {
1659         Brush *br = (Brush *)id;
1660
1661         if (br->flag & BRUSH_CUSTOM_ICON) {
1662                 BKE_icon_id_ensure(id);
1663                 ui_id_icon_render(C, id, true);
1664         }
1665         else {
1666                 WorkSpace *workspace = CTX_wm_workspace(C);
1667                 Object *ob = CTX_data_active_object(C);
1668                 const EnumPropertyItem *items = NULL;
1669                 ePaintMode paint_mode = PAINT_MODE_INVALID;
1670                 ScrArea *sa = CTX_wm_area(C);
1671                 char space_type = sa->spacetype;
1672                 /* When in an unsupported space. */
1673                 if (!ELEM(space_type, SPACE_VIEW3D, SPACE_IMAGE)) {
1674                         space_type = workspace->tools_space_type;
1675                 }
1676
1677                 /* XXX: this is not nice, should probably make brushes
1678                  * be strictly in one paint mode only to avoid
1679                  * checking various context stuff here */
1680
1681                 if ((space_type == SPACE_VIEW3D) && ob) {
1682                         if (ob->mode & OB_MODE_SCULPT) {
1683                                 paint_mode = PAINT_MODE_SCULPT;
1684                         }
1685                         else if (ob->mode & OB_MODE_VERTEX_PAINT) {
1686                                 paint_mode = PAINT_MODE_VERTEX;
1687                         }
1688                         else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1689                                 paint_mode = PAINT_MODE_WEIGHT;
1690                         }
1691                         else if (ob->mode & OB_MODE_TEXTURE_PAINT) {
1692                                 paint_mode = PAINT_MODE_TEXTURE_3D;
1693                         }
1694                 }
1695                 else if (space_type == SPACE_IMAGE) {
1696                         int sima_mode;
1697                         if (sa->spacetype == space_type) {
1698                                 SpaceImage *sima = sa->spacedata.first;
1699                                 sima_mode = sima->mode;
1700                         }
1701                         else {
1702                                 sima_mode = workspace->tools_mode;
1703                         }
1704
1705                         if (sima_mode == SI_MODE_PAINT) {
1706                                 paint_mode = PAINT_MODE_TEXTURE_2D;
1707                         }
1708                 }
1709
1710                 /* reset the icon */
1711                 if ((ob != NULL) &&
1712                     (ob->mode & OB_MODE_PAINT_GPENCIL) &&
1713                     (br->gpencil_settings != NULL))
1714                 {
1715                         switch (br->gpencil_settings->icon_id) {
1716                                 case GP_BRUSH_ICON_PENCIL:
1717                                         br->id.icon_id = ICON_GPBRUSH_PENCIL;
1718                                         break;
1719                                 case GP_BRUSH_ICON_PEN:
1720                                         br->id.icon_id = ICON_GPBRUSH_PEN;
1721                                         break;
1722                                 case GP_BRUSH_ICON_INK:
1723                                         br->id.icon_id = ICON_GPBRUSH_INK;
1724                                         break;
1725                                 case GP_BRUSH_ICON_INKNOISE:
1726                                         br->id.icon_id = ICON_GPBRUSH_INKNOISE;
1727                                         break;
1728                                 case GP_BRUSH_ICON_BLOCK:
1729                                         br->id.icon_id = ICON_GPBRUSH_BLOCK;
1730                                         break;
1731                                 case GP_BRUSH_ICON_MARKER:
1732                                         br->id.icon_id = ICON_GPBRUSH_MARKER;
1733                                         break;
1734                                 case GP_BRUSH_ICON_FILL:
1735                                         br->id.icon_id = ICON_GPBRUSH_FILL;
1736                                         break;
1737                                 case GP_BRUSH_ICON_ERASE_SOFT:
1738                                         br->id.icon_id = ICON_GPBRUSH_ERASE_SOFT;
1739                                         break;
1740                                 case GP_BRUSH_ICON_ERASE_HARD:
1741                                         br->id.icon_id = ICON_GPBRUSH_ERASE_HARD;
1742                                         break;
1743                                 case GP_BRUSH_ICON_ERASE_STROKE:
1744                                         br->id.icon_id = ICON_GPBRUSH_ERASE_STROKE;
1745                                         break;
1746                                 default:
1747                                         br->id.icon_id = ICON_GPBRUSH_PEN;
1748                                         break;
1749                         }
1750                         return id->icon_id;
1751                 }
1752                 else if (paint_mode != PAINT_MODE_INVALID) {
1753                         items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
1754                         const uint tool_offset = BKE_paint_get_brush_tool_offset_from_paintmode(paint_mode);
1755                         const int tool_type = *(char *)POINTER_OFFSET(br, tool_offset);
1756                         if (!items || !RNA_enum_icon_from_value(items, tool_type, &id->icon_id)) {
1757                                 id->icon_id = 0;
1758                         }
1759                 }
1760                 else {
1761                         id->icon_id = 0;
1762                 }
1763         }
1764
1765         return id->icon_id;
1766 }
1767
1768 static int ui_id_screen_get_icon(const bContext *C, ID *id)
1769 {
1770         BKE_icon_id_ensure(id);
1771         /* Don't use jobs here, offscreen rendering doesn't like this and crashes. */
1772         ui_id_icon_render(C, id, false);
1773
1774         return id->icon_id;
1775 }
1776
1777 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
1778 {
1779         int iconid = 0;
1780
1781         /* icon */
1782         switch (GS(id->name)) {
1783                 case ID_BR:
1784                         iconid = ui_id_brush_get_icon(C, id);
1785                         break;
1786                 case ID_MA: /* fall through */
1787                 case ID_TE: /* fall through */
1788                 case ID_IM: /* fall through */
1789                 case ID_WO: /* fall through */
1790                 case ID_LA: /* fall through */
1791                         iconid = BKE_icon_id_ensure(id);
1792                         /* checks if not exists, or changed */
1793                         UI_id_icon_render(C, NULL, id, big, true);
1794                         break;
1795                 case ID_SCR:
1796                         iconid = ui_id_screen_get_icon(C, id);
1797                         break;
1798                 default:
1799                         break;
1800         }
1801
1802         return iconid;
1803 }
1804
1805 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1806 {
1807         ID *id = NULL;
1808
1809         if (!ptr->data)
1810                 return rnaicon;
1811
1812         /* try ID, material, texture or dynapaint slot */
1813         if (RNA_struct_is_ID(ptr->type)) {
1814                 id = ptr->id.data;
1815         }
1816         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1817                 id = RNA_pointer_get(ptr, "material").data;
1818         }
1819         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1820                 id = RNA_pointer_get(ptr, "texture").data;
1821         }
1822         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1823                 DynamicPaintSurface *surface = ptr->data;
1824
1825                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1826                         return ICON_SHADING_TEXTURE;
1827                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1828                         return ICON_OUTLINER_DATA_MESH;
1829                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1830                         return ICON_FILE_IMAGE;
1831         }
1832         else if (RNA_struct_is_a(ptr->type, &RNA_StudioLight)) {
1833                 StudioLight *sl = ptr->data;
1834                 switch (sl->flag & STUDIOLIGHT_FLAG_ORIENTATIONS) {
1835                         case STUDIOLIGHT_TYPE_STUDIO:
1836                                 return sl->icon_id_irradiance;
1837                         case STUDIOLIGHT_TYPE_WORLD:
1838                         default:
1839                                 return sl->icon_id_radiance;
1840                         case STUDIOLIGHT_TYPE_MATCAP:
1841                                 return sl->icon_id_matcap;
1842                 }
1843         }
1844
1845         /* get icon from ID */
1846         if (id) {
1847                 int icon = ui_id_icon_get(C, id, big);
1848
1849                 return icon ? icon : rnaicon;
1850         }
1851
1852         return rnaicon;
1853 }
1854
1855 int UI_idcode_icon_get(const int idcode)
1856 {
1857         switch (idcode) {
1858                 case ID_AC:
1859                         return ICON_ACTION;
1860                 case ID_AR:
1861                         return ICON_ARMATURE_DATA;
1862                 case ID_BR:
1863                         return ICON_BRUSH_DATA;
1864                 case ID_CA:
1865                         return ICON_CAMERA_DATA;
1866                 case ID_CF:
1867                         return ICON_FILE;
1868                 case ID_CU:
1869                         return ICON_CURVE_DATA;
1870                 case ID_GD:
1871                         return ICON_GREASEPENCIL;
1872                 case ID_GR:
1873                         return ICON_GROUP;
1874                 case ID_IM:
1875                         return ICON_IMAGE_DATA;
1876                 case ID_LA:
1877                         return ICON_LIGHT_DATA;
1878                 case ID_LS:
1879                         return ICON_LINE_DATA;
1880                 case ID_LT:
1881                         return ICON_LATTICE_DATA;
1882                 case ID_MA:
1883                         return ICON_MATERIAL_DATA;
1884                 case ID_MB:
1885                         return ICON_META_DATA;
1886                 case ID_MC:
1887                         return ICON_TRACKER;
1888                 case ID_ME:
1889                         return ICON_MESH_DATA;
1890                 case ID_MSK:
1891                         return ICON_MOD_MASK;  /* TODO! this would need its own icon! */
1892                 case ID_NT:
1893                         return ICON_NODETREE;
1894                 case ID_OB:
1895                         return ICON_OBJECT_DATA;
1896                 case ID_PA:
1897                         return ICON_PARTICLE_DATA;
1898                 case ID_PAL:
1899                         return ICON_COLOR;  /* TODO! this would need its own icon! */
1900                 case ID_PC:
1901                         return ICON_CURVE_BEZCURVE;  /* TODO! this would need its own icon! */
1902                 case ID_LP:
1903                         return ICON_LIGHTPROBE_CUBEMAP;
1904                 case ID_SCE:
1905                         return ICON_SCENE_DATA;
1906                 case ID_SPK:
1907                         return ICON_SPEAKER;
1908                 case ID_SO:
1909                         return ICON_SOUND;
1910                 case ID_TE:
1911                         return ICON_TEXTURE_DATA;
1912                 case ID_TXT:
1913                         return ICON_TEXT;
1914                 case ID_VF:
1915                         return ICON_FONT_DATA;
1916                 case ID_WO:
1917                         return ICON_WORLD_DATA;
1918                 default:
1919                         return ICON_NONE;
1920         }
1921 }
1922
1923 static void icon_draw_at_size(
1924         float x, float y, int icon_id, float aspect, float alpha,
1925         enum eIconSizes size, const float desaturate, const char mono_color[4])
1926 {
1927         int draw_size = get_draw_size(size);
1928         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, desaturate, mono_color);
1929 }
1930
1931 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha, const char mono_color[4])
1932 {
1933         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0.0f, mono_color);
1934 }
1935
1936 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3], const char mono_color[4])
1937 {
1938         int draw_size = get_draw_size(ICON_SIZE_ICON);
1939         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, mono_color);
1940 }
1941
1942 void UI_icon_draw_desaturate(float x, float y, int icon_id, float aspect, float alpha, float desaturate, const char mono_color[4])
1943 {
1944         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, desaturate, mono_color);
1945 }
1946
1947 /* draws icon with dpi scale factor */
1948 void UI_icon_draw(float x, float y, int icon_id)
1949 {
1950         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f, NULL);
1951 }
1952
1953 void UI_icon_draw_alpha(float x, float y, int icon_id, float alpha)
1954 {
1955         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, alpha, NULL);
1956 }
1957
1958 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1959 {
1960         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, false, NULL);
1961 }
1962
1963 void UI_icon_draw_preview(float x, float y, int icon_id)
1964 {
1965         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, false, NULL);
1966 }
1967
1968 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1969 {
1970         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, false, NULL);
1971 }
1972
1973 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, float alpha, int size)
1974 {
1975         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, ICON_SIZE_PREVIEW, size, false, NULL);
1976 }