1 /* 2 * Copyright (C) 2024 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 package com.android.internal.widget.remotecompose.player.platform; 17 18 import static com.android.internal.widget.remotecompose.core.operations.Utils.idFromNan; 19 20 import android.graphics.Path; 21 import android.graphics.PathMeasure; 22 23 import com.android.internal.widget.remotecompose.core.operations.PathData; 24 25 public class FloatsToPath { genPath(Path retPath, float[] floatPath, float start, float stop)26 public static void genPath(Path retPath, float[] floatPath, float start, float stop) { 27 int i = 0; 28 Path path = new Path(); // todo this should be cached for performance 29 while (i < floatPath.length) { 30 switch (idFromNan(floatPath[i])) { 31 case PathData.MOVE: 32 i++; 33 path.moveTo(floatPath[i + 0], floatPath[i + 1]); 34 i += 2; 35 break; 36 case PathData.LINE: 37 i += 3; 38 path.lineTo(floatPath[i + 0], floatPath[i + 1]); 39 i += 2; 40 break; 41 case PathData.QUADRATIC: 42 i += 3; 43 path.quadTo( 44 floatPath[i + 0], floatPath[i + 1], floatPath[i + 2], floatPath[i + 3]); 45 i += 4; 46 break; 47 case PathData.CONIC: 48 i += 3; 49 50 path.conicTo( 51 floatPath[i + 0], 52 floatPath[i + 1], 53 floatPath[i + 2], 54 floatPath[i + 3], 55 floatPath[i + 4]); 56 57 i += 5; 58 break; 59 case PathData.CUBIC: 60 i += 3; 61 path.cubicTo( 62 floatPath[i + 0], floatPath[i + 1], 63 floatPath[i + 2], floatPath[i + 3], 64 floatPath[i + 4], floatPath[i + 5]); 65 i += 6; 66 break; 67 case PathData.CLOSE: 68 path.close(); 69 i++; 70 break; 71 case PathData.DONE: 72 i++; 73 break; 74 default: 75 System.err.println(" Odd command " + idFromNan(floatPath[i])); 76 } 77 } 78 79 retPath.reset(); 80 if (start > 0f || stop < 1f) { 81 if (start < stop) { 82 83 PathMeasure measure = new PathMeasure(); // todo cached 84 measure.setPath(path, false); 85 float len = measure.getLength(); 86 float scaleStart = Math.max(start, 0f) * len; 87 float scaleStop = Math.min(stop, 1f) * len; 88 measure.getSegment(scaleStart, scaleStop, retPath, true); 89 } 90 } else { 91 92 retPath.addPath(path); 93 } 94 } 95 } 96