Fix for bug #26590, `Texture preview fails when path to custom brush
[blender-staging.git] / source / blender / editors / render / render_preview.c
1 /* 
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/editors/render/render_preview.c
31  *  \ingroup edrend
32  */
33
34
35 /* global includes */
36
37 #include <stdlib.h>
38 #include <math.h>
39 #include <string.h>
40
41 #ifndef WIN32
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif   
46 #include "MEM_guardedalloc.h"
47
48 #include "BLO_readfile.h" 
49
50 #include "BLI_math.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_threads.h"
53 #include "BLI_utildefines.h"
54
55 #include "DNA_world_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_node_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_lamp_types.h"
61 #include "DNA_space_types.h"
62 #include "DNA_view3d_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_brush_types.h"
65 #include "DNA_screen_types.h"
66
67 #include "BKE_context.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
70 #include "BKE_idprop.h"
71 #include "BKE_image.h"
72 #include "BKE_icons.h"
73 #include "BKE_library.h"
74 #include "BKE_main.h"
75 #include "BKE_material.h"
76 #include "BKE_node.h"
77 #include "BKE_texture.h"
78
79 #include "IMB_imbuf.h"
80 #include "IMB_imbuf_types.h"
81
82 #include "BIF_gl.h"
83 #include "BIF_glutil.h"
84
85 #include "PIL_time.h"
86
87 #include "RE_pipeline.h"
88
89
90 #include "WM_api.h"
91 #include "WM_types.h"
92
93 #include "ED_render.h"
94 #include "ED_view3d.h"
95
96 #include "UI_interface.h"
97
98 #include "render_intern.h"
99
100 #define PR_XMIN         10
101 #define PR_YMIN         5
102 #define PR_XMAX         200
103 #define PR_YMAX         195
104
105 /* XXX */
106 static int qtest(void) {return 0;}
107 /* XXX */
108
109 ImBuf* get_brush_icon(Brush *brush)
110 {
111         static const int flags = IB_rect|IB_multilayer|IB_metadata;
112
113         char path[240];
114         char *folder;
115
116         if (!(brush->icon_imbuf)) {
117                 if (brush->flag & BRUSH_CUSTOM_ICON) {
118
119                         if (brush->icon_filepath[0]) {
120                                 // first use the path directly to try and load the file
121
122                                 BLI_strncpy(path, brush->icon_filepath, sizeof(brush->icon_filepath));
123                                 BLI_path_abs(path, G.main->name);
124
125                                 brush->icon_imbuf= IMB_loadiffname(path, flags);
126
127                                 // otherwise lets try to find it in other directories
128                                 if (!(brush->icon_imbuf)) {
129                                         folder= BLI_get_folder(BLENDER_DATAFILES, "brushicons");
130
131                                         path[0]= 0;
132
133                                         BLI_make_file_string(G.main->name, path, folder, brush->icon_filepath);
134
135                                         if (path[0])
136                                                 brush->icon_imbuf= IMB_loadiffname(path, flags);
137                                 }
138
139                                 if (brush->icon_imbuf)
140                                         BKE_icon_changed(BKE_icon_getid(&brush->id));
141                         }
142                 }
143         }
144
145         if (!(brush->icon_imbuf))
146                 brush->id.icon_id = 0;
147
148         return brush->icon_imbuf;
149 }
150
151 typedef struct ShaderPreview {
152         /* from wmJob */
153         void *owner;
154         short *stop, *do_update;
155         
156         Scene *scene;
157         ID *id;
158         ID *parent;
159         MTex *slot;
160         
161         /* node materials/texture need full copy during preview render, glsl uses it too */
162         Material *matcopy;
163         Tex *texcopy;
164         
165         float col[4];           /* active object color */
166         
167         int sizex, sizey;
168         unsigned int *pr_rect;
169         int pr_method;
170         
171 } ShaderPreview;
172
173
174
175 /* unused now */
176 void draw_tex_crop(Tex *tex)
177 {
178         rcti rct;
179         int ret= 0;
180         
181         if(tex==NULL) return;
182         
183         if(tex->type==TEX_IMAGE) {
184                 if(tex->cropxmin==0.0f) ret++;
185                 if(tex->cropymin==0.0f) ret++;
186                 if(tex->cropxmax==1.0f) ret++;
187                 if(tex->cropymax==1.0f) ret++;
188                 if(ret==4) return;
189                 
190                 rct.xmin= PR_XMIN+2+tex->cropxmin*(PR_XMAX-PR_XMIN-4);
191                 rct.xmax= PR_XMIN+2+tex->cropxmax*(PR_XMAX-PR_XMIN-4);
192                 rct.ymin= PR_YMIN+2+tex->cropymin*(PR_YMAX-PR_YMIN-4);
193                 rct.ymax= PR_YMIN+2+tex->cropymax*(PR_YMAX-PR_YMIN-4);
194
195                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
196
197                 glColor3ub(0, 0, 0);
198                 glRecti(rct.xmin+1,  rct.ymin-1,  rct.xmax+1,  rct.ymax-1); 
199
200                 glColor3ub(255, 255, 255);
201                 glRecti(rct.xmin,  rct.ymin,  rct.xmax,  rct.ymax);
202
203                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                      
204         }
205         
206 }
207
208 /* temporal abuse; if id_code is -1 it only does texture.... solve! */
209 void BIF_preview_changed(short UNUSED(id_code))
210 {
211 #if 0   
212         ScrArea *sa;
213         
214         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
215                 if(sa->spacetype==SPACE_BUTS) {
216                         SpaceButs *sbuts= sa->spacedata.first;
217                         if(sbuts->mainb==CONTEXT_SHADING) {
218                                 int tab= sbuts->tab[CONTEXT_SHADING];
219                                 if(tab==TAB_SHADING_MAT && (id_code==ID_MA || id_code==ID_TE)) {
220                                         if (sbuts->ri) sbuts->ri->curtile= 0;
221                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
222                                 }
223                                 else if(tab==TAB_SHADING_TEX && (id_code==ID_TE || id_code==-1)) {
224                                         if (sbuts->ri) sbuts->ri->curtile= 0;
225                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
226                                 }
227                                 else if(tab==TAB_SHADING_LAMP && (id_code==ID_LA || id_code==ID_TE)) {
228                                         if (sbuts->ri) sbuts->ri->curtile= 0;
229                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
230                                 }
231                                 else if(tab==TAB_SHADING_WORLD && (id_code==ID_WO || id_code==ID_TE)) {
232                                         if (sbuts->ri) sbuts->ri->curtile= 0;
233                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
234                                 }
235                         }
236                         else if (sbuts->ri) 
237                                 sbuts->ri->curtile= 0;  /* ensure changes always result in re-render when context is restored */
238                 }
239                 else if(sa->spacetype==SPACE_NODE) {
240                         SpaceNode *snode= sa->spacedata.first;
241                         if(snode->treetype==NTREE_SHADER && (id_code==ID_MA || id_code==ID_TE)) {
242                                 snode_tag_dirty(snode);
243                         }
244                 }
245                 else if(sa->spacetype==SPACE_VIEW3D) {
246                         View3D *vd= sa->spacedata.first;
247                         /* if is has a renderinfo, we consider that reason for signalling */
248                         if (vd->ri) {
249                                 vd->ri->curtile= 0;
250                                 addafterqueue(sa->win, RENDERPREVIEW, 1);
251                         }
252                 }
253         }
254
255         if(ELEM4(id_code, ID_MA, ID_TE, ID_LA, ID_WO)) {
256                 Object *ob;
257                 Material *ma;
258
259                 if(id_code == ID_WO) {
260                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
261                                 if(ma->gpumaterial.first) {
262                                         GPU_material_free(ma);
263                                 }
264                         }
265                 }
266                 else if(id_code == ID_LA) {
267                         for(ob=G.main->object.first; ob; ob=ob->id.next) {
268                                 if(ob->gpulamp.first) {
269                                         GPU_lamp_free(ob);
270                                 }
271                         }
272
273                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
274                                 if(ma->gpumaterial.first) {
275                                         GPU_material_free(ma);
276                                 }
277                         }
278                 } else if(OBACT) {
279                         Object *ob = OBACT;
280
281                         ma= give_current_material(ob, ob->actcol);
282                         if(ma && ma->gpumaterial.first) {
283                                 GPU_material_free(ma);
284                         }
285                 }
286         }
287 #endif
288 }
289
290 /* *************************** Preview for buttons *********************** */
291
292 static Main *pr_main= NULL;
293
294 void ED_preview_init_dbase(void)
295 {
296         BlendFileData *bfd;
297         extern int datatoc_preview_blend_size;
298         extern char datatoc_preview_blend[];
299         const int fileflags= G.fileflags;
300         
301         G.fileflags |= G_FILE_NO_UI;
302         bfd= BLO_read_from_memory(datatoc_preview_blend, datatoc_preview_blend_size, NULL);
303         if (bfd) {
304                 pr_main= bfd->main;
305                 
306                 MEM_freeN(bfd);
307         }
308         G.fileflags= fileflags;
309 }
310
311 void ED_preview_free_dbase(void)
312 {
313         if(pr_main)
314                 free_main(pr_main);
315 }
316
317 static Object *find_object(ListBase *lb, const char *name)
318 {
319         Object *ob;
320         for(ob= lb->first; ob; ob= ob->id.next)
321                 if(strcmp(ob->id.name+2, name)==0)
322                         break;
323         return ob;
324 }
325
326 static int preview_mat_has_sss(Material *mat, bNodeTree *ntree)
327 {
328         if(mat) {
329                 if(mat->sss_flag & MA_DIFF_SSS)
330                         return 1;
331                 if(mat->nodetree)
332                         if( preview_mat_has_sss(NULL, mat->nodetree))
333                                 return 1;
334         }
335         else if(ntree) {
336                 bNode *node;
337                 for(node= ntree->nodes.first; node; node= node->next) {
338                         if(node->type==NODE_GROUP && node->id) {
339                                 if( preview_mat_has_sss(NULL, (bNodeTree *)node->id))
340                                         return 1;
341                         }
342                         else if(node->id && ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
343                                 mat= (Material *)node->id;
344                                 if(mat->sss_flag & MA_DIFF_SSS)
345                                         return 1;
346                         }
347                 }
348         }
349         return 0;
350 }
351
352 /* call this with a pointer to initialize preview scene */
353 /* call this with NULL to restore assigned ID pointers in preview scene */
354 static Scene *preview_prepare_scene(Scene *scene, ID *id, int id_type, ShaderPreview *sp)
355 {
356         Scene *sce;
357         Base *base;
358         
359         if(pr_main==NULL) return NULL;
360         
361         sce= pr_main->scene.first;
362         if(sce) {
363                 
364                 /* this flag tells render to not execute depsgraph or ipos etc */
365                 sce->r.scemode |= R_PREVIEWBUTS;
366                 /* set world always back, is used now */
367                 sce->world= pr_main->world.first;
368                 /* now: exposure copy */
369                 if(scene->world) {
370                         sce->world->exp= scene->world->exp;
371                         sce->world->range= scene->world->range;
372                 }
373                 
374                 sce->r.color_mgt_flag = scene->r.color_mgt_flag;
375                 
376                 /* prevent overhead for small renders and icons (32) */
377                 if(id && sp->sizex < 40)
378                         sce->r.xparts= sce->r.yparts= 1;
379                 else
380                         sce->r.xparts= sce->r.yparts= 4;
381                 
382                 /* exception: don't color manage texture previews or icons */
383                 if((id && sp->pr_method==PR_ICON_RENDER) || id_type == ID_TE)
384                         sce->r.color_mgt_flag &= ~R_COLOR_MANAGEMENT;
385                 
386                 if((id && sp->pr_method==PR_ICON_RENDER) && id_type != ID_WO)
387                         sce->r.alphamode= R_ALPHAPREMUL;
388                 else
389                         sce->r.alphamode= R_ADDSKY;
390
391                 sce->r.cfra= scene->r.cfra;
392                 strcpy(sce->r.engine, scene->r.engine);
393                 
394                 if(id_type==ID_MA) {
395                         Material *mat= NULL, *origmat= (Material *)id;
396                         
397                         if(id) {
398                                 /* work on a copy */
399                                 mat= localize_material(origmat);
400                                 sp->matcopy= mat;
401                                 BLI_addtail(&pr_main->mat, mat);
402                                 
403                                 init_render_material(mat, 0, NULL);             /* call that retrieves mode_l */
404                                 end_render_material(mat);
405                                 
406                                 /* un-useful option */
407                                 if(sp->pr_method==PR_ICON_RENDER)
408                                         mat->shade_flag &= ~MA_OBCOLOR;
409
410                                 /* turn on raytracing if needed */
411                                 if(mat->mode_l & MA_RAYMIRROR)
412                                         sce->r.mode |= R_RAYTRACE;
413                                 if(mat->material_type == MA_TYPE_VOLUME)
414                                         sce->r.mode |= R_RAYTRACE;
415                                 if((mat->mode_l & MA_RAYTRANSP) && (mat->mode_l & MA_TRANSP))
416                                         sce->r.mode |= R_RAYTRACE;
417                                 if(preview_mat_has_sss(mat, NULL))
418                                         sce->r.mode |= R_SSS;
419                                 
420                                 /* turn off fake shadows if needed */
421                                 /* this only works in a specific case where the preview.blend contains
422                                  * an object starting with 'c' which has a material linked to it (not the obdata)
423                                  * and that material has a fake shadow texture in the active texture slot */
424                                 for(base= sce->base.first; base; base= base->next) {
425                                         if(base->object->id.name[2]=='c') {
426                                                 Material *shadmat= give_current_material(base->object, base->object->actcol);
427                                                 if(shadmat) {
428                                                         if (mat->mode & MA_SHADBUF) shadmat->septex = 0;
429                                                         else shadmat->septex |= 1;
430                                                 }
431                                         }
432                                 }
433                                 
434                                 /* turn off bounce lights for volume, 
435                                  * doesn't make much visual difference and slows it down too */
436                                 if(mat->material_type == MA_TYPE_VOLUME) {
437                                         for(base= sce->base.first; base; base= base->next) {
438                                                 if(base->object->type == OB_LAMP) {
439                                                         /* if doesn't match 'Lamp.002' --> main key light */
440                                                         if( strcmp(base->object->id.name+2, "Lamp.002") != 0 ) {
441                                                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
442                                                         }
443                                                 }
444                                         }
445                                 }
446
447                                 
448                                 if(sp->pr_method==PR_ICON_RENDER) {
449                                         if (mat->material_type == MA_TYPE_HALO) {
450                                                 sce->lay= 1<<MA_FLAT;
451                                         } 
452                                         else {
453                                                 sce->lay= 1<<MA_SPHERE_A;
454                                         }
455                                 }
456                                 else {
457                                         sce->lay= 1<<mat->pr_type;
458                                         if(mat->nodetree && sp->pr_method==PR_NODE_RENDER) {
459                                                 /* two previews, they get copied by wmJob */
460                                                 ntreeInitPreview(mat->nodetree, sp->sizex, sp->sizey);
461                                                 ntreeInitPreview(origmat->nodetree, sp->sizex, sp->sizey);
462                                         }
463                                 }
464                         }
465                         else {
466                                 sce->r.mode &= ~(R_OSA|R_RAYTRACE|R_SSS);
467                                 
468                         }
469                         
470                         for(base= sce->base.first; base; base= base->next) {
471                                 if(base->object->id.name[2]=='p') {
472                                         /* copy over object color, in case material uses it */
473                                         copy_v4_v4(base->object->col, sp->col);
474                                         
475                                         if(ELEM4(base->object->type, OB_MESH, OB_CURVE, OB_SURF, OB_MBALL)) {
476                                                 /* don't use assign_material, it changed mat->id.us, which shows in the UI */
477                                                 Material ***matar= give_matarar(base->object);
478                                                 int actcol= MAX2(base->object->actcol > 0, 1) - 1;
479
480                                                 if(matar && actcol < base->object->totcol)
481                                                         (*matar)[actcol]= mat;
482                                         } else if (base->object->type == OB_LAMP) {
483                                                 base->object->restrictflag &= ~OB_RESTRICT_RENDER;
484                                         }
485                                 }
486                         }
487                 }
488                 else if(id_type==ID_TE) {
489                         Tex *tex= NULL, *origtex= (Tex *)id;
490                         
491                         if(origtex) {
492                                 tex= localize_texture(origtex);
493                                 sp->texcopy= tex;
494                                 BLI_addtail(&pr_main->tex, tex);
495                         }                       
496                         sce->lay= 1<<MA_TEXTURE;
497                         
498                         for(base= sce->base.first; base; base= base->next) {
499                                 if(base->object->id.name[2]=='t') {
500                                         Material *mat= give_current_material(base->object, base->object->actcol);
501                                         if(mat && mat->mtex[0]) {
502                                                 mat->mtex[0]->tex= tex;
503                                                 
504                                                 if(tex && sp->slot)
505                                                         mat->mtex[0]->which_output = sp->slot->which_output;
506                                                 
507                                                 /* show alpha in this case */
508                                                 if(tex==NULL || (tex->flag & TEX_PRV_ALPHA)) {
509                                                         mat->mtex[0]->mapto |= MAP_ALPHA;
510                                                         mat->alpha= 0.0f;
511                                                 }
512                                                 else {
513                                                         mat->mtex[0]->mapto &= ~MAP_ALPHA;
514                                                         mat->alpha= 1.0f;
515                                                 }
516                                         }
517                                 }
518                         }
519
520                         if(tex && tex->nodetree && sp->pr_method==PR_NODE_RENDER) {
521                                 /* two previews, they get copied by wmJob */
522                                 ntreeInitPreview(origtex->nodetree, sp->sizex, sp->sizey);
523                                 ntreeInitPreview(tex->nodetree, sp->sizex, sp->sizey);
524                         }
525                 }
526                 else if(id_type==ID_LA) {
527                         Lamp *la= (Lamp *)id;
528                         
529                         if(la && la->type==LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
530                                 sce->lay= 1<<MA_ATMOS;
531                                 sce->world= scene->world;
532                                 sce->camera= (Object *)find_object(&pr_main->object, "CameraAtmo");
533                         }
534                         else {
535                                 sce->lay= 1<<MA_LAMP;
536                                 sce->world= NULL;
537                                 sce->camera= (Object *)find_object(&pr_main->object, "Camera");
538                         }
539                         sce->r.mode &= ~R_SHADOW;
540                         
541                         for(base= sce->base.first; base; base= base->next) {
542                                 if(base->object->id.name[2]=='p') {
543                                         if(base->object->type==OB_LAMP)
544                                                 base->object->data= la;
545                                 }
546                         }
547                 }
548                 else if(id_type==ID_WO) {
549                         sce->lay= 1<<MA_SKY;
550                         sce->world= (World *)id;
551                 }
552                 
553                 return sce;
554         }
555         
556         return NULL;
557 }
558
559 /* new UI convention: draw is in pixel space already. */
560 /* uses ROUNDBOX button in block to get the rect */
561 static int ed_preview_draw_rect(ScrArea *sa, Scene *sce, ID *id, int split, int first, rcti *rect, rcti *newrect)
562 {
563         Render *re;
564         RenderResult rres;
565         char name[32];
566         int do_gamma_correct=0;
567         int offx=0, newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
568
569         if (id && GS(id->name) != ID_TE) {
570                 /* exception: don't color manage texture previews - show the raw values */
571                 if (sce) do_gamma_correct = sce->r.color_mgt_flag & R_COLOR_MANAGEMENT;
572         }
573
574         if(!split || first) sprintf(name, "Preview %p", (void *)sa);
575         else sprintf(name, "SecondPreview %p", (void *)sa);
576
577         if(split) {
578                 if(first) {
579                         offx= 0;
580                         newx= newx/2;
581                 }
582                 else {
583                         offx= newx/2;
584                         newx= newx - newx/2;
585                 }
586         }
587
588         re= RE_GetRender(name);
589         RE_AcquireResultImage(re, &rres);
590
591         if(rres.rectf) {
592                 
593                 if(ABS(rres.rectx-newx)<2 && ABS(rres.recty-newy)<2) {
594                         newrect->xmax= MAX2(newrect->xmax, rect->xmin + rres.rectx + offx);
595                         newrect->ymax= MAX2(newrect->ymax, rect->ymin + rres.recty);
596
597                         glaDrawPixelsSafe_to32(rect->xmin+offx, rect->ymin, rres.rectx, rres.recty, rres.rectx, rres.rectf, do_gamma_correct);
598
599                         RE_ReleaseResultImage(re);
600                         return 1;
601                 }
602         }
603
604         RE_ReleaseResultImage(re);
605         return 0;
606 }
607
608 void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, rcti *rect)
609 {
610         if(idp) {
611                 ScrArea *sa= CTX_wm_area(C);
612                 Scene *sce = CTX_data_scene(C);
613                 ID *id = (ID *)idp;
614                 ID *parent= (ID *)parentp;
615                 MTex *slot= (MTex *)slotp;
616                 SpaceButs *sbuts= sa->spacedata.first;
617                 rcti newrect;
618                 int ok;
619                 int newx= rect->xmax-rect->xmin, newy= rect->ymax-rect->ymin;
620
621                 newrect.xmin= rect->xmin;
622                 newrect.xmax= rect->xmin;
623                 newrect.ymin= rect->ymin;
624                 newrect.ymax= rect->ymin;
625
626                 if(parent) {
627                         ok = ed_preview_draw_rect(sa, sce, id, 1, 1, rect, &newrect);
628                         ok &= ed_preview_draw_rect(sa, sce, parent, 1, 0, rect, &newrect);
629                 }
630                 else
631                         ok = ed_preview_draw_rect(sa, sce, id, 0, 0, rect, &newrect);
632
633                 if(ok)
634                         *rect= newrect;
635
636                 /* check for spacetype... */
637                 if(sbuts->spacetype==SPACE_BUTS && sbuts->preview) {
638                         sbuts->preview= 0;
639                         ok= 0;
640                 }
641         
642                 if(ok==0) {
643                         ED_preview_shader_job(C, sa, id, parent, slot, newx, newy, PR_BUTS_RENDER);
644                 }
645         }       
646 }
647
648 /* ******************************** Icon Preview **************************** */
649
650 void ED_preview_icon_draw(const bContext *UNUSED(C), void *UNUSED(idp), void *UNUSED(arg1), void *UNUSED(arg2), rcti *UNUSED(rect))
651 {
652 }
653
654 /* *************************** Preview for 3d window *********************** */
655
656 void view3d_previewrender_progress(RenderResult *rr, volatile rcti *renrect)
657 {
658 //      ScrArea *sa= NULL; // XXX
659 //      View3D *v3d= NULL; // XXX
660         RenderLayer *rl;
661         int ofsx=0, ofsy=0;
662         
663         if(renrect) return;
664         
665         rl= rr->layers.first;
666         
667         /* this case is when we render envmaps... */
668 //      if(rr->rectx > v3d->ri->pr_rectx || rr->recty > v3d->ri->pr_recty)
669 //              return;
670         
671 //      ofsx= v3d->ri->disprect.xmin + rr->tilerect.xmin;
672 //      ofsy= v3d->ri->disprect.ymin + rr->tilerect.ymin;
673         
674         glDrawBuffer(GL_FRONT);
675 //      glaDefine2DArea(&sa->winrct);
676         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf, 0);
677         bglFlush();
678         glDrawBuffer(GL_BACK);
679
680 }
681
682 void BIF_view3d_previewrender_signal(ScrArea *UNUSED(sa), short UNUSED(signal))
683 {
684 #if 0
685         View3D *v3d= sa->spacedata.first;
686         
687         /* this can be called from other window... solve! */
688         if(sa->spacetype!=SPACE_VIEW3D)
689                 return; // XXX
690            
691         if(v3d && v3d->ri) {
692                 RenderInfo *ri= v3d->ri;
693                 ri->status &= ~signal;
694                 ri->curtile= 0;
695                 //printf("preview signal %d\n", signal);
696                 if(ri->re && (signal & PR_DBASE))
697                         RE_Database_Free(ri->re);
698
699 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
700         }
701 #endif
702 }
703
704 void BIF_view3d_previewrender_free(View3D *UNUSED(v3d))
705 {
706 #if 0
707         if(v3d->ri) {
708                 RenderInfo *ri= v3d->ri;
709                 if(ri->re) {
710 //                      printf("free render\n");
711                         RE_Database_Free(ri->re);
712                         RE_FreeRender(ri->re);
713                         ri->re= NULL;
714                 }
715                 if (v3d->ri->rect) MEM_freeN(v3d->ri->rect);
716                 MEM_freeN(v3d->ri);
717                 v3d->ri= NULL;
718         }
719 #endif
720 }
721
722 /* returns 1 if OK, do not call while in panel space!  */
723 static int view3d_previewrender_get_rects(ScrArea *sa, rctf *viewplane, RenderInfo *ri, float *clipsta, float *clipend, int *ortho, float *pixsize)
724 {
725         View3D *v3d= NULL; // XXX
726         RegionView3D *rv3d= NULL; // XXX
727         int rectx, recty;
728 //      uiBlock *block;
729         
730 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
731 //      if(block==NULL) return 0;
732         
733         /* calculate preview rect size */
734 //      BLI_init_rctf(viewplane, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
735 //      uiPanelPush(block);
736 //      ui_graphics_to_window_rct(sa->win, viewplane, &ri->disprect);
737 //      uiPanelPop(block);
738         
739         /* correction for gla draw */
740 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
741         
742         *ortho= get_view3d_viewplane(v3d, rv3d, sa->winx, sa->winy, viewplane, clipsta, clipend, pixsize);
743
744         rectx= ri->disprect.xmax - ri->disprect.xmin;
745         recty= ri->disprect.ymax - ri->disprect.ymin;
746         
747         if(rectx<4 || recty<4) return 0;
748         
749         if(ri->rect && (rectx!=ri->pr_rectx || recty!=ri->pr_recty)) {
750                 MEM_freeN(ri->rect);
751                 ri->rect= NULL;
752                 ri->curtile= 0;
753                 printf("changed size\n");
754         }
755         ri->pr_rectx= rectx;
756         ri->pr_recty= recty;
757         
758         return 1;
759 }
760
761 /* called before a panel gets moved/scaled, makes sure we can see through */
762 void BIF_view3d_previewrender_clear(ScrArea *UNUSED(sa))
763 {
764 #if 0
765         View3D *v3d= sa->spacedata.first;
766
767         if(v3d->ri) {
768                 RenderInfo *ri= v3d->ri;
769                 ri->curtile= 0;
770                 if(ri->rect)
771                         MEM_freeN(ri->rect);
772                 ri->rect= NULL;
773         }
774 #endif
775 }
776
777 /* afterqueue call */
778 void BIF_view3d_previewrender(Main *bmain, Scene *scene, ScrArea *sa)
779 {
780         View3D *v3d= sa->spacedata.first;
781         RegionView3D *rv3d= NULL; // XXX
782         Render *re;
783         RenderInfo *ri=NULL;    /* preview struct! */
784         RenderStats *rstats;
785         RenderData rdata;
786         rctf viewplane;
787         float clipsta, clipend, pixsize;
788         int orth;
789         
790         /* first get the render info right */
791 //      if (!v3d->ri) {
792 //              ri= v3d->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
793 //              ri->tottile= 10000;
794 //      }
795 //      ri= v3d->ri;
796         
797         if(0==view3d_previewrender_get_rects(sa, &viewplane, ri, &clipsta, &clipend, &orth, &pixsize))
798                 return;
799         
800         /* render is finished, so return */
801         if(ri->tottile && ri->curtile>=ri->tottile) return;
802
803         /* or return with a new event */
804         if(qtest()) {
805 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
806                 return;
807         }
808         //printf("Enter previewrender\n");
809         /* ok, are we rendering all over? */
810         if(ri->re==NULL) {
811                 char name[32];
812                 
813                 ri->status= 0;
814                 
815                 sprintf(name, "View3dPreview %p", (void *)sa);
816                 re= ri->re= RE_NewRender(name);
817                 //RE_display_draw_cb(re, view3d_previewrender_progress);
818                 //RE_stats_draw_cb(re, view3d_previewrender_stats);
819                 //RE_test_break_cb(re, qtest);
820                 
821                 /* no osa, blur, seq, layers, etc for preview render */
822                 rdata= scene->r;
823                 rdata.mode &= ~(R_OSA|R_MBLUR);
824                 rdata.scemode &= ~(R_DOSEQ|R_DOCOMP|R_FREE_IMAGE);
825                 rdata.layers.first= rdata.layers.last= NULL;
826                 rdata.renderer= R_INTERN;
827                  
828                 RE_InitState(re, NULL, &rdata, NULL, sa->winx, sa->winy, &ri->disprect);
829         
830                 if(orth)
831                         RE_SetOrtho(re, &viewplane, clipsta, clipend);
832                 else
833                         RE_SetWindow(re, &viewplane, clipsta, clipend);
834                 RE_SetPixelSize(re, pixsize);
835                 
836                 /* until here are no escapes */
837                 ri->status |= PR_DISPRECT;
838                 ri->curtile= 0;
839                 //printf("new render\n");
840         }
841
842         re= ri->re;
843         
844         PIL_sleep_ms(100);      /* wait 0.1 second if theres really no event... */
845         if(qtest()==0)  {
846                 
847                 /* check status */
848                 if((ri->status & PR_DISPRECT)==0) {
849                         RE_SetDispRect(ri->re, &ri->disprect);
850                         if(orth)
851                                 RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
852                         else
853                                 RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
854                         RE_SetPixelSize(re, pixsize);
855                         ri->status |= PR_DISPRECT;
856                         ri->curtile= 0;
857                         //printf("disprect update\n");
858                 }
859                 if((ri->status & PR_DBASE)==0) {
860                         unsigned int lay= scene->lay;
861                         
862                         RE_SetView(re, rv3d->viewmat);
863                         
864                         /* allow localview render for objects with lights in normal layers */
865                         if(v3d->lay & 0xFF000000)
866                                 lay |= v3d->lay;
867                         else lay= v3d->lay;
868                         
869                         RE_Database_FromScene(re, bmain, scene, lay, 0);                // 0= dont use camera view
870                         
871                         rstats= RE_GetStats(re);
872                         if(rstats->convertdone) 
873                                 ri->status |= PR_DBASE|PR_PROJECTED|PR_ROTATED;
874                         ri->curtile= 0;
875                         
876                         /* database can have created render-resol data... */
877                         if(rstats->convertdone) 
878                                 DAG_scene_flush_update(bmain, scene, scene->lay, 0);
879                         
880                         //printf("dbase update\n");
881                 }
882                 if((ri->status & PR_PROJECTED)==0) {
883                         if(ri->status & PR_DBASE) {
884                                 if(orth)
885                                         RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
886                                 else
887                                         RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
888                                 RE_DataBase_ApplyWindow(re);
889                                 ri->status |= PR_PROJECTED;
890                         }
891                         ri->curtile= 0;
892                         //printf("project update\n");
893                 }
894         
895                 /* OK, can we enter render code? */
896                 if(ri->status==(PR_DISPRECT|PR_DBASE|PR_PROJECTED|PR_ROTATED)) {
897                         //printf("curtile %d tottile %d\n", ri->curtile, ri->tottile);
898                         RE_TileProcessor(ri->re); //, ri->curtile, 0);
899         
900                         if(ri->rect==NULL)
901                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "preview view3d rect");
902                         
903                         RE_ResultGet32(ri->re, ri->rect);
904                 }
905                 
906                 rstats= RE_GetStats(ri->re);
907 //              if(rstats->totpart==rstats->partsdone && rstats->partsdone)
908 //                      addqueue(sa->win, REDRAW, 1);
909 //              else
910 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
911                 
912                 ri->curtile= rstats->partsdone;
913                 ri->tottile= rstats->totpart;
914         }
915         else {
916 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
917         }
918         
919         //printf("\n");
920 }
921
922 /* in panel space! */
923 static void view3d_previewdraw_rect(ScrArea *UNUSED(sa), uiBlock *UNUSED(block), RenderInfo *ri)
924 {
925 //      rctf dispf;
926         
927         if(ri->rect==NULL)
928                 return;
929         
930 //      BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
931 //      ui_graphics_to_window_rct(sa->win, &dispf, &ri->disprect);
932
933         /* correction for gla draw */
934 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
935         
936         /* when panel scale changed, free rect */
937         if(ri->disprect.xmax-ri->disprect.xmin != ri->pr_rectx ||
938            ri->disprect.ymax-ri->disprect.ymin != ri->pr_recty) {
939                 MEM_freeN(ri->rect);
940                 ri->rect= NULL;
941         }
942         else {
943 //              glaDefine2DArea(&sa->winrct);
944                 glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
945         }       
946 }
947
948 /* is panel callback, supposed to be called with correct panel offset matrix */
949 void BIF_view3d_previewdraw(struct ScrArea *sa, struct uiBlock *block)
950 {
951         RegionView3D *rv3d= NULL;
952
953 //      if (v3d->ri==NULL || v3d->ri->rect==NULL) 
954 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
955 //      else {
956                 view3d_previewdraw_rect(sa, block, rv3d->ri);
957 //              if(v3d->ri->curtile==0) 
958 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
959 //      }
960 }
961
962
963 /* **************************** new shader preview system ****************** */
964
965 /* inside thread, called by renderer, sets job update value */
966 static void shader_preview_draw(void *spv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
967 {
968         ShaderPreview *sp= spv;
969         
970         *(sp->do_update)= 1;
971 }
972
973 /* called by renderer, checks job value */
974 static int shader_preview_break(void *spv)
975 {
976         ShaderPreview *sp= spv;
977
978         return *(sp->stop);
979 }
980
981 /* outside thread, called before redraw notifiers, it moves finished preview over */
982 static void shader_preview_updatejob(void *spv)
983 {
984         ShaderPreview *sp= spv;
985         
986         if(sp->id) {
987                 if(sp->pr_method==PR_NODE_RENDER) {
988                         if( GS(sp->id->name) == ID_MA) {
989                                 Material *mat= (Material *)sp->id;
990                                 
991                                 if(sp->matcopy && mat->nodetree && sp->matcopy->nodetree)
992                                         ntreeLocalSync(sp->matcopy->nodetree, mat->nodetree);
993                         }
994                         else if( GS(sp->id->name) == ID_TE) {
995                                 Tex *tex= (Tex *)sp->id;
996                                 
997                                 if(sp->texcopy && tex->nodetree && sp->texcopy->nodetree)
998                                         ntreeLocalSync(sp->texcopy->nodetree, tex->nodetree);
999                         }
1000                 }               
1001         }
1002 }
1003
1004 static void shader_preview_render(ShaderPreview *sp, ID *id, int split, int first)
1005 {
1006         Render *re;
1007         Scene *sce;
1008         float oldlens;
1009         short idtype= GS(id->name);
1010         char name[32];
1011         int sizex;
1012         
1013         /* get the stuff from the builtin preview dbase */
1014         sce= preview_prepare_scene(sp->scene, id, idtype, sp); // XXX sizex
1015         if(sce==NULL) return;
1016         
1017         if(!split || first) sprintf(name, "Preview %p", sp->owner);
1018         else sprintf(name, "SecondPreview %p", sp->owner);
1019         re= RE_GetRender(name);
1020         
1021         /* full refreshed render from first tile */
1022         if(re==NULL)
1023                 re= RE_NewRender(name);
1024                 
1025         /* sce->r gets copied in RE_InitState! */
1026         sce->r.scemode &= ~(R_MATNODE_PREVIEW|R_TEXNODE_PREVIEW);
1027         sce->r.scemode &= ~R_NO_IMAGE_LOAD;
1028
1029         if(sp->pr_method==PR_ICON_RENDER) {
1030                 sce->r.scemode |= R_NO_IMAGE_LOAD;
1031                 sce->r.mode |= R_OSA;
1032         }
1033         else if(sp->pr_method==PR_NODE_RENDER) {
1034                 if(idtype == ID_MA) sce->r.scemode |= R_MATNODE_PREVIEW;
1035                 else if(idtype == ID_TE) sce->r.scemode |= R_TEXNODE_PREVIEW;
1036                 sce->r.mode &= ~R_OSA;
1037         }
1038         else {  /* PR_BUTS_RENDER */
1039                 sce->r.mode |= R_OSA;
1040         }
1041
1042         /* in case of split preview, use border render */
1043         if(split) {
1044                 if(first) sizex= sp->sizex/2;
1045                 else sizex= sp->sizex - sp->sizex/2;
1046         }
1047         else sizex= sp->sizex;
1048
1049         /* allocates or re-uses render result */
1050         sce->r.xsch= sizex;
1051         sce->r.ysch= sp->sizey;
1052         sce->r.size= 100;
1053
1054         /* callbacs are cleared on GetRender() */
1055         if(ELEM(sp->pr_method, PR_BUTS_RENDER, PR_NODE_RENDER)) {
1056                 RE_display_draw_cb(re, sp, shader_preview_draw);
1057         }
1058         /* set this for all previews, default is react to G.afbreek still */
1059         RE_test_break_cb(re, sp, shader_preview_break);
1060         
1061         /* lens adjust */
1062         oldlens= ((Camera *)sce->camera->data)->lens;
1063         if(sizex > sp->sizey)
1064                 ((Camera *)sce->camera->data)->lens *= (float)sp->sizey/(float)sizex;
1065
1066         /* entire cycle for render engine */
1067         RE_PreviewRender(re, pr_main, sce);
1068
1069         ((Camera *)sce->camera->data)->lens= oldlens;
1070
1071         /* handle results */
1072         if(sp->pr_method==PR_ICON_RENDER) {
1073                 // char *rct= (char *)(sp->pr_rect + 32*16 + 16);
1074                 
1075                 if(sp->pr_rect)
1076                         RE_ResultGet32(re, sp->pr_rect);
1077         }
1078         else {
1079                 /* validate owner */
1080                 //if(ri->rect==NULL)
1081                 //      ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
1082                 //RE_ResultGet32(re, ri->rect);
1083         }
1084
1085         /* unassign the pointers, reset vars */
1086         preview_prepare_scene(sp->scene, NULL, GS(id->name), sp);
1087         
1088         /* XXX bad exception, end-exec is not being called in render, because it uses local main */
1089 //      if(idtype == ID_TE) {
1090 //              Tex *tex= (Tex *)id;
1091 //              if(tex->use_nodes && tex->nodetree)
1092 //                      ntreeEndExecTree(tex->nodetree);
1093 //      }
1094
1095 }
1096
1097 /* runs inside thread for material and icons */
1098 static void shader_preview_startjob(void *customdata, short *stop, short *do_update)
1099 {
1100         ShaderPreview *sp= customdata;
1101
1102         sp->stop= stop;
1103         sp->do_update= do_update;
1104
1105         if(sp->parent) {
1106                 shader_preview_render(sp, sp->id, 1, 1);
1107                 shader_preview_render(sp, sp->parent, 1, 0);
1108         }
1109         else
1110                 shader_preview_render(sp, sp->id, 0, 0);
1111
1112         *do_update= 1;
1113 }
1114
1115 static void shader_preview_free(void *customdata)
1116 {
1117         ShaderPreview *sp= customdata;
1118         
1119         if(sp->matcopy) {
1120                 struct IDProperty *properties;
1121                 int a;
1122                 
1123                 /* node previews */
1124                 shader_preview_updatejob(sp);
1125                 
1126                 /* get rid of copied material */
1127                 BLI_remlink(&pr_main->mat, sp->matcopy);
1128                 
1129                 /* free_material decrements texture, prevent this. hack alert! */
1130                 for(a=0; a<MAX_MTEX; a++) {
1131                         MTex *mtex= sp->matcopy->mtex[a];
1132                         if(mtex && mtex->tex) mtex->tex= NULL;
1133                 }
1134                 
1135                 free_material(sp->matcopy);
1136
1137                 properties= IDP_GetProperties((ID *)sp->matcopy, FALSE);
1138                 if (properties) {
1139                         IDP_FreeProperty(properties);
1140                         MEM_freeN(properties);
1141                 }
1142                 MEM_freeN(sp->matcopy);
1143         }
1144         if(sp->texcopy) {
1145                 struct IDProperty *properties;
1146                 /* node previews */
1147                 shader_preview_updatejob(sp);
1148                 
1149                 /* get rid of copied texture */
1150                 BLI_remlink(&pr_main->tex, sp->texcopy);
1151                 free_texture(sp->texcopy);
1152                 
1153                 properties= IDP_GetProperties((ID *)sp->texcopy, FALSE);
1154                 if (properties) {
1155                         IDP_FreeProperty(properties);
1156                         MEM_freeN(properties);
1157                 }
1158                 MEM_freeN(sp->texcopy);
1159         }
1160         
1161         MEM_freeN(sp);
1162 }
1163
1164 /* ************************* icon preview ********************** */
1165
1166 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
1167 {
1168         struct ImBuf *ima;
1169         unsigned int *drect, *srect;
1170         float scaledx, scaledy;
1171         short ex, ey, dx, dy;
1172
1173         /* paranoia test */
1174         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
1175                 return;
1176         
1177         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
1178         ima = IMB_dupImBuf(ibuf);
1179         
1180         if (!ima) 
1181                 return;
1182         
1183         if (ima->x > ima->y) {
1184                 scaledx = (float)w;
1185                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
1186         }
1187         else {                  
1188                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
1189                 scaledy = (float)h;
1190         }
1191         
1192         ex = (short)scaledx;
1193         ey = (short)scaledy;
1194         
1195         dx = (w - ex) / 2;
1196         dy = (h - ey) / 2;
1197         
1198         IMB_scalefastImBuf(ima, ex, ey);
1199         
1200         /* if needed, convert to 32 bits */
1201         if(ima->rect==NULL)
1202                 IMB_rect_from_float(ima);
1203
1204         srect = ima->rect;
1205         drect = rect;
1206
1207         drect+= dy*w+dx;
1208         for (;ey > 0; ey--){            
1209                 memcpy(drect,srect, ex * sizeof(int));
1210                 drect += w;
1211                 srect += ima->x;
1212         }
1213
1214         IMB_freeImBuf(ima);
1215 }
1216
1217 static void set_alpha(char *cp, int sizex, int sizey, char alpha) 
1218 {
1219         int a, size= sizex*sizey;
1220
1221         for(a=0; a<size; a++, cp+=4)
1222                 cp[3]= alpha;
1223 }
1224
1225 static void icon_preview_startjob(void *customdata, short *stop, short *do_update)
1226 {
1227         ShaderPreview *sp= customdata;
1228         ID *id= sp->id;
1229         short idtype= GS(id->name);
1230
1231         if(idtype == ID_IM) {
1232                 Image *ima= (Image*)id;
1233                 ImBuf *ibuf= NULL;
1234                 ImageUser iuser= {NULL};
1235
1236                 /* ima->ok is zero when Image cannot load */
1237                 if(ima==NULL || ima->ok==0)
1238                         return;
1239
1240                 /* setup dummy image user */
1241                 iuser.ok= iuser.framenr= 1;
1242                 iuser.scene= sp->scene;
1243                 
1244                 /* elubie: this needs to be changed: here image is always loaded if not
1245                    already there. Very expensive for large images. Need to find a way to 
1246                    only get existing ibuf */
1247                 ibuf = BKE_image_get_ibuf(ima, &iuser);
1248                 if(ibuf==NULL || ibuf->rect==NULL)
1249                         return;
1250                 
1251                 icon_copy_rect(ibuf, sp->sizex, sp->sizey, sp->pr_rect);
1252
1253                 *do_update= 1;
1254         }
1255         else if(idtype == ID_BR) {
1256                 Brush *br= (Brush*)id;
1257
1258                 br->icon_imbuf= get_brush_icon(br);
1259
1260                 memset(sp->pr_rect, 0x888888, sp->sizex*sp->sizey*sizeof(unsigned int));
1261
1262                 if(!(br->icon_imbuf) || !(br->icon_imbuf->rect))
1263                         return;
1264
1265                 icon_copy_rect(br->icon_imbuf, sp->sizex, sp->sizey, sp->pr_rect);
1266
1267                 *do_update= 1;
1268         }
1269         else {
1270                 /* re-use shader job */
1271                 shader_preview_startjob(customdata, stop, do_update);
1272
1273                 /* world is rendered with alpha=0, so it wasn't displayed 
1274                    this could be render option for sky to, for later */
1275                 if(idtype == ID_WO) {
1276                         set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
1277                 }
1278                 else if(idtype == ID_MA) {
1279                         Material* ma = (Material*)id;
1280
1281                         if(ma->material_type == MA_TYPE_HALO)
1282                                 set_alpha((char*)sp->pr_rect, sp->sizex, sp->sizey, 255);
1283                 }
1284         }
1285 }
1286
1287 /* use same function for icon & shader, so the job manager
1288    does not run two of them at the same time. */
1289
1290 static void common_preview_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
1291 {
1292         ShaderPreview *sp= customdata;
1293
1294         if(sp->pr_method == PR_ICON_RENDER)
1295                 icon_preview_startjob(customdata, stop, do_update);
1296         else
1297                 shader_preview_startjob(customdata, stop, do_update);
1298 }
1299
1300 static void common_preview_endjob(void *customdata)
1301 {
1302         ShaderPreview *sp= customdata;
1303
1304         if(sp->id && GS(sp->id->name) == ID_BR)
1305                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, sp->id);
1306 }
1307
1308 /* exported functions */
1309
1310 void ED_preview_icon_job(const bContext *C, void *owner, ID *id, unsigned int *rect, int sizex, int sizey)
1311 {
1312         wmJob *steve;
1313         ShaderPreview *sp;
1314         
1315         /* suspended start means it starts after 1 timer step, see WM_jobs_timer below */
1316         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Icon Preview", WM_JOB_EXCL_RENDER|WM_JOB_SUSPEND);
1317         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
1318
1319         /* customdata for preview thread */
1320         sp->scene= CTX_data_scene(C);
1321         sp->owner= id;
1322         sp->sizex= sizex;
1323         sp->sizey= sizey;
1324         sp->pr_method= PR_ICON_RENDER;
1325         sp->pr_rect= rect;
1326         sp->id = id;
1327
1328         /* setup job */
1329         WM_jobs_customdata(steve, sp, shader_preview_free);
1330         WM_jobs_timer(steve, 0.25, NC_MATERIAL, NC_MATERIAL);
1331         WM_jobs_callbacks(steve, common_preview_startjob, NULL, NULL, common_preview_endjob);
1332
1333         WM_jobs_start(CTX_wm_manager(C), steve);
1334 }
1335
1336 void ED_preview_shader_job(const bContext *C, void *owner, ID *id, ID *parent, MTex *slot, int sizex, int sizey, int method)
1337 {
1338         Object *ob= CTX_data_active_object(C);
1339         wmJob *steve;
1340         ShaderPreview *sp;
1341
1342         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), owner, "Shader Preview", WM_JOB_EXCL_RENDER);
1343         sp= MEM_callocN(sizeof(ShaderPreview), "shader preview");
1344
1345         /* customdata for preview thread */
1346         sp->scene= CTX_data_scene(C);
1347         sp->owner= owner;
1348         sp->sizex= sizex;
1349         sp->sizey= sizey;
1350         sp->pr_method= method;
1351         sp->id = id;
1352         sp->parent= parent;
1353         sp->slot= slot;
1354         if(ob && ob->totcol) copy_v4_v4(sp->col, ob->col);
1355         else sp->col[0]= sp->col[1]= sp->col[2]= sp->col[3]= 1.0f;
1356         
1357         /* setup job */
1358         WM_jobs_customdata(steve, sp, shader_preview_free);
1359         WM_jobs_timer(steve, 0.1, NC_MATERIAL, NC_MATERIAL);
1360         WM_jobs_callbacks(steve, common_preview_startjob, NULL, shader_preview_updatejob, NULL);
1361         
1362         WM_jobs_start(CTX_wm_manager(C), steve);
1363 }
1364
1365 void ED_preview_kill_jobs(const struct bContext *C)
1366 {
1367         wmWindowManager *wm= CTX_wm_manager(C);
1368         if(wm)
1369                 WM_jobs_kill(wm, NULL, common_preview_startjob);
1370 }
1371