summaryrefslogtreecommitdiff
path: root/tests/micro-perf/test-journal.c
blob: 84d3c06aec1a8bf3285ed2fedf6edd656ab28dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <glib.h>
#include <cogl/cogl.h>
#include <math.h>

#include "cogl/cogl-profile.h"

#define FRAMEBUFFER_WIDTH 800
#define FRAMEBUFFER_HEIGHT 600

CoglBool run_all = FALSE;

typedef struct _Data
{
  CoglContext *ctx;
  CoglFramebuffer *fb;
  CoglPipeline *pipeline;
  CoglPipeline *alpha_pipeline;
  GTimer *timer;
  int frame;
} Data;

static void
test_rectangles (Data *data)
{
#define RECT_WIDTH 5
#define RECT_HEIGHT 5
  int x;
  int y;

  cogl_framebuffer_clear4f (data->fb, COGL_BUFFER_BIT_COLOR, 1, 1, 1, 1);

  cogl_framebuffer_push_rectangle_clip (data->fb,
                                        10,
                                        10,
                                        FRAMEBUFFER_WIDTH - 10,
                                        FRAMEBUFFER_HEIGHT - 10);

  /* Should the rectangles be randomly positioned/colored/rotated?
   *
   * It could be good to develop equivalent GL and Cairo tests so we can
   * have a sanity check for our Cogl performance.
   *
   * The color should vary to check that we correctly batch color changes
   * The use of alpha should vary so we have a variation of which rectangles
   * require blending.
   *  Should this be a random variation?
   *  It could be good to experiment with focibly enabling blending for
   *  rectangles that don't technically need it for the sake of extending
   *  batching. E.g. if you a long run of interleved rectangles with every
   *  other rectangle needing blending then it may be worth enabling blending
   *  for all the rectangles to avoid the state changes.
   * The modelview should change between rectangles to check the software
   * transform codepath.
   *  Should we group some rectangles under the same modelview? Potentially
   *  we could avoid software transform for long runs of rectangles with the
   *  same modelview.
   *
   */
  for (y = 0; y < FRAMEBUFFER_HEIGHT; y += RECT_HEIGHT)
    {
      for (x = 0; x < FRAMEBUFFER_WIDTH; x += RECT_WIDTH)
        {
          cogl_framebuffer_push_matrix (data->fb);
          cogl_framebuffer_translate (data->fb, x, y, 0);
          cogl_framebuffer_rotate (data->fb, 45, 0, 0, 1);

          cogl_pipeline_set_color4f (data->pipeline,
                                     1,
                                     (1.0f/FRAMEBUFFER_WIDTH)*y,
                                     (1.0f/FRAMEBUFFER_HEIGHT)*x,
                                     1);
          cogl_framebuffer_draw_rectangle (data->fb,
                                           data->pipeline,
                                           0, 0, RECT_WIDTH, RECT_HEIGHT);

          cogl_framebuffer_pop_matrix (data->fb);
        }
    }

  for (y = 0; y < FRAMEBUFFER_HEIGHT; y += RECT_HEIGHT)
    {
      for (x = 0; x < FRAMEBUFFER_WIDTH; x += RECT_WIDTH)
        {
          cogl_framebuffer_push_matrix (data->fb);
          cogl_framebuffer_translate (data->fb, x, y, 0);

          cogl_pipeline_set_color4f (data->alpha_pipeline,
                                     1,
                                     (1.0f/FRAMEBUFFER_WIDTH)*x,
                                     (1.0f/FRAMEBUFFER_HEIGHT)*y,
                                     (1.0f/FRAMEBUFFER_WIDTH)*x);
          cogl_framebuffer_draw_rectangle (data->fb,
                                           data->alpha_pipeline,
                                           0, 0, RECT_WIDTH, RECT_HEIGHT);

          cogl_framebuffer_pop_matrix (data->fb);
        }
    }

  cogl_framebuffer_pop_clip (data->fb);
}

static CoglBool
paint_cb (void *user_data)
{
  Data *data = user_data;
  double elapsed;

  data->frame++;

  test_rectangles (data);

  cogl_onscreen_swap_buffers (COGL_ONSCREEN (data->fb));

  elapsed = g_timer_elapsed (data->timer, NULL);
  if (elapsed > 1.0)
    {
      g_print ("fps = %f\n", data->frame / elapsed);
      g_timer_start (data->timer);
      data->frame = 0;
    }

  return FALSE; /* remove the callback */
}

static void
frame_event_cb (CoglOnscreen *onscreen,
                CoglFrameEvent event,
                CoglFrameInfo *info,
                void *user_data)
{
  if (event == COGL_FRAME_EVENT_SYNC)
    paint_cb (user_data);
}

int
main (int argc, char **argv)
{
  Data data;
  CoglOnscreen *onscreen;
  GSource *cogl_source;
  GMainLoop *loop;
  COGL_STATIC_TIMER (mainloop_timer,
                      NULL, //no parent
                      "Mainloop",
                      "The time spent in the glib mainloop",
                      0);  // no application private data

  data.ctx = cogl_context_new (NULL, NULL);

  onscreen = cogl_onscreen_new (data.ctx,
                                FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT);
  cogl_onscreen_set_swap_throttled (onscreen, FALSE);
  cogl_onscreen_show (onscreen);

  data.fb = COGL_FRAMEBUFFER (onscreen);
  cogl_framebuffer_orthographic (data.fb,
                                 0, 0,
                                 FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT,
                                 -1,
                                 100);

  data.pipeline = cogl_pipeline_new (data.ctx);
  cogl_pipeline_set_color4f (data.pipeline, 1, 1, 1, 1);
  data.alpha_pipeline = cogl_pipeline_new (data.ctx);
  cogl_pipeline_set_color4f (data.alpha_pipeline, 1, 1, 1, 0.5);

  cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT);

  g_source_attach (cogl_source, NULL);

  cogl_onscreen_add_frame_callback (COGL_ONSCREEN (data.fb),
                                    frame_event_cb,
                                    &data,
                                    NULL); /* destroy notify */

  g_idle_add (paint_cb, &data);

  data.frame = 0;
  data.timer = g_timer_new ();
  g_timer_start (data.timer);

  loop = g_main_loop_new (NULL, TRUE);
  COGL_TIMER_START (uprof_get_mainloop_context (), mainloop_timer);
  g_main_loop_run (loop);
  COGL_TIMER_STOP (uprof_get_mainloop_context (), mainloop_timer);

  return 0;
}