test-preview-area.c 6.87 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * Copyright (C) 2004  Sven Neumann <sven@gimp.org>
 * test-preview-area.c
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14 15 16 17
 *
 * 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
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

/* This code is based on testrgb.c from GTK+ - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 */

#include <config.h>

#include <stdlib.h>

#include <gtk/gtk.h>

#include "gimpwidgetstypes.h"

#include "gimppreviewarea.h"


#define WIDTH      256
#define HEIGHT     256
Sven Neumann's avatar
Sven Neumann committed
39
#define NUM_ITERS  100
40 41 42


static void
Sven Neumann's avatar
Sven Neumann committed
43 44
test_run (GtkWidget *area,
          gboolean   visible)
45
{
46
  guchar        buf[WIDTH * HEIGHT * 8];
47
  gint          i, j;
Sven Neumann's avatar
Sven Neumann committed
48
  gint          num_iters = NUM_ITERS;
49 50 51 52 53 54
  guchar        val;
  gdouble       start_time, total_time;
  GTimer       *timer;
  GEnumClass   *enum_class;
  GEnumValue   *enum_value;

Sven Neumann's avatar
Sven Neumann committed
55 56 57
  if (! visible)
    num_iters *= 4;

58 59
  gtk_widget_realize (area);

Sven Neumann's avatar
Sven Neumann committed
60 61 62
  g_print ("\nPerformance tests for GimpPreviewArea "
           "(%d x %d, %s, %d iterations):\n\n",
           WIDTH, HEIGHT, visible ? "visible" : "hidden", num_iters);
Sven Neumann's avatar
Sven Neumann committed
63

64 65 66 67 68 69 70
  val = 0;
  for (j = 0; j < WIDTH * HEIGHT * 8; j++)
    {
      val = (val + ((val + (rand () & 0xff)) >> 1)) >> 1;
      buf[j] = val;
    }

71
  gimp_preview_area_set_colormap (GIMP_PREVIEW_AREA (area), buf, 256);
72 73 74 75 76

  /* Let's warm up the cache, and also wait for the window manager
     to settle. */
  for (i = 0; i < NUM_ITERS; i++)
    {
Sven Neumann's avatar
Sven Neumann committed
77 78
      gint offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;

79 80 81 82 83 84 85 86
      gimp_preview_area_draw (GIMP_PREVIEW_AREA (area),
                              0, 0, WIDTH, HEIGHT,
                              GIMP_RGB_IMAGE,
                              buf + offset,
                              WIDTH * 4);
    }

  gdk_window_process_all_updates ();
Sven Neumann's avatar
Sven Neumann committed
87
  gdk_flush ();
88 89 90 91 92 93 94

  timer = g_timer_new ();

  enum_class = g_type_class_ref (GIMP_TYPE_IMAGE_TYPE);

  for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
    {
95
      /* gimp_preview_area_draw */
96 97
      start_time = g_timer_elapsed (timer, NULL);

Sven Neumann's avatar
Sven Neumann committed
98
      for (i = 0; i < num_iters; i++)
99
        {
Sven Neumann's avatar
Sven Neumann committed
100 101
          gint offset = (rand () % (WIDTH * HEIGHT * 4)) & -4;

102 103 104 105 106 107 108 109 110 111 112
          gimp_preview_area_draw (GIMP_PREVIEW_AREA (area),
                                  0, 0, WIDTH, HEIGHT,
                                  enum_value->value,
                                  buf + offset,
                                  WIDTH * 4);

          gdk_window_process_updates (area->window, FALSE);
        }

      gdk_flush ();
      total_time = g_timer_elapsed (timer, NULL) - start_time;
113

Sven Neumann's avatar
Sven Neumann committed
114
      g_print ("%-20s "
115
               "draw  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
116 117 118 119 120 121 122 123 124 125
               enum_value->value_name,
               total_time,
               num_iters / total_time,
               num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);

      /* gimp_preview_area_blend */
      start_time = g_timer_elapsed (timer, NULL);

      for (i = 0; i < num_iters; i++)
        {
Sven Neumann's avatar
Sven Neumann committed
126 127 128
          gint offset  = (rand () % (WIDTH * HEIGHT * 4)) & -4;
          gint offset2 = (rand () % (WIDTH * HEIGHT * 4)) & -4;

129 130 131 132 133 134 135 136 137 138 139 140 141 142
          gimp_preview_area_blend (GIMP_PREVIEW_AREA (area),
                                   0, 0, WIDTH, HEIGHT,
                                   enum_value->value,
                                   buf + offset,
                                   WIDTH * 4,
                                   buf + offset2,
                                   WIDTH * 4,
                                   rand () & 0xFF);

          gdk_window_process_updates (area->window, FALSE);
        }

      gdk_flush ();
      total_time = g_timer_elapsed (timer, NULL) - start_time;
143

Sven Neumann's avatar
Sven Neumann committed
144
      g_print ("%-20s "
145
               "blend :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
146 147 148 149 150 151 152 153 154 155
               enum_value->value_name,
               total_time,
               num_iters / total_time,
               num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);

      /* gimp_preview_area_mask */
      start_time = g_timer_elapsed (timer, NULL);

      for (i = 0; i < num_iters; i++)
        {
Sven Neumann's avatar
Sven Neumann committed
156 157 158 159
          gint offset  = (rand () % (WIDTH * HEIGHT * 4)) & -4;
          gint offset2 = (rand () % (WIDTH * HEIGHT * 4)) & -4;
          gint offset3 = (rand () % (WIDTH * HEIGHT * 4)) & -4;

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
          gimp_preview_area_mask (GIMP_PREVIEW_AREA (area),
                                  0, 0, WIDTH, HEIGHT,
                                  enum_value->value,
                                  buf + offset,
                                  WIDTH * 4,
                                  buf + offset2,
                                  WIDTH * 4,
                                  buf + offset3,
                                  WIDTH);

          gdk_window_process_updates (area->window, FALSE);
        }

      gdk_flush ();
      total_time = g_timer_elapsed (timer, NULL) - start_time;
175

Sven Neumann's avatar
Sven Neumann committed
176
      g_print ("%-20s "
177
               "mask  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
178 179 180 181
               enum_value->value_name,
               total_time,
               num_iters / total_time,
               num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
182
      g_print ("\n");
183
    }
Sven Neumann's avatar
Sven Neumann committed
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  start_time = g_timer_elapsed (timer, NULL);

  for (i = 0; i < num_iters; i++)
    {
      guchar  r = rand () % 0xFF;
      guchar  g = rand () % 0xFF;
      guchar  b = rand () % 0xFF;

      gimp_preview_area_fill (GIMP_PREVIEW_AREA (area),
                              0, 0, WIDTH, HEIGHT,
                              r, g, b);

      gdk_window_process_updates (area->window, FALSE);
    }

  gdk_flush ();
  total_time = g_timer_elapsed (timer, NULL) - start_time;
Sven Neumann's avatar
Sven Neumann committed
202
  g_print ("%-20s "
203
           "fill  :  %5.2fs, %8.1f fps, %8.2f megapixels/s\n",
204 205 206 207
           "Color fill",
           total_time,
           num_iters / total_time,
           num_iters * (WIDTH * HEIGHT * 1e-6) / total_time);
208
  g_print ("\n");
209 210 211 212 213 214 215 216
}

static void
test_preview_area (void)
{
  GtkWidget *window;
  GtkWidget *area;

Sven Neumann's avatar
Sven Neumann committed
217
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
218

Sven Neumann's avatar
Sven Neumann committed
219 220
  gtk_window_set_accept_focus (GTK_WINDOW (window), FALSE);

221
  area = gimp_preview_area_new ();
Sven Neumann's avatar
Sven Neumann committed
222
  gtk_container_add (GTK_CONTAINER (window), area);
223 224
  gtk_widget_show (area);

Sven Neumann's avatar
Sven Neumann committed
225
  test_run (area, FALSE);
226 227 228

  gtk_widget_show (window);

Sven Neumann's avatar
Sven Neumann committed
229
  test_run (area, TRUE);
230 231 232 233 234 235 236 237 238
}

int
main (int argc, char **argv)
{
  gtk_init (&argc, &argv);

  test_preview_area ();

239
  return EXIT_SUCCESS;
240
}