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 package androidx.compose.ui.graphics.drawscope 18 19 import androidx.compose.ui.geometry.Size 20 import androidx.compose.ui.graphics.Canvas 21 import androidx.compose.ui.graphics.layer.GraphicsLayer 22 import androidx.compose.ui.unit.Density 23 import androidx.compose.ui.unit.LayoutDirection 24 25 /** 26 * Default density value that is used as a stub to provide a non-null density parameter within 27 * CanvasDrawScope. Density is provided as a parameter as part of the draw call to issue drawing 28 * commands into a target canvas so this Density value is never consumed 29 */ 30 internal val DefaultDensity = Density(1.0f, 1.0f) 31 32 /** 33 * Object that provides the dependencies to support a [DrawScope] drawing environment. Namely this 34 * provides the drawing bounds represented as a size as well as the target [Canvas] to issue drawing 35 * commands into. Additionally the [DrawContext] handles updating [Canvas] state during 36 * transformations and updating the size of the drawing bounds that may occur during these 37 * transformations. 38 * 39 * This exposes necessary internal state to the implementation of the [DrawScope] in order to 40 * support inline scoped transformation calls without allowing consumers of [DrawScope] to modify 41 * state directly thus maintaining the stateless API surface 42 */ 43 interface DrawContext { 44 45 /** The current size of the drawing environment */ 46 var size: Size 47 48 /** The target canvas to issue drawing commands */ 49 var canvas: Canvas 50 get() = EmptyCanvas 51 set(_) {} 52 53 /** The controller for issuing transformations to the drawing environment */ 54 val transform: DrawTransform 55 56 /** [LayoutDirection] of the layout being drawn in. */ 57 var layoutDirection: LayoutDirection 58 get() = LayoutDirection.Ltr 59 set(_) {} 60 61 /** 62 * [Density] used to assist in conversions of density independent pixels to raw pixels to draw 63 */ 64 var density: Density 65 get() = DefaultDensity 66 set(_) {} 67 68 /** 69 * Current [GraphicsLayer] we are drawing into. Might be null if the [canvas] is not provided by 70 * a [GraphicsLayer], for example in the case of a software-accelerated drawing. 71 */ 72 var graphicsLayer: GraphicsLayer? 73 get() = null 74 set(_) {} 75 } 76