1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <sys/types.h>
31 #include "gdk.h" /* For gdk_flush() */
33 #include "gdkprivate.h"
34 #include "gdkinternals.h" /* For scratch_image code */
38 * gdk_image_set_colormap:
40 * @colormap: a #GdkColormap
42 * Sets the colormap for the image to the given colormap. Normally
43 * there's no need to use this function, images are created with the
44 * correct colormap if you get the image from a drawable. If you
45 * create the image from scratch, use the colormap of the drawable you
46 * intend to render the image to.
49 gdk_image_set_colormap (GdkImage *image,
50 GdkColormap *colormap)
52 g_return_if_fail (GDK_IS_IMAGE (image));
53 g_return_if_fail (GDK_IS_COLORMAP (colormap));
55 if (image->colormap != colormap)
58 g_object_unref (image->colormap);
60 image->colormap = colormap;
61 g_object_ref (image->colormap);
66 * gdk_image_get_colormap:
69 * Retrieves the colormap for a given image, if it exists. An image
70 * will have a colormap if the drawable from which it was created has
71 * a colormap, or if a colormap was set explicitely with
72 * gdk_image_set_colormap().
74 * Return value: colormap for the image
77 gdk_image_get_colormap (GdkImage *image)
79 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
81 return image->colormap;
84 /* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
85 * up between n_images different images. possible_n_images gives
86 * various divisors of N_REGIONS. The reason for allowing this
87 * flexibility is that we want to create as few images as possible,
88 * but we want to deal with the abberant systems that have a SHMMAX
91 * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
93 * (Are there any such?)
96 static const int possible_n_images[] = { 1, 2, 3, 6 };
98 /* We allocate one GdkScratchImageInfo structure for each
99 * depth where we are allocating scratch images. (Future: one
100 * per depth, per display)
102 typedef struct _GdkScratchImageInfo GdkScratchImageInfo;
104 struct _GdkScratchImageInfo {
108 GdkImage *static_image[N_REGIONS];
109 gint static_image_idx;
111 /* In order to optimize filling fractions, we simultaneously fill in up
112 * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
113 * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
114 * be tiled horizontally. One for images that are wider than
115 * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
116 * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
117 * that we tile in horizontal rows.
124 /* tile_y1 and tile_y2 define the horizontal band into
125 * which we are tiling images. tile_x is the x extent to
126 * which that is filled
136 static GSList *scratch_image_infos = NULL;
139 allocate_scratch_images (GdkScratchImageInfo *info,
145 for (i = 0; i < n_images; i++)
147 info->static_image[i] = _gdk_image_new_for_depth (info->screen,
148 shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
150 GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images),
151 GDK_SCRATCH_IMAGE_HEIGHT,
154 if (!info->static_image[i])
158 for (j = 0; j < i; j++)
159 g_object_unref (info->static_image[j]);
169 scratch_image_info_display_closed (GdkDisplay *display,
171 GdkScratchImageInfo *image_info)
175 g_signal_handlers_disconnect_by_func (display,
176 scratch_image_info_display_closed,
179 scratch_image_infos = g_slist_remove (scratch_image_infos, image_info);
181 for (i = 0; i < image_info->n_images; i++)
182 g_object_unref (image_info->static_image[i]);
187 static GdkScratchImageInfo *
188 scratch_image_info_for_depth (GdkScreen *screen,
192 GdkScratchImageInfo *image_info;
195 tmp_list = scratch_image_infos;
198 image_info = tmp_list->data;
199 if (image_info->depth == depth && image_info->screen == screen)
202 tmp_list = tmp_list->next;
205 image_info = g_new (GdkScratchImageInfo, 1);
207 image_info->depth = depth;
208 image_info->screen = screen;
210 g_signal_connect (gdk_screen_get_display (screen), "closed",
211 G_CALLBACK (scratch_image_info_display_closed),
214 /* Try to allocate as few possible shared images */
215 for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
217 if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
219 image_info->n_images = possible_n_images[i];
224 /* If that fails, just allocate N_REGIONS normal images */
225 if (i == G_N_ELEMENTS (possible_n_images))
227 allocate_scratch_images (image_info, N_REGIONS, FALSE);
228 image_info->n_images = N_REGIONS;
231 image_info->static_image_idx = 0;
233 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
234 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
235 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
236 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
238 scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);
243 /* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
244 for performance evaluation. */
249 static gint sincelast;
253 alloc_scratch_image (GdkScratchImageInfo *image_info)
255 if (image_info->static_image_idx == N_REGIONS)
261 g_print ("flush, %d puts since last flush\n", sincelast);
264 image_info->static_image_idx = 0;
266 /* Mark all regions that we might be filling in as completely
267 * full, to force new tiles to be allocated for subsequent
270 image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
271 image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
272 image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
273 image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
275 return image_info->static_image_idx++;
279 * _gdk_image_get_scratch:
280 * @screen: a #GdkScreen
281 * @width: desired width
282 * @height: desired height
283 * @depth: depth of image
284 * @x: X location within returned image of scratch image
285 * @y: Y location within returned image of scratch image
287 * Allocates an image of size width/height, up to a maximum
288 * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
289 * suitable to use on @screen.
291 * Return value: a scratch image. This must be used by a
292 * call to gdk_image_put() before any other calls to
293 * _gdk_image_get_scratch()
296 _gdk_image_get_scratch (GdkScreen *screen,
303 GdkScratchImageInfo *image_info;
307 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
309 image_info = scratch_image_info_for_depth (screen, depth);
311 if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
313 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
315 idx = alloc_scratch_image (image_info);
321 if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
323 image_info->horiz_idx = alloc_scratch_image (image_info);
324 image_info->horiz_y = 0;
326 idx = image_info->horiz_idx;
328 *y = image_info->horiz_y;
329 image_info->horiz_y += height;
334 if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
336 if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
338 image_info->vert_idx = alloc_scratch_image (image_info);
339 image_info->vert_x = 0;
341 idx = image_info->vert_idx;
342 *x = image_info->vert_x;
344 /* using 3 and -4 would be slightly more efficient on 32-bit machines
345 with > 1bpp displays */
346 image_info->vert_x += (width + 7) & -8;
350 if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
352 image_info->tile_y1 = image_info->tile_y2;
353 image_info->tile_x = 0;
355 if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
357 image_info->tile_idx = alloc_scratch_image (image_info);
358 image_info->tile_x = 0;
359 image_info->tile_y1 = 0;
360 image_info->tile_y2 = 0;
362 if (height + image_info->tile_y1 > image_info->tile_y2)
363 image_info->tile_y2 = height + image_info->tile_y1;
364 idx = image_info->tile_idx;
365 *x = image_info->tile_x;
366 *y = image_info->tile_y1;
367 image_info->tile_x += (width + 7) & -8;
370 image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
371 *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
373 g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
380 gdk_image_new (GdkImageType type,
385 return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
386 visual, width, height, -1);
389 #define __GDK_IMAGE_C__
390 #include "gdkaliasdef.c"