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