gtkpixelcache.c 12.7 KB
Newer Older
Alexander Larsson's avatar
Alexander Larsson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GTK - The GIMP Toolkit
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

20
#include "gtkdebug.h"
Alexander Larsson's avatar
Alexander Larsson committed
21 22
#include "gtkpixelcacheprivate.h"

23 24
#define BLOW_CACHE_TIMEOUT_SEC 20

Alexander Larsson's avatar
Alexander Larsson committed
25 26
/* The extra size of the offscreen surface we allocate
   to make scrolling more efficient */
27
#define DEFAULT_EXTRA_SIZE 64
Alexander Larsson's avatar
Alexander Larsson committed
28

29
/* When resizing viewport we allow this extra
Alexander Larsson's avatar
Alexander Larsson committed
30
   size to avoid constantly reallocating when resizing */
31
#define ALLOW_SMALLER_SIZE 32
Alexander Larsson's avatar
Alexander Larsson committed
32 33 34 35
#define ALLOW_LARGER_SIZE 32

struct _GtkPixelCache {
  cairo_surface_t *surface;
36
  cairo_content_t content;
Alexander Larsson's avatar
Alexander Larsson committed
37 38 39 40 41 42

  /* Valid if surface != NULL */
  int surface_x;
  int surface_y;
  int surface_w;
  int surface_h;
43
  double surface_scale;
Alexander Larsson's avatar
Alexander Larsson committed
44 45 46

  /* may be null if not dirty */
  cairo_region_t *surface_dirty;
47 48

  guint timeout_tag;
49 50 51

  guint extra_width;
  guint extra_height;
Alexander Larsson's avatar
Alexander Larsson committed
52 53 54 55 56 57 58 59
};

GtkPixelCache *
_gtk_pixel_cache_new ()
{
  GtkPixelCache *cache;

  cache = g_new0 (GtkPixelCache, 1);
60 61
  cache->extra_width = DEFAULT_EXTRA_SIZE;
  cache->extra_height = DEFAULT_EXTRA_SIZE;
Alexander Larsson's avatar
Alexander Larsson committed
62 63 64 65 66 67 68 69 70 71

  return cache;
}

void
_gtk_pixel_cache_free (GtkPixelCache *cache)
{
  if (cache == NULL)
    return;

72 73 74
  if (cache->timeout_tag)
    g_source_remove (cache->timeout_tag);

Alexander Larsson's avatar
Alexander Larsson committed
75 76 77 78 79 80 81 82 83
  if (cache->surface != NULL)
    cairo_surface_destroy (cache->surface);

  if (cache->surface_dirty != NULL)
    cairo_region_destroy (cache->surface_dirty);

  g_free (cache);
}

84 85 86 87 88 89 90 91 92
void
_gtk_pixel_cache_set_extra_size (GtkPixelCache *cache,
                                 guint          extra_width,
                                 guint          extra_height)
{
  cache->extra_width = extra_width ? extra_width : DEFAULT_EXTRA_SIZE;
  cache->extra_height = extra_height ? extra_height : DEFAULT_EXTRA_SIZE;
}

93 94 95 96 97 98 99 100 101 102 103 104
void
_gtk_pixel_cache_get_extra_size (GtkPixelCache *cache,
                                 guint         *extra_width,
                                 guint         *extra_height)
{
  if (extra_width)
    *extra_width = cache->extra_width;

  if (extra_height)
    *extra_height = cache->extra_height;
}

105 106 107 108 109 110 111 112
void
_gtk_pixel_cache_set_content (GtkPixelCache   *cache,
			      cairo_content_t  content)
{
  cache->content = content;
  _gtk_pixel_cache_invalidate (cache, NULL);
}

Alexander Larsson's avatar
Alexander Larsson committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* Region is in canvas coordinates */
void
_gtk_pixel_cache_invalidate (GtkPixelCache *cache,
			     cairo_region_t *region)
{
  cairo_rectangle_int_t r;
  cairo_region_t *free_region = NULL;

  if (cache->surface == NULL ||
      (region != NULL && cairo_region_is_empty (region)))
    return;

  if (region == NULL)
    {
      r.x = cache->surface_x;
      r.y = cache->surface_y;
      r.width = cache->surface_w;
      r.height = cache->surface_h;

      free_region = region =
	cairo_region_create_rectangle (&r);
    }

  if (cache->surface_dirty == NULL)
    {
      cache->surface_dirty = cairo_region_copy (region);
      cairo_region_translate (cache->surface_dirty,
			      -cache->surface_x,
			      -cache->surface_y);
    }
  else
    {
      cairo_region_translate (region,
			      -cache->surface_x,
			      -cache->surface_y);
      cairo_region_union (cache->surface_dirty, region);
      cairo_region_translate (region,
			      cache->surface_x,
			      cache->surface_y);
    }

  if (free_region)
    cairo_region_destroy (free_region);

  r.x = 0;
  r.y = 0;
  r.width = cache->surface_w;
  r.height = cache->surface_h;

  cairo_region_intersect_rectangle (cache->surface_dirty, &r);
}

static void
_gtk_pixel_cache_create_surface_if_needed (GtkPixelCache         *cache,
					   GdkWindow             *window,
					   cairo_rectangle_int_t *view_rect,
					   cairo_rectangle_int_t *canvas_rect)
{
  cairo_rectangle_int_t rect;
  int surface_w, surface_h;
  cairo_content_t content;
  cairo_pattern_t *bg;
  double red, green, blue, alpha;

177 178 179 180 181
#ifdef G_ENABLE_DEBUG
  if (gtk_get_debug_flags () & GTK_DEBUG_NO_PIXEL_CACHE)
    return;
#endif

182 183 184 185 186 187 188 189 190 191 192
  content = cache->content;
  if (!content)
    {
      content = CAIRO_CONTENT_COLOR_ALPHA;
      bg = gdk_window_get_background_pattern (window);
      if (bg != NULL &&
          cairo_pattern_get_type (bg) == CAIRO_PATTERN_TYPE_SOLID &&
          cairo_pattern_get_rgba (bg, &red, &green, &blue, &alpha) == CAIRO_STATUS_SUCCESS &&
          alpha == 1.0)
        content = CAIRO_CONTENT_COLOR;
    }
Alexander Larsson's avatar
Alexander Larsson committed
193 194 195

  surface_w = view_rect->width;
  if (canvas_rect->width > surface_w)
196
    surface_w = MIN (surface_w + cache->extra_width, canvas_rect->width);
Alexander Larsson's avatar
Alexander Larsson committed
197 198 199

  surface_h = view_rect->height;
  if (canvas_rect->height > surface_h)
200
    surface_h = MIN (surface_h + cache->extra_height, canvas_rect->height);
Alexander Larsson's avatar
Alexander Larsson committed
201 202 203 204

  /* If current surface can't fit view_rect or is too large, kill it */
  if (cache->surface != NULL &&
      (cairo_surface_get_content (cache->surface) != content ||
205
       cache->surface_w < MAX(view_rect->width, surface_w - ALLOW_SMALLER_SIZE) ||
Alexander Larsson's avatar
Alexander Larsson committed
206
       cache->surface_w > surface_w + ALLOW_LARGER_SIZE ||
207
       cache->surface_h < MAX(view_rect->height, surface_h - ALLOW_SMALLER_SIZE) ||
208 209
       cache->surface_h > surface_h + ALLOW_LARGER_SIZE ||
       cache->surface_scale != gdk_window_get_scale_factor (window)))
Alexander Larsson's avatar
Alexander Larsson committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    {
      cairo_surface_destroy (cache->surface);
      cache->surface = NULL;
      if (cache->surface_dirty)
	cairo_region_destroy (cache->surface_dirty);
      cache->surface_dirty = NULL;
    }

  /* Don't allocate a surface if view >= canvas, as we won't
     be scrolling then anyway */
  if (cache->surface == NULL &&
      (view_rect->width < canvas_rect->width ||
       view_rect->height < canvas_rect->height))
    {
      cache->surface_x = -canvas_rect->x;
      cache->surface_y = -canvas_rect->y;
      cache->surface_w = surface_w;
      cache->surface_h = surface_h;
228
      cache->surface_scale = gdk_window_get_scale_factor (window);
Alexander Larsson's avatar
Alexander Larsson committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

      cache->surface =
	gdk_window_create_similar_surface (window, content,
					   surface_w, surface_h);
      rect.x = 0;
      rect.y = 0;
      rect.width = surface_w;
      rect.height = surface_h;
      cache->surface_dirty =
	cairo_region_create_rectangle (&rect);
    }
}

void
_gtk_pixel_cache_set_position (GtkPixelCache         *cache,
			       cairo_rectangle_int_t *view_rect,
			       cairo_rectangle_int_t *canvas_rect)
{
  cairo_rectangle_int_t r, view_pos;
  cairo_region_t *copy_region;
  int new_surf_x, new_surf_y;
  cairo_t *backing_cr;

  if (cache->surface == NULL)
    return;

  /* Position of view inside canvas */
  view_pos.x = -canvas_rect->x;
  view_pos.y = -canvas_rect->y;
  view_pos.width = view_rect->width;
  view_pos.height = view_rect->height;

  /* Reposition so all is visible */
  if (view_pos.x < cache->surface_x ||
      view_pos.x + view_pos.width >
      cache->surface_x + cache->surface_w ||
      view_pos.y < cache->surface_y ||
      view_pos.y + view_pos.height >
      cache->surface_y + cache->surface_h)
    {
      new_surf_x = cache->surface_x;
      if (view_pos.x < cache->surface_x)
	new_surf_x = MAX (view_pos.x + view_pos.width - cache->surface_w, 0);
      else if (view_pos.x + view_pos.width >
	       cache->surface_x + cache->surface_w)
	new_surf_x = MIN (view_pos.x, canvas_rect->width - cache->surface_w);

      new_surf_y = cache->surface_y;
      if (view_pos.y < cache->surface_y)
	new_surf_y = MAX (view_pos.y + view_pos.height - cache->surface_h, 0);
      else if (view_pos.y + view_pos.height >
	       cache->surface_y + cache->surface_h)
	new_surf_y = MIN (view_pos.y, canvas_rect->height - cache->surface_h);

      r.x = 0;
      r.y = 0;
      r.width = cache->surface_w;
      r.height = cache->surface_h;
      copy_region = cairo_region_create_rectangle (&r);

      if (cache->surface_dirty)
	{
	  cairo_region_subtract (copy_region, cache->surface_dirty);
	  cairo_region_destroy (cache->surface_dirty);
	  cache->surface_dirty = NULL;
	}

      cairo_region_translate (copy_region,
			      cache->surface_x - new_surf_x,
			      cache->surface_y - new_surf_y);
      cairo_region_intersect_rectangle (copy_region, &r);

      backing_cr = cairo_create (cache->surface);
      gdk_cairo_region (backing_cr, copy_region);
      cairo_set_operator (backing_cr, CAIRO_OPERATOR_SOURCE);
      cairo_clip (backing_cr);
      cairo_push_group (backing_cr);
      cairo_set_source_surface (backing_cr, cache->surface,
				cache->surface_x - new_surf_x,
				cache->surface_y - new_surf_y);
      cairo_paint (backing_cr);
      cairo_pop_group_to_source (backing_cr);
      cairo_paint (backing_cr);
      cairo_destroy (backing_cr);

      cache->surface_x = new_surf_x;
      cache->surface_y = new_surf_y;

      cairo_region_xor_rectangle (copy_region, &r);
      cache->surface_dirty = copy_region;
    }
}

void
_gtk_pixel_cache_repaint (GtkPixelCache *cache,
			  GtkPixelCacheDrawFunc draw,
			  cairo_rectangle_int_t *view_rect,
			  cairo_rectangle_int_t *canvas_rect,
			  gpointer user_data)
{
  cairo_t *backing_cr;
330 331
  cairo_region_t *region_dirty = cache->surface_dirty;
  cache->surface_dirty = NULL;
Alexander Larsson's avatar
Alexander Larsson committed
332 333

  if (cache->surface &&
334 335
      region_dirty &&
      !cairo_region_is_empty (region_dirty))
Alexander Larsson's avatar
Alexander Larsson committed
336 337
    {
      backing_cr = cairo_create (cache->surface);
338
      gdk_cairo_region (backing_cr, region_dirty);
Alexander Larsson's avatar
Alexander Larsson committed
339 340 341 342
      cairo_clip (backing_cr);
      cairo_translate (backing_cr,
		       -cache->surface_x - canvas_rect->x - view_rect->x,
		       -cache->surface_y - canvas_rect->y - view_rect->y);
343 344

      cairo_save (backing_cr);
Alexander Larsson's avatar
Alexander Larsson committed
345 346 347 348
      cairo_set_source_rgba (backing_cr,
			     0.0, 0, 0, 0.0);
      cairo_set_operator (backing_cr, CAIRO_OPERATOR_SOURCE);
      cairo_paint (backing_cr);
349
      cairo_restore (backing_cr);
Alexander Larsson's avatar
Alexander Larsson committed
350

351
      cairo_save (backing_cr);
Alexander Larsson's avatar
Alexander Larsson committed
352
      draw (backing_cr, user_data);
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
      cairo_restore (backing_cr);

#ifdef G_ENABLE_DEBUG
      if (gtk_get_debug_flags () & GTK_DEBUG_PIXEL_CACHE)
	{
	  GdkRGBA colors[] = {
	    { 1, 0, 0, 0.08},
	    { 0, 1, 0, 0.08},
	    { 0, 0, 1, 0.08},
	    { 1, 0, 1, 0.08},
	    { 1, 1, 0, 0.08},
	    { 0, 1, 1, 0.08},
	  };
	  static int current_color = 0;

	  gdk_cairo_set_source_rgba (backing_cr, &colors[(current_color++) % G_N_ELEMENTS (colors)]);
	  cairo_paint (backing_cr);
	}
#endif
Alexander Larsson's avatar
Alexander Larsson committed
372 373 374 375

      cairo_destroy (backing_cr);
    }

376 377
  if (region_dirty)
    cairo_region_destroy (region_dirty);
Alexander Larsson's avatar
Alexander Larsson committed
378 379
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
static gboolean
blow_cache_cb  (gpointer user_data)
{
  GtkPixelCache *cache = user_data;

  cache->timeout_tag = 0;

  if (cache->surface)
    {
      cairo_surface_destroy (cache->surface);
      cache->surface = NULL;
      if (cache->surface_dirty)
	cairo_region_destroy (cache->surface_dirty);
      cache->surface_dirty = NULL;
    }

  return G_SOURCE_REMOVE;
}

399 400 401 402 403 404 405 406 407 408 409 410 411
static gboolean
context_is_unscaled (cairo_t *cr)
{
  cairo_matrix_t matrix;
  gdouble x, y;

  x = y = 1;
  cairo_get_matrix (cr, &matrix);
  cairo_matrix_transform_distance (&matrix, &x, &y);

  return x == 1 && y == 1;
}

412

Alexander Larsson's avatar
Alexander Larsson committed
413 414 415 416 417 418 419 420 421 422 423
void
_gtk_pixel_cache_draw (GtkPixelCache *cache,
		       cairo_t *cr,
		       GdkWindow *window,
		       /* View position in widget coords */
		       cairo_rectangle_int_t *view_rect,
		       /* Size and position of canvas in view coords */
		       cairo_rectangle_int_t *canvas_rect,
		       GtkPixelCacheDrawFunc draw,
		       gpointer user_data)
{
424 425 426 427 428
  if (cache->timeout_tag)
    g_source_remove (cache->timeout_tag);

  cache->timeout_tag = g_timeout_add_seconds (BLOW_CACHE_TIMEOUT_SEC,
					      blow_cache_cb, cache);
429
  g_source_set_name_by_id (cache->timeout_tag, "[gtk+] blow_cache_cb");
430

Alexander Larsson's avatar
Alexander Larsson committed
431 432 433 434 435
  _gtk_pixel_cache_create_surface_if_needed (cache, window,
					     view_rect, canvas_rect);
  _gtk_pixel_cache_set_position (cache, view_rect, canvas_rect);
  _gtk_pixel_cache_repaint (cache, draw, view_rect, canvas_rect, user_data);

436
  if (cache->surface && context_is_unscaled (cr) &&
Alexander Larsson's avatar
Alexander Larsson committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
      /* Don't use backing surface if rendering elsewhere */
      cairo_surface_get_type (cache->surface) == cairo_surface_get_type (cairo_get_target (cr)))
    {
      cairo_save (cr);
      cairo_set_source_surface (cr, cache->surface,
				cache->surface_x + view_rect->x + canvas_rect->x,
				cache->surface_y + view_rect->y + canvas_rect->y);
      cairo_rectangle (cr, view_rect->x, view_rect->y,
		       view_rect->width, view_rect->height);
      cairo_fill (cr);
      cairo_restore (cr);
    }
  else
    {
      cairo_rectangle (cr,
		       view_rect->x, view_rect->y,
		       view_rect->width, view_rect->height);
      cairo_clip (cr);
      draw (cr, user_data);
    }
}