blob: e0d42b31e08bb6b86e9b39c845bd02b2275a00d3 [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
13#include <glad/glad.h>
14#include <GLFW/glfw3.h>
15
16//
17//
18//
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <conio.h>
23
Allan MacKinnonc110e792018-06-21 09:09:56 -070024#include "skc_create_cl.h"
25
26#include "common/cl/find_cl.h"
27#include "common/cl/assert_cl.h"
28
Allan MacKinnon4359d522018-06-19 13:57:04 -070029#include "svg/svg_doc.h"
30#include "svg2skc/svg2skc.h"
31#include "svg2skc/transform_stack.h"
32
33//
34//
35//
36
37#include <CL/opencl.h>
Allan MacKinnon879c9892018-06-20 08:29:07 -070038#include "platforms/cl_12/gl/interop.h"
Allan MacKinnon4359d522018-06-19 13:57:04 -070039
40//
41//
42//
43
44void
45skc_runtime_cl_12_debug(struct skc_context * const context);
46
47//
48//
49//
50
51
52
53//
54//
55//
56
Allan MacKinnonc110e792018-06-21 09:09:56 -070057static
Allan MacKinnon4359d522018-06-19 13:57:04 -070058void
59is_render_complete(skc_surface_t surface,
60 skc_styling_t styling,
61 skc_composition_t composition,
62 bool * quit)
63{
64 *quit = true;
65}
66
67//
68//
69//
70
71int
72main(int argc, char** argv)
73{
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 //
86 // #include "test/lion.inl"
87
88 struct svg_doc * svg_doc = svg_doc_parse(argv[1],false);
89
90 fprintf(stderr,"p/r/l = %u / %u / %u\n",
91 svg_doc_path_count(svg_doc),
92 svg_doc_raster_count(svg_doc),
93 svg_doc_layer_count(svg_doc));
94
95 //
96 // fire up GL
97 //
98 GLFWwindow * window;
99
100 skc_interop_init(&window);
101
102 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700103 // find platform and device by name
104 //
105 cl_platform_id platform_id_cl;
106 cl_device_id device_id_cl;
107
108 cl(FindIdsByName("Intel","Graphics",
109 &platform_id_cl,
110 &device_id_cl,
111 0,NULL,NULL,
112 true));
113
114 //
Allan MacKinnon4359d522018-06-19 13:57:04 -0700115 // get GL and device contexts
116 //
117 HGLRC hGLRC = wglGetCurrentContext();
118 HDC hDC = wglGetCurrentDC();
119
120 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700121 // create the CL context
Allan MacKinnon4359d522018-06-19 13:57:04 -0700122 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700123 cl_context_properties context_properties_cl[] =
Allan MacKinnon4359d522018-06-19 13:57:04 -0700124 {
Allan MacKinnonc110e792018-06-21 09:09:56 -0700125 CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id_cl,
Allan MacKinnon4359d522018-06-19 13:57:04 -0700126 CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC,
127 CL_WGL_HDC_KHR, (cl_context_properties)hDC,
128 0
129 };
Allan MacKinnonc110e792018-06-21 09:09:56 -0700130
131 cl_int cl_err;
132 cl_context context_cl = clCreateContext(context_properties_cl,
133 1,
134 &device_id_cl,
135 NULL,
136 NULL,
137 &cl_err); cl_ok(cl_err);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700138 //
Allan MacKinnonc110e792018-06-21 09:09:56 -0700139 // create SKC context
Allan MacKinnon4359d522018-06-19 13:57:04 -0700140 //
141 skc_context_t context;
142
Allan MacKinnonc110e792018-06-21 09:09:56 -0700143 skc_err err = skc_context_create_cl(&context,
144 context_cl,
145 device_id_cl);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700146
147 //
148 // associate
149 //
150 skc_interop_register(context);
151
152 //
153 // create path builder
154 //
155 skc_path_builder_t path_builder;
156
157 err = skc_path_builder_create(context,&path_builder);
158
159 //
160 // create raster builder
161 //
162 skc_raster_builder_t raster_builder;
163
164 err = skc_raster_builder_create(context,&raster_builder);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700165
Allan MacKinnon4359d522018-06-19 13:57:04 -0700166 //
167 // create a composition
168 //
169 skc_composition_t composition;
170
171 err = skc_composition_create(context,&composition);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700172
Allan MacKinnon4359d522018-06-19 13:57:04 -0700173 //
174 // create a styling instance
175 //
176 skc_styling_t styling;
177
178 err = skc_styling_create(context,
179 &styling,
180 svg_doc_layer_count(svg_doc),
181 1000,
182 2 * 1024 * 1024);
Allan MacKinnonc110e792018-06-21 09:09:56 -0700183
Allan MacKinnon4359d522018-06-19 13:57:04 -0700184 //
185 // create a surface
186 //
187 skc_surface_t surface;
188
189 err = skc_surface_create(context,&surface);
190
191 //
192 // create a transform stack
193 //
194 struct skc_transform_stack * ts = skc_transform_stack_create(32);
195
196 // prime the transform stack with subpixel scale
197 skc_transform_stack_push_scale(ts,32.0,32.0);
198
199 //
200 // rasterize, render and reclaim svg until escape
201 //
202 while (!glfwWindowShouldClose(window))
203 {
204 // save stack
205 uint32_t const ts_save = skc_transform_stack_save(ts);
206
207 // poll glfw
208 skc_interop_poll(window,ts);
209
210 // decode paths
211 skc_path_t * paths = svg_doc_paths_decode(svg_doc,path_builder);
212
213 // decode rasters
214 skc_raster_t * rasters = svg_doc_rasters_decode(svg_doc,ts,paths,raster_builder);
215
216 // restore the transform stack
217 skc_transform_stack_restore(ts,ts_save);
218
219 // decode layers -- places rasters
Allan MacKinnonc110e792018-06-21 09:09:56 -0700220 svg_doc_layers_decode(svg_doc,rasters,composition,styling,true/*is_srgb*/);
Allan MacKinnon4359d522018-06-19 13:57:04 -0700221
222 // seal the composition
223 skc_composition_seal(composition);
224
225 bool quit = false;
226 uint32_t const clip[] = { 0, 0, 65535, 65535 }; // tile clip is <= 9 bits (512)
227
228 // render the styled composition to the surface
229 skc_surface_render(surface,clip,styling,composition,
230 is_render_complete,&quit,
231 skc_interop_get_fb(window));
232
233 // release the paths
234 svg_doc_paths_release(svg_doc,paths,context);
235
236 // rasters have been released
237 svg_doc_rasters_release(svg_doc,rasters,context);
238
239 // spin until framebuffer is rendered
240 while (!quit) {
241 // fprintf(stderr,"WAITING ON: !quit\n");
242 skc_context_wait(context);
243 }
244
245 // blit and swap
246 skc_interop_blit(window);
247
248 // print out some useful debug info
249 skc_runtime_cl_12_debug(context);
250
251 // rewind the doc
252 svg_doc_rewind(svg_doc);
253
254 //
255 // I don't like this being here
256 //
257 float const rgba[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
258 uint32_t rect[4] = { 0 };
259
260 skc_interop_get_dim(rect+2);
261
262 skc_surface_clear(surface,rgba,rect,
263 skc_interop_get_fb(window));
264
265 // exit(EXIT_SUCCESS);
266
267 // reset styling
268 skc_styling_reset(styling);
269
270 // unseal the composition
271 skc_composition_unseal(composition,true);
272 }
Allan MacKinnonc110e792018-06-21 09:09:56 -0700273
Allan MacKinnon4359d522018-06-19 13:57:04 -0700274 //
275 // dispose of mundane resources
276 //
277 skc_transform_stack_release(ts);
278
279 //
280 // dispose of all SKC resources
281 //
282 err = skc_surface_release(surface);
283 err = skc_styling_release(styling);
284 err = skc_composition_release(composition);
285 err = skc_raster_builder_release(raster_builder);
286 err = skc_path_builder_release(path_builder);
287 err = skc_context_release(context);
288
289 //
290 // GLFW CLEANUP
291 //
292 glfwDestroyWindow(window);
293 glfwTerminate();
294
295 //
296 //
297 //
298 return EXIT_SUCCESS;
299}
300
301//
302//
303//