Skip to content

Mark parseGlyfGlyph prototype as gcsafe#358

Merged
treeform merged 1 commit intomasterfrom
unknown repository
Jan 9, 2022
Merged

Mark parseGlyfGlyph prototype as gcsafe#358
treeform merged 1 commit intomasterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Jan 4, 2022

This causes problems for procs that call fillText and need to be gcsafe.

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 6, 2022

Could you provide a short example of where this happens? I am not sure what the problem is.

@ghost
Copy link
Copy Markdown
Author

ghost commented Jan 6, 2022

Sorry, the proc that needed to be marked was further down in opentype.nim.

The problem is that when a proc is declared without a body the compiler will assume that it is not gcsafe, and any proc that references that proc declaration becomes implicitly un-gcsafe as well.

@ghost ghost changed the title Mark fillText prototype as gcsafe Mark parseGlyfGlyph prototype as gcsafe Jan 6, 2022
@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 6, 2022

What happens when you move gcsafe not further down, but up into your own code?

@ghost
Copy link
Copy Markdown
Author

ghost commented Jan 6, 2022

If I put gcsafe high up in my code things work, but I'm rendering in nested callbacks, so the trace back to the original function takes some effort to follow. And if I mark something high up as gcsafe to silience an error then I might miss an actually gcunsafe proc later.

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 6, 2022

So you have threads and callbacks, which requires gc safe? This feels like a nim issue not a pixie issue?

Eventually all pixie methods would require gc safe, no? I don't think it's workable to mark only the methods you use as gc safe? Or do you think only some key methods need the gc safe tag for things to work?

We need a simple test case of this before we merge. And a better explanation. I don't get why you need gc safe at all and can't have it in your user code.

@ghost
Copy link
Copy Markdown
Author

ghost commented Jan 7, 2022

https://nim-lang.org/docs/manual.html#effect-system-gc-safety-effect

Doesn't compile:

import std/asyncdispatch
import pixie

proc main() {.gcsafe.} =
  let image = newImage(200, 200)
  image.fill(rgba(255, 255, 255, 255))
  
  var font = readFont("examples/data/Roboto-Regular_1.ttf")
  font.size = 20
  
  let text = "Hello world"
  
  image.fillText(font.typeset(text, vec2(180, 180)), translate(vec2(10, 10)))
  image.writeFile("examples/text.png")

waitFor main()

Gcsafe is necessary for safe callbacks, and using callbacks is unavoidable in some cases.

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 7, 2022

I think you have a bug in your code, waitFor needs an async proc not a gcsafe proc, it throws some strange error instead. I think the correct code is (compiles on my machine ™️):

import std/asyncdispatch
import pixie

proc main() {.async.} =
  let image = newImage(200, 200)
  image.fill(rgba(255, 255, 255, 255))

  var font = readFont("/p/pixie/examples/data/Roboto-Regular_1.ttf")
  font.size = 20

  let text = "Hello world"

  image.fillText(font.typeset(text, vec2(180, 180)), translate(vec2(10, 10)))
  image.writeFile("text.png")

waitFor main()

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 7, 2022

I also don't get why it correctly infers that that:

  • fillText is gc safe
  • textUber is gc safe
  • getGlyphPath is gc safe
  • parseGlyph is gc safe
  • parseGlyfGlyph is for some reason not?

What is that reason?

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 7, 2022

It appears that Nim can't GC safe recursive proc calls.

parseGlyfGlyph calls parseCompositeGlyph which can call parseGlyfGlyph

It gets confused and marks whole tree not gc safe even though it is?

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 7, 2022

I have filed a bug with Nim: nim-lang/Nim#19339

@treeform
Copy link
Copy Markdown
Owner

treeform commented Jan 9, 2022

Hey @ehmry, you where right! It looks like we need to add gc safe to all forward declaration.

@treeform treeform merged commit 8e64260 into treeform:master Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant