Cleanup: style, use braces for editor/spaces
[blender.git] / source / blender / editors / space_image / space_image.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spimage
22  */
23
24 #include "DNA_gpencil_types.h"
25 #include "DNA_mesh_types.h"
26 #include "DNA_mask_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_object_types.h"
29 #include "DNA_scene_types.h"
30 #include "DNA_image_types.h"
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_math.h"
36 #include "BLI_threads.h"
37
38 #include "BKE_colortools.h"
39 #include "BKE_context.h"
40 #include "BKE_editmesh.h"
41 #include "BKE_image.h"
42 #include "BKE_layer.h"
43 #include "BKE_library.h"
44 #include "BKE_material.h"
45 #include "BKE_scene.h"
46 #include "BKE_screen.h"
47 #include "BKE_workspace.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51 #include "RNA_enum_types.h"
52
53 #include "DEG_depsgraph.h"
54
55 #include "IMB_imbuf_types.h"
56
57 #include "ED_image.h"
58 #include "ED_mask.h"
59 #include "ED_mesh.h"
60 #include "ED_node.h"
61 #include "ED_render.h"
62 #include "ED_space_api.h"
63 #include "ED_screen.h"
64 #include "ED_uvedit.h"
65 #include "ED_transform.h"
66
67
68 #include "WM_api.h"
69 #include "WM_types.h"
70 #include "WM_message.h"
71
72 #include "UI_resources.h"
73 #include "UI_interface.h"
74 #include "UI_view2d.h"
75
76 #include "image_intern.h"
77 #include "GPU_framebuffer.h"
78 #include "GPU_batch_presets.h"
79 #include "GPU_viewport.h"
80
81 /* TODO(fclem) remove bad level calls */
82 #include "../draw/DRW_engine.h"
83 #include "wm_draw.h"
84
85 /**************************** common state *****************************/
86
87 static void image_scopes_tag_refresh(ScrArea *sa)
88 {
89         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
90         ARegion *ar;
91
92         /* only while histogram is visible */
93         for (ar = sa->regionbase.first; ar; ar = ar->next) {
94                 if (ar->regiontype == RGN_TYPE_TOOL_PROPS && ar->flag & RGN_FLAG_HIDDEN) {
95                         return;
96                 }
97         }
98
99         sima->scopes.ok = 0;
100 }
101
102 static void image_user_refresh_scene(const bContext *C, SpaceImage *sima)
103 {
104         /* Update scene image user for acquiring render results. */
105         sima->iuser.scene = CTX_data_scene(C);
106
107         if (sima->image && sima->image->type == IMA_TYPE_R_RESULT) {
108                 /* While rendering, prefer scene that is being rendered. */
109                 Scene *render_scene = ED_render_job_get_current_scene(C);
110                 if (render_scene) {
111                         sima->iuser.scene = render_scene;
112                 }
113         }
114
115         /* Auto switch image to show in UV editor when selection changes. */
116         ED_space_image_auto_set(C, sima);
117 }
118
119 /* ******************** manage regions ********************* */
120
121 ARegion *image_has_buttons_region(ScrArea *sa)
122 {
123         ARegion *ar, *arnew;
124
125         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
126         if (ar) {
127                 return ar;
128         }
129
130         /* add subdiv level; after header */
131         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
132
133         /* is error! */
134         if (ar == NULL) {
135                 return NULL;
136         }
137
138         arnew = MEM_callocN(sizeof(ARegion), "buttons for image");
139
140         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
141         arnew->regiontype = RGN_TYPE_UI;
142         arnew->alignment = RGN_ALIGN_RIGHT;
143
144         arnew->flag = RGN_FLAG_HIDDEN;
145
146         return arnew;
147 }
148
149 ARegion *image_has_tools_region(ScrArea *sa)
150 {
151         ARegion *ar, *arnew;
152
153         ar = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
154         if (ar) {
155                 return ar;
156         }
157
158         /* add subdiv level; after buttons */
159         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
160
161         /* is error! */
162         if (ar == NULL) {
163                 return NULL;
164         }
165
166         arnew = MEM_callocN(sizeof(ARegion), "scopes for image");
167
168         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
169         arnew->regiontype = RGN_TYPE_TOOLS;
170         arnew->alignment = RGN_ALIGN_LEFT;
171
172         arnew->flag = RGN_FLAG_HIDDEN;
173
174         image_scopes_tag_refresh(sa);
175
176         return arnew;
177 }
178
179 /* ******************** default callbacks for image space ***************** */
180
181 static SpaceLink *image_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
182 {
183         ARegion *ar;
184         SpaceImage *simage;
185
186         simage = MEM_callocN(sizeof(SpaceImage), "initimage");
187         simage->spacetype = SPACE_IMAGE;
188         simage->zoom = 1.0f;
189         simage->lock = true;
190         simage->flag = SI_SHOW_GPENCIL | SI_USE_ALPHA | SI_COORDFLOATS;
191
192         simage->iuser.ok = true;
193         simage->iuser.frames = 100;
194         simage->iuser.flag = IMA_SHOW_STEREO | IMA_ANIM_ALWAYS;
195
196         scopes_new(&simage->scopes);
197         simage->sample_line_hist.height = 100;
198
199         /* header */
200         ar = MEM_callocN(sizeof(ARegion), "header for image");
201
202         BLI_addtail(&simage->regionbase, ar);
203         ar->regiontype = RGN_TYPE_HEADER;
204         ar->alignment = (U.uiflag & USER_HEADER_BOTTOM) ? RGN_ALIGN_BOTTOM : RGN_ALIGN_TOP;
205
206         /* buttons/list view */
207         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
208
209         BLI_addtail(&simage->regionbase, ar);
210         ar->regiontype = RGN_TYPE_UI;
211         ar->alignment = RGN_ALIGN_RIGHT;
212         ar->flag = RGN_FLAG_HIDDEN;
213
214         /* scopes/uv sculpt/paint */
215         ar = MEM_callocN(sizeof(ARegion), "buttons for image");
216
217         BLI_addtail(&simage->regionbase, ar);
218         ar->regiontype = RGN_TYPE_TOOLS;
219         ar->alignment = RGN_ALIGN_LEFT;
220         ar->flag = RGN_FLAG_HIDDEN;
221
222         /* main area */
223         ar = MEM_callocN(sizeof(ARegion), "main area for image");
224
225         BLI_addtail(&simage->regionbase, ar);
226         ar->regiontype = RGN_TYPE_WINDOW;
227
228         return (SpaceLink *)simage;
229 }
230
231 /* not spacelink itself */
232 static void image_free(SpaceLink *sl)
233 {
234         SpaceImage *simage = (SpaceImage *) sl;
235
236         scopes_free(&simage->scopes);
237 }
238
239
240 /* spacetype; init callback, add handlers */
241 static void image_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
242 {
243         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
244
245         /* add drop boxes */
246         WM_event_add_dropbox_handler(&sa->handlers, lb);
247
248 }
249
250 static SpaceLink *image_duplicate(SpaceLink *sl)
251 {
252         SpaceImage *simagen = MEM_dupallocN(sl);
253
254         /* clear or remove stuff from old */
255
256         scopes_new(&simagen->scopes);
257
258         return (SpaceLink *)simagen;
259 }
260
261 static void image_operatortypes(void)
262 {
263         WM_operatortype_append(IMAGE_OT_view_all);
264         WM_operatortype_append(IMAGE_OT_view_pan);
265         WM_operatortype_append(IMAGE_OT_view_selected);
266         WM_operatortype_append(IMAGE_OT_view_zoom);
267         WM_operatortype_append(IMAGE_OT_view_zoom_in);
268         WM_operatortype_append(IMAGE_OT_view_zoom_out);
269         WM_operatortype_append(IMAGE_OT_view_zoom_ratio);
270         WM_operatortype_append(IMAGE_OT_view_zoom_border);
271 #ifdef WITH_INPUT_NDOF
272         WM_operatortype_append(IMAGE_OT_view_ndof);
273 #endif
274
275         WM_operatortype_append(IMAGE_OT_new);
276         WM_operatortype_append(IMAGE_OT_open);
277         WM_operatortype_append(IMAGE_OT_match_movie_length);
278         WM_operatortype_append(IMAGE_OT_replace);
279         WM_operatortype_append(IMAGE_OT_reload);
280         WM_operatortype_append(IMAGE_OT_save);
281         WM_operatortype_append(IMAGE_OT_save_as);
282         WM_operatortype_append(IMAGE_OT_save_sequence);
283         WM_operatortype_append(IMAGE_OT_pack);
284         WM_operatortype_append(IMAGE_OT_unpack);
285
286         WM_operatortype_append(IMAGE_OT_invert);
287
288         WM_operatortype_append(IMAGE_OT_cycle_render_slot);
289         WM_operatortype_append(IMAGE_OT_clear_render_slot);
290         WM_operatortype_append(IMAGE_OT_add_render_slot);
291         WM_operatortype_append(IMAGE_OT_remove_render_slot);
292
293         WM_operatortype_append(IMAGE_OT_sample);
294         WM_operatortype_append(IMAGE_OT_sample_line);
295         WM_operatortype_append(IMAGE_OT_curves_point_set);
296
297         WM_operatortype_append(IMAGE_OT_properties);
298         WM_operatortype_append(IMAGE_OT_toolshelf);
299
300         WM_operatortype_append(IMAGE_OT_change_frame);
301
302         WM_operatortype_append(IMAGE_OT_read_viewlayers);
303         WM_operatortype_append(IMAGE_OT_render_border);
304         WM_operatortype_append(IMAGE_OT_clear_render_border);
305 }
306
307 static void image_keymap(struct wmKeyConfig *keyconf)
308 {
309         WM_keymap_ensure(keyconf, "Image Generic", SPACE_IMAGE, 0);
310         WM_keymap_ensure(keyconf, "Image", SPACE_IMAGE, 0);
311 }
312
313 /* dropboxes */
314 static bool image_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
315 {
316         if (drag->type == WM_DRAG_PATH) {
317                 /* rule might not work? */
318                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE, ICON_FILE_BLANK)) {
319                         return 1;
320                 }
321         }
322         return 0;
323 }
324
325 static void image_drop_copy(wmDrag *drag, wmDropBox *drop)
326 {
327         /* copy drag path to properties */
328         RNA_string_set(drop->ptr, "filepath", drag->path);
329 }
330
331 /* area+region dropbox definition */
332 static void image_dropboxes(void)
333 {
334         ListBase *lb = WM_dropboxmap_find("Image", SPACE_IMAGE, 0);
335
336         WM_dropbox_add(lb, "IMAGE_OT_open", image_drop_poll, image_drop_copy);
337 }
338
339 /**
340  * \note take care not to get into feedback loop here,
341  *       calling composite job causes viewer to refresh.
342  */
343 static void image_refresh(const bContext *C, ScrArea *sa)
344 {
345         Scene *scene = CTX_data_scene(C);
346         SpaceImage *sima = sa->spacedata.first;
347         Image *ima;
348
349         ima = ED_space_image(sima);
350
351         BKE_image_user_frame_calc(&sima->iuser, scene->r.cfra);
352
353         /* check if we have to set the image from the editmesh */
354         if (ima && (ima->source == IMA_SRC_VIEWER && sima->mode == SI_MODE_MASK)) {
355                 if (scene->nodetree) {
356                         Mask *mask = ED_space_image_get_mask(sima);
357                         if (mask) {
358                                 ED_node_composite_job(C, scene->nodetree, scene);
359                         }
360                 }
361         }
362 }
363
364 static void image_listener(wmWindow *win, ScrArea *sa, wmNotifier *wmn, Scene *UNUSED(scene))
365 {
366         SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
367
368         /* context changes */
369         switch (wmn->category) {
370                 case NC_WINDOW:
371                         /* notifier comes from editing color space */
372                         image_scopes_tag_refresh(sa);
373                         ED_area_tag_redraw(sa);
374                         break;
375                 case NC_SCENE:
376                         switch (wmn->data) {
377                                 case ND_FRAME:
378                                         image_scopes_tag_refresh(sa);
379                                         ED_area_tag_refresh(sa);
380                                         ED_area_tag_redraw(sa);
381                                         break;
382                                 case ND_MODE:
383                                         if (wmn->subtype == NS_EDITMODE_MESH) {
384                                                 ED_area_tag_refresh(sa);
385                                         }
386                                         ED_area_tag_redraw(sa);
387                                         break;
388                                 case ND_RENDER_RESULT:
389                                 case ND_RENDER_OPTIONS:
390                                 case ND_COMPO_RESULT:
391                                         if (ED_space_image_show_render(sima)) {
392                                                 image_scopes_tag_refresh(sa);
393                                         }
394                                         ED_area_tag_redraw(sa);
395                                         break;
396                         }
397                         break;
398                 case NC_IMAGE:
399                         if (wmn->reference == sima->image || !wmn->reference) {
400                                 if (wmn->action != NA_PAINTING) {
401                                         image_scopes_tag_refresh(sa);
402                                         ED_area_tag_refresh(sa);
403                                         ED_area_tag_redraw(sa);
404                                 }
405                         }
406                         break;
407                 case NC_SPACE:
408                         if (wmn->data == ND_SPACE_IMAGE) {
409                                 image_scopes_tag_refresh(sa);
410                                 ED_area_tag_redraw(sa);
411                         }
412                         break;
413                 case NC_MASK:
414                 {
415                         // Scene *scene = wmn->window->screen->scene;
416                         /* ideally would check for: ED_space_image_check_show_maskedit(scene, sima)
417                          * but we cant get the scene */
418                         if (sima->mode == SI_MODE_MASK) {
419                                 switch (wmn->data) {
420                                         case ND_SELECT:
421                                                 ED_area_tag_redraw(sa);
422                                                 break;
423                                         case ND_DATA:
424                                         case ND_DRAW:
425                                                 /* causes node-recalc */
426                                                 ED_area_tag_redraw(sa);
427                                                 ED_area_tag_refresh(sa);
428                                                 break;
429                                 }
430                                 switch (wmn->action) {
431                                         case NA_SELECTED:
432                                                 ED_area_tag_redraw(sa);
433                                                 break;
434                                         case NA_EDITED:
435                                                 /* causes node-recalc */
436                                                 ED_area_tag_redraw(sa);
437                                                 ED_area_tag_refresh(sa);
438                                                 break;
439                                 }
440                         }
441                         break;
442                 }
443                 case NC_GEOM:
444                 {
445                         switch (wmn->data) {
446                                 case ND_DATA:
447                                 case ND_SELECT:
448                                         image_scopes_tag_refresh(sa);
449                                         ED_area_tag_refresh(sa);
450                                         ED_area_tag_redraw(sa);
451                                         break;
452                         }
453                         break;
454                 }
455                 case NC_OBJECT:
456                 {
457                         switch (wmn->data) {
458                                 case ND_TRANSFORM:
459                                 case ND_MODIFIER:
460                                 {
461                                         ViewLayer *view_layer = WM_window_get_active_view_layer(win);
462                                         Object *ob = OBACT(view_layer);
463                                         if (ob && (ob == wmn->reference) && (ob->mode & OB_MODE_EDIT)) {
464                                                 if (sima->lock && (sima->flag & SI_DRAWSHADOW)) {
465                                                         ED_area_tag_refresh(sa);
466                                                         ED_area_tag_redraw(sa);
467                                                 }
468                                         }
469                                         break;
470                                 }
471                         }
472
473                         break;
474                 }
475                 case NC_ID:
476                 {
477                         if (wmn->action == NA_RENAME) {
478                                 ED_area_tag_redraw(sa);
479                         }
480                         break;
481                 }
482                 case NC_WM:
483                         if (wmn->data == ND_UNDO) {
484                                 ED_area_tag_redraw(sa);
485                                 ED_area_tag_refresh(sa);
486                         }
487                         break;
488         }
489 }
490
491 const char *image_context_dir[] = {"edit_image", "edit_mask", NULL};
492
493 static int image_context(const bContext *C, const char *member, bContextDataResult *result)
494 {
495         SpaceImage *sima = CTX_wm_space_image(C);
496
497         if (CTX_data_dir(member)) {
498                 CTX_data_dir_set(result, image_context_dir);
499         }
500         else if (CTX_data_equals(member, "edit_image")) {
501                 CTX_data_id_pointer_set(result, (ID *)ED_space_image(sima));
502                 return 1;
503         }
504         else if (CTX_data_equals(member, "edit_mask")) {
505                 Mask *mask = ED_space_image_get_mask(sima);
506                 if (mask) {
507                         CTX_data_id_pointer_set(result, &mask->id);
508                 }
509                 return true;
510         }
511         return 0;
512 }
513
514 static void IMAGE_GGT_gizmo2d(wmGizmoGroupType *gzgt)
515 {
516         gzgt->name = "UV Transform Gizmo";
517         gzgt->idname = "IMAGE_GGT_gizmo2d";
518
519         gzgt->gzmap_params.spaceid = SPACE_IMAGE;
520         gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW;
521
522         gzgt->poll = ED_widgetgroup_gizmo2d_poll;
523         gzgt->setup = ED_widgetgroup_gizmo2d_setup;
524         gzgt->refresh = ED_widgetgroup_gizmo2d_refresh;
525         gzgt->draw_prepare = ED_widgetgroup_gizmo2d_draw_prepare;
526 }
527
528 static void image_widgets(void)
529 {
530         WM_gizmogrouptype_append(IMAGE_GGT_gizmo2d);
531 }
532
533 /************************** main region ***************************/
534
535 /* sets up the fields of the View2D from zoom and offset */
536 static void image_main_region_set_view2d(SpaceImage *sima, ARegion *ar)
537 {
538         Image *ima = ED_space_image(sima);
539
540         int width, height;
541         ED_space_image_get_size(sima, &width, &height);
542
543         float w = width;
544         float h = height;
545
546         if (ima) {
547                 h *= ima->aspy / ima->aspx;
548         }
549
550         int winx = BLI_rcti_size_x(&ar->winrct) + 1;
551         int winy = BLI_rcti_size_y(&ar->winrct) + 1;
552
553         /* For region overlap, move center so image doesn't overlap header. */
554         rcti visible_rect;
555         ED_region_visible_rect(ar, &visible_rect);
556         const int visible_winy = BLI_rcti_size_y(&visible_rect) + 1;
557         int visible_centerx = 0;
558         int visible_centery = visible_rect.ymin + (visible_winy - winy) / 2;
559
560         ar->v2d.tot.xmin = 0;
561         ar->v2d.tot.ymin = 0;
562         ar->v2d.tot.xmax = w;
563         ar->v2d.tot.ymax = h;
564
565         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
566         ar->v2d.mask.xmax = winx;
567         ar->v2d.mask.ymax = winy;
568
569         /* which part of the image space do we see? */
570         float x1 = ar->winrct.xmin + visible_centerx + (winx - sima->zoom * w) / 2.0f;
571         float y1 = ar->winrct.ymin + visible_centery + (winy - sima->zoom * h) / 2.0f;
572
573         x1 -= sima->zoom * sima->xof;
574         y1 -= sima->zoom * sima->yof;
575
576         /* relative display right */
577         ar->v2d.cur.xmin = ((ar->winrct.xmin - (float)x1) / sima->zoom);
578         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sima->zoom);
579
580         /* relative display left */
581         ar->v2d.cur.ymin = ((ar->winrct.ymin - (float)y1) / sima->zoom);
582         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sima->zoom);
583
584         /* normalize 0.0..1.0 */
585         ar->v2d.cur.xmin /= w;
586         ar->v2d.cur.xmax /= w;
587         ar->v2d.cur.ymin /= h;
588         ar->v2d.cur.ymax /= h;
589 }
590
591 /* add handlers, stuff you only do once or on area/region changes */
592 static void image_main_region_init(wmWindowManager *wm, ARegion *ar)
593 {
594         wmKeyMap *keymap;
595
596         // image space manages own v2d
597         // UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
598
599         /* mask polls mode */
600         keymap = WM_keymap_ensure(wm->defaultconf, "Mask Editing", 0, 0);
601         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
602
603         /* image paint polls for mode */
604         keymap = WM_keymap_ensure(wm->defaultconf, "Curve", 0, 0);
605         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
606
607         keymap = WM_keymap_ensure(wm->defaultconf, "Paint Curve", 0, 0);
608         WM_event_add_keymap_handler(&ar->handlers, keymap);
609
610         keymap = WM_keymap_ensure(wm->defaultconf, "Image Paint", 0, 0);
611         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
612
613         keymap = WM_keymap_ensure(wm->defaultconf, "UV Editor", 0, 0);
614         WM_event_add_keymap_handler(&ar->handlers, keymap);
615
616         keymap = WM_keymap_ensure(wm->defaultconf, "UV Sculpt", 0, 0);
617         WM_event_add_keymap_handler(&ar->handlers, keymap);
618
619         /* own keymaps */
620         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
621         WM_event_add_keymap_handler(&ar->handlers, keymap);
622         keymap = WM_keymap_ensure(wm->defaultconf, "Image", SPACE_IMAGE, 0);
623         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
624 }
625
626 static void image_main_region_draw(const bContext *C, ARegion *ar)
627 {
628         /* draw entirely, view changes should be handled here */
629         SpaceImage *sima = CTX_wm_space_image(C);
630         Object *obact = CTX_data_active_object(C);
631         Object *obedit = CTX_data_edit_object(C);
632         Depsgraph *depsgraph = CTX_data_depsgraph(C);
633         Mask *mask = NULL;
634         bool curve = false;
635         Scene *scene = CTX_data_scene(C);
636         ViewLayer *view_layer = CTX_data_view_layer(C);
637         View2D *v2d = &ar->v2d;
638         //View2DScrollers *scrollers;
639         float col[3];
640
641         /* XXX This is in order to draw UI batches with the DRW
642          * olg context since we now use it for drawing the entire area */
643         gpu_batch_presets_reset();
644
645         /* TODO(fclem) port to draw manager and remove the depth buffer allocation. */
646         GPUViewport *viewport = ar->draw_buffer->viewport[ar->draw_buffer->stereo ? sima->iuser.multiview_eye : 0];
647         DefaultFramebufferList *fbl = GPU_viewport_framebuffer_list_get(viewport);
648         GPU_framebuffer_bind(fbl->color_only_fb);
649
650         /* XXX not supported yet, disabling for now */
651         scene->r.scemode &= ~R_COMP_CROP;
652
653         /* clear and setup matrix */
654         UI_GetThemeColor3fv(TH_BACK, col);
655         GPU_clear_color(col[0], col[1], col[2], 0.0);
656         GPU_clear(GPU_COLOR_BIT);
657
658         image_user_refresh_scene(C, sima);
659
660         /* we set view2d from own zoom and offset each time */
661         image_main_region_set_view2d(sima, ar);
662
663         /* we draw image in pixelspace */
664         draw_image_main(C, ar);
665
666         /* and uvs in 0.0-1.0 space */
667         UI_view2d_view_ortho(v2d);
668
669         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
670
671         ED_uvedit_draw_main(sima, ar, scene, view_layer, obedit, obact, depsgraph);
672
673         /* check for mask (delay draw) */
674         if (ED_space_image_show_uvedit(sima, obedit)) {
675                 /* pass */
676         }
677         else if (sima->mode == SI_MODE_MASK) {
678                 mask = ED_space_image_get_mask(sima);
679         }
680         else if (ED_space_image_paint_curve(C)) {
681                 curve = true;
682         }
683
684         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
685
686         if (sima->flag & SI_SHOW_GPENCIL) {
687                 /* Grease Pencil too (in addition to UV's) */
688                 draw_image_grease_pencil((bContext *)C, true);
689         }
690
691         /* sample line */
692         draw_image_sample_line(sima);
693
694         UI_view2d_view_restore(C);
695
696         if (sima->flag & SI_SHOW_GPENCIL) {
697                 /* draw Grease Pencil - screen space only */
698                 draw_image_grease_pencil((bContext *)C, false);
699         }
700
701         if (mask) {
702                 Image *image = ED_space_image(sima);
703                 int width, height, show_viewer;
704                 float aspx, aspy;
705
706                 show_viewer = (image && image->source == IMA_SRC_VIEWER);
707
708                 if (show_viewer) {
709                         /* ED_space_image_get* will acquire image buffer which requires
710                          * lock here by the same reason why lock is needed in draw_image_main
711                          */
712                         BLI_thread_lock(LOCK_DRAW_IMAGE);
713                 }
714
715                 ED_space_image_get_size(sima, &width, &height);
716                 ED_space_image_get_aspect(sima, &aspx, &aspy);
717
718                 if (show_viewer) {
719                         BLI_thread_unlock(LOCK_DRAW_IMAGE);
720                 }
721
722                 ED_mask_draw_region(mask, ar,
723                                     sima->mask_info.draw_flag,
724                                     sima->mask_info.draw_type,
725                                     sima->mask_info.overlay_mode,
726                                     width, height,
727                                     aspx, aspy,
728                                     true, false,
729                                     NULL, C);
730
731                 UI_view2d_view_ortho(v2d);
732                 ED_image_draw_cursor(ar, sima->cursor);
733                 UI_view2d_view_restore(C);
734         }
735         else if (curve) {
736                 UI_view2d_view_ortho(v2d);
737                 ED_image_draw_cursor(ar, sima->cursor);
738                 UI_view2d_view_restore(C);
739         }
740
741         WM_gizmomap_draw(ar->gizmo_map, C, WM_GIZMOMAP_DRAWSTEP_2D);
742
743         draw_image_cache(C, ar);
744
745         /* scrollers? */
746 #if 0
747         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
748         UI_view2d_scrollers_draw(C, v2d, scrollers);
749         UI_view2d_scrollers_free(scrollers);
750 #endif
751 }
752
753 static void image_main_region_listener(
754         wmWindow *UNUSED(win), ScrArea *sa, ARegion *ar,
755         wmNotifier *wmn, const Scene *UNUSED(scene))
756 {
757         /* context changes */
758         switch (wmn->category) {
759                 case NC_GEOM:
760                         if (ELEM(wmn->data, ND_DATA, ND_SELECT)) {
761                                 WM_gizmomap_tag_refresh(ar->gizmo_map);
762                         }
763                         break;
764                 case NC_GPENCIL:
765                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
766                                 ED_region_tag_redraw(ar);
767                         }
768                         else if (wmn->data & ND_GPENCIL_EDITMODE) {
769                                 ED_region_tag_redraw(ar);
770                         }
771                         break;
772                 case NC_IMAGE:
773                         if (wmn->action == NA_PAINTING) {
774                                 ED_region_tag_redraw(ar);
775                         }
776                         WM_gizmomap_tag_refresh(ar->gizmo_map);
777                         break;
778                 case NC_MATERIAL:
779                         if (wmn->data == ND_SHADING_LINKS) {
780                                 SpaceImage *sima = sa->spacedata.first;
781
782                                 if (sima->iuser.scene && (sima->iuser.scene->toolsettings->uv_flag & UV_SHOW_SAME_IMAGE)) {
783                                         ED_region_tag_redraw(ar);
784                                 }
785                         }
786                         break;
787                 case NC_SCREEN:
788                         if (ELEM(wmn->data, ND_LAYER)) {
789                                 ED_region_tag_redraw(ar);
790                         }
791                         break;
792         }
793 }
794
795 /* *********************** buttons region ************************ */
796
797 /* add handlers, stuff you only do once or on area/region changes */
798 static void image_buttons_region_init(wmWindowManager *wm, ARegion *ar)
799 {
800         wmKeyMap *keymap;
801
802         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
803         ED_region_panels_init(wm, ar);
804
805         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
806         WM_event_add_keymap_handler(&ar->handlers, keymap);
807 }
808
809 static void image_buttons_region_draw(const bContext *C, ARegion *ar)
810 {
811         SpaceImage *sima = CTX_wm_space_image(C);
812         Scene *scene = CTX_data_scene(C);
813         void *lock;
814         ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock);
815         /* XXX performance regression if name of scopes category changes! */
816         PanelCategoryStack *category = UI_panel_category_active_find(ar, "Scopes");
817
818         /* only update scopes if scope category is active */
819         if (category) {
820                 if (ibuf) {
821                         if (!sima->scopes.ok) {
822                                 BKE_histogram_update_sample_line(&sima->sample_line_hist, ibuf, &scene->view_settings, &scene->display_settings);
823                         }
824                         if (sima->image->flag & IMA_VIEW_AS_RENDER) {
825                                 ED_space_image_scopes_update(C, sima, ibuf, true);
826                         }
827                         else {
828                                 ED_space_image_scopes_update(C, sima, ibuf, false);
829                         }
830                 }
831         }
832         ED_space_image_release_buffer(sima, ibuf, lock);
833
834         ED_region_panels(C, ar);
835 }
836
837 static void image_buttons_region_listener(
838         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
839         wmNotifier *wmn, const Scene *UNUSED(scene))
840 {
841         /* context changes */
842         switch (wmn->category) {
843                 case NC_TEXTURE:
844                 case NC_MATERIAL:
845                         /* sending by texture render job and needed to properly update displaying
846                          * brush texture icon */
847                         ED_region_tag_redraw(ar);
848                         break;
849                 case NC_SCENE:
850                         switch (wmn->data) {
851                                 case ND_MODE:
852                                 case ND_RENDER_RESULT:
853                                 case ND_COMPO_RESULT:
854                                         ED_region_tag_redraw(ar);
855                                         break;
856                         }
857                         break;
858                 case NC_IMAGE:
859                         if (wmn->action != NA_PAINTING) {
860                                 ED_region_tag_redraw(ar);
861                         }
862                         break;
863                 case NC_NODE:
864                         ED_region_tag_redraw(ar);
865                         break;
866                 case NC_GPENCIL:
867                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
868                                 ED_region_tag_redraw(ar);
869                         }
870                         break;
871         }
872 }
873
874 /* *********************** scopes region ************************ */
875
876 /* add handlers, stuff you only do once or on area/region changes */
877 static void image_tools_region_init(wmWindowManager *wm, ARegion *ar)
878 {
879         wmKeyMap *keymap;
880
881         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
882         ED_region_panels_init(wm, ar);
883
884         keymap = WM_keymap_ensure(wm->defaultconf, "Image Generic", SPACE_IMAGE, 0);
885         WM_event_add_keymap_handler(&ar->handlers, keymap);
886 }
887
888 static void image_tools_region_draw(const bContext *C, ARegion *ar)
889 {
890         ED_region_panels(C, ar);
891 }
892
893 static void image_tools_region_listener(
894         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
895         wmNotifier *wmn, const Scene *UNUSED(scene))
896 {
897         /* context changes */
898         switch (wmn->category) {
899                 case NC_GPENCIL:
900                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
901                                 ED_region_tag_redraw(ar);
902                         }
903                         break;
904                 case NC_BRUSH:
905                         /* NA_SELECTED is used on brush changes */
906                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
907                                 ED_region_tag_redraw(ar);
908                         }
909                         break;
910                 case NC_SCENE:
911                         switch (wmn->data) {
912                                 case ND_MODE:
913                                 case ND_RENDER_RESULT:
914                                 case ND_COMPO_RESULT:
915                                         ED_region_tag_redraw(ar);
916                                         break;
917                         }
918                         break;
919                 case NC_IMAGE:
920                         if (wmn->action != NA_PAINTING) {
921                                 ED_region_tag_redraw(ar);
922                         }
923                         break;
924                 case NC_NODE:
925                         ED_region_tag_redraw(ar);
926                         break;
927
928         }
929 }
930
931 /************************* header region **************************/
932
933 /* add handlers, stuff you only do once or on area/region changes */
934 static void image_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
935 {
936         ED_region_header_init(ar);
937 }
938
939 static void image_header_region_draw(const bContext *C, ARegion *ar)
940 {
941         ScrArea *sa = CTX_wm_area(C);
942         SpaceImage *sima = sa->spacedata.first;
943
944         image_user_refresh_scene(C, sima);
945
946         ED_region_header(C, ar);
947 }
948
949 static void image_header_region_listener(
950         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
951         wmNotifier *wmn, const Scene *UNUSED(scene))
952 {
953         /* context changes */
954         switch (wmn->category) {
955                 case NC_SCENE:
956                         switch (wmn->data) {
957                                 case ND_MODE:
958                                 case ND_TOOLSETTINGS:
959                                         ED_region_tag_redraw(ar);
960                                         break;
961                         }
962                         break;
963                 case NC_GEOM:
964                         switch (wmn->data) {
965                                 case ND_DATA:
966                                 case ND_SELECT:
967                                         ED_region_tag_redraw(ar);
968                                         break;
969                         }
970                         break;
971         }
972 }
973
974 static void image_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
975 {
976         SpaceImage *simg = (SpaceImage *)slink;
977
978         if (!ELEM(GS(old_id->name), ID_IM, ID_GD, ID_MSK)) {
979                 return;
980         }
981
982         if ((ID *)simg->image == old_id) {
983                 simg->image = (Image *)new_id;
984                 id_us_ensure_real(new_id);
985         }
986
987         if ((ID *)simg->gpd == old_id) {
988                 simg->gpd = (bGPdata *)new_id;
989                 id_us_min(old_id);
990                 id_us_plus(new_id);
991         }
992
993         if ((ID *)simg->mask_info.mask == old_id) {
994                 simg->mask_info.mask = (Mask *)new_id;
995                 id_us_ensure_real(new_id);
996         }
997 }
998
999 /**
1000  * \note Used for splitting out a subset of modes is more involved,
1001  * The previous non-uv-edit mode is stored so switching back to the
1002  * image doesn't always reset the sub-mode.
1003  */
1004 static int image_space_subtype_get(ScrArea *sa)
1005 {
1006         SpaceImage *sima = sa->spacedata.first;
1007         return sima->mode == SI_MODE_UV ? SI_MODE_UV : SI_MODE_VIEW;
1008 }
1009
1010 static void image_space_subtype_set(ScrArea *sa, int value)
1011 {
1012         SpaceImage *sima = sa->spacedata.first;
1013         if (value == SI_MODE_UV) {
1014                 if (sima->mode != SI_MODE_UV) {
1015                         sima->mode_prev = sima->mode;
1016                 }
1017                 sima->mode = value;
1018         }
1019         else {
1020                 sima->mode = sima->mode_prev;
1021         }
1022 }
1023
1024 static void image_space_subtype_item_extend(
1025         bContext *UNUSED(C), EnumPropertyItem **item, int *totitem)
1026 {
1027         RNA_enum_items_add(item, totitem, rna_enum_space_image_mode_items);
1028 }
1029
1030 /**************************** spacetype *****************************/
1031
1032 /* only called once, from space/spacetypes.c */
1033 void ED_spacetype_image(void)
1034 {
1035         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype image");
1036         ARegionType *art;
1037
1038         st->spaceid = SPACE_IMAGE;
1039         strncpy(st->name, "Image", BKE_ST_MAXNAME);
1040
1041         st->new = image_new;
1042         st->free = image_free;
1043         st->init = image_init;
1044         st->duplicate = image_duplicate;
1045         st->operatortypes = image_operatortypes;
1046         st->keymap = image_keymap;
1047         st->dropboxes = image_dropboxes;
1048         st->refresh = image_refresh;
1049         st->listener = image_listener;
1050         st->context = image_context;
1051         st->gizmos = image_widgets;
1052         st->id_remap = image_id_remap;
1053         st->space_subtype_item_extend = image_space_subtype_item_extend;
1054         st->space_subtype_get = image_space_subtype_get;
1055         st->space_subtype_set = image_space_subtype_set;
1056
1057         /* regions: main window */
1058         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1059         art->regionid = RGN_TYPE_WINDOW;
1060         art->keymapflag = ED_KEYMAP_GIZMO | ED_KEYMAP_TOOL | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
1061         art->init = image_main_region_init;
1062         art->draw = image_main_region_draw;
1063         art->listener = image_main_region_listener;
1064         BLI_addhead(&st->regiontypes, art);
1065
1066         /* regions: listview/buttons/scopes */
1067         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1068         art->regionid = RGN_TYPE_UI;
1069         art->prefsizex = 220; // XXX
1070         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1071         art->listener = image_buttons_region_listener;
1072         art->init = image_buttons_region_init;
1073         art->draw = image_buttons_region_draw;
1074         BLI_addhead(&st->regiontypes, art);
1075
1076         ED_uvedit_buttons_register(art);
1077         image_buttons_register(art);
1078
1079         /* regions: tool(bar) */
1080         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1081         art->regionid = RGN_TYPE_TOOLS;
1082         art->prefsizex = 58; /* XXX */
1083         art->prefsizey = 50; /* XXX */
1084         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1085         art->listener = image_tools_region_listener;
1086         art->message_subscribe = ED_region_generic_tools_region_message_subscribe;
1087         art->snap_size = ED_region_generic_tools_region_snap_size;
1088         art->init = image_tools_region_init;
1089         art->draw = image_tools_region_draw;
1090         BLI_addhead(&st->regiontypes, art);
1091
1092         /* regions: header */
1093         art = MEM_callocN(sizeof(ARegionType), "spacetype image region");
1094         art->regionid = RGN_TYPE_HEADER;
1095         art->prefsizey = HEADERY;
1096         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1097         art->listener = image_header_region_listener;
1098         art->init = image_header_region_init;
1099         art->draw = image_header_region_draw;
1100
1101         BLI_addhead(&st->regiontypes, art);
1102
1103         /* regions: hud */
1104         art = ED_area_type_hud(st->spaceid);
1105         BLI_addhead(&st->regiontypes, art);
1106
1107         BKE_spacetype_register(st);
1108 }