1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup edinterface
22  */
23 
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "GPU_batch.h"
31 #include "GPU_batch_presets.h"
32 #include "GPU_immediate.h"
33 #include "GPU_matrix.h"
34 #include "GPU_state.h"
35 #include "GPU_texture.h"
36 
37 #include "BLI_blenlib.h"
38 #include "BLI_fileops_types.h"
39 #include "BLI_math_color_blend.h"
40 #include "BLI_math_vector.h"
41 #include "BLI_utildefines.h"
42 
43 #include "DNA_brush_types.h"
44 #include "DNA_collection_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_dynamicpaint_types.h"
47 #include "DNA_gpencil_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_space_types.h"
51 
52 #include "RNA_access.h"
53 
54 #include "BKE_appdir.h"
55 #include "BKE_context.h"
56 #include "BKE_global.h"
57 #include "BKE_icons.h"
58 #include "BKE_paint.h"
59 #include "BKE_studiolight.h"
60 
61 #include "IMB_imbuf.h"
62 #include "IMB_imbuf_types.h"
63 #include "IMB_thumbs.h"
64 
65 #include "BIF_glutil.h"
66 
67 #include "ED_datafiles.h"
68 #include "ED_keyframes_draw.h"
69 #include "ED_render.h"
70 
71 #include "UI_interface.h"
72 #include "UI_interface_icons.h"
73 
74 #include "WM_api.h"
75 #include "WM_types.h"
76 
77 #include "interface_intern.h"
78 
79 #ifndef WITH_HEADLESS
80 #  define ICON_GRID_COLS 26
81 #  define ICON_GRID_ROWS 30
82 
83 #  define ICON_MONO_BORDER_OUTSET 2
84 #  define ICON_GRID_MARGIN 10
85 #  define ICON_GRID_W 32
86 #  define ICON_GRID_H 32
87 #endif /* WITH_HEADLESS */
88 
89 typedef struct IconImage {
90   int w;
91   int h;
92   uint *rect;
93   const uchar *datatoc_rect;
94   int datatoc_size;
95 } IconImage;
96 
97 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
98 
99 #define ICON_TYPE_PREVIEW 0
100 #define ICON_TYPE_COLOR_TEXTURE 1
101 #define ICON_TYPE_MONO_TEXTURE 2
102 #define ICON_TYPE_BUFFER 3
103 #define ICON_TYPE_VECTOR 4
104 #define ICON_TYPE_GEOM 5
105 #define ICON_TYPE_EVENT 6 /* draw keymap entries using custom renderer. */
106 #define ICON_TYPE_GPLAYER 7
107 #define ICON_TYPE_BLANK 8
108 
109 typedef struct DrawInfo {
110   int type;
111 
112   union {
113     /* type specific data */
114     struct {
115       VectorDrawFunc func;
116     } vector;
117     struct {
118       ImBuf *image_cache;
119       bool inverted;
120     } geom;
121     struct {
122       IconImage *image;
123     } buffer;
124     struct {
125       int x, y, w, h;
126       int theme_color;
127     } texture;
128     struct {
129       /* Can be packed into a single int. */
130       short event_type;
131       short event_value;
132       int icon;
133       /* Allow lookups. */
134       struct DrawInfo *next;
135     } input;
136   } data;
137 } DrawInfo;
138 
139 typedef struct IconTexture {
140   struct GPUTexture *tex[2];
141   int num_textures;
142   int w;
143   int h;
144   float invw;
145   float invh;
146 } IconTexture;
147 
148 typedef struct IconType {
149   int type;
150   int theme_color;
151 } IconType;
152 
153 /* ******************* STATIC LOCAL VARS ******************* */
154 /* Static here to cache results of icon directory scan, so it's not
155  * scanning the file-system each time the menu is drawn. */
156 static struct ListBase iconfilelist = {NULL, NULL};
157 static IconTexture icongltex = {{NULL, NULL}, 0, 0, 0, 0.0f, 0.0f};
158 
159 #ifndef WITH_HEADLESS
160 
161 static const IconType icontypes[] = {
162 #  define DEF_ICON(name) {ICON_TYPE_MONO_TEXTURE, 0},
163 #  define DEF_ICON_SCENE(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_SCENE},
164 #  define DEF_ICON_COLLECTION(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_COLLECTION},
165 #  define DEF_ICON_OBJECT(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT},
166 #  define DEF_ICON_OBJECT_DATA(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT_DATA},
167 #  define DEF_ICON_MODIFIER(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_MODIFIER},
168 #  define DEF_ICON_SHADING(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_SHADING},
169 #  define DEF_ICON_FOLDER(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_FOLDER},
170 #  define DEF_ICON_FUND(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_FUND},
171 #  define DEF_ICON_VECTOR(name) {ICON_TYPE_VECTOR, 0},
172 #  define DEF_ICON_COLOR(name) {ICON_TYPE_COLOR_TEXTURE, 0},
173 #  define DEF_ICON_BLANK(name) {ICON_TYPE_BLANK, 0},
174 #  include "UI_icons.h"
175 };
176 
177 /* **************************************************** */
178 
def_internal_icon(ImBuf * bbuf,int icon_id,int xofs,int yofs,int size,int type,int theme_color)179 static DrawInfo *def_internal_icon(
180     ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type, int theme_color)
181 {
182   Icon *new_icon = MEM_callocN(sizeof(Icon), "texicon");
183 
184   new_icon->obj = NULL; /* icon is not for library object */
185   new_icon->id_type = 0;
186 
187   DrawInfo *di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
188   di->type = type;
189 
190   if (ELEM(type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
191     di->data.texture.theme_color = theme_color;
192     di->data.texture.x = xofs;
193     di->data.texture.y = yofs;
194     di->data.texture.w = size;
195     di->data.texture.h = size;
196   }
197   else if (type == ICON_TYPE_BUFFER) {
198     IconImage *iimg = MEM_callocN(sizeof(IconImage), "icon_img");
199     iimg->w = size;
200     iimg->h = size;
201 
202     /* icon buffers can get initialized runtime now, via datatoc */
203     if (bbuf) {
204       int y, imgsize;
205 
206       iimg->rect = MEM_mallocN(size * size * sizeof(uint), "icon_rect");
207 
208       /* Here we store the rect in the icon - same as before */
209       if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0) {
210         memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
211       }
212       else {
213         /* this code assumes square images */
214         imgsize = bbuf->x;
215         for (y = 0; y < size; y++) {
216           memcpy(
217               &iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
218         }
219       }
220     }
221     di->data.buffer.image = iimg;
222   }
223 
224   new_icon->drawinfo_free = UI_icons_free_drawinfo;
225   new_icon->drawinfo = di;
226 
227   BKE_icon_set(icon_id, new_icon);
228 
229   return di;
230 }
231 
def_internal_vicon(int icon_id,VectorDrawFunc drawFunc)232 static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
233 {
234   Icon *new_icon = MEM_callocN(sizeof(Icon), "texicon");
235 
236   new_icon->obj = NULL; /* icon is not for library object */
237   new_icon->id_type = 0;
238 
239   DrawInfo *di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
240   di->type = ICON_TYPE_VECTOR;
241   di->data.vector.func = drawFunc;
242 
243   new_icon->drawinfo_free = NULL;
244   new_icon->drawinfo = di;
245 
246   BKE_icon_set(icon_id, new_icon);
247 }
248 
249 /* Vector Icon Drawing Routines */
250 
251 /* Utilities */
252 
viconutil_set_point(int pt[2],int x,int y)253 static void viconutil_set_point(int pt[2], int x, int y)
254 {
255   pt[0] = x;
256   pt[1] = y;
257 }
258 
vicon_small_tri_right_draw(int x,int y,int w,int UNUSED (h),float alpha)259 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
260 {
261   int pts[3][2];
262   const int cx = x + w / 2 - 4;
263   const int cy = y + w / 2;
264   const int d = w / 5, d2 = w / 7;
265 
266   viconutil_set_point(pts[0], cx - d2, cy + d);
267   viconutil_set_point(pts[1], cx - d2, cy - d);
268   viconutil_set_point(pts[2], cx + d2, cy);
269 
270   uint pos = GPU_vertformat_attr_add(
271       immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
272   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
273   immUniformColor4f(0.2f, 0.2f, 0.2f, alpha);
274 
275   immBegin(GPU_PRIM_TRIS, 3);
276   immVertex2iv(pos, pts[0]);
277   immVertex2iv(pos, pts[1]);
278   immVertex2iv(pos, pts[2]);
279   immEnd();
280 
281   immUnbindProgram();
282 }
283 
vicon_keytype_draw_wrapper(int x,int y,int w,int h,float alpha,short key_type,short handle_type)284 static void vicon_keytype_draw_wrapper(
285     int x, int y, int w, int h, float alpha, short key_type, short handle_type)
286 {
287   /* init dummy theme state for Action Editor - where these colors are defined
288    * (since we're doing this offscreen, free from any particular space_id)
289    */
290   struct bThemeState theme_state;
291 
292   UI_Theme_Store(&theme_state);
293   UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
294 
295   /* the "x" and "y" given are the bottom-left coordinates of the icon,
296    * while the draw_keyframe_shape() function needs the midpoint for
297    * the keyframe
298    */
299   const float xco = x + w / 2 + 0.5f;
300   const float yco = y + h / 2 + 0.5f;
301 
302   GPUVertFormat *format = immVertexFormat();
303   const uint pos_id = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
304   const uint size_id = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
305   uint color_id = GPU_vertformat_attr_add(
306       format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
307   uint outline_color_id = GPU_vertformat_attr_add(
308       format, "outlineColor", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
309   const uint flags_id = GPU_vertformat_attr_add(format, "flags", GPU_COMP_U32, 1, GPU_FETCH_INT);
310 
311   GPU_program_point_size(true);
312   immBindBuiltinProgram(GPU_SHADER_KEYFRAME_DIAMOND);
313   immUniform1f("outline_scale", 1.0f);
314   immUniform2f("ViewportSize", -1.0f, -1.0f);
315   immBegin(GPU_PRIM_POINTS, 1);
316 
317   /* draw keyframe
318    * - size: (default icon size == 16, default dopesheet icon size == 10)
319    * - sel: true unless in handletype icons (so that "keyframe" state shows the iconic yellow icon)
320    */
321   const bool sel = (handle_type == KEYFRAME_HANDLE_NONE);
322 
323   draw_keyframe_shape(xco,
324                       yco,
325                       (10.0f / 16.0f) * h,
326                       sel,
327                       key_type,
328                       KEYFRAME_SHAPE_BOTH,
329                       alpha,
330                       pos_id,
331                       size_id,
332                       color_id,
333                       outline_color_id,
334                       flags_id,
335                       handle_type,
336                       KEYFRAME_EXTREME_NONE);
337 
338   immEnd();
339   GPU_program_point_size(false);
340   immUnbindProgram();
341 
342   UI_Theme_Restore(&theme_state);
343 }
344 
vicon_keytype_keyframe_draw(int x,int y,int w,int h,float alpha)345 static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
346 {
347   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_NONE);
348 }
349 
vicon_keytype_breakdown_draw(int x,int y,int w,int h,float alpha)350 static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
351 {
352   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN, KEYFRAME_HANDLE_NONE);
353 }
354 
vicon_keytype_extreme_draw(int x,int y,int w,int h,float alpha)355 static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
356 {
357   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME, KEYFRAME_HANDLE_NONE);
358 }
359 
vicon_keytype_jitter_draw(int x,int y,int w,int h,float alpha)360 static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
361 {
362   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER, KEYFRAME_HANDLE_NONE);
363 }
364 
vicon_keytype_moving_hold_draw(int x,int y,int w,int h,float alpha)365 static void vicon_keytype_moving_hold_draw(int x, int y, int w, int h, float alpha)
366 {
367   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_MOVEHOLD, KEYFRAME_HANDLE_NONE);
368 }
369 
vicon_handletype_free_draw(int x,int y,int w,int h,float alpha)370 static void vicon_handletype_free_draw(int x, int y, int w, int h, float alpha)
371 {
372   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_FREE);
373 }
374 
vicon_handletype_aligned_draw(int x,int y,int w,int h,float alpha)375 static void vicon_handletype_aligned_draw(int x, int y, int w, int h, float alpha)
376 {
377   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_ALIGNED);
378 }
379 
vicon_handletype_vector_draw(int x,int y,int w,int h,float alpha)380 static void vicon_handletype_vector_draw(int x, int y, int w, int h, float alpha)
381 {
382   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_VECTOR);
383 }
384 
vicon_handletype_auto_draw(int x,int y,int w,int h,float alpha)385 static void vicon_handletype_auto_draw(int x, int y, int w, int h, float alpha)
386 {
387   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO);
388 }
389 
vicon_handletype_auto_clamp_draw(int x,int y,int w,int h,float alpha)390 static void vicon_handletype_auto_clamp_draw(int x, int y, int w, int h, float alpha)
391 {
392   vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO_CLAMP);
393 }
394 
vicon_colorset_draw(int index,int x,int y,int w,int h,float UNUSED (alpha))395 static void vicon_colorset_draw(int index, int x, int y, int w, int h, float UNUSED(alpha))
396 {
397   bTheme *btheme = UI_GetTheme();
398   const ThemeWireColor *cs = &btheme->tarm[index];
399 
400   /* Draw three bands of color: One per color
401    *    x-----a-----b-----c
402    *    |  N  |  S  |  A  |
403    *    x-----a-----b-----c
404    */
405   const int a = x + w / 3;
406   const int b = x + w / 3 * 2;
407   const int c = x + w;
408 
409   uint pos = GPU_vertformat_attr_add(
410       immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
411   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
412 
413   /* XXX: Include alpha into this... */
414   /* normal */
415   immUniformColor3ubv(cs->solid);
416   immRecti(pos, x, y, a, y + h);
417 
418   /* selected */
419   immUniformColor3ubv(cs->select);
420   immRecti(pos, a, y, b, y + h);
421 
422   /* active */
423   immUniformColor3ubv(cs->active);
424   immRecti(pos, b, y, c, y + h);
425 
426   immUnbindProgram();
427 }
428 
429 #  define DEF_ICON_VECTOR_COLORSET_DRAW_NTH(prefix, index) \
430     static void vicon_colorset_draw_##prefix(int x, int y, int w, int h, float alpha) \
431     { \
432       vicon_colorset_draw(index, x, y, w, h, alpha); \
433     }
434 
435 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(01, 0)
436 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(02, 1)
437 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(03, 2)
438 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(04, 3)
439 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(05, 4)
440 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(06, 5)
441 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(07, 6)
442 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(08, 7)
443 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(09, 8)
444 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(10, 9)
445 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(11, 10)
446 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(12, 11)
447 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(13, 12)
448 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(14, 13)
449 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(15, 14)
450 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(16, 15)
451 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(17, 16)
452 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(18, 17)
453 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(19, 18)
454 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(20, 19)
455 
456 #  undef DEF_ICON_VECTOR_COLORSET_DRAW_NTH
457 
vicon_collection_color_draw(short color_tag,int x,int y,int UNUSED (w),int UNUSED (h),float UNUSED (alpha))458 static void vicon_collection_color_draw(
459     short color_tag, int x, int y, int UNUSED(w), int UNUSED(h), float UNUSED(alpha))
460 {
461   bTheme *btheme = UI_GetTheme();
462   const ThemeCollectionColor *collection_color = &btheme->collection_color[color_tag];
463 
464   UI_icon_draw_ex(
465       x, y, ICON_OUTLINER_COLLECTION, U.inv_dpi_fac, 1.0f, 0.0f, collection_color->color, true);
466 }
467 
468 #  define DEF_ICON_COLLECTION_COLOR_DRAW(index, color) \
469     static void vicon_collection_color_draw_##index(int x, int y, int w, int h, float alpha) \
470     { \
471       vicon_collection_color_draw(color, x, y, w, h, alpha); \
472     }
473 
474 DEF_ICON_COLLECTION_COLOR_DRAW(01, COLLECTION_COLOR_01);
475 DEF_ICON_COLLECTION_COLOR_DRAW(02, COLLECTION_COLOR_02);
476 DEF_ICON_COLLECTION_COLOR_DRAW(03, COLLECTION_COLOR_03);
477 DEF_ICON_COLLECTION_COLOR_DRAW(04, COLLECTION_COLOR_04);
478 DEF_ICON_COLLECTION_COLOR_DRAW(05, COLLECTION_COLOR_05);
479 DEF_ICON_COLLECTION_COLOR_DRAW(06, COLLECTION_COLOR_06);
480 DEF_ICON_COLLECTION_COLOR_DRAW(07, COLLECTION_COLOR_07);
481 DEF_ICON_COLLECTION_COLOR_DRAW(08, COLLECTION_COLOR_08);
482 
483 #  undef DEF_ICON_COLLECTION_COLOR_DRAW
484 
485 /* Dynamically render icon instead of rendering a plain color to a texture/buffer
486  * This is not strictly a "vicon", as it needs access to icon->obj to get the color info,
487  * but it works in a very similar way.
488  */
vicon_gplayer_color_draw(Icon * icon,int x,int y,int w,int h)489 static void vicon_gplayer_color_draw(Icon *icon, int x, int y, int w, int h)
490 {
491   bGPDlayer *gpl = (bGPDlayer *)icon->obj;
492 
493   /* Just draw a colored rect - Like for vicon_colorset_draw() */
494   /* TODO: Make this have rounded corners, and maybe be a bit smaller.
495    * However, UI_draw_roundbox_aa() draws the colors too dark, so can't be used.
496    */
497   uint pos = GPU_vertformat_attr_add(
498       immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
499   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
500 
501   immUniformColor3fv(gpl->color);
502   immRecti(pos, x, y, x + w - 1, y + h - 1);
503 
504   immUnbindProgram();
505 }
506 
init_brush_icons(void)507 static void init_brush_icons(void)
508 {
509 
510 #  define INIT_BRUSH_ICON(icon_id, name) \
511     { \
512       uchar *rect = (uchar *)datatoc_##name##_png; \
513       const int size = datatoc_##name##_png_size; \
514       DrawInfo *di; \
515 \
516       di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER, 0); \
517       di->data.buffer.image->datatoc_rect = rect; \
518       di->data.buffer.image->datatoc_size = size; \
519     } \
520     ((void)0)
521   /* end INIT_BRUSH_ICON */
522 
523   const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
524 
525   INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
526   INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
527   INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
528   INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
529   INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
530   INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
531   INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
532   INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
533   INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
534   INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
535   INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
536   INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
537   INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
538   INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
539   INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
540   INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
541   INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
542   INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
543   INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
544   INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
545   INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
546   INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
547   INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
548   INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
549   INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
550   INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
551 
552   /* grease pencil sculpt */
553   INIT_BRUSH_ICON(ICON_GPBRUSH_SMOOTH, gp_brush_smooth);
554   INIT_BRUSH_ICON(ICON_GPBRUSH_THICKNESS, gp_brush_thickness);
555   INIT_BRUSH_ICON(ICON_GPBRUSH_STRENGTH, gp_brush_strength);
556   INIT_BRUSH_ICON(ICON_GPBRUSH_GRAB, gp_brush_grab);
557   INIT_BRUSH_ICON(ICON_GPBRUSH_PUSH, gp_brush_push);
558   INIT_BRUSH_ICON(ICON_GPBRUSH_TWIST, gp_brush_twist);
559   INIT_BRUSH_ICON(ICON_GPBRUSH_PINCH, gp_brush_pinch);
560   INIT_BRUSH_ICON(ICON_GPBRUSH_RANDOMIZE, gp_brush_randomize);
561   INIT_BRUSH_ICON(ICON_GPBRUSH_CLONE, gp_brush_clone);
562   INIT_BRUSH_ICON(ICON_GPBRUSH_WEIGHT, gp_brush_weight);
563 
564   /* grease pencil drawing brushes */
565   INIT_BRUSH_ICON(ICON_GPBRUSH_PENCIL, gp_brush_pencil);
566   INIT_BRUSH_ICON(ICON_GPBRUSH_PEN, gp_brush_pen);
567   INIT_BRUSH_ICON(ICON_GPBRUSH_INK, gp_brush_ink);
568   INIT_BRUSH_ICON(ICON_GPBRUSH_INKNOISE, gp_brush_inknoise);
569   INIT_BRUSH_ICON(ICON_GPBRUSH_BLOCK, gp_brush_block);
570   INIT_BRUSH_ICON(ICON_GPBRUSH_MARKER, gp_brush_marker);
571   INIT_BRUSH_ICON(ICON_GPBRUSH_FILL, gp_brush_fill);
572   INIT_BRUSH_ICON(ICON_GPBRUSH_AIRBRUSH, gp_brush_airbrush);
573   INIT_BRUSH_ICON(ICON_GPBRUSH_CHISEL, gp_brush_chisel);
574   INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_SOFT, gp_brush_erase_soft);
575   INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_HARD, gp_brush_erase_hard);
576   INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_STROKE, gp_brush_erase_stroke);
577 
578 #  undef INIT_BRUSH_ICON
579 }
580 
581 static DrawInfo *g_di_event_list = NULL;
582 
UI_icon_from_event_type(short event_type,short event_value)583 int UI_icon_from_event_type(short event_type, short event_value)
584 {
585   if (event_type == EVT_RIGHTSHIFTKEY) {
586     event_type = EVT_LEFTSHIFTKEY;
587   }
588   else if (event_type == EVT_RIGHTCTRLKEY) {
589     event_type = EVT_LEFTCTRLKEY;
590   }
591   else if (event_type == EVT_RIGHTALTKEY) {
592     event_type = EVT_LEFTALTKEY;
593   }
594   else if (event_type == EVT_TWEAK_L) {
595     event_type = LEFTMOUSE;
596     event_value = KM_CLICK_DRAG;
597   }
598   else if (event_type == EVT_TWEAK_M) {
599     event_type = MIDDLEMOUSE;
600     event_value = KM_CLICK_DRAG;
601   }
602   else if (event_type == EVT_TWEAK_R) {
603     event_type = RIGHTMOUSE;
604     event_value = KM_CLICK_DRAG;
605   }
606 
607   DrawInfo *di = g_di_event_list;
608   do {
609     if (di->data.input.event_type == event_type) {
610       return di->data.input.icon;
611     }
612   } while ((di = di->data.input.next));
613 
614   if (event_type == LEFTMOUSE) {
615     return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_LMB : ICON_MOUSE_LMB_DRAG;
616   }
617   if (event_type == MIDDLEMOUSE) {
618     return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_MMB : ICON_MOUSE_MMB_DRAG;
619   }
620   if (event_type == RIGHTMOUSE) {
621     return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_RMB : ICON_MOUSE_RMB_DRAG;
622   }
623 
624   return ICON_NONE;
625 }
626 
UI_icon_from_keymap_item(const wmKeyMapItem * kmi,int r_icon_mod[4])627 int UI_icon_from_keymap_item(const wmKeyMapItem *kmi, int r_icon_mod[4])
628 {
629   if (r_icon_mod) {
630     memset(r_icon_mod, 0x0, sizeof(int[4]));
631     int i = 0;
632     if (!ELEM(kmi->ctrl, KM_NOTHING, KM_ANY)) {
633       r_icon_mod[i++] = ICON_EVENT_CTRL;
634     }
635     if (!ELEM(kmi->alt, KM_NOTHING, KM_ANY)) {
636       r_icon_mod[i++] = ICON_EVENT_ALT;
637     }
638     if (!ELEM(kmi->shift, KM_NOTHING, KM_ANY)) {
639       r_icon_mod[i++] = ICON_EVENT_SHIFT;
640     }
641     if (!ELEM(kmi->oskey, KM_NOTHING, KM_ANY)) {
642       r_icon_mod[i++] = ICON_EVENT_OS;
643     }
644   }
645   return UI_icon_from_event_type(kmi->type, kmi->val);
646 }
647 
init_event_icons(void)648 static void init_event_icons(void)
649 {
650   DrawInfo *di_next = NULL;
651 
652 #  define INIT_EVENT_ICON(icon_id, type, value) \
653     { \
654       DrawInfo *di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_EVENT, 0); \
655       di->data.input.event_type = type; \
656       di->data.input.event_value = value; \
657       di->data.input.icon = icon_id; \
658       di->data.input.next = di_next; \
659       di_next = di; \
660     } \
661     ((void)0)
662   /* end INIT_EVENT_ICON */
663 
664   const int w = 16; /* DUMMY */
665 
666   INIT_EVENT_ICON(ICON_EVENT_A, EVT_AKEY, KM_ANY);
667   INIT_EVENT_ICON(ICON_EVENT_B, EVT_BKEY, KM_ANY);
668   INIT_EVENT_ICON(ICON_EVENT_C, EVT_CKEY, KM_ANY);
669   INIT_EVENT_ICON(ICON_EVENT_D, EVT_DKEY, KM_ANY);
670   INIT_EVENT_ICON(ICON_EVENT_E, EVT_EKEY, KM_ANY);
671   INIT_EVENT_ICON(ICON_EVENT_F, EVT_FKEY, KM_ANY);
672   INIT_EVENT_ICON(ICON_EVENT_G, EVT_GKEY, KM_ANY);
673   INIT_EVENT_ICON(ICON_EVENT_H, EVT_HKEY, KM_ANY);
674   INIT_EVENT_ICON(ICON_EVENT_I, EVT_IKEY, KM_ANY);
675   INIT_EVENT_ICON(ICON_EVENT_J, EVT_JKEY, KM_ANY);
676   INIT_EVENT_ICON(ICON_EVENT_K, EVT_KKEY, KM_ANY);
677   INIT_EVENT_ICON(ICON_EVENT_L, EVT_LKEY, KM_ANY);
678   INIT_EVENT_ICON(ICON_EVENT_M, EVT_MKEY, KM_ANY);
679   INIT_EVENT_ICON(ICON_EVENT_N, EVT_NKEY, KM_ANY);
680   INIT_EVENT_ICON(ICON_EVENT_O, EVT_OKEY, KM_ANY);
681   INIT_EVENT_ICON(ICON_EVENT_P, EVT_PKEY, KM_ANY);
682   INIT_EVENT_ICON(ICON_EVENT_Q, EVT_QKEY, KM_ANY);
683   INIT_EVENT_ICON(ICON_EVENT_R, EVT_RKEY, KM_ANY);
684   INIT_EVENT_ICON(ICON_EVENT_S, EVT_SKEY, KM_ANY);
685   INIT_EVENT_ICON(ICON_EVENT_T, EVT_TKEY, KM_ANY);
686   INIT_EVENT_ICON(ICON_EVENT_U, EVT_UKEY, KM_ANY);
687   INIT_EVENT_ICON(ICON_EVENT_V, EVT_VKEY, KM_ANY);
688   INIT_EVENT_ICON(ICON_EVENT_W, EVT_WKEY, KM_ANY);
689   INIT_EVENT_ICON(ICON_EVENT_X, EVT_XKEY, KM_ANY);
690   INIT_EVENT_ICON(ICON_EVENT_Y, EVT_YKEY, KM_ANY);
691   INIT_EVENT_ICON(ICON_EVENT_Z, EVT_ZKEY, KM_ANY);
692   INIT_EVENT_ICON(ICON_EVENT_SHIFT, EVT_LEFTSHIFTKEY, KM_ANY);
693   INIT_EVENT_ICON(ICON_EVENT_CTRL, EVT_LEFTCTRLKEY, KM_ANY);
694   INIT_EVENT_ICON(ICON_EVENT_ALT, EVT_LEFTALTKEY, KM_ANY);
695   INIT_EVENT_ICON(ICON_EVENT_OS, EVT_OSKEY, KM_ANY);
696   INIT_EVENT_ICON(ICON_EVENT_F1, EVT_F1KEY, KM_ANY);
697   INIT_EVENT_ICON(ICON_EVENT_F2, EVT_F2KEY, KM_ANY);
698   INIT_EVENT_ICON(ICON_EVENT_F3, EVT_F3KEY, KM_ANY);
699   INIT_EVENT_ICON(ICON_EVENT_F4, EVT_F4KEY, KM_ANY);
700   INIT_EVENT_ICON(ICON_EVENT_F5, EVT_F5KEY, KM_ANY);
701   INIT_EVENT_ICON(ICON_EVENT_F6, EVT_F6KEY, KM_ANY);
702   INIT_EVENT_ICON(ICON_EVENT_F7, EVT_F7KEY, KM_ANY);
703   INIT_EVENT_ICON(ICON_EVENT_F8, EVT_F8KEY, KM_ANY);
704   INIT_EVENT_ICON(ICON_EVENT_F9, EVT_F9KEY, KM_ANY);
705   INIT_EVENT_ICON(ICON_EVENT_F10, EVT_F10KEY, KM_ANY);
706   INIT_EVENT_ICON(ICON_EVENT_F11, EVT_F11KEY, KM_ANY);
707   INIT_EVENT_ICON(ICON_EVENT_F12, EVT_F12KEY, KM_ANY);
708   INIT_EVENT_ICON(ICON_EVENT_ESC, EVT_ESCKEY, KM_ANY);
709   INIT_EVENT_ICON(ICON_EVENT_TAB, EVT_TABKEY, KM_ANY);
710   INIT_EVENT_ICON(ICON_EVENT_PAGEUP, EVT_PAGEUPKEY, KM_ANY);
711   INIT_EVENT_ICON(ICON_EVENT_PAGEDOWN, EVT_PAGEDOWNKEY, KM_ANY);
712   INIT_EVENT_ICON(ICON_EVENT_RETURN, EVT_RETKEY, KM_ANY);
713   INIT_EVENT_ICON(ICON_EVENT_SPACEKEY, EVT_SPACEKEY, KM_ANY);
714 
715   g_di_event_list = di_next;
716 
717 #  undef INIT_EVENT_ICON
718 }
719 
icon_verify_datatoc(IconImage * iimg)720 static void icon_verify_datatoc(IconImage *iimg)
721 {
722   /* if it has own rect, things are all OK */
723   if (iimg->rect) {
724     return;
725   }
726 
727   if (iimg->datatoc_rect) {
728     ImBuf *bbuf = IMB_ibImageFromMemory(
729         iimg->datatoc_rect, iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
730     /* w and h were set on initialize */
731     if (bbuf->x != iimg->h && bbuf->y != iimg->w) {
732       IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
733     }
734 
735     iimg->rect = bbuf->rect;
736     bbuf->rect = NULL;
737     IMB_freeImBuf(bbuf);
738   }
739 }
740 
create_mono_icon_with_border(ImBuf * buf,int resolution_divider,float border_intensity)741 static ImBuf *create_mono_icon_with_border(ImBuf *buf,
742                                            int resolution_divider,
743                                            float border_intensity)
744 {
745   ImBuf *result = IMB_dupImBuf(buf);
746   const float border_sharpness = 16.0 / (resolution_divider * resolution_divider);
747 
748   float blurred_alpha_buffer[(ICON_GRID_W + 2 * ICON_MONO_BORDER_OUTSET) *
749                              (ICON_GRID_H + 2 * ICON_MONO_BORDER_OUTSET)];
750   const int icon_width = (ICON_GRID_W + 2 * ICON_MONO_BORDER_OUTSET) / resolution_divider;
751   const int icon_height = (ICON_GRID_W + 2 * ICON_MONO_BORDER_OUTSET) / resolution_divider;
752 
753   for (int y = 0; y < ICON_GRID_ROWS; y++) {
754     for (int x = 0; x < ICON_GRID_COLS; x++) {
755       const IconType icontype = icontypes[y * ICON_GRID_COLS + x];
756       if (icontype.type != ICON_TYPE_MONO_TEXTURE) {
757         continue;
758       }
759 
760       int sx = x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN - ICON_MONO_BORDER_OUTSET;
761       int sy = y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN - ICON_MONO_BORDER_OUTSET;
762       sx = sx / resolution_divider;
763       sy = sy / resolution_divider;
764 
765       /* blur the alpha channel and store it in blurred_alpha_buffer */
766       const int blur_size = 2 / resolution_divider;
767       for (int bx = 0; bx < icon_width; bx++) {
768         const int asx = MAX2(bx - blur_size, 0);
769         const int aex = MIN2(bx + blur_size + 1, icon_width);
770         for (int by = 0; by < icon_height; by++) {
771           const int asy = MAX2(by - blur_size, 0);
772           const int aey = MIN2(by + blur_size + 1, icon_height);
773 
774           /* blur alpha channel */
775           const int write_offset = by * (ICON_GRID_W + 2 * ICON_MONO_BORDER_OUTSET) + bx;
776           float alpha_accum = 0.0;
777           uint alpha_samples = 0;
778           for (int ax = asx; ax < aex; ax++) {
779             for (int ay = asy; ay < aey; ay++) {
780               const int offset_read = (sy + ay) * buf->x + (sx + ax);
781               const uint color_read = buf->rect[offset_read];
782               const float alpha_read = ((color_read & 0xff000000) >> 24) / 255.0;
783               alpha_accum += alpha_read;
784               alpha_samples += 1;
785             }
786           }
787           blurred_alpha_buffer[write_offset] = alpha_accum / alpha_samples;
788         }
789       }
790 
791       /* apply blurred alpha */
792       for (int bx = 0; bx < icon_width; bx++) {
793         for (int by = 0; by < icon_height; by++) {
794           const int blurred_alpha_offset = by * (ICON_GRID_W + 2 * ICON_MONO_BORDER_OUTSET) + bx;
795           const int offset_write = (sy + by) * buf->x + (sx + bx);
796           const float blurred_alpha = blurred_alpha_buffer[blurred_alpha_offset];
797           const float border_srgb[4] = {
798               0, 0, 0, MIN2(1.0, blurred_alpha * border_sharpness) * border_intensity};
799 
800           const uint color_read = buf->rect[offset_write];
801           const uchar *orig_color = (uchar *)&color_read;
802 
803           float border_rgba[4];
804           float orig_rgba[4];
805           float dest_rgba[4];
806           float dest_srgb[4];
807 
808           srgb_to_linearrgb_v4(border_rgba, border_srgb);
809           srgb_to_linearrgb_uchar4(orig_rgba, orig_color);
810           blend_color_interpolate_float(dest_rgba, orig_rgba, border_rgba, 1.0 - orig_rgba[3]);
811           linearrgb_to_srgb_v4(dest_srgb, dest_rgba);
812 
813           const uint alpha_mask = ((uint)(dest_srgb[3] * 255)) << 24;
814           const uint cpack = rgb_to_cpack(dest_srgb[0], dest_srgb[1], dest_srgb[2]) | alpha_mask;
815           result->rect[offset_write] = cpack;
816         }
817       }
818     }
819   }
820   return result;
821 }
822 
free_icons_textures(void)823 static void free_icons_textures(void)
824 {
825   if (icongltex.num_textures > 0) {
826     for (int i = 0; i < 2; i++) {
827       if (icongltex.tex[i]) {
828         GPU_texture_free(icongltex.tex[i]);
829         icongltex.tex[i] = NULL;
830       }
831     }
832     icongltex.num_textures = 0;
833   }
834 }
835 
836 /* Reload the textures for internal icons.
837  * This function will release the previous textures. */
UI_icons_reload_internal_textures(void)838 void UI_icons_reload_internal_textures(void)
839 {
840   bTheme *btheme = UI_GetTheme();
841   ImBuf *b16buf = NULL, *b32buf = NULL, *b16buf_border = NULL, *b32buf_border = NULL;
842   const float icon_border_intensity = btheme->tui.icon_border_intensity;
843   const bool need_icons_with_border = icon_border_intensity > 0.0f;
844 
845   if (b16buf == NULL) {
846     b16buf = IMB_ibImageFromMemory((const uchar *)datatoc_blender_icons16_png,
847                                    datatoc_blender_icons16_png_size,
848                                    IB_rect,
849                                    NULL,
850                                    "<blender icons>");
851   }
852   if (b16buf) {
853     if (need_icons_with_border) {
854       b16buf_border = create_mono_icon_with_border(b16buf, 2, icon_border_intensity);
855       IMB_premultiply_alpha(b16buf_border);
856     }
857     IMB_premultiply_alpha(b16buf);
858   }
859 
860   if (b32buf == NULL) {
861     b32buf = IMB_ibImageFromMemory((const uchar *)datatoc_blender_icons32_png,
862                                    datatoc_blender_icons32_png_size,
863                                    IB_rect,
864                                    NULL,
865                                    "<blender icons>");
866   }
867   if (b32buf) {
868     if (need_icons_with_border) {
869       b32buf_border = create_mono_icon_with_border(b32buf, 1, icon_border_intensity);
870       IMB_premultiply_alpha(b32buf_border);
871     }
872     IMB_premultiply_alpha(b32buf);
873   }
874 
875   if (b16buf && b32buf) {
876     /* Free existing texture if any. */
877     free_icons_textures();
878 
879     /* Allocate OpenGL texture. */
880     icongltex.num_textures = need_icons_with_border ? 2 : 1;
881 
882     /* Note the filter and LOD bias were tweaked to better preserve icon
883      * sharpness at different UI scales. */
884     if (icongltex.tex[0] == NULL) {
885       icongltex.w = b32buf->x;
886       icongltex.h = b32buf->y;
887       icongltex.invw = 1.0f / b32buf->x;
888       icongltex.invh = 1.0f / b32buf->y;
889 
890       icongltex.tex[0] = GPU_texture_create_2d("icons", b32buf->x, b32buf->y, 2, GPU_RGBA8, NULL);
891       GPU_texture_update_mipmap(icongltex.tex[0], 0, GPU_DATA_UNSIGNED_BYTE, b32buf->rect);
892       GPU_texture_update_mipmap(icongltex.tex[0], 1, GPU_DATA_UNSIGNED_BYTE, b16buf->rect);
893     }
894 
895     if (need_icons_with_border && icongltex.tex[1] == NULL) {
896       icongltex.tex[1] = GPU_texture_create_2d(
897           "icons_border", b32buf_border->x, b32buf_border->y, 2, GPU_RGBA8, NULL);
898       GPU_texture_update_mipmap(icongltex.tex[1], 0, GPU_DATA_UNSIGNED_BYTE, b32buf_border->rect);
899       GPU_texture_update_mipmap(icongltex.tex[1], 1, GPU_DATA_UNSIGNED_BYTE, b16buf_border->rect);
900     }
901   }
902 
903   IMB_freeImBuf(b16buf);
904   IMB_freeImBuf(b32buf);
905   IMB_freeImBuf(b16buf_border);
906   IMB_freeImBuf(b32buf_border);
907 }
908 
init_internal_icons(void)909 static void init_internal_icons(void)
910 {
911 #  if 0 /* temp disabled */
912   if ((btheme != NULL) && btheme->tui.iconfile[0]) {
913     char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
914     char iconfilestr[FILE_MAX];
915 
916     if (icondir) {
917       BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
918 
919       /* if the image is missing bbuf will just be NULL */
920       bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL);
921 
922       if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
923         printf(
924             "\n***WARNING***\n"
925             "Icons file '%s' too small.\n"
926             "Using built-in Icons instead\n",
927             iconfilestr);
928         IMB_freeImBuf(bbuf);
929         bbuf = NULL;
930       }
931     }
932     else {
933       printf("%s: 'icons' data path not found, continuing\n", __func__);
934     }
935   }
936 #  endif
937 
938   /* Define icons. */
939   for (int y = 0; y < ICON_GRID_ROWS; y++) {
940     /* Row W has monochrome icons. */
941     for (int x = 0; x < ICON_GRID_COLS; x++) {
942       const IconType icontype = icontypes[y * ICON_GRID_COLS + x];
943       if (!ELEM(icontype.type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
944         continue;
945       }
946 
947       def_internal_icon(NULL,
948                         BIFICONID_FIRST + y * ICON_GRID_COLS + x,
949                         x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
950                         y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
951                         ICON_GRID_W,
952                         icontype.type,
953                         icontype.theme_color);
954     }
955   }
956 
957   def_internal_vicon(ICON_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
958 
959   def_internal_vicon(ICON_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
960   def_internal_vicon(ICON_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
961   def_internal_vicon(ICON_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
962   def_internal_vicon(ICON_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
963   def_internal_vicon(ICON_KEYTYPE_MOVING_HOLD_VEC, vicon_keytype_moving_hold_draw);
964 
965   def_internal_vicon(ICON_HANDLETYPE_FREE_VEC, vicon_handletype_free_draw);
966   def_internal_vicon(ICON_HANDLETYPE_ALIGNED_VEC, vicon_handletype_aligned_draw);
967   def_internal_vicon(ICON_HANDLETYPE_VECTOR_VEC, vicon_handletype_vector_draw);
968   def_internal_vicon(ICON_HANDLETYPE_AUTO_VEC, vicon_handletype_auto_draw);
969   def_internal_vicon(ICON_HANDLETYPE_AUTO_CLAMP_VEC, vicon_handletype_auto_clamp_draw);
970 
971   def_internal_vicon(ICON_COLORSET_01_VEC, vicon_colorset_draw_01);
972   def_internal_vicon(ICON_COLORSET_02_VEC, vicon_colorset_draw_02);
973   def_internal_vicon(ICON_COLORSET_03_VEC, vicon_colorset_draw_03);
974   def_internal_vicon(ICON_COLORSET_04_VEC, vicon_colorset_draw_04);
975   def_internal_vicon(ICON_COLORSET_05_VEC, vicon_colorset_draw_05);
976   def_internal_vicon(ICON_COLORSET_06_VEC, vicon_colorset_draw_06);
977   def_internal_vicon(ICON_COLORSET_07_VEC, vicon_colorset_draw_07);
978   def_internal_vicon(ICON_COLORSET_08_VEC, vicon_colorset_draw_08);
979   def_internal_vicon(ICON_COLORSET_09_VEC, vicon_colorset_draw_09);
980   def_internal_vicon(ICON_COLORSET_10_VEC, vicon_colorset_draw_10);
981   def_internal_vicon(ICON_COLORSET_11_VEC, vicon_colorset_draw_11);
982   def_internal_vicon(ICON_COLORSET_12_VEC, vicon_colorset_draw_12);
983   def_internal_vicon(ICON_COLORSET_13_VEC, vicon_colorset_draw_13);
984   def_internal_vicon(ICON_COLORSET_14_VEC, vicon_colorset_draw_14);
985   def_internal_vicon(ICON_COLORSET_15_VEC, vicon_colorset_draw_15);
986   def_internal_vicon(ICON_COLORSET_16_VEC, vicon_colorset_draw_16);
987   def_internal_vicon(ICON_COLORSET_17_VEC, vicon_colorset_draw_17);
988   def_internal_vicon(ICON_COLORSET_18_VEC, vicon_colorset_draw_18);
989   def_internal_vicon(ICON_COLORSET_19_VEC, vicon_colorset_draw_19);
990   def_internal_vicon(ICON_COLORSET_20_VEC, vicon_colorset_draw_20);
991 
992   def_internal_vicon(ICON_COLLECTION_COLOR_01, vicon_collection_color_draw_01);
993   def_internal_vicon(ICON_COLLECTION_COLOR_02, vicon_collection_color_draw_02);
994   def_internal_vicon(ICON_COLLECTION_COLOR_03, vicon_collection_color_draw_03);
995   def_internal_vicon(ICON_COLLECTION_COLOR_04, vicon_collection_color_draw_04);
996   def_internal_vicon(ICON_COLLECTION_COLOR_05, vicon_collection_color_draw_05);
997   def_internal_vicon(ICON_COLLECTION_COLOR_06, vicon_collection_color_draw_06);
998   def_internal_vicon(ICON_COLLECTION_COLOR_07, vicon_collection_color_draw_07);
999   def_internal_vicon(ICON_COLLECTION_COLOR_08, vicon_collection_color_draw_08);
1000 }
1001 
init_iconfile_list(struct ListBase * list)1002 static void init_iconfile_list(struct ListBase *list)
1003 {
1004   BLI_listbase_clear(list);
1005   const char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
1006 
1007   if (icondir == NULL) {
1008     return;
1009   }
1010 
1011   struct direntry *dir;
1012   int totfile = BLI_filelist_dir_contents(icondir, &dir);
1013 
1014   int index = 1;
1015   for (int i = 0; i < totfile; i++) {
1016     if ((dir[i].type & S_IFREG)) {
1017       const char *filename = dir[i].relname;
1018 
1019       if (BLI_path_extension_check(filename, ".png")) {
1020         /* loading all icons on file start is overkill & slows startup
1021          * its possible they change size after blender load anyway. */
1022 #  if 0
1023         int ifilex, ifiley;
1024         char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
1025         ImBuf *bbuf = NULL;
1026         /* check to see if the image is the right size, continue if not */
1027         /* copying strings here should go ok, assuming that we never get back
1028          * a complete path to file longer than 256 chars */
1029         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
1030         bbuf = IMB_loadiffname(iconfilestr, IB_rect);
1031 
1032         if (bbuf) {
1033           ifilex = bbuf->x;
1034           ifiley = bbuf->y;
1035           IMB_freeImBuf(bbuf);
1036         }
1037         else {
1038           ifilex = ifiley = 0;
1039         }
1040 
1041         /* bad size or failed to load */
1042         if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
1043           printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
1044           continue;
1045         }
1046 #  endif /* removed */
1047 
1048         /* found a potential icon file, so make an entry for it in the cache list */
1049         IconFile *ifile = MEM_callocN(sizeof(IconFile), "IconFile");
1050 
1051         BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
1052         ifile->index = index;
1053 
1054         BLI_addtail(list, ifile);
1055 
1056         index++;
1057       }
1058     }
1059   }
1060 
1061   BLI_filelist_free(dir, totfile);
1062   dir = NULL;
1063 }
1064 
free_iconfile_list(struct ListBase * list)1065 static void free_iconfile_list(struct ListBase *list)
1066 {
1067   IconFile *ifile = NULL, *next_ifile = NULL;
1068 
1069   for (ifile = list->first; ifile; ifile = next_ifile) {
1070     next_ifile = ifile->next;
1071     BLI_freelinkN(list, ifile);
1072   }
1073 }
1074 
1075 #else
1076 
UI_icons_reload_internal_textures(void)1077 void UI_icons_reload_internal_textures(void)
1078 {
1079 }
1080 
1081 #endif /* WITH_HEADLESS */
1082 
UI_iconfile_get_index(const char * filename)1083 int UI_iconfile_get_index(const char *filename)
1084 {
1085   IconFile *ifile;
1086   ListBase *list = &(iconfilelist);
1087 
1088   for (ifile = list->first; ifile; ifile = ifile->next) {
1089     if (BLI_path_cmp(filename, ifile->filename) == 0) {
1090       return ifile->index;
1091     }
1092   }
1093 
1094   return 0;
1095 }
1096 
UI_iconfile_list(void)1097 ListBase *UI_iconfile_list(void)
1098 {
1099   ListBase *list = &(iconfilelist);
1100 
1101   return list;
1102 }
1103 
UI_icons_free(void)1104 void UI_icons_free(void)
1105 {
1106 #ifndef WITH_HEADLESS
1107   free_icons_textures();
1108   free_iconfile_list(&iconfilelist);
1109 #endif
1110   BKE_icons_free();
1111 }
1112 
UI_icons_free_drawinfo(void * drawinfo)1113 void UI_icons_free_drawinfo(void *drawinfo)
1114 {
1115   DrawInfo *di = drawinfo;
1116 
1117   if (di == NULL) {
1118     return;
1119   }
1120 
1121   if (di->type == ICON_TYPE_BUFFER) {
1122     if (di->data.buffer.image) {
1123       if (di->data.buffer.image->rect) {
1124         MEM_freeN(di->data.buffer.image->rect);
1125       }
1126       MEM_freeN(di->data.buffer.image);
1127     }
1128   }
1129   else if (di->type == ICON_TYPE_GEOM) {
1130     if (di->data.geom.image_cache) {
1131       IMB_freeImBuf(di->data.geom.image_cache);
1132     }
1133   }
1134 
1135   MEM_freeN(di);
1136 }
1137 
1138 /**
1139  * #Icon.data_type and #Icon.obj
1140  */
icon_create_drawinfo(Icon * icon)1141 static DrawInfo *icon_create_drawinfo(Icon *icon)
1142 {
1143   const int icon_data_type = icon->obj_type;
1144 
1145   DrawInfo *di = MEM_callocN(sizeof(DrawInfo), "di_icon");
1146 
1147   if (ELEM(icon_data_type, ICON_DATA_ID, ICON_DATA_PREVIEW)) {
1148     di->type = ICON_TYPE_PREVIEW;
1149   }
1150   else if (icon_data_type == ICON_DATA_GEOM) {
1151     di->type = ICON_TYPE_GEOM;
1152   }
1153   else if (icon_data_type == ICON_DATA_STUDIOLIGHT) {
1154     di->type = ICON_TYPE_BUFFER;
1155   }
1156   else if (icon_data_type == ICON_DATA_GPLAYER) {
1157     di->type = ICON_TYPE_GPLAYER;
1158   }
1159   else {
1160     BLI_assert(0);
1161   }
1162 
1163   return di;
1164 }
1165 
icon_ensure_drawinfo(Icon * icon)1166 static DrawInfo *icon_ensure_drawinfo(Icon *icon)
1167 {
1168   if (icon->drawinfo) {
1169     return icon->drawinfo;
1170   }
1171   DrawInfo *di = icon_create_drawinfo(icon);
1172   icon->drawinfo = di;
1173   icon->drawinfo_free = UI_icons_free_drawinfo;
1174   return di;
1175 }
1176 
1177 /* note!, returns unscaled by DPI */
UI_icon_get_width(int icon_id)1178 int UI_icon_get_width(int icon_id)
1179 {
1180   Icon *icon = BKE_icon_get(icon_id);
1181 
1182   if (icon == NULL) {
1183     if (G.debug & G_DEBUG) {
1184       printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1185     }
1186     return 0;
1187   }
1188 
1189   DrawInfo *di = icon_ensure_drawinfo(icon);
1190   if (di) {
1191     return ICON_DEFAULT_WIDTH;
1192   }
1193 
1194   return 0;
1195 }
1196 
UI_icon_get_height(int icon_id)1197 int UI_icon_get_height(int icon_id)
1198 {
1199   Icon *icon = BKE_icon_get(icon_id);
1200   if (icon == NULL) {
1201     if (G.debug & G_DEBUG) {
1202       printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1203     }
1204     return 0;
1205   }
1206 
1207   DrawInfo *di = icon_ensure_drawinfo(icon);
1208   if (di) {
1209     return ICON_DEFAULT_HEIGHT;
1210   }
1211 
1212   return 0;
1213 }
1214 
UI_icon_get_theme_color(int icon_id,uchar color[4])1215 bool UI_icon_get_theme_color(int icon_id, uchar color[4])
1216 {
1217   Icon *icon = BKE_icon_get(icon_id);
1218   if (icon == NULL) {
1219     return false;
1220   }
1221 
1222   DrawInfo *di = icon_ensure_drawinfo(icon);
1223   return UI_GetIconThemeColor4ubv(di->data.texture.theme_color, color);
1224 }
1225 
UI_icons_init()1226 void UI_icons_init()
1227 {
1228 #ifndef WITH_HEADLESS
1229   init_iconfile_list(&iconfilelist);
1230   UI_icons_reload_internal_textures();
1231   init_internal_icons();
1232   init_brush_icons();
1233   init_event_icons();
1234 #endif
1235 }
1236 
1237 /* Render size for preview images and icons
1238  */
UI_icon_preview_to_render_size(enum eIconSizes size)1239 int UI_icon_preview_to_render_size(enum eIconSizes size)
1240 {
1241   switch (size) {
1242     case ICON_SIZE_ICON:
1243       return ICON_RENDER_DEFAULT_HEIGHT;
1244     case ICON_SIZE_PREVIEW:
1245       return PREVIEW_RENDER_DEFAULT_HEIGHT;
1246     default:
1247       return 0;
1248   }
1249 }
1250 
1251 /* Create rect for the icon
1252  */
icon_create_rect(struct PreviewImage * prv_img,enum eIconSizes size)1253 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
1254 {
1255   const uint render_size = UI_icon_preview_to_render_size(size);
1256 
1257   if (!prv_img) {
1258     if (G.debug & G_DEBUG) {
1259       printf("%s, error: requested preview image does not exist", __func__);
1260     }
1261   }
1262   else if (!prv_img->rect[size]) {
1263     prv_img->w[size] = render_size;
1264     prv_img->h[size] = render_size;
1265     prv_img->flag[size] |= PRV_CHANGED;
1266     prv_img->changed_timestamp[size] = 0;
1267     prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(uint), "prv_rect");
1268   }
1269 }
1270 
1271 static void ui_id_preview_image_render_size(
1272     const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job);
1273 
ui_studiolight_icon_job_exec(void * customdata,short * UNUSED (stop),short * UNUSED (do_update),float * UNUSED (progress))1274 static void ui_studiolight_icon_job_exec(void *customdata,
1275                                          short *UNUSED(stop),
1276                                          short *UNUSED(do_update),
1277                                          float *UNUSED(progress))
1278 {
1279   Icon **tmp = (Icon **)customdata;
1280   Icon *icon = *tmp;
1281   DrawInfo *di = icon_ensure_drawinfo(icon);
1282   StudioLight *sl = icon->obj;
1283   BKE_studiolight_preview(di->data.buffer.image->rect, sl, icon->id_type);
1284 }
1285 
ui_studiolight_kill_icon_preview_job(wmWindowManager * wm,int icon_id)1286 static void ui_studiolight_kill_icon_preview_job(wmWindowManager *wm, int icon_id)
1287 {
1288   Icon *icon = BKE_icon_get(icon_id);
1289   WM_jobs_kill_type(wm, icon, WM_JOB_TYPE_STUDIOLIGHT);
1290   icon->obj = NULL;
1291 }
1292 
ui_studiolight_free_function(StudioLight * sl,void * data)1293 static void ui_studiolight_free_function(StudioLight *sl, void *data)
1294 {
1295   wmWindowManager *wm = data;
1296 
1297   /* Happens if job was canceled or already finished. */
1298   if (wm == NULL) {
1299     return;
1300   }
1301 
1302   /* get icons_id, get icons and kill wm jobs */
1303   if (sl->icon_id_radiance) {
1304     ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_radiance);
1305   }
1306   if (sl->icon_id_irradiance) {
1307     ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_irradiance);
1308   }
1309   if (sl->icon_id_matcap) {
1310     ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap);
1311   }
1312   if (sl->icon_id_matcap_flipped) {
1313     ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap_flipped);
1314   }
1315 }
1316 
ui_studiolight_icon_job_end(void * customdata)1317 static void ui_studiolight_icon_job_end(void *customdata)
1318 {
1319   Icon **tmp = (Icon **)customdata;
1320   Icon *icon = *tmp;
1321   StudioLight *sl = icon->obj;
1322   BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, NULL);
1323 }
1324 
ui_icon_ensure_deferred(const bContext * C,const int icon_id,const bool big)1325 void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
1326 {
1327   Icon *icon = BKE_icon_get(icon_id);
1328 
1329   if (icon == NULL) {
1330     return;
1331   }
1332 
1333   DrawInfo *di = icon_ensure_drawinfo(icon);
1334 
1335   if (di == NULL) {
1336     return;
1337   }
1338 
1339   switch (di->type) {
1340     case ICON_TYPE_PREVIEW: {
1341       ID *id = (icon->id_type != 0) ? icon->obj : NULL;
1342       PreviewImage *prv = id ? BKE_previewimg_id_ensure(id) : icon->obj;
1343       /* Using jobs for screen previews crashes due to offscreen rendering.
1344        * XXX would be nicer if PreviewImage could store if it supports jobs */
1345       const bool use_jobs = !id || (GS(id->name) != ID_SCR);
1346 
1347       if (prv) {
1348         const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
1349 
1350         if (id || (prv->tag & PRV_TAG_DEFFERED) != 0) {
1351           ui_id_preview_image_render_size(C, NULL, id, prv, size, use_jobs);
1352         }
1353       }
1354       break;
1355     }
1356     case ICON_TYPE_BUFFER: {
1357       if (icon->obj_type == ICON_DATA_STUDIOLIGHT) {
1358         if (di->data.buffer.image == NULL) {
1359           wmWindowManager *wm = CTX_wm_manager(C);
1360           StudioLight *sl = icon->obj;
1361           BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, wm);
1362           IconImage *img = MEM_mallocN(sizeof(IconImage), __func__);
1363 
1364           img->w = STUDIOLIGHT_ICON_SIZE;
1365           img->h = STUDIOLIGHT_ICON_SIZE;
1366           const size_t size = STUDIOLIGHT_ICON_SIZE * STUDIOLIGHT_ICON_SIZE * sizeof(uint);
1367           img->rect = MEM_mallocN(size, __func__);
1368           memset(img->rect, 0, size);
1369           di->data.buffer.image = img;
1370 
1371           wmJob *wm_job = WM_jobs_get(
1372               wm, CTX_wm_window(C), icon, "StudioLight Icon", 0, WM_JOB_TYPE_STUDIOLIGHT);
1373           Icon **tmp = MEM_callocN(sizeof(Icon *), __func__);
1374           *tmp = icon;
1375           WM_jobs_customdata_set(wm_job, tmp, MEM_freeN);
1376           WM_jobs_timer(wm_job, 0.01, 0, NC_WINDOW);
1377           WM_jobs_callbacks(
1378               wm_job, ui_studiolight_icon_job_exec, NULL, NULL, ui_studiolight_icon_job_end);
1379           WM_jobs_start(CTX_wm_manager(C), wm_job);
1380         }
1381       }
1382       break;
1383     }
1384   }
1385 }
1386 
1387 /* only called when icon has changed */
1388 /* only call with valid pointer from UI_icon_draw */
icon_set_image(const bContext * C,Scene * scene,ID * id,PreviewImage * prv_img,enum eIconSizes size,const bool use_job)1389 static void icon_set_image(const bContext *C,
1390                            Scene *scene,
1391                            ID *id,
1392                            PreviewImage *prv_img,
1393                            enum eIconSizes size,
1394                            const bool use_job)
1395 {
1396   if (!prv_img) {
1397     if (G.debug & G_DEBUG) {
1398       printf("%s: no preview image for this ID: %s\n", __func__, id->name);
1399     }
1400     return;
1401   }
1402 
1403   if (prv_img->flag[size] & PRV_USER_EDITED) {
1404     /* user-edited preview, do not auto-update! */
1405     return;
1406   }
1407 
1408   const bool delay = prv_img->rect[size] != NULL;
1409   icon_create_rect(prv_img, size);
1410 
1411   if (use_job) {
1412     /* Job (background) version */
1413     ED_preview_icon_job(
1414         C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size], delay);
1415   }
1416   else {
1417     if (!scene) {
1418       scene = CTX_data_scene(C);
1419     }
1420     /* Immediate version */
1421     ED_preview_icon_render(
1422         CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1423   }
1424 }
1425 
UI_icon_to_preview(int icon_id)1426 PreviewImage *UI_icon_to_preview(int icon_id)
1427 {
1428   Icon *icon = BKE_icon_get(icon_id);
1429 
1430   if (icon == NULL) {
1431     return NULL;
1432   }
1433 
1434   DrawInfo *di = (DrawInfo *)icon->drawinfo;
1435 
1436   if (di == NULL) {
1437     return NULL;
1438   }
1439 
1440   if (di->type == ICON_TYPE_PREVIEW) {
1441     PreviewImage *prv = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) :
1442                                                icon->obj;
1443 
1444     if (prv) {
1445       return BKE_previewimg_copy(prv);
1446     }
1447   }
1448   else if (di->data.buffer.image) {
1449     ImBuf *bbuf;
1450 
1451     bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect,
1452                                  di->data.buffer.image->datatoc_size,
1453                                  IB_rect,
1454                                  NULL,
1455                                  __func__);
1456     if (bbuf) {
1457       PreviewImage *prv = BKE_previewimg_create();
1458 
1459       prv->rect[0] = bbuf->rect;
1460 
1461       prv->w[0] = bbuf->x;
1462       prv->h[0] = bbuf->y;
1463 
1464       bbuf->rect = NULL;
1465       IMB_freeImBuf(bbuf);
1466 
1467       return prv;
1468     }
1469   }
1470 
1471   return NULL;
1472 }
1473 
icon_draw_rect(float x,float y,int w,int h,float UNUSED (aspect),int rw,int rh,uint * rect,float alpha,const float desaturate)1474 static void icon_draw_rect(float x,
1475                            float y,
1476                            int w,
1477                            int h,
1478                            float UNUSED(aspect),
1479                            int rw,
1480                            int rh,
1481                            uint *rect,
1482                            float alpha,
1483                            const float desaturate)
1484 {
1485   ImBuf *ima = NULL;
1486   int draw_w = w;
1487   int draw_h = h;
1488   int draw_x = x;
1489   int draw_y = y;
1490 
1491   /* sanity check */
1492   if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
1493     printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
1494     BLI_assert(!"invalid icon size");
1495     return;
1496   }
1497   /* modulate color */
1498   const float col[4] = {alpha, alpha, alpha, alpha};
1499 
1500   /* rect contains image in 'rendersize', we only scale if needed */
1501   if (rw != w || rh != h) {
1502     /* preserve aspect ratio and center */
1503     if (rw > rh) {
1504       draw_w = w;
1505       draw_h = (int)(((float)rh / (float)rw) * (float)w);
1506       draw_y += (h - draw_h) / 2;
1507     }
1508     else if (rw < rh) {
1509       draw_w = (int)(((float)rw / (float)rh) * (float)h);
1510       draw_h = h;
1511       draw_x += (w - draw_w) / 2;
1512     }
1513     /* if the image is squared, the draw_ initialization values are good */
1514 
1515     /* first allocate imbuf for scaling and copy preview into it */
1516     ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1517     memcpy(ima->rect, rect, rw * rh * sizeof(uint));
1518     IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
1519     rect = ima->rect;
1520   }
1521 
1522   /* draw */
1523   eGPUBuiltinShader shader;
1524   if (desaturate != 0.0f) {
1525     shader = GPU_SHADER_2D_IMAGE_DESATURATE_COLOR;
1526   }
1527   else {
1528     shader = GPU_SHADER_2D_IMAGE_COLOR;
1529   }
1530   IMMDrawPixelsTexState state = immDrawPixelsTexSetup(shader);
1531 
1532   if (shader == GPU_SHADER_2D_IMAGE_DESATURATE_COLOR) {
1533     immUniform1f("factor", desaturate);
1534   }
1535 
1536   immDrawPixelsTex(
1537       &state, draw_x, draw_y, draw_w, draw_h, GPU_RGBA8, false, rect, 1.0f, 1.0f, col);
1538 
1539   if (ima) {
1540     IMB_freeImBuf(ima);
1541   }
1542 }
1543 
1544 /* High enough to make a difference, low enough so that
1545  * small draws are still efficient with the use of glUniform.
1546  * NOTE TODO: We could use UBO but we would need some triple
1547  * buffer system + persistent mapping for this to be more
1548  * efficient than simple glUniform calls. */
1549 #define ICON_DRAW_CACHE_SIZE 16
1550 
1551 typedef struct IconDrawCall {
1552   rctf pos;
1553   rctf tex;
1554   float color[4];
1555 } IconDrawCall;
1556 
1557 typedef struct IconTextureDrawCall {
1558   IconDrawCall drawcall_cache[ICON_DRAW_CACHE_SIZE];
1559   int calls; /* Number of calls batched together */
1560 } IconTextureDrawCall;
1561 
1562 static struct {
1563   IconTextureDrawCall normal;
1564   IconTextureDrawCall border;
1565   bool enabled;
1566 } g_icon_draw_cache = {{{{{0}}}}};
1567 
UI_icon_draw_cache_begin(void)1568 void UI_icon_draw_cache_begin(void)
1569 {
1570   BLI_assert(g_icon_draw_cache.enabled == false);
1571   g_icon_draw_cache.enabled = true;
1572 }
1573 
icon_draw_cache_texture_flush_ex(GPUTexture * texture,IconTextureDrawCall * texture_draw_calls)1574 static void icon_draw_cache_texture_flush_ex(GPUTexture *texture,
1575                                              IconTextureDrawCall *texture_draw_calls)
1576 {
1577   if (texture_draw_calls->calls == 0) {
1578     return;
1579   }
1580 
1581   GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR);
1582   GPU_shader_bind(shader);
1583 
1584   const int img_binding = GPU_shader_get_texture_binding(shader, "image");
1585   const int data_loc = GPU_shader_get_uniform(shader, "calls_data");
1586 
1587   GPU_texture_bind_ex(texture, GPU_SAMPLER_ICON, img_binding, false);
1588   GPU_shader_uniform_vector(
1589       shader, data_loc, 4, ICON_DRAW_CACHE_SIZE * 3, (float *)texture_draw_calls->drawcall_cache);
1590 
1591   GPUBatch *quad = GPU_batch_preset_quad();
1592   GPU_batch_set_shader(quad, shader);
1593   GPU_batch_draw_instanced(quad, texture_draw_calls->calls);
1594 
1595   GPU_texture_unbind(texture);
1596 
1597   texture_draw_calls->calls = 0;
1598 }
1599 
icon_draw_cache_flush_ex(bool only_full_caches)1600 static void icon_draw_cache_flush_ex(bool only_full_caches)
1601 {
1602   bool should_draw = false;
1603   if (only_full_caches) {
1604     should_draw = g_icon_draw_cache.normal.calls == ICON_DRAW_CACHE_SIZE ||
1605                   g_icon_draw_cache.border.calls == ICON_DRAW_CACHE_SIZE;
1606   }
1607   else {
1608     should_draw = g_icon_draw_cache.normal.calls || g_icon_draw_cache.border.calls;
1609   }
1610 
1611   if (should_draw) {
1612     /* We need to flush widget base first to ensure correct ordering. */
1613     UI_widgetbase_draw_cache_flush();
1614 
1615     GPU_blend(GPU_BLEND_ALPHA_PREMULT);
1616 
1617     if (!only_full_caches || g_icon_draw_cache.normal.calls == ICON_DRAW_CACHE_SIZE) {
1618       icon_draw_cache_texture_flush_ex(icongltex.tex[0], &g_icon_draw_cache.normal);
1619     }
1620 
1621     if (!only_full_caches || g_icon_draw_cache.border.calls == ICON_DRAW_CACHE_SIZE) {
1622       icon_draw_cache_texture_flush_ex(icongltex.tex[1], &g_icon_draw_cache.border);
1623     }
1624 
1625     GPU_blend(GPU_BLEND_ALPHA);
1626   }
1627 }
1628 
UI_icon_draw_cache_end(void)1629 void UI_icon_draw_cache_end(void)
1630 {
1631   BLI_assert(g_icon_draw_cache.enabled == true);
1632   g_icon_draw_cache.enabled = false;
1633 
1634   /* Don't change blend state if it's not needed. */
1635   if (g_icon_draw_cache.border.calls == 0 && g_icon_draw_cache.normal.calls == 0) {
1636     return;
1637   }
1638 
1639   GPU_blend(GPU_BLEND_ALPHA);
1640   icon_draw_cache_flush_ex(false);
1641   GPU_blend(GPU_BLEND_NONE);
1642 }
1643 
icon_draw_texture_cached(float x,float y,float w,float h,int ix,int iy,int UNUSED (iw),int ih,float alpha,const float rgb[3],bool with_border)1644 static void icon_draw_texture_cached(float x,
1645                                      float y,
1646                                      float w,
1647                                      float h,
1648                                      int ix,
1649                                      int iy,
1650                                      int UNUSED(iw),
1651                                      int ih,
1652                                      float alpha,
1653                                      const float rgb[3],
1654                                      bool with_border)
1655 {
1656 
1657   float mvp[4][4];
1658   GPU_matrix_model_view_projection_get(mvp);
1659 
1660   IconTextureDrawCall *texture_call = with_border ? &g_icon_draw_cache.border :
1661                                                     &g_icon_draw_cache.normal;
1662 
1663   IconDrawCall *call = &texture_call->drawcall_cache[texture_call->calls];
1664   texture_call->calls++;
1665 
1666   /* Manual mat4*vec2 */
1667   call->pos.xmin = x * mvp[0][0] + y * mvp[1][0] + mvp[3][0];
1668   call->pos.ymin = x * mvp[0][1] + y * mvp[1][1] + mvp[3][1];
1669   call->pos.xmax = call->pos.xmin + w * mvp[0][0] + h * mvp[1][0];
1670   call->pos.ymax = call->pos.ymin + w * mvp[0][1] + h * mvp[1][1];
1671 
1672   call->tex.xmin = ix * icongltex.invw;
1673   call->tex.xmax = (ix + ih) * icongltex.invw;
1674   call->tex.ymin = iy * icongltex.invh;
1675   call->tex.ymax = (iy + ih) * icongltex.invh;
1676 
1677   if (rgb) {
1678     copy_v4_fl4(call->color, rgb[0], rgb[1], rgb[2], alpha);
1679   }
1680   else {
1681     copy_v4_fl(call->color, alpha);
1682   }
1683 
1684   if (texture_call->calls == ICON_DRAW_CACHE_SIZE) {
1685     icon_draw_cache_flush_ex(true);
1686   }
1687 }
1688 
icon_draw_texture(float x,float y,float w,float h,int ix,int iy,int iw,int ih,float alpha,const float rgb[3],bool with_border)1689 static void icon_draw_texture(float x,
1690                               float y,
1691                               float w,
1692                               float h,
1693                               int ix,
1694                               int iy,
1695                               int iw,
1696                               int ih,
1697                               float alpha,
1698                               const float rgb[3],
1699                               bool with_border)
1700 {
1701   if (g_icon_draw_cache.enabled) {
1702     icon_draw_texture_cached(x, y, w, h, ix, iy, iw, ih, alpha, rgb, with_border);
1703     return;
1704   }
1705 
1706   /* We need to flush widget base first to ensure correct ordering. */
1707   UI_widgetbase_draw_cache_flush();
1708 
1709   GPU_blend(GPU_BLEND_ALPHA_PREMULT);
1710 
1711   float x1 = ix * icongltex.invw;
1712   float x2 = (ix + ih) * icongltex.invw;
1713   float y1 = iy * icongltex.invh;
1714   float y2 = (iy + ih) * icongltex.invh;
1715 
1716   GPUTexture *texture = with_border ? icongltex.tex[1] : icongltex.tex[0];
1717 
1718   GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_RECT_COLOR);
1719   GPU_shader_bind(shader);
1720 
1721   const int img_binding = GPU_shader_get_texture_binding(shader, "image");
1722   const int color_loc = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR);
1723   const int rect_tex_loc = GPU_shader_get_uniform(shader, "rect_icon");
1724   const int rect_geom_loc = GPU_shader_get_uniform(shader, "rect_geom");
1725 
1726   if (rgb) {
1727     GPU_shader_uniform_vector(shader, color_loc, 4, 1, (float[4]){UNPACK3(rgb), alpha});
1728   }
1729   else {
1730     GPU_shader_uniform_vector(shader, color_loc, 4, 1, (float[4]){alpha, alpha, alpha, alpha});
1731   }
1732 
1733   GPU_shader_uniform_vector(shader, rect_tex_loc, 4, 1, (float[4]){x1, y1, x2, y2});
1734   GPU_shader_uniform_vector(shader, rect_geom_loc, 4, 1, (float[4]){x, y, x + w, y + h});
1735 
1736   GPU_texture_bind_ex(texture, GPU_SAMPLER_ICON, img_binding, false);
1737 
1738   GPUBatch *quad = GPU_batch_preset_quad();
1739   GPU_batch_set_shader(quad, shader);
1740   GPU_batch_draw(quad);
1741 
1742   GPU_texture_unbind(texture);
1743 
1744   GPU_blend(GPU_BLEND_ALPHA);
1745 }
1746 
1747 /* Drawing size for preview images */
get_draw_size(enum eIconSizes size)1748 static int get_draw_size(enum eIconSizes size)
1749 {
1750   switch (size) {
1751     case ICON_SIZE_ICON:
1752       return ICON_DEFAULT_HEIGHT;
1753     case ICON_SIZE_PREVIEW:
1754       return PREVIEW_DEFAULT_HEIGHT;
1755     default:
1756       return 0;
1757   }
1758 }
1759 
icon_draw_size(float x,float y,int icon_id,float aspect,float alpha,enum eIconSizes size,int draw_size,const float desaturate,const uchar mono_rgba[4],const bool mono_border)1760 static void icon_draw_size(float x,
1761                            float y,
1762                            int icon_id,
1763                            float aspect,
1764                            float alpha,
1765                            enum eIconSizes size,
1766                            int draw_size,
1767                            const float desaturate,
1768                            const uchar mono_rgba[4],
1769                            const bool mono_border)
1770 {
1771   bTheme *btheme = UI_GetTheme();
1772   const float fdraw_size = (float)draw_size;
1773 
1774   Icon *icon = BKE_icon_get(icon_id);
1775   alpha *= btheme->tui.icon_alpha;
1776 
1777   if (icon == NULL) {
1778     if (G.debug & G_DEBUG) {
1779       printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1780     }
1781     return;
1782   }
1783 
1784   /* scale width and height according to aspect */
1785   int w = (int)(fdraw_size / aspect + 0.5f);
1786   int h = (int)(fdraw_size / aspect + 0.5f);
1787 
1788   DrawInfo *di = icon_ensure_drawinfo(icon);
1789 
1790   /* We need to flush widget base first to ensure correct ordering. */
1791   UI_widgetbase_draw_cache_flush();
1792 
1793   if (di->type == ICON_TYPE_VECTOR) {
1794     /* vector icons use the uiBlock transformation, they are not drawn
1795      * with untransformed coordinates like the other icons */
1796     di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1797   }
1798   else if (di->type == ICON_TYPE_GEOM) {
1799 #ifdef USE_UI_TOOLBAR_HACK
1800     /* TODO(campbell): scale icons up for toolbar,
1801      * we need a way to detect larger buttons and do this automatic. */
1802     {
1803       float scale = (float)ICON_DEFAULT_HEIGHT_TOOLBAR / (float)ICON_DEFAULT_HEIGHT;
1804       y = (y + (h / 2)) - ((h * scale) / 2);
1805       w *= scale;
1806       h *= scale;
1807     }
1808 #endif
1809 
1810     /* If the theme is light, we will adjust the icon colors. */
1811     const bool invert = (rgb_to_grayscale_byte(btheme->tui.wcol_toolbar_item.inner) > 128);
1812     const bool geom_inverted = di->data.geom.inverted;
1813 
1814     /* This could re-generate often if rendered at different sizes in the one interface.
1815      * TODO(campbell): support caching multiple sizes. */
1816     ImBuf *ibuf = di->data.geom.image_cache;
1817     if ((ibuf == NULL) || (ibuf->x != w) || (ibuf->y != h) || (invert != geom_inverted)) {
1818       if (ibuf) {
1819         IMB_freeImBuf(ibuf);
1820       }
1821       if (invert != geom_inverted) {
1822         BKE_icon_geom_invert_lightness(icon->obj);
1823       }
1824       ibuf = BKE_icon_geom_rasterize(icon->obj, w, h);
1825       di->data.geom.image_cache = ibuf;
1826       di->data.geom.inverted = invert;
1827     }
1828 
1829     GPU_blend(GPU_BLEND_ALPHA_PREMULT);
1830     icon_draw_rect(x, y, w, h, aspect, w, h, ibuf->rect, alpha, desaturate);
1831     GPU_blend(GPU_BLEND_ALPHA);
1832   }
1833   else if (di->type == ICON_TYPE_EVENT) {
1834     const short event_type = di->data.input.event_type;
1835     const short event_value = di->data.input.event_value;
1836     icon_draw_rect_input(x, y, w, h, alpha, event_type, event_value);
1837   }
1838   else if (di->type == ICON_TYPE_COLOR_TEXTURE) {
1839     /* texture image use premul alpha for correct scaling */
1840     icon_draw_texture(x,
1841                       y,
1842                       (float)w,
1843                       (float)h,
1844                       di->data.texture.x,
1845                       di->data.texture.y,
1846                       di->data.texture.w,
1847                       di->data.texture.h,
1848                       alpha,
1849                       NULL,
1850                       false);
1851   }
1852   else if (di->type == ICON_TYPE_MONO_TEXTURE) {
1853     /* Monochrome icon that uses text or theme color. */
1854     const bool with_border = mono_border && (btheme->tui.icon_border_intensity > 0.0f);
1855     float color[4];
1856     if (mono_rgba) {
1857       rgba_uchar_to_float(color, (const uchar *)mono_rgba);
1858     }
1859     else {
1860       UI_GetThemeColor4fv(TH_TEXT, color);
1861     }
1862 
1863     mul_v4_fl(color, alpha);
1864 
1865     float border_outset = 0.0;
1866     uint border_texel = 0;
1867 #ifndef WITH_HEADLESS
1868     if (with_border) {
1869       const float scale = (float)ICON_GRID_W / (float)ICON_DEFAULT_WIDTH;
1870       border_texel = ICON_MONO_BORDER_OUTSET;
1871       border_outset = ICON_MONO_BORDER_OUTSET / (scale * aspect);
1872     }
1873 #endif
1874     icon_draw_texture(x - border_outset,
1875                       y - border_outset,
1876                       (float)w + 2 * border_outset,
1877                       (float)h + 2 * border_outset,
1878                       di->data.texture.x - border_texel,
1879                       di->data.texture.y - border_texel,
1880                       di->data.texture.w + 2 * border_texel,
1881                       di->data.texture.h + 2 * border_texel,
1882                       color[3],
1883                       color,
1884                       with_border);
1885   }
1886 
1887   else if (di->type == ICON_TYPE_BUFFER) {
1888     /* it is a builtin icon */
1889     IconImage *iimg = di->data.buffer.image;
1890 #ifndef WITH_HEADLESS
1891     icon_verify_datatoc(iimg);
1892 #endif
1893     if (!iimg->rect) {
1894       /* something has gone wrong! */
1895       return;
1896     }
1897 
1898     icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, desaturate);
1899   }
1900   else if (di->type == ICON_TYPE_PREVIEW) {
1901     PreviewImage *pi = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) :
1902                                               icon->obj;
1903 
1904     if (pi) {
1905       /* no create icon on this level in code */
1906       if (!pi->rect[size]) {
1907         /* Something has gone wrong! */
1908         return;
1909       }
1910 
1911       /* Preview images use premultiplied alpha. */
1912       GPU_blend(GPU_BLEND_ALPHA_PREMULT);
1913       icon_draw_rect(
1914           x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, desaturate);
1915       GPU_blend(GPU_BLEND_ALPHA);
1916     }
1917   }
1918   else if (di->type == ICON_TYPE_GPLAYER) {
1919     BLI_assert(icon->obj != NULL);
1920 
1921     /* Just draw a colored rect - Like for vicon_colorset_draw() */
1922 #ifndef WITH_HEADLESS
1923     vicon_gplayer_color_draw(icon, (int)x, (int)y, w, h);
1924 #endif
1925   }
1926 }
1927 
ui_id_preview_image_render_size(const bContext * C,Scene * scene,ID * id,PreviewImage * pi,int size,const bool use_job)1928 static void ui_id_preview_image_render_size(
1929     const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1930 {
1931   /* changed only ever set by dynamic icons */
1932   if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) {
1933     /* create the rect if necessary */
1934     icon_set_image(C, scene, id, pi, size, use_job);
1935 
1936     pi->flag[size] &= ~PRV_CHANGED;
1937   }
1938 }
1939 
UI_icon_render_id(const bContext * C,Scene * scene,ID * id,const bool big,const bool use_job)1940 void UI_icon_render_id(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1941 {
1942   PreviewImage *pi = BKE_previewimg_id_ensure(id);
1943 
1944   if (pi == NULL) {
1945     return;
1946   }
1947 
1948   if (big) {
1949     /* bigger preview size */
1950     ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);
1951   }
1952   else {
1953     /* icon size */
1954     ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);
1955   }
1956 }
1957 
ui_id_icon_render(const bContext * C,ID * id,bool use_jobs)1958 static void ui_id_icon_render(const bContext *C, ID *id, bool use_jobs)
1959 {
1960   PreviewImage *pi = BKE_previewimg_id_ensure(id);
1961 
1962   if (!pi) {
1963     return;
1964   }
1965 
1966   for (enum eIconSizes i = 0; i < NUM_ICON_SIZES; i++) {
1967     /* check if rect needs to be created; changed
1968      * only set by dynamic icons */
1969     if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
1970       icon_set_image(C, NULL, id, pi, i, use_jobs);
1971       pi->flag[i] &= ~PRV_CHANGED;
1972     }
1973   }
1974 }
1975 
ui_id_brush_get_icon(const bContext * C,ID * id)1976 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1977 {
1978   Brush *br = (Brush *)id;
1979 
1980   if (br->flag & BRUSH_CUSTOM_ICON) {
1981     BKE_icon_id_ensure(id);
1982     ui_id_icon_render(C, id, true);
1983   }
1984   else {
1985     Object *ob = CTX_data_active_object(C);
1986     const EnumPropertyItem *items = NULL;
1987     ePaintMode paint_mode = PAINT_MODE_INVALID;
1988     ScrArea *area = CTX_wm_area(C);
1989     char space_type = area->spacetype;
1990     /* Fallback to 3D view. */
1991     if (space_type == SPACE_PROPERTIES) {
1992       space_type = SPACE_VIEW3D;
1993     }
1994 
1995     /* XXX: this is not nice, should probably make brushes
1996      * be strictly in one paint mode only to avoid
1997      * checking various context stuff here */
1998 
1999     if ((space_type == SPACE_VIEW3D) && ob) {
2000       if (ob->mode & OB_MODE_SCULPT) {
2001         paint_mode = PAINT_MODE_SCULPT;
2002       }
2003       else if (ob->mode & OB_MODE_VERTEX_PAINT) {
2004         paint_mode = PAINT_MODE_VERTEX;
2005       }
2006       else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2007         paint_mode = PAINT_MODE_WEIGHT;
2008       }
2009       else if (ob->mode & OB_MODE_TEXTURE_PAINT) {
2010         paint_mode = PAINT_MODE_TEXTURE_3D;
2011       }
2012     }
2013     else if (space_type == SPACE_IMAGE) {
2014       if (area->spacetype == space_type) {
2015         const SpaceImage *sima = area->spacedata.first;
2016         if (sima->mode == SI_MODE_PAINT) {
2017           paint_mode = PAINT_MODE_TEXTURE_2D;
2018         }
2019       }
2020     }
2021 
2022     /* reset the icon */
2023     if ((ob != NULL) &&
2024         (ob->mode & (OB_MODE_PAINT_GPENCIL | OB_MODE_VERTEX_GPENCIL | OB_MODE_SCULPT_GPENCIL |
2025                      OB_MODE_WEIGHT_GPENCIL)) &&
2026         (br->gpencil_settings != NULL)) {
2027       switch (br->gpencil_settings->icon_id) {
2028         case GP_BRUSH_ICON_PENCIL:
2029           br->id.icon_id = ICON_GPBRUSH_PENCIL;
2030           break;
2031         case GP_BRUSH_ICON_PEN:
2032           br->id.icon_id = ICON_GPBRUSH_PEN;
2033           break;
2034         case GP_BRUSH_ICON_INK:
2035           br->id.icon_id = ICON_GPBRUSH_INK;
2036           break;
2037         case GP_BRUSH_ICON_INKNOISE:
2038           br->id.icon_id = ICON_GPBRUSH_INKNOISE;
2039           break;
2040         case GP_BRUSH_ICON_BLOCK:
2041           br->id.icon_id = ICON_GPBRUSH_BLOCK;
2042           break;
2043         case GP_BRUSH_ICON_MARKER:
2044           br->id.icon_id = ICON_GPBRUSH_MARKER;
2045           break;
2046         case GP_BRUSH_ICON_FILL:
2047           br->id.icon_id = ICON_GPBRUSH_FILL;
2048           break;
2049         case GP_BRUSH_ICON_AIRBRUSH:
2050           br->id.icon_id = ICON_GPBRUSH_AIRBRUSH;
2051           break;
2052         case GP_BRUSH_ICON_CHISEL:
2053           br->id.icon_id = ICON_GPBRUSH_CHISEL;
2054           break;
2055         case GP_BRUSH_ICON_ERASE_SOFT:
2056           br->id.icon_id = ICON_GPBRUSH_ERASE_SOFT;
2057           break;
2058         case GP_BRUSH_ICON_ERASE_HARD:
2059           br->id.icon_id = ICON_GPBRUSH_ERASE_HARD;
2060           break;
2061         case GP_BRUSH_ICON_ERASE_STROKE:
2062           br->id.icon_id = ICON_GPBRUSH_ERASE_STROKE;
2063           break;
2064         case GP_BRUSH_ICON_TINT:
2065           br->id.icon_id = ICON_BRUSH_TEXDRAW;
2066           break;
2067         case GP_BRUSH_ICON_VERTEX_DRAW:
2068           br->id.icon_id = ICON_BRUSH_MIX;
2069           break;
2070         case GP_BRUSH_ICON_VERTEX_BLUR:
2071           br->id.icon_id = ICON_BRUSH_BLUR;
2072           break;
2073         case GP_BRUSH_ICON_VERTEX_AVERAGE:
2074           br->id.icon_id = ICON_BRUSH_BLUR;
2075           break;
2076         case GP_BRUSH_ICON_VERTEX_SMEAR:
2077           br->id.icon_id = ICON_BRUSH_BLUR;
2078           break;
2079         case GP_BRUSH_ICON_VERTEX_REPLACE:
2080           br->id.icon_id = ICON_BRUSH_MIX;
2081           break;
2082         case GP_BRUSH_ICON_GPBRUSH_SMOOTH:
2083           br->id.icon_id = ICON_GPBRUSH_SMOOTH;
2084           break;
2085         case GP_BRUSH_ICON_GPBRUSH_THICKNESS:
2086           br->id.icon_id = ICON_GPBRUSH_THICKNESS;
2087           break;
2088         case GP_BRUSH_ICON_GPBRUSH_STRENGTH:
2089           br->id.icon_id = ICON_GPBRUSH_STRENGTH;
2090           break;
2091         case GP_BRUSH_ICON_GPBRUSH_RANDOMIZE:
2092           br->id.icon_id = ICON_GPBRUSH_RANDOMIZE;
2093           break;
2094         case GP_BRUSH_ICON_GPBRUSH_GRAB:
2095           br->id.icon_id = ICON_GPBRUSH_GRAB;
2096           break;
2097         case GP_BRUSH_ICON_GPBRUSH_PUSH:
2098           br->id.icon_id = ICON_GPBRUSH_PUSH;
2099           break;
2100         case GP_BRUSH_ICON_GPBRUSH_TWIST:
2101           br->id.icon_id = ICON_GPBRUSH_TWIST;
2102           break;
2103         case GP_BRUSH_ICON_GPBRUSH_PINCH:
2104           br->id.icon_id = ICON_GPBRUSH_PINCH;
2105           break;
2106         case GP_BRUSH_ICON_GPBRUSH_CLONE:
2107           br->id.icon_id = ICON_GPBRUSH_CLONE;
2108           break;
2109         case GP_BRUSH_ICON_GPBRUSH_WEIGHT:
2110           br->id.icon_id = ICON_GPBRUSH_WEIGHT;
2111           break;
2112         default:
2113           br->id.icon_id = ICON_GPBRUSH_PEN;
2114           break;
2115       }
2116       return id->icon_id;
2117     }
2118 
2119     if (paint_mode != PAINT_MODE_INVALID) {
2120       items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
2121       const uint tool_offset = BKE_paint_get_brush_tool_offset_from_paintmode(paint_mode);
2122       const int tool_type = *(char *)POINTER_OFFSET(br, tool_offset);
2123       if (!items || !RNA_enum_icon_from_value(items, tool_type, &id->icon_id)) {
2124         id->icon_id = 0;
2125       }
2126     }
2127     else {
2128       id->icon_id = 0;
2129     }
2130   }
2131 
2132   return id->icon_id;
2133 }
2134 
ui_id_screen_get_icon(const bContext * C,ID * id)2135 static int ui_id_screen_get_icon(const bContext *C, ID *id)
2136 {
2137   BKE_icon_id_ensure(id);
2138   /* Don't use jobs here, offscreen rendering doesn't like this and crashes. */
2139   ui_id_icon_render(C, id, false);
2140 
2141   return id->icon_id;
2142 }
2143 
ui_id_icon_get(const bContext * C,ID * id,const bool big)2144 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
2145 {
2146   int iconid = 0;
2147 
2148   /* icon */
2149   switch (GS(id->name)) {
2150     case ID_BR:
2151       iconid = ui_id_brush_get_icon(C, id);
2152       break;
2153     case ID_MA: /* fall through */
2154     case ID_TE: /* fall through */
2155     case ID_IM: /* fall through */
2156     case ID_WO: /* fall through */
2157     case ID_LA: /* fall through */
2158       iconid = BKE_icon_id_ensure(id);
2159       /* checks if not exists, or changed */
2160       UI_icon_render_id(C, NULL, id, big, true);
2161       break;
2162     case ID_SCR:
2163       iconid = ui_id_screen_get_icon(C, id);
2164       break;
2165     case ID_GR:
2166       iconid = UI_icon_color_from_collection((Collection *)id);
2167       break;
2168     default:
2169       break;
2170   }
2171 
2172   return iconid;
2173 }
2174 
UI_icon_from_library(const ID * id)2175 int UI_icon_from_library(const ID *id)
2176 {
2177   if (ID_IS_LINKED(id)) {
2178     if (id->tag & LIB_TAG_MISSING) {
2179       return ICON_LIBRARY_DATA_BROKEN;
2180     }
2181     if (id->tag & LIB_TAG_INDIRECT) {
2182       return ICON_LIBRARY_DATA_INDIRECT;
2183     }
2184     return ICON_LIBRARY_DATA_DIRECT;
2185   }
2186   if (ID_IS_OVERRIDE_LIBRARY(id)) {
2187     return ICON_LIBRARY_DATA_OVERRIDE;
2188   }
2189 
2190   return ICON_NONE;
2191 }
2192 
UI_icon_from_rnaptr(bContext * C,PointerRNA * ptr,int rnaicon,const bool big)2193 int UI_icon_from_rnaptr(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
2194 {
2195   ID *id = NULL;
2196 
2197   if (!ptr->data) {
2198     return rnaicon;
2199   }
2200 
2201   /* try ID, material, texture or dynapaint slot */
2202   if (RNA_struct_is_ID(ptr->type)) {
2203     id = ptr->owner_id;
2204   }
2205   else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
2206     id = RNA_pointer_get(ptr, "material").data;
2207   }
2208   else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
2209     id = RNA_pointer_get(ptr, "texture").data;
2210   }
2211   else if (RNA_struct_is_a(ptr->type, &RNA_FileBrowserFSMenuEntry)) {
2212     return RNA_int_get(ptr, "icon");
2213   }
2214   else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
2215     DynamicPaintSurface *surface = ptr->data;
2216 
2217     if (surface->format == MOD_DPAINT_SURFACE_F_PTEX) {
2218       return ICON_SHADING_TEXTURE;
2219     }
2220     if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) {
2221       return ICON_OUTLINER_DATA_MESH;
2222     }
2223     if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ) {
2224       return ICON_FILE_IMAGE;
2225     }
2226   }
2227   else if (RNA_struct_is_a(ptr->type, &RNA_StudioLight)) {
2228     StudioLight *sl = ptr->data;
2229     switch (sl->flag & STUDIOLIGHT_FLAG_ORIENTATIONS) {
2230       case STUDIOLIGHT_TYPE_STUDIO:
2231         return sl->icon_id_irradiance;
2232       case STUDIOLIGHT_TYPE_WORLD:
2233       default:
2234         return sl->icon_id_radiance;
2235       case STUDIOLIGHT_TYPE_MATCAP:
2236         return sl->icon_id_matcap;
2237     }
2238   }
2239 
2240   /* get icon from ID */
2241   if (id) {
2242     const int icon = ui_id_icon_get(C, id, big);
2243 
2244     return icon ? icon : rnaicon;
2245   }
2246 
2247   return rnaicon;
2248 }
2249 
UI_icon_from_idcode(const int idcode)2250 int UI_icon_from_idcode(const int idcode)
2251 {
2252   switch ((ID_Type)idcode) {
2253     case ID_AC:
2254       return ICON_ACTION;
2255     case ID_AR:
2256       return ICON_ARMATURE_DATA;
2257     case ID_BR:
2258       return ICON_BRUSH_DATA;
2259     case ID_CA:
2260       return ICON_CAMERA_DATA;
2261     case ID_CF:
2262       return ICON_FILE;
2263     case ID_CU:
2264       return ICON_CURVE_DATA;
2265     case ID_GD:
2266       return ICON_OUTLINER_DATA_GREASEPENCIL;
2267     case ID_GR:
2268       return ICON_OUTLINER_COLLECTION;
2269     case ID_IM:
2270       return ICON_IMAGE_DATA;
2271     case ID_LA:
2272       return ICON_LIGHT_DATA;
2273     case ID_LS:
2274       return ICON_LINE_DATA;
2275     case ID_LT:
2276       return ICON_LATTICE_DATA;
2277     case ID_MA:
2278       return ICON_MATERIAL_DATA;
2279     case ID_MB:
2280       return ICON_META_DATA;
2281     case ID_MC:
2282       return ICON_TRACKER;
2283     case ID_ME:
2284       return ICON_MESH_DATA;
2285     case ID_MSK:
2286       return ICON_MOD_MASK; /* TODO! this would need its own icon! */
2287     case ID_NT:
2288       return ICON_NODETREE;
2289     case ID_OB:
2290       return ICON_OBJECT_DATA;
2291     case ID_PA:
2292       return ICON_PARTICLE_DATA;
2293     case ID_PAL:
2294       return ICON_COLOR; /* TODO! this would need its own icon! */
2295     case ID_PC:
2296       return ICON_CURVE_BEZCURVE; /* TODO! this would need its own icon! */
2297     case ID_LP:
2298       return ICON_OUTLINER_DATA_LIGHTPROBE;
2299     case ID_SCE:
2300       return ICON_SCENE_DATA;
2301     case ID_SPK:
2302       return ICON_SPEAKER;
2303     case ID_SO:
2304       return ICON_SOUND;
2305     case ID_TE:
2306       return ICON_TEXTURE_DATA;
2307     case ID_TXT:
2308       return ICON_TEXT;
2309     case ID_VF:
2310       return ICON_FONT_DATA;
2311     case ID_HA:
2312       return ICON_HAIR_DATA;
2313     case ID_PT:
2314       return ICON_POINTCLOUD_DATA;
2315     case ID_VO:
2316       return ICON_VOLUME_DATA;
2317     case ID_WO:
2318       return ICON_WORLD_DATA;
2319     case ID_WS:
2320       return ICON_WORKSPACE;
2321     case ID_SIM:
2322       /* TODO: Use correct icon. */
2323       return ICON_PHYSICS;
2324 
2325     /* No icons for these ID-types. */
2326     case ID_LI:
2327     case ID_IP:
2328     case ID_KE:
2329     case ID_SCR:
2330     case ID_WM:
2331       break;
2332   }
2333   return ICON_NONE;
2334 }
2335 
UI_icon_from_object_mode(const int mode)2336 int UI_icon_from_object_mode(const int mode)
2337 {
2338   switch ((eObjectMode)mode) {
2339     case OB_MODE_OBJECT:
2340       return ICON_OBJECT_DATAMODE;
2341     case OB_MODE_EDIT:
2342     case OB_MODE_EDIT_GPENCIL:
2343       return ICON_EDITMODE_HLT;
2344     case OB_MODE_SCULPT:
2345     case OB_MODE_SCULPT_GPENCIL:
2346       return ICON_SCULPTMODE_HLT;
2347     case OB_MODE_VERTEX_PAINT:
2348     case OB_MODE_VERTEX_GPENCIL:
2349       return ICON_VPAINT_HLT;
2350     case OB_MODE_WEIGHT_PAINT:
2351     case OB_MODE_WEIGHT_GPENCIL:
2352       return ICON_WPAINT_HLT;
2353     case OB_MODE_TEXTURE_PAINT:
2354       return ICON_TPAINT_HLT;
2355     case OB_MODE_PARTICLE_EDIT:
2356       return ICON_PARTICLEMODE;
2357     case OB_MODE_POSE:
2358       return ICON_POSE_HLT;
2359     case OB_MODE_PAINT_GPENCIL:
2360       return ICON_GREASEPENCIL;
2361   }
2362   return ICON_NONE;
2363 }
2364 
UI_icon_color_from_collection(const Collection * collection)2365 int UI_icon_color_from_collection(const Collection *collection)
2366 {
2367   int icon = ICON_OUTLINER_COLLECTION;
2368 
2369   if (collection->color_tag != COLLECTION_COLOR_NONE) {
2370     icon = ICON_COLLECTION_COLOR_01 + collection->color_tag;
2371   }
2372 
2373   return icon;
2374 }
2375 
2376 /* draws icon with dpi scale factor */
UI_icon_draw(float x,float y,int icon_id)2377 void UI_icon_draw(float x, float y, int icon_id)
2378 {
2379   UI_icon_draw_ex(x, y, icon_id, U.inv_dpi_fac, 1.0f, 0.0f, NULL, false);
2380 }
2381 
UI_icon_draw_alpha(float x,float y,int icon_id,float alpha)2382 void UI_icon_draw_alpha(float x, float y, int icon_id, float alpha)
2383 {
2384   UI_icon_draw_ex(x, y, icon_id, U.inv_dpi_fac, alpha, 0.0f, NULL, false);
2385 }
2386 
UI_icon_draw_preview(float x,float y,int icon_id,float aspect,float alpha,int size)2387 void UI_icon_draw_preview(float x, float y, int icon_id, float aspect, float alpha, int size)
2388 {
2389   icon_draw_size(x, y, icon_id, aspect, alpha, ICON_SIZE_PREVIEW, size, false, NULL, false);
2390 }
2391 
UI_icon_draw_ex(float x,float y,int icon_id,float aspect,float alpha,float desaturate,const uchar mono_color[4],const bool mono_border)2392 void UI_icon_draw_ex(float x,
2393                      float y,
2394                      int icon_id,
2395                      float aspect,
2396                      float alpha,
2397                      float desaturate,
2398                      const uchar mono_color[4],
2399                      const bool mono_border)
2400 {
2401   const int draw_size = get_draw_size(ICON_SIZE_ICON);
2402   icon_draw_size(x,
2403                  y,
2404                  icon_id,
2405                  aspect,
2406                  alpha,
2407                  ICON_SIZE_ICON,
2408                  draw_size,
2409                  desaturate,
2410                  mono_color,
2411                  mono_border);
2412 }
2413 
2414 /* ********** Alert Icons ********** */
2415 
UI_icon_alert_imbuf_get(eAlertIcon icon)2416 ImBuf *UI_icon_alert_imbuf_get(eAlertIcon icon)
2417 {
2418 #ifdef WITH_HEADLESS
2419   return NULL;
2420 #else
2421   const int ALERT_IMG_SIZE = 256;
2422   icon = MIN2(icon, ALERT_ICON_MAX - 1);
2423   const int left = icon * ALERT_IMG_SIZE;
2424   const rcti crop = {left, left + ALERT_IMG_SIZE - 1, 0, ALERT_IMG_SIZE - 1};
2425   ImBuf *ibuf = IMB_ibImageFromMemory((const uchar *)datatoc_alert_icons_png,
2426                                       datatoc_alert_icons_png_size,
2427                                       IB_rect,
2428                                       NULL,
2429                                       "alert_icon");
2430   IMB_rect_crop(ibuf, &crop);
2431   IMB_premultiply_alpha(ibuf);
2432   return ibuf;
2433 #endif
2434 }
2435