Skip to content

Conversation

@CedricGuillemet
Copy link
Contributor

@CedricGuillemet CedricGuillemet commented Apr 2, 2025

Replaces #1488

  • Fix wrongly named CreateInstance with Initialize
  • Canvas/Context unit test
  • Implement GetCanvas
  • Fix parameters and objectReference between Canvas and Context
  • Keep and return the object reference created with getContext
  • Install specified CMake version for Windows platform (see Support more recent CMake version #1492 )
  • Update to Babylon.js 8.0

Copy link
Contributor

@bghgary bghgary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except for the circular reference

Comment on lines 22 to 25
var texSize = 512;
var dynamicTexture = new BABYLON.DynamicTexture("dynamic texture", texSize, scene);
var context = dynamicTexture.getContext();
var otherContext = dynamicTexture.getContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why var and not const?

return Context::CreateInstance(info.Env(), this);
if (m_contextObject.IsEmpty())
{
m_contextObject = Napi::Persistent(Context::CreateInstance(info.Env(), info.This()).As<Napi::Object>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Canvas holds context and context holds canvas. This is probably going to prevent the GC from collecting these objects once created. Can you check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With canvasObject persistent, context is not disposed nor Canvas when disposing the dynamic texture. When using Napi::Weak Canvas and Context are properly disposed when disposing dyn. texture.

Context::Context(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Context>{info}
, m_canvas{info[0].As<Napi::External<NativeCanvas>>().Data()}
, m_canvasObject{Napi::Weak(info[0].As<Napi::Object>())}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does there need to be a js reference from context to canvas? If the js side only references the context, we don't want the canvas to be gc'd, right? I assume this is how it works in the browser?

Copy link
Contributor Author

@CedricGuillemet CedricGuillemet Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference from context to canvas is needed by the getCanvas method.

If the js side only references the context, we don't want the canvas to be gc'd, right? I assume this is how it works in the browser?

That was my guess. But in that case, when is the Canvas and Context are collected if both keep a strong reference on each other? Is there a way to detect that?

@CedricGuillemet CedricGuillemet changed the base branch from master to CanvasTest April 4, 2025 14:45
… into ContextGetCanvas

# Conflicts:
#	Apps/package-lock.json
#	Apps/package.json
#	Polyfills/Canvas/Source/Canvas.cpp
#	Polyfills/Canvas/Source/Context.cpp
#	Polyfills/Canvas/Source/Context.h
@CedricGuillemet CedricGuillemet merged commit 5ef4c23 into BabylonJS:CanvasTest Apr 4, 2025
3 checks passed
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.

4 participants