2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/screen.c
33 # include "BLI_winstuff.h"
40 #include "MEM_guardedalloc.h"
42 #include "GPU_compositing.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_view3d_types.h"
49 #include "BLI_listbase.h"
50 #include "BLI_utildefines.h"
53 #include "BKE_idprop.h"
54 #include "BKE_screen.h"
56 /* ************ Spacetype/regiontype handling ************** */
58 /* keep global; this has to be accessible outside of windowmanager */
59 static ListBase spacetypes = {NULL, NULL};
61 /* not SpaceType itself */
62 static void spacetype_free(SpaceType *st)
68 for (art = st->regiontypes.first; art; art = art->next) {
69 BLI_freelistN(&art->drawcalls);
71 for (pt = art->paneltypes.first; pt; pt = pt->next)
73 pt->ext.free(pt->ext.data);
75 for (ht = art->headertypes.first; ht; ht = ht->next)
77 ht->ext.free(ht->ext.data);
79 BLI_freelistN(&art->paneltypes);
80 BLI_freelistN(&art->headertypes);
83 BLI_freelistN(&st->regiontypes);
84 BLI_freelistN(&st->toolshelf);
88 void BKE_spacetypes_free(void)
92 for (st = spacetypes.first; st; st = st->next) {
96 BLI_freelistN(&spacetypes);
99 SpaceType *BKE_spacetype_from_id(int spaceid)
103 for (st = spacetypes.first; st; st = st->next) {
104 if (st->spaceid == spaceid)
110 ARegionType *BKE_regiontype_from_id(SpaceType *st, int regionid)
114 for (art = st->regiontypes.first; art; art = art->next)
115 if (art->regionid == regionid)
118 printf("Error, region type %d missing in - name:\"%s\", id:%d\n", regionid, st->name, st->spaceid);
119 return st->regiontypes.first;
123 const ListBase *BKE_spacetypes_list(void)
128 void BKE_spacetype_register(SpaceType *st)
133 stype = BKE_spacetype_from_id(st->spaceid);
135 printf("error: redefinition of spacetype %s\n", stype->name);
136 spacetype_free(stype);
140 BLI_addtail(&spacetypes, st);
143 bool BKE_spacetype_exists(int spaceid)
145 return BKE_spacetype_from_id(spaceid) != NULL;
148 /* ***************** Space handling ********************** */
150 void BKE_spacedata_freelist(ListBase *lb)
155 for (sl = lb->first; sl; sl = sl->next) {
156 SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
158 /* free regions for pushed spaces */
159 for (ar = sl->regionbase.first; ar; ar = ar->next)
160 BKE_area_region_free(st, ar);
162 BLI_freelistN(&sl->regionbase);
171 ARegion *BKE_area_region_copy(SpaceType *st, ARegion *ar)
173 ARegion *newar = MEM_dupallocN(ar);
174 Panel *pa, *newpa, *patab;
176 newar->prev = newar->next = NULL;
177 BLI_listbase_clear(&newar->handlers);
178 BLI_listbase_clear(&newar->uiblocks);
179 BLI_listbase_clear(&newar->panels_category);
180 BLI_listbase_clear(&newar->panels_category_active);
181 BLI_listbase_clear(&newar->ui_lists);
183 newar->regiontimer = NULL;
184 newar->headerstr = NULL;
186 /* use optional regiondata callback */
187 if (ar->regiondata) {
188 ARegionType *art = BKE_regiontype_from_id(st, ar->regiontype);
190 if (art && art->duplicate)
191 newar->regiondata = art->duplicate(ar->regiondata);
193 newar->regiondata = MEM_dupallocN(ar->regiondata);
196 if (ar->v2d.tab_offset)
197 newar->v2d.tab_offset = MEM_dupallocN(ar->v2d.tab_offset);
199 BLI_listbase_clear(&newar->panels);
200 BLI_duplicatelist(&newar->panels, &ar->panels);
202 BLI_listbase_clear(&newar->ui_previews);
203 BLI_duplicatelist(&newar->ui_previews, &ar->ui_previews);
205 /* copy panel pointers */
206 for (newpa = newar->panels.first; newpa; newpa = newpa->next) {
207 patab = newar->panels.first;
208 pa = ar->panels.first;
210 if (newpa->paneltab == pa) {
211 newpa->paneltab = patab;
223 /* from lb2 to lb1, lb1 is supposed to be freed */
224 static void region_copylist(SpaceType *st, ListBase *lb1, ListBase *lb2)
229 BLI_listbase_clear(lb1);
231 for (ar = lb2->first; ar; ar = ar->next) {
232 ARegion *arnew = BKE_area_region_copy(st, ar);
233 BLI_addtail(lb1, arnew);
238 /* lb1 should be empty */
239 void BKE_spacedata_copylist(ListBase *lb1, ListBase *lb2)
243 BLI_listbase_clear(lb1); /* to be sure */
245 for (sl = lb2->first; sl; sl = sl->next) {
246 SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
248 if (st && st->duplicate) {
249 SpaceLink *slnew = st->duplicate(sl);
251 BLI_addtail(lb1, slnew);
253 region_copylist(st, &slnew->regionbase, &sl->regionbase);
258 /* facility to set locks for drawing to survive (render) threads accessing drawing data */
259 /* lock can become bitflag too */
260 /* should be replaced in future by better local data handling for threads */
261 void BKE_spacedata_draw_locks(int set)
265 for (st = spacetypes.first; st; st = st->next) {
268 for (art = st->regiontypes.first; art; art = art->next) {
270 art->do_lock = art->lock;
272 art->do_lock = false;
277 static void (*spacedata_id_remap_cb)(struct ScrArea *sa, struct SpaceLink *sl, ID *old_id, ID *new_id) = NULL;
279 void BKE_spacedata_callback_id_remap_set(void (*func)(ScrArea *sa, SpaceLink *sl, ID *, ID *))
281 spacedata_id_remap_cb = func;
285 void BKE_spacedata_id_unref(struct ScrArea *sa, struct SpaceLink *sl, struct ID *id)
287 if (spacedata_id_remap_cb) {
288 spacedata_id_remap_cb(sa, sl, id, NULL);
292 /* not region itself */
293 void BKE_area_region_free(SpaceType *st, ARegion *ar)
298 ARegionType *art = BKE_regiontype_from_id(st, ar->regiontype);
300 if (art && art->free)
304 printf("regiondata free error\n");
306 else if (ar->type && ar->type->free)
309 if (ar->v2d.tab_offset) {
310 MEM_freeN(ar->v2d.tab_offset);
311 ar->v2d.tab_offset = NULL;
314 if (!BLI_listbase_is_empty(&ar->panels)) {
316 for (pa = ar->panels.first; pa; pa = pa_next) {
318 if (pa->activedata) {
319 MEM_freeN(pa->activedata);
325 for (uilst = ar->ui_lists.first; uilst; uilst = uilst->next) {
326 if (uilst->dyn_data) {
327 uiListDyn *dyn_data = uilst->dyn_data;
328 if (dyn_data->items_filter_flags) {
329 MEM_freeN(dyn_data->items_filter_flags);
331 if (dyn_data->items_filter_neworder) {
332 MEM_freeN(dyn_data->items_filter_neworder);
336 if (uilst->properties) {
337 IDP_FreeProperty(uilst->properties);
338 MEM_freeN(uilst->properties);
341 BLI_freelistN(&ar->ui_lists);
342 BLI_freelistN(&ar->ui_previews);
343 BLI_freelistN(&ar->panels_category);
344 BLI_freelistN(&ar->panels_category_active);
347 /* not area itself */
348 void BKE_screen_area_free(ScrArea *sa)
350 SpaceType *st = BKE_spacetype_from_id(sa->spacetype);
353 for (ar = sa->regionbase.first; ar; ar = ar->next)
354 BKE_area_region_free(st, ar);
356 BLI_freelistN(&sa->regionbase);
358 BKE_spacedata_freelist(&sa->spacedata);
360 BLI_freelistN(&sa->actionzones);
363 /** Free (or release) any data used by this screen (does not free the screen itself). */
364 void BKE_screen_free(bScreen *sc)
369 /* No animdata here. */
371 for (ar = sc->regionbase.first; ar; ar = ar->next)
372 BKE_area_region_free(NULL, ar);
374 BLI_freelistN(&sc->regionbase);
376 for (sa = sc->areabase.first; sa; sa = san) {
378 BKE_screen_area_free(sa);
381 BLI_freelistN(&sc->vertbase);
382 BLI_freelistN(&sc->edgebase);
383 BLI_freelistN(&sc->areabase);
385 /* Region and timer are freed by the window manager. */
386 MEM_SAFE_FREE(sc->tool_tip);
390 unsigned int BKE_screen_visible_layers(bScreen *screen, Scene *scene)
393 unsigned int layer = 0;
396 /* get all used view3d layers */
397 for (sa = screen->areabase.first; sa; sa = sa->next)
398 if (sa->spacetype == SPACE_VIEW3D)
399 layer |= ((View3D *)sa->spacedata.first)->lay;
408 /* ***************** Utilities ********************** */
410 /* Find a region of the specified type from the given area */
411 ARegion *BKE_area_find_region_type(ScrArea *sa, int type)
416 for (ar = sa->regionbase.first; ar; ar = ar->next) {
417 if (ar->regiontype == type)
424 ARegion *BKE_area_find_region_active_win(ScrArea *sa)
427 ARegion *ar = BLI_findlink(&sa->regionbase, sa->region_active_win);
428 if (ar && (ar->regiontype == RGN_TYPE_WINDOW)) {
432 /* fallback to any */
433 return BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
438 ARegion *BKE_area_find_region_xy(ScrArea *sa, const int regiontype, int x, int y)
440 ARegion *ar_found = NULL;
443 for (ar = sa->regionbase.first; ar; ar = ar->next) {
444 if ((regiontype == RGN_TYPE_ANY) || (ar->regiontype == regiontype)) {
445 if (BLI_rcti_isect_pt(&ar->winrct, x, y)) {
456 * \note, ideally we can get the area from the context,
457 * there are a few places however where this isn't practical.
459 ScrArea *BKE_screen_find_area_from_space(struct bScreen *sc, SpaceLink *sl)
463 for (sa = sc->areabase.first; sa; sa = sa->next) {
464 if (BLI_findindex(&sa->spacedata, sl) != -1) {
473 * \note Using this function is generally a last resort, you really want to be
474 * using the context when you can - campbell
476 ScrArea *BKE_screen_find_big_area(bScreen *sc, const int spacetype, const short min)
478 ScrArea *sa, *big = NULL;
479 int size, maxsize = 0;
481 for (sa = sc->areabase.first; sa; sa = sa->next) {
482 if ((spacetype == SPACE_TYPE_ANY) || (sa->spacetype == spacetype)) {
483 if (min <= sa->winx && min <= sa->winy) {
484 size = sa->winx * sa->winy;
485 if (size > maxsize) {
496 ScrArea *BKE_screen_find_area_xy(bScreen *sc, const int spacetype, int x, int y)
498 ScrArea *sa, *sa_found = NULL;
500 for (sa = sc->areabase.first; sa; sa = sa->next) {
501 if (BLI_rcti_isect_pt(&sa->totrct, x, y)) {
502 if ((spacetype == SPACE_TYPE_ANY) || (sa->spacetype == spacetype)) {
513 * Utility function to get the active layer to use when adding new objects.
515 unsigned int BKE_screen_view3d_layer_active_ex(const View3D *v3d, const Scene *scene, bool use_localvd)
518 if ((v3d == NULL) || (v3d->scenelock && !v3d->localvd)) {
526 if (v3d && v3d->localvd) {
533 unsigned int BKE_screen_view3d_layer_active(const struct View3D *v3d, const struct Scene *scene)
535 return BKE_screen_view3d_layer_active_ex(v3d, scene, true);
539 * Accumulate all visible layers on this screen.
541 unsigned int BKE_screen_view3d_layer_all(const bScreen *sc)
544 unsigned int lay = 0;
545 for (sa = sc->areabase.first; sa; sa = sa->next) {
546 if (sa->spacetype == SPACE_VIEW3D) {
547 View3D *v3d = sa->spacedata.first;
555 void BKE_screen_view3d_sync(View3D *v3d, struct Scene *scene)
559 if (v3d->scenelock && v3d->localvd == NULL) {
560 v3d->lay = scene->lay;
561 v3d->camera = scene->camera;
563 if (v3d->camera == NULL) {
566 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
567 if (ar->regiontype == RGN_TYPE_WINDOW) {
568 RegionView3D *rv3d = ar->regiondata;
569 if (rv3d->persp == RV3D_CAMOB)
570 rv3d->persp = RV3D_PERSP;
575 if ((v3d->lay & v3d->layact) == 0) {
576 for (bit = 0; bit < 32; bit++) {
577 if (v3d->lay & (1u << bit)) {
578 v3d->layact = (1u << bit);
586 void BKE_screen_view3d_scene_sync(bScreen *sc)
588 /* are there cameras in the views that are not in the scene? */
590 for (sa = sc->areabase.first; sa; sa = sa->next) {
592 for (sl = sa->spacedata.first; sl; sl = sl->next) {
593 if (sl->spacetype == SPACE_VIEW3D) {
594 View3D *v3d = (View3D *) sl;
595 BKE_screen_view3d_sync(v3d, sc->scene);
601 void BKE_screen_view3d_main_sync(ListBase *screen_lb, Scene *scene)
607 /* from scene copy to the other views */
608 for (sc = screen_lb->first; sc; sc = sc->id.next) {
609 if (sc->scene != scene)
612 for (sa = sc->areabase.first; sa; sa = sa->next)
613 for (sl = sa->spacedata.first; sl; sl = sl->next)
614 if (sl->spacetype == SPACE_VIEW3D)
615 BKE_screen_view3d_sync((View3D *)sl, scene);
619 void BKE_screen_view3d_twmode_remove(View3D *v3d, const int i)
621 const int selected_index = (v3d->twmode - V3D_MANIP_CUSTOM);
622 if (selected_index == i) {
623 v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global */
625 else if (selected_index > i) {
630 void BKE_screen_view3d_main_twmode_remove(ListBase *screen_lb, Scene *scene, const int i)
634 for (sc = screen_lb->first; sc; sc = sc->id.next) {
635 if (sc->scene == scene) {
637 for (sa = sc->areabase.first; sa; sa = sa->next) {
639 for (sl = sa->spacedata.first; sl; sl = sl->next) {
640 if (sl->spacetype == SPACE_VIEW3D) {
641 View3D *v3d = (View3D *)sl;
642 BKE_screen_view3d_twmode_remove(v3d, i);
650 /* magic zoom calculation, no idea what
651 * it signifies, if you find out, tell me! -zr
654 /* simple, its magic dude!
655 * well, to be honest, this gives a natural feeling zooming
656 * with multiple keypad presses (ton)
658 float BKE_screen_view3d_zoom_to_fac(float camzoom)
660 return powf(((float)M_SQRT2 + camzoom / 50.0f), 2.0f) / 4.0f;
663 float BKE_screen_view3d_zoom_from_fac(float zoomfac)
665 return ((sqrtf(4.0f * zoomfac) - (float)M_SQRT2) * 50.0f);
668 void BKE_screen_gpu_fx_validate(GPUFXSettings *fx_settings)
670 /* currently we use DOF from the camera _only_,
671 * so we never allocate this, only copy from the Camera */
673 if ((fx_settings->dof == NULL) &&
674 (fx_settings->fx_flag & GPU_FX_FLAG_DOF))
676 GPUDOFSettings *fx_dof;
677 fx_dof = fx_settings->dof = MEM_callocN(sizeof(GPUDOFSettings), __func__);
681 if ((fx_settings->ssao == NULL) &&
682 (fx_settings->fx_flag & GPU_FX_FLAG_SSAO))
684 GPUSSAOSettings *fx_ssao;
685 fx_ssao = fx_settings->ssao = MEM_callocN(sizeof(GPUSSAOSettings), __func__);
687 GPU_fx_compositor_init_ssao_settings(fx_ssao);