|
| 1 | +/* |
| 2 | + * Copyright 2020 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// From |
| 18 | +// org.jetbrains.compose.ui:ui-js:klib:1.3.1 |
| 19 | +// DrawCache.kt |
| 20 | +// package androidx.compose.ui.graphics.vector |
| 21 | +package dev.icerock.moko.resources.compose.internal |
| 22 | + |
| 23 | +import androidx.compose.ui.graphics.BlendMode |
| 24 | +import androidx.compose.ui.graphics.Canvas |
| 25 | +import androidx.compose.ui.graphics.Color |
| 26 | +import androidx.compose.ui.graphics.ColorFilter |
| 27 | +import androidx.compose.ui.graphics.ImageBitmap |
| 28 | +import androidx.compose.ui.graphics.drawscope.CanvasDrawScope |
| 29 | +import androidx.compose.ui.graphics.drawscope.DrawScope |
| 30 | +import androidx.compose.ui.unit.Density |
| 31 | +import androidx.compose.ui.unit.IntSize |
| 32 | +import androidx.compose.ui.unit.LayoutDirection |
| 33 | +import androidx.compose.ui.unit.toSize |
| 34 | + |
| 35 | +/** |
| 36 | + * Creates a drawing environment that directs its drawing commands to an [ImageBitmap] |
| 37 | + * which can be drawn directly in another [DrawScope] instance. This is useful to cache |
| 38 | + * complicated drawing commands across frames especially if the content has not changed. |
| 39 | + * Additionally some drawing operations such as rendering paths are done purely in |
| 40 | + * software so it is beneficial to cache the result and render the contents |
| 41 | + * directly through a texture as done by [DrawScope.drawImage] |
| 42 | + */ |
| 43 | +internal class DrawCache { |
| 44 | + |
| 45 | + @PublishedApi |
| 46 | + internal var mCachedImage: ImageBitmap? = null |
| 47 | + private var cachedCanvas: Canvas? = null |
| 48 | + private var scopeDensity: Density? = null |
| 49 | + private var layoutDirection: LayoutDirection = LayoutDirection.Ltr |
| 50 | + private var size: IntSize = IntSize.Zero |
| 51 | + |
| 52 | + private val cacheScope = CanvasDrawScope() |
| 53 | + |
| 54 | + /** |
| 55 | + * Draw the contents of the lambda with receiver scope into an [ImageBitmap] with the provided |
| 56 | + * size. If the same size is provided across calls, the same [ImageBitmap] instance is |
| 57 | + * re-used and the contents are cleared out before drawing content in it again |
| 58 | + */ |
| 59 | + fun drawCachedImage( |
| 60 | + size: IntSize, |
| 61 | + density: Density, |
| 62 | + layoutDirection: LayoutDirection, |
| 63 | + block: DrawScope.() -> Unit |
| 64 | + ) { |
| 65 | + this.scopeDensity = density |
| 66 | + this.layoutDirection = layoutDirection |
| 67 | + var targetImage = mCachedImage |
| 68 | + var targetCanvas = cachedCanvas |
| 69 | + @Suppress("ComplexCondition") |
| 70 | + if (targetImage == null || |
| 71 | + targetCanvas == null || |
| 72 | + size.width > targetImage.width || |
| 73 | + size.height > targetImage.height |
| 74 | + ) { |
| 75 | + targetImage = ImageBitmap(size.width, size.height) |
| 76 | + targetCanvas = Canvas(targetImage) |
| 77 | + |
| 78 | + mCachedImage = targetImage |
| 79 | + cachedCanvas = targetCanvas |
| 80 | + } |
| 81 | + this.size = size |
| 82 | + cacheScope.draw(density, layoutDirection, targetCanvas, size.toSize()) { |
| 83 | + clear() |
| 84 | + block() |
| 85 | + } |
| 86 | + targetImage.prepareToDraw() |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Draw the cached content into the provided [DrawScope] instance |
| 91 | + */ |
| 92 | + fun drawInto( |
| 93 | + target: DrawScope, |
| 94 | + alpha: Float = 1.0f, |
| 95 | + colorFilter: ColorFilter? = null |
| 96 | + ) { |
| 97 | + val targetImage = mCachedImage |
| 98 | + check(targetImage != null) { |
| 99 | + "drawCachedImage must be invoked first before attempting to draw the result " + |
| 100 | + "into another destination" |
| 101 | + } |
| 102 | + target.drawImage(targetImage, srcSize = size, alpha = alpha, colorFilter = colorFilter) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Helper method to clear contents of the draw environment from the given bounds of the |
| 107 | + * DrawScope |
| 108 | + */ |
| 109 | + private fun DrawScope.clear() { |
| 110 | + drawRect(color = Color.Black, blendMode = BlendMode.Clear) |
| 111 | + } |
| 112 | +} |
0 commit comments