Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Performance #236

Merged
merged 3 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ steps:
rm: true
when:
branch:
- master
event:
- push
status:
- success
- failure
- master
status: [ success, failure ]
- name: upload-artifacts
image: appleboy/drone-scp
settings:
Expand All @@ -43,4 +39,25 @@ steps:
strip_components: 2
target: ~/artifacts.acornui.com/mvn
source:
- build/artifacts
- build/artifacts
when:
branch:
- master
- name: slack
image: plugins/slack
settings:
webhook:
from_secret: SLACK_ACORNBOT_WEBHOOK
channel: acorn-bot
template: >
{{#success build.status}}
build {{build.number}} succeeded. Good job.
{{else}}
build {{build.number}} failed. Fix me please.
{{/success}}
when:
status: [ success, failure ]

trigger:
event:
- push
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import com.acornui.async.PendingDisposablesRegistry
import com.acornui.async.Work
import com.acornui.async.applicationScopeKey
import com.acornui.async.mainScope
import com.acornui.component.Stage
import com.acornui.component.StageImpl
import com.acornui.di.*
import com.acornui.gl.core.CachedGl20
import com.acornui.gl.core.DefaultShaderProgram
import com.acornui.io.BinaryLoader
import com.acornui.io.TextLoader
import com.acornui.io.file.Files
Expand All @@ -39,7 +43,7 @@ import kotlinx.coroutines.cancel
*/
interface Application {

suspend fun start(appConfig: AppConfig = AppConfig(), onReady: Owned.() -> Unit)
suspend fun start(appConfig: AppConfig = AppConfig(), onReady: Stage.() -> Unit)
}

/**
Expand Down Expand Up @@ -105,6 +109,8 @@ abstract class ApplicationBase : Application {
BinaryLoader()
}

protected open fun createStage(injector: Injector): Stage = StageImpl(injector)

fun <T : Any> task(dKey: DKey<T>, timeout: Float = 10f, isOptional: Boolean = false, work: Work<T>) = bootstrap.task<ApplicationBase, T>(dKey, timeout, isOptional, work)

protected open fun dispose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ interface RedrawRegionsRo {

interface RedrawRegions : RedrawRegionsRo, Clearable {

/**
* Invalidates the redraw region, in screen coordinates.
*/
fun invalidate(x: Int, y: Int, width: Int, height: Int)
fun invalidate(region: IntRectangleRo) = invalidate(region.x, region.y, region.width, region.height)

/**
* Invalidates the redraw region, in screen coordinates.
*/
fun invalidate(screenRegion: IntRectangleRo) = invalidate(screenRegion.x, screenRegion.y, screenRegion.width, screenRegion.height)

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package com.acornui.component

import com.acornui.gl.core.CachedGl20
import com.acornui.recycle.Clearable
import com.acornui.graphic.*
import com.acornui.gl.core.GlState
import com.acornui.math.Matrix4
import com.acornui.math.Matrix4Ro

class Atlas(private val glState: GlState) : BasicRenderable, Clearable {
class Atlas(val gl: CachedGl20) : BasicRenderable, Clearable {

var region: AtlasRegionData? = null
private set
Expand Down Expand Up @@ -57,7 +57,7 @@ class Atlas(private val glState: GlState) : BasicRenderable, Clearable {
if (region.splits == null) {
ninePatch = null
if (sprite == null) {
sprite = Sprite(glState).apply {
sprite = Sprite(gl).apply {
blendMode = r.blendMode
premultipliedAlpha = r.premultipliedAlpha
setScaling(r.scaleX, r.scaleY)
Expand All @@ -68,7 +68,7 @@ class Atlas(private val glState: GlState) : BasicRenderable, Clearable {
} else {
sprite = null
if (ninePatch == null) {
ninePatch = NinePatch(glState).apply {
ninePatch = NinePatch(gl).apply {
blendMode = r.blendMode
premultipliedAlpha = r.premultipliedAlpha
setScaling(r.scaleX, r.scaleY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class AtlasComponent(owner: Owned) : RenderableComponent<Atlas>(owner), Cle

private var texture: Texture? = null

override val renderable = Atlas(glState)
override val renderable = Atlas(gl)

private var group: CachedGroup? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ import com.acornui.math.Matrix4Ro

interface BasicRenderable {

/**
* This renderable's natural width, in points.
*/
val naturalWidth: Float

/**
* This renderable's natural height, in points.
*/
val naturalHeight: Float

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package com.acornui.component

import com.acornui.graphic.yDown
import com.acornui.gl.core.Framebuffer

import com.acornui.graphic.yDown
import com.acornui.math.*

interface CanvasTransformableRo : ModelTransformableRo {
Expand Down Expand Up @@ -111,7 +110,9 @@ fun CanvasTransformableRo.canvasToLocal(canvasCoord: Vector2): Vector2 {
}

/**
* Converts a local coordinate to a global (world) coordinate.
* Converts a local coordinate to a canvas coordinate.
* @see localToGlobal
* @see globalToCanvas
*/
fun CanvasTransformableRo.localToCanvas(localCoord: Vector3): Vector3 {
localToGlobal(localCoord)
Expand Down Expand Up @@ -180,8 +181,6 @@ fun CanvasTransformableRo.localToCanvas(localRect: Rectangle): Rectangle {
return localRect
}



/**
* Converts a bounding rectangle from canvas to local coordinates.
* Warning: this does require a matrix inversion calculation, which is a fairly expensive operation.
Expand All @@ -205,3 +204,21 @@ fun CanvasTransformableRo.canvasToLocal(minMax: MinMax): MinMax {
Vector2.free(tmp)
return minMax
}

//fun CanvasTransformableRo.localToScreen(bounds: RectangleRo, out: IntRectangle): IntRectangle {
//
//}
//
///**
// * Takes the box in canvas coordinates (unscaled, yDown, floats), and converts it to screen coordinates
// * (dpi scaled, yUp, ints)
// */
//fun CanvasTransformableRo.canvasToScreen(bounds: RectangleRo, out: IntRectangle): IntRectangle {
// val sX = scaleX
// val sY = scaleY
// val newX = (bounds.x * sX + 0.001f).toInt()
// val newY = (bounds.y * sY + 0.001f).toInt()
// val newR = ceilInt(bounds.right * sX - 0.001f)
// val newB = ceilInt(bounds.bottom * sY - 0.001f)
// return out.set(newX, if (yDown) newY else height - newB, newR - newX, newB - newY)
//}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun Scoped.createSmoothCorner(
strokeThicknessY: Float? = null,
flipX: Boolean = false,
flipY: Boolean = false,
spriteOut: Sprite,
spriteOut: Sprite = Sprite(inject(CachedGl20)),
useCache: Boolean = true
): Sprite {
val window = inject(Window)
Expand All @@ -74,31 +74,22 @@ fun Scoped.createSmoothCorner(
val framebuffer = if (useCache && smoothCornerMap.containsKey(cacheKey)) {
smoothCornerMap[cacheKey]!!
} else {
val gl = inject(Gl20)
val glState = inject(GlState)
val gl = inject(CachedGl20)
val batch = gl.batch
if (curvedShader == null)
curvedShader = disposeOnShutdown(CurvedRectShaderProgram(gl))
val framebuffer = framebuffer(ceil(cRX).toInt(), ceil(cRY).toInt())
val previousShader = glState.shader
val curvedShader = curvedShader!!
val uniforms = curvedShader.uniforms
glState.shader = curvedShader
framebuffer.begin()
gl.clearAndReset()
glState.blendMode(BlendMode.NONE, premultipliedAlpha = false)
glState.useViewport(0, 0, framebuffer.widthPixels, framebuffer.heightPixels) {
gl.useProgram(curvedShader.program) {
val uniforms = gl.uniforms
framebuffer.begin()
gl.clearAndReset()
uniforms.put("u_cornerRadius", cRX, cRY)

uniforms.getUniformLocation("u_strokeThickness")?.let {
uniforms.put(it, sX, sY)
}
glState.blendMode(BlendMode.NONE, premultipliedAlpha = false)
val batch = glState.batch
batch.begin()
uniforms.putOptional("u_strokeThickness", sX, sY)
batch.begin(blendMode = BlendMode.NONE, premultipliedAlpha = false)
batch.putIdtQuad()
framebuffer.end()
}
framebuffer.end()
glState.shader = previousShader
if (useCache)
smoothCornerMap[cacheKey] = disposeOnShutdown(framebuffer)
framebuffer
Expand Down Expand Up @@ -132,7 +123,7 @@ fun Scoped.createSmoothCorner(
return spriteOut
}

private class CurvedRectShaderProgram(gl: Gl20) : ShaderProgramBase(
private class CurvedRectShaderProgram(gl: CachedGl20) : ShaderProgramBase(
gl, vertexShaderSrc = """

$DEFAULT_SHADER_HEADER
Expand Down Expand Up @@ -168,5 +159,5 @@ void main() {
if (a < 0.001) discard;
gl_FragColor = vec4(1.0, 1.0, 1.0, a);
}""",
vertexAttributes = mapOf(VertexAttributeUsage.POSITION to CommonShaderAttributes.A_POSITION)
vertexAttributes = mapOf(VertexAttributeLocation.POSITION to CommonShaderAttributes.A_POSITION)
)
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ImageButton(
}

override fun draw() {
glState.uniforms.useColorTransformation(colorTransformation) {
gl.uniforms.useColorTransformation(colorTransformation) {
super.draw()
}
}
Expand Down
19 changes: 7 additions & 12 deletions acornui-core/src/commonMain/kotlin/com/acornui/component/Mask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
package com.acornui.component

import com.acornui.di.inject
import com.acornui.gl.core.*
import com.acornui.graphic.Window
import com.acornui.gl.core.Gl20
import com.acornui.gl.core.GlState
import com.acornui.gl.core.ShaderBatch
import com.acornui.gl.core.useScissor
import com.acornui.math.IntRectangle
import com.acornui.math.Vector3
import kotlin.math.abs
Expand Down Expand Up @@ -81,27 +78,25 @@ fun UiComponentRo.scissorLocal(inner: () -> Unit) {
* Note that this will not work properly for rotated components.
*/
fun UiComponentRo.scissorLocal(x: Float, y: Float, width: Float, height: Float, inner: () -> Unit) {
val tmp = Vector3.obtain()
val tmp = Vector3()
localToCanvas(tmp.set(x, y, 0f))
val sX1 = tmp.x
val sY1 = tmp.y
localToCanvas(tmp.set(width, height, 0f))
val sX2 = tmp.x
val sY2 = tmp.y
Vector3.free(tmp)

val gl = inject(CachedGl20)
val window = inject(Window)
val glState = inject(GlState)
val intR = IntRectangle.obtain()
intR.set(glState.viewport)
val viewport = gl.getParameteriv(Gl20.VIEWPORT, IntArray(4))

val sX = window.scaleX
val sY = window.scaleY
glState.useScissor(
gl.useScissor(
(minOf(sX1, sX2) * sX).roundToInt(),
(intR.height - maxOf(sY1, sY2) * sY).roundToInt(),
(viewport[3] - maxOf(sY1, sY2) * sY).roundToInt(),
(abs(sX2 - sX1) * sX).roundToInt(),
(abs(sY2 - sY1) * sY).roundToInt(),
inner
)
IntRectangle.free(intR)
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fun ModelTransformableRo.globalToLocal(minMax: MinMax): MinMax {
* element's plane.
* @return Returns true if the provided Ray intersects with this plane, or false if the Ray is parallel.
*/
fun ModelTransformableRo.rayToPlane(ray: RayRo, out: Vector2): Boolean {
fun rayToPlane(ray: RayRo, out: Vector2): Boolean {
if (ray.direction.z == 0f) return false
val m = -ray.origin.z * ray.directionInv.z
out.x = ray.origin.x + m * ray.direction.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.acornui.component

import com.acornui.gl.core.GlState
import com.acornui.gl.core.CachedGl20
import com.acornui.gl.core.putIndices
import com.acornui.gl.core.putVertex
import com.acornui.graphic.*
Expand All @@ -28,7 +28,7 @@ import com.acornui.recycle.Clearable
import kotlin.math.abs
import kotlin.properties.Delegates

class NinePatch(val glState: GlState) : BasicRenderable, Clearable {
class NinePatch(val gl: CachedGl20) : BasicRenderable, Clearable {

var isRotated: Boolean = false
private set
Expand Down Expand Up @@ -276,11 +276,8 @@ class NinePatch(val glState: GlState) : BasicRenderable, Clearable {
val tint = tint
val normal = normal

glState.setTexture(texture)
glState.blendMode(blendMode, premultipliedAlpha)

val batch = glState.batch
batch.begin()
val batch = gl.batch
batch.begin(texture = texture, blendMode = blendMode, premultipliedAlpha = premultipliedAlpha)

if (isRotated) {
val splitLeftV = splitLeft / texture.heightPixels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlin.contracts.contract
*/
class NinePatchComponent(owner: Owned) : RenderableComponent<BasicRenderable>(owner) {

override val renderable: NinePatch = NinePatch(glState)
override val renderable: NinePatch = NinePatch(gl)

private var cached: CachedGroup? = null

Expand Down
Loading