Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 19 | namespace android { |
| 20 | namespace NinePatchUtils { |
| 21 | |
| 22 | static inline void SetLatticeDivs(SkCanvas::Lattice* lattice, const Res_png_9patch& chunk, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | int width, int height) { |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 24 | lattice->fXCount = chunk.numXDivs; |
| 25 | lattice->fYCount = chunk.numYDivs; |
| 26 | lattice->fXDivs = chunk.getXDivs(); |
| 27 | lattice->fYDivs = chunk.getYDivs(); |
| 28 | |
| 29 | // We'll often see ninepatches where the last div is equal to the width or height. |
| 30 | // This doesn't provide any additional information and is not supported by Skia. |
| 31 | if (lattice->fXCount > 0 && width == lattice->fXDivs[lattice->fXCount - 1]) { |
| 32 | lattice->fXCount--; |
| 33 | } |
| 34 | if (lattice->fYCount > 0 && height == lattice->fYDivs[lattice->fYCount - 1]) { |
| 35 | lattice->fYCount--; |
| 36 | } |
| 37 | } |
| 38 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 39 | static inline int NumDistinctRects(const SkCanvas::Lattice& lattice) { |
| 40 | int xRects; |
| 41 | if (lattice.fXCount > 0) { |
| 42 | xRects = (0 == lattice.fXDivs[0]) ? lattice.fXCount : lattice.fXCount + 1; |
| 43 | } else { |
| 44 | xRects = 1; |
| 45 | } |
| 46 | |
| 47 | int yRects; |
| 48 | if (lattice.fYCount > 0) { |
| 49 | yRects = (0 == lattice.fYDivs[0]) ? lattice.fYCount : lattice.fYCount + 1; |
| 50 | } else { |
| 51 | yRects = 1; |
| 52 | } |
| 53 | return xRects * yRects; |
| 54 | } |
| 55 | |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 56 | static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, |
| 57 | SkCanvas::Lattice::RectType* flags, int numFlags, const Res_png_9patch& chunk, |
| 58 | SkColor* colors) { |
| 59 | lattice->fRectTypes = flags; |
| 60 | lattice->fColors = colors; |
| 61 | sk_bzero(flags, numFlags * sizeof(SkCanvas::Lattice::RectType)); |
| 62 | sk_bzero(colors, numFlags * sizeof(SkColor)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 63 | |
| 64 | bool needPadRow = lattice->fYCount > 0 && 0 == lattice->fYDivs[0]; |
| 65 | bool needPadCol = lattice->fXCount > 0 && 0 == lattice->fXDivs[0]; |
| 66 | |
| 67 | int yCount = lattice->fYCount; |
| 68 | if (needPadRow) { |
| 69 | // Skip flags for the degenerate first row of rects. |
| 70 | flags += lattice->fXCount + 1; |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 71 | colors += lattice->fXCount + 1; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 72 | yCount--; |
| 73 | } |
| 74 | |
| 75 | int i = 0; |
| 76 | bool setFlags = false; |
| 77 | for (int y = 0; y < yCount + 1; y++) { |
| 78 | for (int x = 0; x < lattice->fXCount + 1; x++) { |
| 79 | if (0 == x && needPadCol) { |
| 80 | // First rect of each column is degenerate, skip the flag. |
| 81 | flags++; |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 82 | colors++; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 86 | uint32_t currentColor = chunk.getColors()[i++]; |
| 87 | if (Res_png_9patch::TRANSPARENT_COLOR == currentColor) { |
| 88 | *flags = SkCanvas::Lattice::kTransparent; |
| 89 | setFlags = true; |
| 90 | } else if (Res_png_9patch::NO_COLOR != currentColor) { |
| 91 | *flags = SkCanvas::Lattice::kFixedColor; |
| 92 | *colors = currentColor; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 93 | setFlags = true; |
| 94 | } |
| 95 | |
| 96 | flags++; |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 97 | colors++; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | if (!setFlags) { |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame^] | 102 | lattice->fRectTypes = nullptr; |
| 103 | lattice->fColors = nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | }; // namespace NinePatchUtils |
| 108 | }; // namespace android |