blob: 2af8ebb6fc5ddba9324d951023f01b8909421063 [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 MacKinnon4359d522018-06-19 13:57:04 -070020#include "svg/svg_doc.h"
21#include "svg2skc/svg2skc.h"
22#include "svg2skc/transform_stack.h"
23
24//
25//
26//
27
Allan MacKinnonebf160f2018-06-24 08:31:14 -070028#include "platforms/cl_12/skc_cl.h"
29#include "interop.h"
Allan MacKinnon4359d522018-06-19 13:57:04 -070030
31//
32//
33//
34
Allan MacKinnonebf160f2018-06-24 08:31:14 -070035typedef enum skc_pipeline_start_at_e {
36 SKC_PIPELINE_START_AT_DEFINE_PATHS = '1',
37 SKC_PIPELINE_START_AT_RASTERIZE = '2',
38 SKC_PIPELINE_START_AT_COMPOSITION = '3',
39 SKC_PIPELINE_START_AT_RENDER = '4'
40} skc_pipeline_start_at_e;
Allan MacKinnon4359d522018-06-19 13:57:04 -070041
42//
Allan MacKinnonebf160f2018-06-24 08:31:14 -070043// Callback for explicitly waiting for render completion
Allan MacKinnon4359d522018-06-19 13:57:04 -070044//
45
Allan MacKinnonebf160f2018-06-24 08:31:14 -070046#if 0
Allan MacKinnonc110e792018-06-21 09:09:56 -070047static
Allan MacKinnon4359d522018-06-19 13:57:04 -070048void
49is_render_complete(skc_surface_t surface,
50 skc_styling_t styling,
51 skc_composition_t composition,
Allan MacKinnonebf160f2018-06-24 08:31:14 -070052 skc_framebuffer_t fb,
53 void * data)
Allan MacKinnon4359d522018-06-19 13:57:04 -070054{
Allan MacKinnonebf160f2018-06-24 08:31:14 -070055 // exit while loop
56 *(bool*)data = true;
Allan MacKinnon4359d522018-06-19 13:57:04 -070057}
Allan MacKinnonebf160f2018-06-24 08:31:14 -070058#endif
59
60//
61// FIXME - for debugging purposes declare this internal prototype
62//
63
64void
65skc_runtime_cl_12_debug(struct skc_context * const context);
Allan MacKinnon4359d522018-06-19 13:57:04 -070066
67//
68//
69//
70
71int
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -070072main(int argc, char const * argv[])
Allan MacKinnon4359d522018-06-19 13:57:04 -070073{
74 //
Allan MacKinnon4359d522018-06-19 13:57:04 -070075 //
Allan MacKinnonc110e792018-06-21 09:09:56 -070076 //
77 if (argc <= 1)
Allan MacKinnon4359d522018-06-19 13:57:04 -070078 {
79 fprintf(stderr,"-- missing filename\n");
80 return EXIT_FAILURE; // no filename
81 }
82
83 //
84 // load test file
85 //
Allan MacKinnon4359d522018-06-19 13:57:04 -070086 struct svg_doc * svg_doc = svg_doc_parse(argv[1],false);
87
88 fprintf(stderr,"p/r/l = %u / %u / %u\n",
89 svg_doc_path_count(svg_doc),
90 svg_doc_raster_count(svg_doc),
91 svg_doc_layer_count(svg_doc));
92
93 //
94 // fire up GL
95 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -070096 struct skc_interop * interop = skc_interop_create();
Allan MacKinnon4359d522018-06-19 13:57:04 -070097
98 //
Allan MacKinnonc110e792018-06-21 09:09:56 -070099 // find platform and device by name
100 //
101 cl_platform_id platform_id_cl;
102 cl_device_id device_id_cl;
103
104 cl(FindIdsByName("Intel","Graphics",
105 &platform_id_cl,
106 &device_id_cl,
107 0,NULL,NULL,
108 true));
109
110 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700111 // create the CL context with GL interop
Allan MacKinnon4359d522018-06-19 13:57:04 -0700112 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700113#ifdef _WIN32
Allan MacKinnonc110e792018-06-21 09:09:56 -0700114 cl_context_properties context_properties_cl[] =
Allan MacKinnon4359d522018-06-19 13:57:04 -0700115 {
Allan MacKinnonc110e792018-06-21 09:09:56 -0700116 CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id_cl,
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700117 CL_GL_CONTEXT_KHR, skc_interop_get_wgl_context(),
118 CL_WGL_HDC_KHR, skc_interop_get_wgl_dc(),
Allan MacKinnon4359d522018-06-19 13:57:04 -0700119 0
120 };
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700121#else
122#error "Missing a system-compatible context!"
123#endif
Allan MacKinnonc110e792018-06-21 09:09:56 -0700124
125 cl_int cl_err;
126 cl_context context_cl = clCreateContext(context_properties_cl,
127 1,
128 &device_id_cl,
129 NULL,
130 NULL,
131 &cl_err); cl_ok(cl_err);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700132 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700133 // register cl_context with GL interop
134 //
135 skc_interop_set_cl_context(interop,context_cl);
136
137 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700138 // create SKC context
Allan MacKinnon4359d522018-06-19 13:57:04 -0700139 //
140 skc_context_t context;
141
Allan MacKinnonc110e792018-06-21 09:09:56 -0700142 skc_err err = skc_context_create_cl(&context,
143 context_cl,
144 device_id_cl);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700145
146 //
Allan MacKinnon4359d522018-06-19 13:57:04 -0700147 // create path builder
148 //
149 skc_path_builder_t path_builder;
150
151 err = skc_path_builder_create(context,&path_builder);
152
153 //
154 // create raster builder
155 //
156 skc_raster_builder_t raster_builder;
157
158 err = skc_raster_builder_create(context,&raster_builder);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700159
Allan MacKinnon4359d522018-06-19 13:57:04 -0700160 //
161 // create a composition
162 //
163 skc_composition_t composition;
164
165 err = skc_composition_create(context,&composition);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700166
Allan MacKinnon4359d522018-06-19 13:57:04 -0700167 //
168 // create a styling instance
169 //
170 skc_styling_t styling;
171
172 err = skc_styling_create(context,
173 &styling,
174 svg_doc_layer_count(svg_doc),
175 1000,
176 2 * 1024 * 1024);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700177
Allan MacKinnon4359d522018-06-19 13:57:04 -0700178 //
179 // create a surface
180 //
181 skc_surface_t surface;
182
183 err = skc_surface_create(context,&surface);
184
185 //
186 // create a transform stack
187 //
188 struct skc_transform_stack * ts = skc_transform_stack_create(32);
189
190 // prime the transform stack with subpixel scale
191 skc_transform_stack_push_scale(ts,32.0,32.0);
192
193 //
194 // rasterize, render and reclaim svg until escape
195 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700196 skc_pipeline_start_at_e pipeline_start_at_base = SKC_PIPELINE_START_AT_DEFINE_PATHS;
Hal Canary14195342018-07-11 16:10:14 -0400197 skc_pipeline_start_at_e pipeline_start_at_loop = SKC_PIPELINE_START_AT_DEFINE_PATHS;
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700198 skc_path_t * paths;
199 skc_raster_t * rasters;
200
201 while (!skc_interop_should_exit(interop))
Allan MacKinnon4359d522018-06-19 13:57:04 -0700202 {
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700203 // redefine the paths?
204 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_DEFINE_PATHS)
205 {
206 // decode paths
207 paths = svg_doc_paths_decode(svg_doc,path_builder);
208 }
Allan MacKinnon4359d522018-06-19 13:57:04 -0700209
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700210 // rasterize the paths?
211 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_RASTERIZE)
212 {
213 // save stack
214 uint32_t const ts_save = skc_transform_stack_save(ts);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700215
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700216 // update transform
217 skc_interop_transform(interop,ts);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700218
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700219 // decode rasters
220 rasters = svg_doc_rasters_decode(svg_doc,ts,paths,raster_builder);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700221
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700222 // restore the transform stack
223 skc_transform_stack_restore(ts,ts_save);
224 }
Allan MacKinnon4359d522018-06-19 13:57:04 -0700225
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700226 // decode the styling and composition?
227 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_COMPOSITION)
228 {
229 // reset styling
230 skc_styling_reset(styling);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700231
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700232 // unseal and reset the composition
233 skc_composition_unseal(composition,true);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700234
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700235 // decode layers -- places rasters
236 svg_doc_layers_decode(svg_doc,rasters,composition,styling,true/*is_srgb*/);
237
238 // seal the styling -- render will seal if not called
239 skc_styling_seal(styling);
240
241 // seal the composition -- render will seal if not called
242 skc_composition_seal(composition);
243 }
244
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -0700245 uint32_t const clip[] = { 0, 0, 65535, 65535 };
246 int32_t const txty[] = { 0, 0 };
Allan MacKinnon4359d522018-06-19 13:57:04 -0700247
248 // render the styled composition to the surface
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700249 skc_surface_render(surface,
250 styling,
251 composition,
252 skc_interop_get_framebuffer(interop),
253 clip,
Allan MacKinnon9e0d7e42018-07-16 15:57:05 -0700254 txty,
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700255 NULL,
256 NULL);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700257
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700258 //
259 // poll for events and maybe start from a different point in the
260 // pipeline
261 //
262 int key;
Allan MacKinnon4359d522018-06-19 13:57:04 -0700263
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700264 // poll for window events
265 bool const transform_changed = skc_interop_poll(interop,&key);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700266
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700267 // how many blocks are in use?
268 if (key == 'I')
269 skc_runtime_cl_12_debug(context);
270
271 // do we only want to run part of the pipeline?
272 if ((key >= SKC_PIPELINE_START_AT_DEFINE_PATHS) && (key <= SKC_PIPELINE_START_AT_RENDER))
273 pipeline_start_at_base = key;
Hal Canary14195342018-07-11 16:10:14 -0400274
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700275 // valid for a loop
276 pipeline_start_at_loop = pipeline_start_at_base;
277
278 // if the transform changed then we must start at rasterize or before
279 if (transform_changed)
280 pipeline_start_at_loop = min(pipeline_start_at_loop,SKC_PIPELINE_START_AT_RASTERIZE);
281
282 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_COMPOSITION)
283 {
284 // rewind the svg doc
285 svg_doc_rewind(svg_doc);
286
287 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_DEFINE_PATHS)
288 {
289 // release the paths
290 svg_doc_paths_release(svg_doc,paths,context);
291 }
292
293 if (pipeline_start_at_loop <= SKC_PIPELINE_START_AT_RASTERIZE)
294 {
295 // release the rasters
296 svg_doc_rasters_release(svg_doc,rasters,context);
297 }
298 }
299
300#if 0
301 //
302 // Note that we don't need to explicitly wait for the render()
303 // to complete since SKC is fully concurrent and the styling and
304 // compsition unseal() operations will "clock" the render loop.
305 //
306
307 //
308 // explicitly spin until framebuffer is rendered
309 //
310 bool quit = false;
311
Allan MacKinnon4359d522018-06-19 13:57:04 -0700312 while (!quit) {
313 // fprintf(stderr,"WAITING ON: !quit\n");
314 skc_context_wait(context);
315 }
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700316#endif
Allan MacKinnon4359d522018-06-19 13:57:04 -0700317 }
Allan MacKinnonc110e792018-06-21 09:09:56 -0700318
Allan MacKinnon4359d522018-06-19 13:57:04 -0700319 //
320 // dispose of mundane resources
321 //
322 skc_transform_stack_release(ts);
323
324 //
325 // dispose of all SKC resources
326 //
327 err = skc_surface_release(surface);
328 err = skc_styling_release(styling);
329 err = skc_composition_release(composition);
330 err = skc_raster_builder_release(raster_builder);
331 err = skc_path_builder_release(path_builder);
332 err = skc_context_release(context);
333
334 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700335 // dispose of GL interop
Allan MacKinnon4359d522018-06-19 13:57:04 -0700336 //
Allan MacKinnonebf160f2018-06-24 08:31:14 -0700337 skc_interop_destroy(interop);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700338
339 //
340 //
341 //
342 return EXIT_SUCCESS;
343}
344
345//
346//
347//