Skip to content

Commit 27de730

Browse files
committed
enable highdpi display
though mag display and compute needs doing too
1 parent ecc8c84 commit 27de730

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

src/imagedisplay.c

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ struct _Imagedisplay {
7979
double scale;
8080
double x, y;
8181

82+
/* The size of physical display pixels in gtk coordinates, eg. for a 200%
83+
* desktop this would be 0.5.
84+
*/
85+
double pixel_size;
86+
8287
/* Draw the screen in debug mode.
8388
*/
8489
gboolean debug;
@@ -126,6 +131,9 @@ enum {
126131
*/
127132
PROP_DEBUG,
128133

134+
/* Read out display density with this.
135+
*/
136+
PROP_PIXEL_SIZE,
129137
};
130138

131139
enum {
@@ -476,6 +484,8 @@ imagedisplay_set_property(GObject *object,
476484
{
477485
Imagedisplay *imagedisplay = (Imagedisplay *) object;
478486

487+
double d;
488+
479489
#ifdef DEBUG
480490
{
481491
g_autofree char *str = g_strdup_value_contents(value);
@@ -573,6 +583,15 @@ imagedisplay_set_property(GObject *object,
573583
gtk_widget_queue_draw(GTK_WIDGET(imagedisplay));
574584
break;
575585

586+
case PROP_PIXEL_SIZE:
587+
d = g_value_get_double(value);
588+
if (imagedisplay->pixel_size != d) {
589+
imagedisplay->pixel_size = d;
590+
imagedisplay_layout(imagedisplay);
591+
gtk_widget_queue_draw(GTK_WIDGET(imagedisplay));
592+
}
593+
break;
594+
576595
default:
577596
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
578597
break;
@@ -632,6 +651,10 @@ imagedisplay_get_property(GObject *object,
632651
g_value_set_boolean(value, imagedisplay->debug);
633652
break;
634653

654+
case PROP_PIXEL_SIZE:
655+
g_value_set_double(value, imagedisplay->pixel_size);
656+
break;
657+
635658
default:
636659
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
637660
break;
@@ -649,6 +672,15 @@ imagedisplay_snapshot(GtkWidget *widget, GtkSnapshot *snapshot)
649672

650673
GTK_WIDGET_CLASS(imagedisplay_parent_class)->snapshot(widget, snapshot);
651674

675+
/* This can change on each repaint as windows are dragged.
676+
*/
677+
GtkNative *native = gtk_widget_get_native(widget);
678+
GdkSurface *surface = gtk_native_get_surface(native);
679+
double pixel_size = 1.0 / gdk_surface_get_scale_factor(surface);
680+
g_object_set(imagedisplay, "pixel-size", pixel_size, NULL);
681+
682+
gtk_snapshot_scale(snapshot, pixel_size, pixel_size);
683+
652684
#ifdef HAVE_GTK_SNAPSHOT_SET_SNAP
653685
gtk_snapshot_set_snap(snapshot, GSK_RECT_SNAP_ROUND);
654686
#endif /*HAVE_GTK_SNAPSHOT_SET_SNAP*/
@@ -658,26 +690,21 @@ imagedisplay_snapshot(GtkWidget *widget, GtkSnapshot *snapshot)
658690
*/
659691
gtk_snapshot_push_clip(snapshot,
660692
&GRAPHENE_RECT_INIT(0, 0,
661-
gtk_widget_get_width(widget), gtk_widget_get_height(widget)));
693+
gtk_widget_get_width(widget) / pixel_size,
694+
gtk_widget_get_height(widget) / pixel_size));
662695

663696
graphene_rect_t paint;
664-
paint.origin.x = imagedisplay->paint_rect.left;
665-
paint.origin.y = imagedisplay->paint_rect.top;
666-
paint.size.width = imagedisplay->paint_rect.width;
667-
paint.size.height = imagedisplay->paint_rect.height;
668-
669-
/* If there's no gtk snapping, we do our own based on the hardware pixel
670-
* size for the surface this snapshot will be rendered to.
671-
*/
672-
GtkNative *native = gtk_widget_get_native(widget);
673-
GdkSurface *surface = gtk_native_get_surface(native);
674-
double pixel_size = 1.0 / gdk_surface_get_scale_factor(surface);
697+
paint.origin.x = imagedisplay->paint_rect.left / pixel_size;
698+
paint.origin.y = imagedisplay->paint_rect.top / pixel_size;
699+
paint.size.width = imagedisplay->paint_rect.width / pixel_size;
700+
paint.size.height = imagedisplay->paint_rect.height / pixel_size;
675701

676702
if (imagedisplay->tilecache &&
677703
imagedisplay->tilecache->n_levels > 0)
678704
tilecache_snapshot(imagedisplay->tilecache, snapshot,
679-
pixel_size,
680-
imagedisplay->scale, imagedisplay->x, imagedisplay->y,
705+
imagedisplay->scale / pixel_size,
706+
imagedisplay->x / pixel_size,
707+
imagedisplay->y / pixel_size,
681708
&paint, imagedisplay->debug);
682709

683710
// draw any overlays
@@ -828,6 +855,13 @@ imagedisplay_class_init(ImagedisplayClass *class)
828855
FALSE,
829856
G_PARAM_READWRITE));
830857

858+
g_object_class_install_property(gobject_class, PROP_PIXEL_SIZE,
859+
g_param_spec_double("pixel-size",
860+
_("Pixel size"),
861+
_("Size of hardware display pixels in gtk coordinates"),
862+
0.0, 10.0, 0.0,
863+
G_PARAM_READWRITE));
864+
831865
g_object_class_override_property(gobject_class,
832866
PROP_HADJUSTMENT, "hadjustment");
833867
g_object_class_override_property(gobject_class,

src/tilecache.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -841,14 +841,12 @@ tilecache_draw_bounds(GtkSnapshot *snapshot,
841841
* white lines on tile edges.
842842
*/
843843
static void
844-
tilecache_snap_rect_to_boundary(graphene_rect_t *bounds, double pixel_size)
844+
tilecache_snap_rect_to_boundary(graphene_rect_t *bounds)
845845
{
846-
double left = rint(bounds->origin.x * pixel_size) / pixel_size;
847-
double top = rint(bounds->origin.y * pixel_size) / pixel_size;
848-
double right =
849-
rint((bounds->origin.x + bounds->size.width) * pixel_size) / pixel_size;
850-
double bottom =
851-
rint((bounds->origin.y + bounds->size.height) * pixel_size) / pixel_size;
846+
double left = rint(bounds->origin.x);
847+
double top = rint(bounds->origin.y);
848+
double right = rint(bounds->origin.x + bounds->size.width);
849+
double bottom = rint(bounds->origin.y + bounds->size.height);
852850

853851
bounds->origin.x = left;
854852
bounds->origin.y = top;
@@ -860,16 +858,12 @@ tilecache_snap_rect_to_boundary(graphene_rect_t *bounds, double pixel_size)
860858
/* Scale is how much the level0 image has been scaled, x/y is the position of
861859
* the top-left corner of @paint in the scaled image.
862860
*
863-
* @pixel_scale is gdk_surface_get_scale() for the surface this snapshot will
864-
* be rendered to.
865-
*
866861
* @paint is the pixel area in gtk coordinates that we paint in the widget.
867862
*
868863
* Set debug to draw tile boundaries for debugging.
869864
*/
870865
void
871866
tilecache_snapshot(Tilecache *tilecache, GtkSnapshot *snapshot,
872-
double pixel_size,
873867
double scale, double x, double y, graphene_rect_t *paint, gboolean debug)
874868
{
875869
/* In debug mode, scale and offset so we can see tile clipping.
@@ -941,7 +935,7 @@ tilecache_snapshot(Tilecache *tilecache, GtkSnapshot *snapshot,
941935
*/
942936
graphene_rect_t backdrop = *paint;
943937
#ifndef HAVE_GTK_SNAPSHOT_SET_SNAP
944-
tilecache_snap_rect_to_boundary(&backdrop, pixel_size);
938+
tilecache_snap_rect_to_boundary(&backdrop);
945939
#endif /*!HAVE_GTK_SNAPSHOT_SET_SNAP*/
946940
gtk_snapshot_push_repeat(snapshot, &backdrop, NULL);
947941

@@ -965,7 +959,7 @@ tilecache_snapshot(Tilecache *tilecache, GtkSnapshot *snapshot,
965959
* blur the image. For zooming out, we want trilinear to get
966960
* mipmaps and antialiasing.
967961
*/
968-
GskScalingFilter filter = scale >= pixel_size ?
962+
GskScalingFilter filter = scale >= 1.0 ?
969963
GSK_SCALING_FILTER_NEAREST : GSK_SCALING_FILTER_TRILINEAR;
970964

971965
graphene_rect_t bounds;
@@ -976,7 +970,7 @@ tilecache_snapshot(Tilecache *tilecache, GtkSnapshot *snapshot,
976970
bounds.size.height = tile->bounds0.height * scale;
977971

978972
#ifndef HAVE_GTK_SNAPSHOT_SET_SNAP
979-
tilecache_snap_rect_to_boundary(&bounds, pixel_size);
973+
tilecache_snap_rect_to_boundary(&bounds);
980974
#endif /*!HAVE_GTK_SNAPSHOT_SET_SNAP*/
981975

982976
gtk_snapshot_append_scaled_texture(snapshot,

src/tilecache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ Tilecache *tilecache_new();
110110
/* Render the tiles to a snapshot.
111111
*/
112112
void tilecache_snapshot(Tilecache *tilecache, GtkSnapshot *snapshot,
113-
double pixel_size,
114113
double scale, double x, double y, graphene_rect_t *paint, gboolean debug);
115114

116115
#endif /*__TILECACHE_H*/

0 commit comments

Comments
 (0)