File: overlay_multi_stresstest.c

package info (click to toggle)
gxr 0.15.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 1,728 kB
  • sloc: ansic: 15,256; sh: 72; xml: 71; awk: 35; makefile: 12
file content (198 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (3)
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
191
192
193
194
195
196
197
198
/*
 * gxr
 * Copyright 2018 Collabora Ltd.
 * Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#include <math.h>
#include <glib.h>
#include <GL/gl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdk.h>

#include "gxr.h"

#define MAXWIDTH 1920
#define MAXHEIGHT 1080
#define MAXSTRIDE MAXWIDTH * 4

#include "cairo_content.h"

#define OVERLAY_NUM 10

typedef struct Example
{
  GMainLoop *loop;
  GxrContext *context;
  GxrOverlay *overlays[OVERLAY_NUM];
  GulkanTexture *textures[OVERLAY_NUM];
  int tex_count;
} Example;

typedef struct ExampleOverlayNum
{
  Example *example;
  int i;
} ExampleOverlayNum;

static cairo_surface_t*
create_cairo_surface (unsigned char *image, char *text)
{

  int WIDTH =  MAXWIDTH;
  int HEIGHT = MAXHEIGHT;
  /* To test with randomized texture dimensions
  WIDTH -= rand () % 50;
  HEIGHT -= rand () % 50;
   */
  int STRIDE = (WIDTH * 4);
  cairo_surface_t *surface =
    cairo_image_surface_create_for_data (image,
                                         CAIRO_FORMAT_ARGB32,
                                         WIDTH, HEIGHT,
                                         STRIDE);

  cairo_t *cr = cairo_create (surface);

  cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
  cairo_set_source_rgb (cr, 0.2, 0, 0);
  cairo_fill (cr);

  cairo_select_font_face (cr, "cairo :monospace",
      CAIRO_FONT_SLANT_NORMAL,
      CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size (cr, 72);

  cairo_move_to (cr, 50, 50);
  cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
  cairo_show_text (cr, text);

  cairo_destroy (cr);

  return surface;
}

static unsigned char image[MAXSTRIDE*MAXHEIGHT];
static gboolean
timeout_callback (gpointer data)
{
  ExampleOverlayNum *en = (ExampleOverlayNum*) data;
  Example *self = en->example;
  GxrOverlay *overlay = self->overlays[en->i];

  char key[16];
  snprintf (key, 16, "texture-%d", self->tex_count++);

  cairo_surface_t *surface = create_cairo_surface (image, key);

  if (surface == NULL) {
    fprintf (stderr, "Could not create cairo surface.\n");
    return -1;
  }

  GulkanClient *gc = gxr_context_get_gulkan (self->context);

  GulkanTexture *n =
    gulkan_texture_new_from_cairo_surface (gc,
                                           surface,
                                           VK_FORMAT_R8G8B8A8_UNORM,
                                           VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
  gxr_overlay_submit_texture (overlay, n);

  if (self->textures[en->i] != NULL)
    {
      g_object_unref (self->textures[en->i]);
      self->textures[en->i] = NULL;
    }
  self->textures[en->i] = n;

  cairo_surface_destroy (surface);

  gxr_overlay_poll_event (overlay);
  return TRUE;
}

static void
_press_cb (GxrOverlay  *overlay,
           GdkEventButton *event,
           gpointer        data)
{
  (void) overlay;
  Example *self = (Example*) data;
  g_print ("press: %d %f %f (%d)\n",
           event->button, event->x, event->y, event->time);
  GMainLoop *loop = self->loop;
  g_main_loop_quit (loop);
}

static void
_destroy_cb (GxrOverlay *overlay,
             gpointer       data)
{
  (void) overlay;
  Example *self = (Example*) data;
  g_print ("destroy\n");
  g_main_loop_quit (self->loop);
}

int main () {
  Example ex = {
    .loop = g_main_loop_new (NULL, FALSE),
    .tex_count = 0,
    .context = gxr_context_new (GXR_APP_OVERLAY, "Overlay Multi Stresstest", 1)
  };

  ExampleOverlayNum nums[OVERLAY_NUM];
  for (int i = 0; i < OVERLAY_NUM; i++)
    {
      nums[i].example = &ex;
      nums[i].i = i;
    }

  /* create openvr overlay */
  float x_offset = 0;
  for (int i = 0; i < OVERLAY_NUM; i++)
    {
      ex.textures[i] = NULL;
      char key[16];
      snprintf (key, 16, "test-%d", i);
      ex.overlays[i] = gxr_overlay_new (ex.context, key);
      if (ex.overlays[i] == NULL)
      {
        fprintf (stderr, "Overlay unavailable.\n");
        return -1;
      }

      float diff = 0;
      if (i > 0)
         gxr_overlay_get_width_meters (ex.overlays[i-1], &diff);
      x_offset += diff;

      g_print ("Offset %f\n", x_offset);

      graphene_point3d_t p = { .x = -2 + x_offset, .y = 0.5, .z = -2 };
      gxr_overlay_set_translation (ex.overlays[i], &p);

      if (!gxr_overlay_show (ex.overlays[i]))
        return -1;

      g_signal_connect (ex.overlays[i], "button-press-event", (GCallback) _press_cb, &ex);
      g_signal_connect (ex.overlays[i], "destroy", (GCallback) _destroy_cb, &ex);

      g_timeout_add (75, timeout_callback, &nums[i]);
    }

  g_main_loop_run (ex.loop);
  g_main_loop_unref (ex.loop);

  for (int i = 0; i < OVERLAY_NUM; i++)
    {
      g_object_unref (ex.overlays[i]);
      g_object_unref (ex.textures[i]);
    }

  g_object_unref (ex.context);

  return 0;
}