|
| 1 | +// |
| 2 | +// Renderer.CoreGraphics+Cost.swift |
| 3 | +// SwiftDraw |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 31/8/25. |
| 6 | +// Copyright 2025 WhileLoop Pty Ltd. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive zlib license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/SwiftDraw |
| 12 | +// |
| 13 | +// This software is provided 'as-is', without any express or implied |
| 14 | +// warranty. In no event will the authors be held liable for any damages |
| 15 | +// arising from the use of this software. |
| 16 | +// |
| 17 | +// Permission is granted to anyone to use this software for any purpose, |
| 18 | +// including commercial applications, and to alter it and redistribute it |
| 19 | +// freely, subject to the following restrictions: |
| 20 | +// |
| 21 | +// 1. The origin of this software must not be misrepresented; you must not |
| 22 | +// claim that you wrote the original software. If you use this software |
| 23 | +// in a product, an acknowledgment in the product documentation would be |
| 24 | +// appreciated but is not required. |
| 25 | +// |
| 26 | +// 2. Altered source versions must be plainly marked as such, and must not be |
| 27 | +// misrepresented as being the original software. |
| 28 | +// |
| 29 | +// 3. This notice may not be removed or altered from any source distribution. |
| 30 | +// |
| 31 | + |
| 32 | +#if canImport(CoreGraphics) |
| 33 | +import CoreGraphics |
| 34 | + |
| 35 | +extension [RendererCommand<CGTypes>] { |
| 36 | + |
| 37 | + var estimatedCost: Int { |
| 38 | + let commandCost = MemoryLayout<Self>.stride * count |
| 39 | + let pathCost = Set(allPaths).reduce(0) { $0 + $1.estimatedCost } |
| 40 | + let imageCost = Set(allImages).reduce(0) { $0 + $1.estimatedCost } |
| 41 | + return commandCost + pathCost + imageCost |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +extension CGPath { |
| 46 | + |
| 47 | + var estimatedCost: Int { |
| 48 | + var total = 0 |
| 49 | + applyWithBlock { element in |
| 50 | + switch element.pointee.type { |
| 51 | + case .moveToPoint, .addLineToPoint, .closeSubpath: |
| 52 | + total += MemoryLayout<CGPathElement>.stride + MemoryLayout<CGPoint>.stride |
| 53 | + case .addQuadCurveToPoint: |
| 54 | + total += MemoryLayout<CGPathElement>.stride + 2 * MemoryLayout<CGPoint>.stride |
| 55 | + case .addCurveToPoint: |
| 56 | + total += MemoryLayout<CGPathElement>.stride + 3 * MemoryLayout<CGPoint>.stride |
| 57 | + @unknown default: |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + return MemoryLayout<CGPath>.size + total |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +extension CGImage { |
| 66 | + var estimatedCost: Int { bytesPerRow * height } |
| 67 | +} |
| 68 | + |
| 69 | +extension RendererCommand<CGTypes> { |
| 70 | + |
| 71 | + var allPaths: [CGPath] { |
| 72 | + switch self { |
| 73 | + case .setClip(path: let p, rule: _): |
| 74 | + return [p] |
| 75 | + case .setFillPattern(let p): |
| 76 | + return p.contents.allPaths |
| 77 | + case .stroke(let p): |
| 78 | + return [p] |
| 79 | + case .clipStrokeOutline(let p): |
| 80 | + return [p] |
| 81 | + case .fill(let p, rule: _): |
| 82 | + return [p] |
| 83 | + default: |
| 84 | + return [] |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + var allImages: [CGImage] { |
| 89 | + switch self { |
| 90 | + case .setFillPattern(let p): |
| 91 | + return p.contents.allImages |
| 92 | + case .draw(image: let i, in: _): |
| 93 | + return [i] |
| 94 | + default: |
| 95 | + return [] |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extension [RendererCommand<CGTypes>] { |
| 101 | + |
| 102 | + var allPaths: [CGPath] { |
| 103 | + var paths = [CGPath]() |
| 104 | + for command in self { |
| 105 | + paths.append(contentsOf: command.allPaths) |
| 106 | + } |
| 107 | + return paths |
| 108 | + } |
| 109 | + |
| 110 | + var allImages: [CGImage] { |
| 111 | + var images = [CGImage]() |
| 112 | + for command in self { |
| 113 | + images.append(contentsOf: command.allImages) |
| 114 | + } |
| 115 | + return images |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#endif |
0 commit comments