Enable fast drawing for solid color nine patch rectangles
Pass a hint to Skia, about which lattice rectangles are solid
color rectangles.
Bug: 69796044
Test: Measured ninepatch performance using sample app from the bug
Change-Id: Ib07b1b64c78ab16195f9af88a989d28682084565
diff --git a/core/jni/android/graphics/NinePatch.cpp b/core/jni/android/graphics/NinePatch.cpp
index 460a0d7..564afeb 100644
--- a/core/jni/android/graphics/NinePatch.cpp
+++ b/core/jni/android/graphics/NinePatch.cpp
@@ -101,7 +101,8 @@
SkIRect src = SkIRect::MakeWH(bitmap.width(), bitmap.height());
lattice.fBounds = &src;
NinePatchUtils::SetLatticeDivs(&lattice, *chunk, bitmap.width(), bitmap.height());
- lattice.fFlags = nullptr;
+ lattice.fRectTypes = nullptr;
+ lattice.fColors = nullptr;
SkRegion* region = nullptr;
if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), lattice)) {
diff --git a/libs/hwui/NinePatchUtils.h b/libs/hwui/NinePatchUtils.h
index 8f866f5..db9509f 100644
--- a/libs/hwui/NinePatchUtils.h
+++ b/libs/hwui/NinePatchUtils.h
@@ -53,10 +53,13 @@
return xRects * yRects;
}
-static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, SkCanvas::Lattice::Flags* flags,
- int numFlags, const Res_png_9patch& chunk) {
- lattice->fFlags = flags;
- sk_bzero(flags, numFlags * sizeof(SkCanvas::Lattice::Flags));
+static inline void SetLatticeFlags(SkCanvas::Lattice* lattice,
+ SkCanvas::Lattice::RectType* flags, int numFlags, const Res_png_9patch& chunk,
+ SkColor* colors) {
+ lattice->fRectTypes = flags;
+ lattice->fColors = colors;
+ sk_bzero(flags, numFlags * sizeof(SkCanvas::Lattice::RectType));
+ sk_bzero(colors, numFlags * sizeof(SkColor));
bool needPadRow = lattice->fYCount > 0 && 0 == lattice->fYDivs[0];
bool needPadCol = lattice->fXCount > 0 && 0 == lattice->fXDivs[0];
@@ -65,6 +68,7 @@
if (needPadRow) {
// Skip flags for the degenerate first row of rects.
flags += lattice->fXCount + 1;
+ colors += lattice->fXCount + 1;
yCount--;
}
@@ -75,20 +79,28 @@
if (0 == x && needPadCol) {
// First rect of each column is degenerate, skip the flag.
flags++;
+ colors++;
continue;
}
- if (0 == chunk.getColors()[i++]) {
- *flags = SkCanvas::Lattice::kTransparent_Flags;
+ uint32_t currentColor = chunk.getColors()[i++];
+ if (Res_png_9patch::TRANSPARENT_COLOR == currentColor) {
+ *flags = SkCanvas::Lattice::kTransparent;
+ setFlags = true;
+ } else if (Res_png_9patch::NO_COLOR != currentColor) {
+ *flags = SkCanvas::Lattice::kFixedColor;
+ *colors = currentColor;
setFlags = true;
}
flags++;
+ colors++;
}
}
if (!setFlags) {
- lattice->fFlags = nullptr;
+ lattice->fRectTypes = nullptr;
+ lattice->fColors = nullptr;
}
}
diff --git a/libs/hwui/SkiaCanvas.cpp b/libs/hwui/SkiaCanvas.cpp
index eb0d161..13e1ebe 100644
--- a/libs/hwui/SkiaCanvas.cpp
+++ b/libs/hwui/SkiaCanvas.cpp
@@ -699,7 +699,8 @@
SkCanvas::Lattice lattice;
NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
- lattice.fFlags = nullptr;
+ lattice.fRectTypes = nullptr;
+ lattice.fColors = nullptr;
int numFlags = 0;
if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
// We can expect the framework to give us a color for every distinct rect.
@@ -707,9 +708,10 @@
numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
}
- SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
+ SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
+ SkAutoSTMalloc<25, SkColor> colors(numFlags);
if (numFlags > 0) {
- NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
+ NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
}
lattice.fBounds = nullptr;
diff --git a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
index 91b35c2..035cea3 100644
--- a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
+++ b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
@@ -218,7 +218,8 @@
SkCanvas::Lattice lattice;
NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
- lattice.fFlags = nullptr;
+ lattice.fRectTypes = nullptr;
+ lattice.fColors = nullptr;
int numFlags = 0;
if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
// We can expect the framework to give us a color for every distinct rect.
@@ -226,9 +227,10 @@
numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
}
- SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
+ SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
+ SkAutoSTMalloc<25, SkColor> colors(numFlags);
if (numFlags > 0) {
- NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
+ NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
}
lattice.fBounds = nullptr;