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.Offset
20 import androidx.compose.ui.geometry.Rect
21 import androidx.compose.ui.graphics.BlendMode
22 import androidx.compose.ui.graphics.Canvas
23 import androidx.compose.ui.graphics.ClipOp
24 import androidx.compose.ui.graphics.ImageBitmap
25 import androidx.compose.ui.graphics.Matrix
26 import androidx.compose.ui.graphics.Paint
27 import androidx.compose.ui.graphics.Path
28 import androidx.compose.ui.graphics.PointMode
29 import androidx.compose.ui.graphics.Vertices
30 import androidx.compose.ui.unit.IntOffset
31 import androidx.compose.ui.unit.IntSize
32 
33 /**
34  * Stub implementation of [Canvas] to be used to ensure the internal canvas object within
35  * [DrawScope] is never null. All methods here are no-ops to ensure no null pointer exceptions are
36  * thrown at runtime. During normal use, the canvas used within [DrawScope] is consuming a valid
37  * Canvas that draws content into a valid destination
38  */
39 internal object EmptyCanvas : Canvas {
savenull40     override fun save() {
41         throw UnsupportedOperationException()
42     }
43 
restorenull44     override fun restore() {
45         throw UnsupportedOperationException()
46     }
47 
saveLayernull48     override fun saveLayer(bounds: Rect, paint: Paint) {
49         throw UnsupportedOperationException()
50     }
51 
translatenull52     override fun translate(dx: Float, dy: Float) {
53         throw UnsupportedOperationException()
54     }
55 
scalenull56     override fun scale(sx: Float, sy: Float) {
57         throw UnsupportedOperationException()
58     }
59 
rotatenull60     override fun rotate(degrees: Float) {
61         throw UnsupportedOperationException()
62     }
63 
skewnull64     override fun skew(sx: Float, sy: Float) {
65         throw UnsupportedOperationException()
66     }
67 
concatnull68     override fun concat(matrix: Matrix) {
69         throw UnsupportedOperationException()
70     }
71 
clipRectnull72     override fun clipRect(left: Float, top: Float, right: Float, bottom: Float, clipOp: ClipOp) {
73         throw UnsupportedOperationException()
74     }
75 
clipPathnull76     override fun clipPath(path: Path, clipOp: ClipOp) {
77         throw UnsupportedOperationException()
78     }
79 
drawLinenull80     override fun drawLine(p1: Offset, p2: Offset, paint: Paint) {
81         throw UnsupportedOperationException()
82     }
83 
drawRectnull84     override fun drawRect(left: Float, top: Float, right: Float, bottom: Float, paint: Paint) {
85         throw UnsupportedOperationException()
86     }
87 
drawRoundRectnull88     override fun drawRoundRect(
89         left: Float,
90         top: Float,
91         right: Float,
92         bottom: Float,
93         radiusX: Float,
94         radiusY: Float,
95         paint: Paint
96     ) {
97         throw UnsupportedOperationException()
98     }
99 
drawOvalnull100     override fun drawOval(left: Float, top: Float, right: Float, bottom: Float, paint: Paint) {
101         throw UnsupportedOperationException()
102     }
103 
drawCirclenull104     override fun drawCircle(center: Offset, radius: Float, paint: Paint) {
105         throw UnsupportedOperationException()
106     }
107 
drawArcnull108     override fun drawArc(
109         left: Float,
110         top: Float,
111         right: Float,
112         bottom: Float,
113         startAngle: Float,
114         sweepAngle: Float,
115         useCenter: Boolean,
116         paint: Paint
117     ) {
118         throw UnsupportedOperationException()
119     }
120 
drawPathnull121     override fun drawPath(path: Path, paint: Paint) {
122         throw UnsupportedOperationException()
123     }
124 
drawImagenull125     override fun drawImage(image: ImageBitmap, topLeftOffset: Offset, paint: Paint) {
126         throw UnsupportedOperationException()
127     }
128 
drawImageRectnull129     override fun drawImageRect(
130         image: ImageBitmap,
131         srcOffset: IntOffset,
132         srcSize: IntSize,
133         dstOffset: IntOffset,
134         dstSize: IntSize,
135         paint: Paint
136     ) {
137         throw UnsupportedOperationException()
138     }
139 
drawPointsnull140     override fun drawPoints(pointMode: PointMode, points: List<Offset>, paint: Paint) {
141         throw UnsupportedOperationException()
142     }
143 
drawRawPointsnull144     override fun drawRawPoints(pointMode: PointMode, points: FloatArray, paint: Paint) {
145         throw UnsupportedOperationException()
146     }
147 
drawVerticesnull148     override fun drawVertices(vertices: Vertices, blendMode: BlendMode, paint: Paint) {
149         throw UnsupportedOperationException()
150     }
151 
enableZnull152     override fun enableZ() {
153         throw UnsupportedOperationException()
154     }
155 
disableZnull156     override fun disableZ() {
157         throw UnsupportedOperationException()
158     }
159 }
160