Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e12d7315 authored by Stan Iliev's avatar Stan Iliev
Browse files

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
parent 1f397705
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -101,7 +101,8 @@ public:
        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)) {
+19 −7
Original line number Diff line number Diff line
@@ -53,10 +53,13 @@ static inline int NumDistinctRects(const SkCanvas::Lattice& lattice) {
    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 @@ static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, SkCanvas::Lattice
    if (needPadRow) {
        // Skip flags for the degenerate first row of rects.
        flags += lattice->fXCount + 1;
        colors += lattice->fXCount + 1;
        yCount--;
    }

@@ -75,20 +79,28 @@ static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, SkCanvas::Lattice
            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;
    }
}

+5 −3
Original line number Diff line number Diff line
@@ -699,7 +699,8 @@ void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, floa
    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 @@ void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, floa
        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;
+5 −3
Original line number Diff line number Diff line
@@ -218,7 +218,8 @@ void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& ch
    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 @@ void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& ch
        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;