aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compute/skc/main.c
blob: 8833b0bb1c499aae02fde920efa636000fa9663c (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can
 * be found in the LICENSE file.
 *
 */

//
//
//

#include <glad/glad.h>
#include <GLFW/glfw3.h>

//
//
//

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#include "svg/svg_doc.h"
#include "svg2skc/svg2skc.h"
#include "svg2skc/transform_stack.h"

//
//
//

#include <CL/opencl.h>
#include "interop.h"

//
//
//

void
skc_runtime_cl_12_debug(struct skc_context * const context);

//
//
//



//
//
//

static
void
is_render_complete(skc_surface_t     surface,
                   skc_styling_t     styling,
                   skc_composition_t composition,
                   bool            * quit)
{
  *quit = true;
}

//
//
//

int
main(int argc, char** argv)
{
  //
  //
  //
  if (argc <= 1)
    {
      fprintf(stderr,"-- missing filename\n");
      return EXIT_FAILURE; // no filename
    }

  //
  // load test file
  //
  // #include "test/lion.inl"

  struct svg_doc * svg_doc = svg_doc_parse(argv[1],false);

  fprintf(stderr,"p/r/l = %u / %u / %u\n",
          svg_doc_path_count(svg_doc),
          svg_doc_raster_count(svg_doc),
          svg_doc_layer_count(svg_doc));

  //
  // fire up GL
  //
  GLFWwindow * window;

  skc_interop_init(&window);

  //
  // get GL and device contexts
  //
  HGLRC hGLRC = wglGetCurrentContext();
  HDC   hDC   = wglGetCurrentDC();

  //
  //
  //
  cl_context_properties context_properties[] =
    {
      CL_CONTEXT_PLATFORM, (cl_context_properties)-1,
      CL_GL_CONTEXT_KHR,   (cl_context_properties)hGLRC,
      CL_WGL_HDC_KHR,      (cl_context_properties)hDC,
      0
    };

  //
  // create context
  //
  skc_context_t context;

  skc_err err = skc_context_create(&context,"Intel","Graphics",context_properties);

  //
  // associate
  //
  skc_interop_register(context);

  //
  // create path builder
  //
  skc_path_builder_t path_builder;

  err = skc_path_builder_create(context,&path_builder);

  //
  // create raster builder
  //
  skc_raster_builder_t raster_builder;

  err = skc_raster_builder_create(context,&raster_builder);

  //
  // create a composition
  //
  skc_composition_t composition;

  err = skc_composition_create(context,&composition);

  //
  // create a styling instance
  //
  skc_styling_t styling;

  err = skc_styling_create(context,
                           &styling,
                           svg_doc_layer_count(svg_doc),
                           1000,
                           2 * 1024 * 1024);

  //
  // create a surface
  //
  skc_surface_t surface;

  err = skc_surface_create(context,&surface);

  //
  // create a transform stack
  //
  struct skc_transform_stack * ts = skc_transform_stack_create(32);

  // prime the transform stack with subpixel scale
  skc_transform_stack_push_scale(ts,32.0,32.0);

  //
  // rasterize, render and reclaim svg until escape
  //
  while (!glfwWindowShouldClose(window))
    {
      // save stack
      uint32_t const ts_save = skc_transform_stack_save(ts);

      // poll glfw
      skc_interop_poll(window,ts);

      // decode paths
      skc_path_t * paths = svg_doc_paths_decode(svg_doc,path_builder);

      // decode rasters
      skc_raster_t * rasters = svg_doc_rasters_decode(svg_doc,ts,paths,raster_builder);

      // restore the transform stack
      skc_transform_stack_restore(ts,ts_save);

      // decode layers -- places rasters
      svg_doc_layers_decode(svg_doc,rasters,composition,styling,true/*is_srgb*/);

      // seal the composition
      skc_composition_seal(composition);

      bool           quit   = false;
      uint32_t const clip[] = { 0, 0, 65535, 65535 }; // tile clip is <= 9 bits (512)

      // render the styled composition to the surface
      skc_surface_render(surface,clip,styling,composition,
                         is_render_complete,&quit,
                         skc_interop_get_fb(window));

      // release the paths
      svg_doc_paths_release(svg_doc,paths,context);

      // rasters have been released
      svg_doc_rasters_release(svg_doc,rasters,context);

      // spin until framebuffer is rendered
      while (!quit) {
        // fprintf(stderr,"WAITING ON: !quit\n");
        skc_context_wait(context);
      }

      // blit and swap
      skc_interop_blit(window);

      // print out some useful debug info
      skc_runtime_cl_12_debug(context);

      // rewind the doc
      svg_doc_rewind(svg_doc);

      //
      // I don't like this being here
      //
      float    const rgba[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
      uint32_t       rect[4] = { 0 };

      skc_interop_get_dim(rect+2);

      skc_surface_clear(surface,rgba,rect,
                        skc_interop_get_fb(window));

      // exit(EXIT_SUCCESS);

      // reset styling
      skc_styling_reset(styling);

      // unseal the composition
      skc_composition_unseal(composition,true);
    }

  //
  // dispose of mundane resources
  //
  skc_transform_stack_release(ts);

  //
  // dispose of all SKC resources
  //
  err = skc_surface_release(surface);
  err = skc_styling_release(styling);
  err = skc_composition_release(composition);
  err = skc_raster_builder_release(raster_builder);
  err = skc_path_builder_release(path_builder);
  err = skc_context_release(context);

  //
  // GLFW CLEANUP
  //
  glfwDestroyWindow(window);
  glfwTerminate();

  //
  //
  //
  return EXIT_SUCCESS;
}

//
//
//