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