NinePatchUtils.h revision 021693b967a2c5556dddd183eb0247df4079e1ad
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 17#pragma once 18 19namespace android { 20namespace NinePatchUtils { 21 22static inline void SetLatticeDivs(SkCanvas::Lattice* lattice, const Res_png_9patch& chunk, 23 int width, int height) { 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 39static 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 56static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, SkCanvas::Lattice::Flags* flags, 57 int numFlags, const Res_png_9patch& chunk) { 58 lattice->fFlags = flags; 59 sk_bzero(flags, numFlags * sizeof(SkCanvas::Lattice::Flags)); 60 61 bool needPadRow = lattice->fYCount > 0 && 0 == lattice->fYDivs[0]; 62 bool needPadCol = lattice->fXCount > 0 && 0 == lattice->fXDivs[0]; 63 64 int yCount = lattice->fYCount; 65 if (needPadRow) { 66 // Skip flags for the degenerate first row of rects. 67 flags += lattice->fXCount + 1; 68 yCount--; 69 } 70 71 int i = 0; 72 bool setFlags = false; 73 for (int y = 0; y < yCount + 1; y++) { 74 for (int x = 0; x < lattice->fXCount + 1; x++) { 75 if (0 == x && needPadCol) { 76 // First rect of each column is degenerate, skip the flag. 77 flags++; 78 continue; 79 } 80 81 if (0 == chunk.getColors()[i++]) { 82 *flags = SkCanvas::Lattice::kTransparent_Flags; 83 setFlags = true; 84 } 85 86 flags++; 87 } 88 } 89 90 if (!setFlags) { 91 lattice->fFlags = nullptr; 92 } 93} 94 95}; // namespace NinePatchUtils 96}; // namespace android 97