Unverified Commit 5ad212b5 authored by Victor Kropp's avatar Victor Kropp Committed by GitHub

Add binding for SkGraphics methods (#538)

* Add binding for SkGraphics methods

* Add binding for SkGraphics methods in nativeJsMain

* Add smoke tests for SkGraphics bindings

* explicitly resolve java.awt.Graphics reference

* fix external symbol names for SkGraphics bindings
parent 278b187b
......@@ -3,8 +3,9 @@ package org.jetbrains.skiko
import org.jetbrains.skia.*
import org.jetbrains.skia.Canvas
import org.jetbrains.skiko.redrawer.Redrawer
import java.awt.*
import java.awt.Color
import java.awt.Component
import java.awt.Window
import java.awt.event.*
import java.awt.im.InputMethodRequests
import java.util.concurrent.CancellationException
......@@ -68,7 +69,7 @@ actual open class SkiaLayer internal constructor(
isOpaque = false
layout = null
backedLayer = object : HardwareLayer(externalAccessibleFactory) {
override fun paint(g: Graphics) {
override fun paint(g: java.awt.Graphics) {
// 1. JPanel.paint is not always called (in rare cases).
// For example if we call 'jframe.isResizable = false` on Ubuntu
//
......@@ -342,7 +343,7 @@ actual open class SkiaLayer internal constructor(
redrawer?.syncSize()
}
override fun paint(g: Graphics) {
override fun paint(g: java.awt.Graphics) {
super.paint(g)
if (backedLayer.checkContentScale()) {
notifyChange(PropertyKind.ContentScale)
......
package org.jetbrains.skia
import org.jetbrains.skia.impl.Library.Companion.staticLoad
import org.jetbrains.skia.impl.Stats
class Graphics {
companion object {
init {
staticLoad()
}
/**
* The max number of bytes that should be used by the font cache.
*/
var fontCacheLimit: Int
get() {
Stats.onNativeCall()
return _nGetFontCacheLimit()
}
set(value) {
Stats.onNativeCall()
_nSetFontCacheLimit(value)
}
/**
* The number of bytes currently used by the font cache.
*/
val fontCacheUsed: Int
get() {
Stats.onNativeCall()
return _nGetFontCacheUsed()
}
/**
* The max number of entries in the font cache.
*/
var fontCacheCountLimit: Int
get() {
Stats.onNativeCall()
return _nGetFontCacheCountLimit()
}
set(value) {
Stats.onNativeCall()
_nSetFontCacheCountLimit(value)
}
/**
* The number of entries currently in the font cache.
*/
val fontCacheCountUsed: Int
get() {
Stats.onNativeCall()
return _nGetFontCacheCountUsed()
}
/**
* The memory usage limit in bytes for the resource cache, used for temporary bitmaps and other resources.
*
* Entries are purged from the cache when the memory useage exceeds this limit.
*/
var resourceCacheTotalLimit: Int
get() {
Stats.onNativeCall()
return _nGetResourceCacheTotalByteLimit()
}
set(value) {
Stats.onNativeCall()
_nSetResourceCacheTotalByteLimit(value)
}
/**
* When the cachable entry is very large (e.g. a large scaled bitmap), adding it to the cache
* can cause most/all of the existing entries to be purged. To avoid this, the client can set
* a limit for a single allocation. If a cacheable entry would have been cached, but its size
* exceeds this limit, then we do not attempt to cache it at all.
*
* Zero is the default value, meaning we always attempt to cache entries.
*/
var resourceCacheSingleAllocationByteLimit: Int
get() {
Stats.onNativeCall()
return _nGetResourceCacheSingleAllocationByteLimit()
}
set(value) {
Stats.onNativeCall()
_nSetResourceCacheSingleAllocationByteLimit(value)
}
/**
* The memory in bytes used for temporary images and other resources.
*/
val resourceCacheTotalUsed: Int
get() {
Stats.onNativeCall()
return _nGetResourceCacheTotalBytesUsed()
}
/**
* For debugging purposes, this will attempt to purge the font cache.
*
* It does not change the limit, but will cause subsequent font measures and draws to be recreated,
* since they will no longer be in the cache.
*/
fun purgeFontCache() {
Stats.onNativeCall()
_nPurgeFontCache()
}
/**
* For debugging purposes, this will attempt to purge the resource cache.
*
* It does not change the limit.
*/
fun purgeResourceCache() {
Stats.onNativeCall()
_nPurgeResourceCache()
}
/**
* Free as much globally cached memory as possible. This will purge all private caches in Skia,
* including font and image caches.
*
* If there are caches associated with GPU context, those will not be affected by this call.
*/
fun purgeAllCaches() {
Stats.onNativeCall()
_nPurgeAllCaches()
}
}
}
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetFontCacheLimit")
private external fun _nGetFontCacheLimit(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nSetFontCacheLimit")
private external fun _nSetFontCacheLimit(bytes: Int): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetFontCacheUsed")
private external fun _nGetFontCacheUsed(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountLimit")
private external fun _nGetFontCacheCountLimit(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nSetFontCacheCountLimit")
private external fun _nSetFontCacheCountLimit(count: Int): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountUsed")
private external fun _nGetFontCacheCountUsed(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalByteLimit")
private external fun _nGetResourceCacheTotalByteLimit(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nSetResourceCacheTotalByteLimit")
private external fun _nSetResourceCacheTotalByteLimit(bytes: Int): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetResourceCacheSingleAllocationByteLimit")
private external fun _nGetResourceCacheSingleAllocationByteLimit(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nSetResourceCacheSingleAllocationByteLimit")
private external fun _nSetResourceCacheSingleAllocationByteLimit(bytes: Int): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalBytesUsed")
private external fun _nGetResourceCacheTotalBytesUsed(): Int
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nPurgeFontCache")
private external fun _nPurgeFontCache()
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nPurgeResourceCache")
private external fun _nPurgeResourceCache()
@ExternalSymbolName("org_jetbrains_skia_GraphicsKt__1nPurgeAllCaches")
private external fun _nPurgeAllCaches()
package org.jetbrains.skia
import org.jetbrains.skiko.tests.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
class GraphicsTest {
@Test
fun canSetAndGetProperties() = runTest {
Graphics.fontCacheCountLimit = 7
assertEquals(7, Graphics.fontCacheCountLimit)
Graphics.fontCacheLimit = 42
assertEquals(42, Graphics.fontCacheLimit)
Graphics.resourceCacheSingleAllocationByteLimit = 123
assertEquals(123, Graphics.resourceCacheSingleAllocationByteLimit)
Graphics.resourceCacheTotalLimit = 321
assertEquals(321, Graphics.resourceCacheTotalLimit)
}
@Test
fun canPurgeFontCache() = runTest {
Graphics.purgeFontCache()
assertEquals(0, Graphics.fontCacheCountUsed)
assertEquals(0, Graphics.fontCacheUsed)
}
@Test
fun canPurgeResourceCache() = runTest {
Graphics.purgeResourceCache()
assertEquals(0, Graphics.resourceCacheTotalUsed)
}
@Test
fun canPurgeAllCaches() = runTest {
Graphics.purgeAllCaches()
assertEquals(0, Graphics.fontCacheCountUsed)
assertEquals(0, Graphics.fontCacheUsed)
assertEquals(0, Graphics.resourceCacheTotalUsed)
}
}
#include <iostream>
#include <jni.h>
#include "SkGraphics.h"
#include "interop.hh"
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetFontCacheLimit
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetFontCacheLimit();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nSetFontCacheLimit
(JNIEnv* env, jclass jclass, jint bytes) {
return SkGraphics::SetFontCacheLimit(bytes);
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetFontCacheUsed
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetFontCacheUsed();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountLimit
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetFontCacheCountLimit();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nSetFontCacheCountLimit
(JNIEnv* env, jclass jclass, jint count) {
return SkGraphics::SetFontCacheCountLimit(count);
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountUsed
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetFontCacheCountUsed();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalByteLimit
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetResourceCacheTotalByteLimit();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nSetResourceCacheTotalByteLimit
(JNIEnv* env, jclass jclass, jint bytes) {
return SkGraphics::SetResourceCacheTotalByteLimit(bytes);
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetResourceCacheSingleAllocationByteLimit
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetResourceCacheSingleAllocationByteLimit();
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nSetResourceCacheSingleAllocationByteLimit
(JNIEnv* env, jclass jclass, jint bytes) {
return SkGraphics::SetResourceCacheSingleAllocationByteLimit(bytes);
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalBytesUsed
(JNIEnv* env, jclass jclass) {
return SkGraphics::GetResourceCacheTotalBytesUsed();
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_GraphicsKt__1nPurgeFontCache
(JNIEnv* env, jclass jclass) {
SkGraphics::PurgeFontCache();
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_GraphicsKt__1nPurgeResourceCache
(JNIEnv* env, jclass jclass) {
SkGraphics::PurgeResourceCache();
}
extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_GraphicsKt__1nPurgeAllCaches
(JNIEnv* env, jclass jclass) {
SkGraphics::PurgeAllCaches();
}
#include "SkGraphics.h"
#include "common.h"
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetFontCacheLimit
() {
return SkGraphics::GetFontCacheLimit();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nSetFontCacheLimit
(KInt bytes) {
return SkGraphics::SetFontCacheLimit(bytes);
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetFontCacheUsed
() {
return SkGraphics::GetFontCacheUsed();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountLimit
() {
return SkGraphics::GetFontCacheCountLimit();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nSetFontCacheCountLimit
(KInt count) {
return SkGraphics::SetFontCacheCountLimit(count);
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetFontCacheCountUsed
() {
return SkGraphics::GetFontCacheCountUsed();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalByteLimit
() {
return SkGraphics::GetResourceCacheTotalByteLimit();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nSetResourceCacheTotalByteLimit
(KInt bytes) {
return SkGraphics::SetResourceCacheTotalByteLimit(bytes);
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetResourceCacheSingleAllocationByteLimit
() {
return SkGraphics::GetResourceCacheSingleAllocationByteLimit();
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nSetResourceCacheSingleAllocationByteLimit
(KInt bytes) {
return SkGraphics::SetResourceCacheSingleAllocationByteLimit(bytes);
}
SKIKO_EXPORT KInt org_jetbrains_skia_GraphicsKt__1nGetResourceCacheTotalBytesUsed
() {
return SkGraphics::GetResourceCacheTotalBytesUsed();
}
SKIKO_EXPORT void org_jetbrains_skia_GraphicsKt__1nPurgeFontCache
() {
SkGraphics::PurgeFontCache();
}
SKIKO_EXPORT void org_jetbrains_skia_GraphicsKt__1nPurgeResourceCache
() {
SkGraphics::PurgeResourceCache();
}
SKIKO_EXPORT void org_jetbrains_skia_GraphicsKt__1nPurgeAllCaches
() {
SkGraphics::PurgeAllCaches();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment