Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
Ravi Mistry | 14538dd | 2018-06-20 09:11:24 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 5 | * 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 24 | #include "skc_create_cl.h" |
| 25 | |
| 26 | #include "common/cl/find_cl.h" |
| 27 | #include "common/cl/assert_cl.h" |
| 28 | |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 29 | #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 MacKinnon | 879c989 | 2018-06-20 08:29:07 -0700 | [diff] [blame] | 38 | #include "platforms/cl_12/gl/interop.h" |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 39 | |
| 40 | // |
| 41 | // |
| 42 | // |
| 43 | |
| 44 | void |
| 45 | skc_runtime_cl_12_debug(struct skc_context * const context); |
| 46 | |
| 47 | // |
| 48 | // |
| 49 | // |
| 50 | |
| 51 | |
| 52 | |
| 53 | // |
| 54 | // |
| 55 | // |
| 56 | |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 57 | static |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 58 | void |
| 59 | is_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 | |
| 71 | int |
| 72 | main(int argc, char** argv) |
| 73 | { |
| 74 | // |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 75 | // |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 76 | // |
| 77 | if (argc <= 1) |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 78 | { |
| 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 103 | // 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 MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 115 | // get GL and device contexts |
| 116 | // |
| 117 | HGLRC hGLRC = wglGetCurrentContext(); |
| 118 | HDC hDC = wglGetCurrentDC(); |
| 119 | |
| 120 | // |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 121 | // create the CL context |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 122 | // |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 123 | cl_context_properties context_properties_cl[] = |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 124 | { |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 125 | CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id_cl, |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 126 | CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, |
| 127 | CL_WGL_HDC_KHR, (cl_context_properties)hDC, |
| 128 | 0 |
| 129 | }; |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 130 | |
| 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 MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 138 | // |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 139 | // create SKC context |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 140 | // |
| 141 | skc_context_t context; |
| 142 | |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 143 | skc_err err = skc_context_create_cl(&context, |
| 144 | context_cl, |
| 145 | device_id_cl); |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 146 | |
| 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 165 | |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 166 | // |
| 167 | // create a composition |
| 168 | // |
| 169 | skc_composition_t composition; |
| 170 | |
| 171 | err = skc_composition_create(context,&composition); |
Allan MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 172 | |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 173 | // |
| 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 183 | |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 184 | // |
| 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 220 | svg_doc_layers_decode(svg_doc,rasters,composition,styling,true/*is_srgb*/); |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 221 | |
| 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 MacKinnon | c110e79 | 2018-06-21 09:09:56 -0700 | [diff] [blame^] | 273 | |
Allan MacKinnon | 4359d52 | 2018-06-19 13:57:04 -0700 | [diff] [blame] | 274 | // |
| 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 | // |