* The Runtime is one of the core class of Valdi. It is primarily used to create a * View tree for its document name. Because it is backed by a C++ instance, destroy() * should be called when the Runtime is not needed to release the native resources. * The Runtime is stateful and holds a cache. Because of this, you should typically only * have a single instance per app. This is especially true
| 47 | * enabled at all time. |
| 48 | */ |
| 49 | class ValdiRuntime( |
| 50 | val native: RuntimeNative, |
| 51 | override val context: Context, |
| 52 | val logger: Logger, |
| 53 | var manager: ValdiRuntimeManager?, |
| 54 | val viewRefSupport: ViewRefSupport, |
| 55 | val isMain: Boolean |
| 56 | ) : JSThreadDispatcher, MainThreadBatchDispatcher, IValdiRuntime, AssetsManager, FontManager.ModuleTypefaceDataProvider { |
| 57 | |
| 58 | var performGcOnContextDestroy = false |
| 59 | |
| 60 | private var cachingJSRuntime: ValdiJSRuntime? = null |
| 61 | |
| 62 | var nativeModules: ValdiNativeModules? = null |
| 63 | |
| 64 | init { |
| 65 | // Used to verify that Valdi objects are correctly collected. |
| 66 | // scheduleGc() |
| 67 | |
| 68 | manager?.fontManager?.registerModuleTypefaceDataProvider(this) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | private fun scheduleGc() { |
| 73 | runOnMainThreadDelayed(5000L) { |
| 74 | println("[valdi] GC!") |
| 75 | performGcNow(true) |
| 76 | Runtime.getRuntime().gc() |
| 77 | scheduleGc() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Destroy the Runtime and release all its native resources. |
| 83 | */ |
| 84 | fun destroy() { |
| 85 | nativeModules?.dispose() |
| 86 | |
| 87 | if (native.nativeHandle != 0L) { |
| 88 | NativeBridge.setRuntimeAttachedObject(native.nativeHandle, null) |
| 89 | native.destroy() |
| 90 | } |
| 91 | |
| 92 | manager?.fontManager?.unregisterModuleTypefaceDataProvider(this) |
| 93 | manager = null |
| 94 | } |
| 95 | |
| 96 | override fun dispose() { |
| 97 | destroy() |
| 98 | } |
| 99 | |
| 100 | private fun shouldEnableSnapDrawingRenderBackend(requestedRenderBackend: RenderBackend): Boolean { |
| 101 | val manager = this.manager ?: return false |
| 102 | |
| 103 | return manager.shouldUseSnapDrawingRenderBackend(requestedRenderBackend) |
| 104 | } |
| 105 | |
| 106 | private fun shouldEnableLegacyMeasureBehavior(useLegacyMeasureBehavior: Boolean?): Boolean { |
no test coverage detected