blob: bae9532edf46f136dd735fde84cc3bee93e876c1 [file] [log] [blame]
Allan MacKinnon4359d522018-06-19 13:57:04 -07001/*
2 * Copyright 2017 Google Inc.
3 *
Ravi Mistry14538dd2018-06-20 09:11:24 -04004 * Use of this source code is governed by a BSD-style license that can
Allan MacKinnon4359d522018-06-19 13:57:04 -07005 * be found in the LICENSE file.
6 *
7 */
8
9//
10//
11//
12
Allan MacKinnon4359d522018-06-19 13:57:04 -070013#include <stdio.h>
14#include <stdlib.h>
15#include <conio.h>
16
Allan MacKinnonc110e792018-06-21 09:09:56 -070017#include "common/cl/find_cl.h"
18#include "common/cl/assert_cl.h"
19
Allan MacKinnon6ddc9592018-10-01 09:16:33 -070020#include "ts/transform_stack.h"
21
22//
23//
24//
25
26// #define SKC_TEST_SVG
27#ifdef SKC_TEST_SVG
28//
29// SVG
30//
Allan MacKinnon4359d522018-06-19 13:57:04 -070031#include "svg/svg_doc.h"
32#include "svg2skc/svg2skc.h"
Allan MacKinnon6ddc9592018-10-01 09:16:33 -070033
34#define SKC_TEST(f,...) svg_doc_##f(svg_doc,__VA_ARGS__)
35
36void
37svg_doc_toggle(struct svg_doc * sd) { ; };
38
39#else
40
41#include "tests/groups/groups.h"
42
43#define SKC_TEST(f,...) groups_##f(__VA_ARGS__)
44
45#endif
Allan MacKinnon4359d522018-06-19 13:57:04 -070046
47//
48//
49//
50
Allan MacKinnonebf160f2018-06-24 08:31:14 -070051#include "platforms/cl_12/skc_cl.h"
52#include "interop.h"
Allan MacKinnon4359d522018-06-19 13:57:04 -070053
54//
55//
56//
57
Allan MacKinnonebf160f2018-06-24 08:31:14 -070058typedef enum skc_pipeline_start_at_e {
59 SKC_PIPELINE_START_AT_DEFINE_PATHS = '1',
60 SKC_PIPELINE_START_AT_RASTERIZE = '2',
61 SKC_PIPELINE_START_AT_COMPOSITION = '3',
62 SKC_PIPELINE_START_AT_RENDER = '4'
63} skc_pipeline_start_at_e;
Allan MacKinnon4359d522018-06-19 13:57:04 -070064
65//
Allan MacKinnonebf160f2018-06-24 08:31:14 -070066// Callback for explicitly waiting for render completion
Allan MacKinnon4359d522018-06-19 13:57:04 -070067//
68
Allan MacKinnonebf160f2018-06-24 08:31:14 -070069#if 0
Allan MacKinnonc110e792018-06-21 09:09:56 -070070static
Allan MacKinnon4359d522018-06-19 13:57:04 -070071void
72is_render_complete(skc_surface_t surface,
73 skc_styling_t styling,
74 skc_composition_t composition,
Allan MacKinnonebf160f2018-06-24 08:31:14 -070075 skc_framebuffer_t fb,
76 void * data)
Allan MacKinnon4359d522018-06-19 13:57:04 -070077{
Allan MacKinnonebf160f2018-06-24 08:31:14 -070078 // exit while loop
79 *(bool*)data = true;
Allan MacKinnon4359d522018-06-19 13:57:04 -070080}
Allan MacKinnonebf160f2018-06-24 08:31:14 -070081#endif
82
83//
84// FIXME - for debugging purposes declare this internal prototype
85//
86
87void
88skc_runtime_cl_12_debug(struct skc_context * const context);
Allan MacKinnon4359d522018-06-19 13:57:04 -070089
90//
91//
92//
93
94int
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -070095main(int argc, char const * argv[])
Allan MacKinnon4359d522018-06-19 13:57:04 -070096{
97 //
Allan MacKinnon4359d522018-06-19 13:57:04 -070098 //
Allan MacKinnonc110e792018-06-21 09:09:56 -070099 //
100 if (argc <= 1)
Allan MacKinnon4359d522018-06-19 13:57:04 -0700101 {
102 fprintf(stderr,"-- missing filename\n");
103 return EXIT_FAILURE; // no filename
104 }
105
106 //
107 // load test file
108 //
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700109#ifdef SKC_TEST_SVG
Allan MacKinnon4359d522018-06-19 13:57:04 -0700110 struct svg_doc * svg_doc = svg_doc_parse(argv[1],false);
111
112 fprintf(stderr,"p/r/l = %u / %u / %u\n",
113 svg_doc_path_count(svg_doc),
114 svg_doc_raster_count(svg_doc),
115 svg_doc_layer_count(svg_doc));
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700116#endif
Allan MacKinnon4359d522018-06-19 13:57:04 -0700117
118 //
119 // fire up GL
120 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700121 struct skc_interop * interop = skc_interop_create();
Allan MacKinnon4359d522018-06-19 13:57:04 -0700122
123 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700124 // find platform and device by name
125 //
126 cl_platform_id platform_id_cl;
127 cl_device_id device_id_cl;
128
129 cl(FindIdsByName("Intel","Graphics",
130 &platform_id_cl,
131 &device_id_cl,
132 0,NULL,NULL,
133 true));
134
135 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700136 // create the CL context with GL interop
Allan MacKinnon4359d522018-06-19 13:57:04 -0700137 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700138#ifdef _WIN32
Allan MacKinnonc110e792018-06-21 09:09:56 -0700139 cl_context_properties context_properties_cl[] =
Allan MacKinnon4359d522018-06-19 13:57:04 -0700140 {
Allan MacKinnonc110e792018-06-21 09:09:56 -0700141 CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id_cl,
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700142 CL_GL_CONTEXT_KHR, skc_interop_get_wgl_context(),
143 CL_WGL_HDC_KHR, skc_interop_get_wgl_dc(),
Allan MacKinnon4359d522018-06-19 13:57:04 -0700144 0
145 };
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700146#else
147#error "Missing a system-compatible context!"
148#endif
Allan MacKinnonc110e792018-06-21 09:09:56 -0700149
150 cl_int cl_err;
151 cl_context context_cl = clCreateContext(context_properties_cl,
152 1,
153 &device_id_cl,
154 NULL,
155 NULL,
156 &cl_err); cl_ok(cl_err);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700157 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700158 // register cl_context with GL interop
159 //
160 skc_interop_set_cl_context(interop,context_cl);
161
162 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700163 // create SKC context
Allan MacKinnon4359d522018-06-19 13:57:04 -0700164 //
165 skc_context_t context;
166
Allan MacKinnonc110e792018-06-21 09:09:56 -0700167 skc_err err = skc_context_create_cl(&context,
168 context_cl,
169 device_id_cl);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700170
171 //
Allan MacKinnon4359d522018-06-19 13:57:04 -0700172 // create path builder
173 //
174 skc_path_builder_t path_builder;
175
176 err = skc_path_builder_create(context,&path_builder);
177
178 //
179 // create raster builder
180 //
181 skc_raster_builder_t raster_builder;
182
183 err = skc_raster_builder_create(context,&raster_builder);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700184
Allan MacKinnon4359d522018-06-19 13:57:04 -0700185 //
186 // create a composition
187 //
188 skc_composition_t composition;
189
190 err = skc_composition_create(context,&composition);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700191
Allan MacKinnon4359d522018-06-19 13:57:04 -0700192 //
193 // create a styling instance
194 //
195 skc_styling_t styling;
196
197 err = skc_styling_create(context,
198 &styling,
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700199 SKC_TEST(layer_count),
Allan MacKinnon4359d522018-06-19 13:57:04 -0700200 1000,
201 2 * 1024 * 1024);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700202
Allan MacKinnon4359d522018-06-19 13:57:04 -0700203 //
204 // create a surface
205 //
206 skc_surface_t surface;
207
208 err = skc_surface_create(context,&surface);
209
210 //
211 // create a transform stack
212 //
Allan MacKinnon5cd77772018-09-13 15:00:16 -0700213 struct ts_transform_stack * ts = ts_transform_stack_create(32);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700214
215 // prime the transform stack with subpixel scale
Allan MacKinnon5cd77772018-09-13 15:00:16 -0700216 ts_transform_stack_push_scale(ts,32.0,32.0);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700217
218 //
219 // rasterize, render and reclaim svg until escape
220 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700221 skc_pipeline_start_at_e pipeline_start_at_base = SKC_PIPELINE_START_AT_DEFINE_PATHS;
Hal Canary14195342018-07-11 16:10:14 -0400222 skc_pipeline_start_at_e pipeline_start_at_loop = SKC_PIPELINE_START_AT_DEFINE_PATHS;
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700223 skc_path_t * paths;
224 skc_raster_t * rasters;
225
226 while (!skc_interop_should_exit(interop))
Allan MacKinnon4359d522018-06-19 13:57:04 -0700227 {
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700228 // redefine the paths?
229 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_DEFINE_PATHS)
230 {
231 // decode paths
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700232 paths = SKC_TEST(paths_decode,path_builder);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700233 }
Allan MacKinnon4359d522018-06-19 13:57:04 -0700234
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700235 // rasterize the paths?
236 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_RASTERIZE)
237 {
238 // save stack
Allan MacKinnon5cd77772018-09-13 15:00:16 -0700239 uint32_t const ts_save = ts_transform_stack_save(ts);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700240
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700241 // update transform
242 skc_interop_transform(interop,ts);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700243
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700244 // decode rasters
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700245 rasters = SKC_TEST(rasters_decode,ts,paths,raster_builder);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700246
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700247 // restore the transform stack
Allan MacKinnon5cd77772018-09-13 15:00:16 -0700248 ts_transform_stack_restore(ts,ts_save);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700249 }
Allan MacKinnon4359d522018-06-19 13:57:04 -0700250
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700251 // decode the styling and composition?
252 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_COMPOSITION)
253 {
254 // reset styling
255 skc_styling_reset(styling);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700256
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700257 // unseal and reset the composition
258 skc_composition_unseal(composition,true);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700259
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700260 // decode layers -- places rasters
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700261 SKC_TEST(layers_decode,rasters,composition,styling,true/*is_srgb*/);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700262
263 // seal the styling -- render will seal if not called
264 skc_styling_seal(styling);
265
266 // seal the composition -- render will seal if not called
267 skc_composition_seal(composition);
268 }
269
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -0700270 uint32_t const clip[] = { 0, 0, 65535, 65535 };
271 int32_t const txty[] = { 0, 0 };
Allan MacKinnon4359d522018-06-19 13:57:04 -0700272
273 // render the styled composition to the surface
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700274 skc_surface_render(surface,
275 styling,
276 composition,
277 skc_interop_get_framebuffer(interop),
278 clip,
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -0700279 txty,
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700280 NULL,
281 NULL);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700282
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700283 //
284 // poll for events and maybe start from a different point in the
285 // pipeline
286 //
287 int key;
Allan MacKinnon4359d522018-06-19 13:57:04 -0700288
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700289 // poll for window events
290 bool const transform_changed = skc_interop_poll(interop,&key);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700291
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700292 // how many blocks are in use?
293 if (key == 'I')
294 skc_runtime_cl_12_debug(context);
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700295 else if (key == 'T')
296 SKC_TEST(toggle);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700297
298 // do we only want to run part of the pipeline?
299 if ((key >= SKC_PIPELINE_START_AT_DEFINE_PATHS) && (key <= SKC_PIPELINE_START_AT_RENDER))
300 pipeline_start_at_base = key;
Hal Canary14195342018-07-11 16:10:14 -0400301
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700302 // valid for a loop
303 pipeline_start_at_loop = pipeline_start_at_base;
304
305 // if the transform changed then we must start at rasterize or before
306 if (transform_changed)
307 pipeline_start_at_loop = min(pipeline_start_at_loop,SKC_PIPELINE_START_AT_RASTERIZE);
308
309 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_COMPOSITION)
310 {
311 // rewind the svg doc
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700312 SKC_TEST(rewind);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700313
314 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_DEFINE_PATHS)
315 {
316 // release the paths
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700317 SKC_TEST(paths_release,context,paths);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700318 }
319
320 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_RASTERIZE)
321 {
322 // release the rasters
Allan MacKinnon6ddc9592018-10-01 09:16:33 -0700323 SKC_TEST(rasters_release,context,rasters);
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700324 }
325 }
326
327#if 0
328 //
329 // Note that we don't need to explicitly wait for the render()
330 // to complete since SKC is fully concurrent and the styling and
331 // compsition unseal() operations will "clock" the render loop.
332 //
333
334 //
335 // explicitly spin until framebuffer is rendered
336 //
337 bool quit = false;
338
Allan MacKinnon4359d522018-06-19 13:57:04 -0700339 while (!quit) {
340 // fprintf(stderr,"WAITING ON: !quit\n");
341 skc_context_wait(context);
342 }
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700343#endif
Allan MacKinnon4359d522018-06-19 13:57:04 -0700344 }
Allan MacKinnonc110e792018-06-21 09:09:56 -0700345
Allan MacKinnon4359d522018-06-19 13:57:04 -0700346 //
347 // dispose of mundane resources
348 //
Allan MacKinnon5cd77772018-09-13 15:00:16 -0700349 ts_transform_stack_release(ts);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700350
351 //
352 // dispose of all SKC resources
353 //
354 err = skc_surface_release(surface);
355 err = skc_styling_release(styling);
356 err = skc_composition_release(composition);
357 err = skc_raster_builder_release(raster_builder);
358 err = skc_path_builder_release(path_builder);
359 err = skc_context_release(context);
360
361 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700362 // dispose of GL interop
Allan MacKinnon4359d522018-06-19 13:57:04 -0700363 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700364 skc_interop_destroy(interop);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700365
366 //
367 //
368 //
369 return EXIT_SUCCESS;
370}
371
372//
373//
374//