diff options
author | msarett <msarett@google.com> | 2016-09-30 12:41:42 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-30 12:41:42 -0700 |
commit | 71df2d7bc1bbc83ad4cf005f9027df4cb3b88a9b (patch) | |
tree | 9113c13e0d2285f1204e0a4bee8d89eb644d16ed /gm/lattice.cpp | |
parent | 9c8a32ff4507481aadf1a190637ee8d55c8dc217 (diff) |
Add a src rect to drawImageLattice() API
This will allow us to draw ninepatches directly from an asset
texture without having to upload them individually.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2382893002
Review-Url: https://codereview.chromium.org/2382893002
Diffstat (limited to 'gm/lattice.cpp')
-rw-r--r-- | gm/lattice.cpp | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/gm/lattice.cpp b/gm/lattice.cpp index 919fb3588e..46c5a94989 100644 --- a/gm/lattice.cpp +++ b/gm/lattice.cpp @@ -8,31 +8,39 @@ #include "gm.h" #include "SkSurface.h" -static sk_sp<SkSurface> make_surface(SkCanvas* root, int N) { - SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); +static sk_sp<SkSurface> make_surface(SkCanvas* root, int N, int padLeft, int padTop, + int padRight, int padBottom) { + SkImageInfo info = SkImageInfo::MakeN32Premul(N + padLeft + padRight, N + padTop + padBottom); auto surface = root->makeSurface(info); if (!surface) { surface = SkSurface::MakeRaster(info); } + return surface; } -static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs) { +static sk_sp<SkImage> make_image(SkCanvas* root, int* xDivs, int* yDivs, int padLeft, int padTop, + int padRight, int padBottom) { const int kCap = 28; const int kMid = 8; const int kSize = 2*kCap + 3*kMid; - auto surface(make_surface(root, kSize)); + auto surface(make_surface(root, kSize, padLeft, padTop, padRight, padBottom)); SkCanvas* canvas = surface->getCanvas(); + canvas->translate((float) padLeft, (float) padTop); SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize)); const SkScalar strokeWidth = SkIntToScalar(6); const SkScalar radius = SkIntToScalar(kCap) - strokeWidth/2; - xDivs[0] = yDivs[0] = kCap; - xDivs[1] = yDivs[1] = kCap + kMid; - xDivs[2] = yDivs[2] = kCap + 2 * kMid; - xDivs[3] = yDivs[3] = kCap + 3 * kMid; + xDivs[0] = kCap + padLeft; + yDivs[0] = kCap + padTop; + xDivs[1] = kCap + kMid + padLeft; + yDivs[1] = kCap + kMid + padTop; + xDivs[2] = kCap + 2 * kMid + padLeft; + yDivs[2] = kCap + 2 * kMid + padTop; + xDivs[3] = kCap + 3 * kMid + padLeft; + yDivs[3] = kCap + 3 * kMid + padTop; SkPaint paint; paint.setAntiAlias(true); @@ -83,17 +91,20 @@ protected: } SkISize onISize() override { - return SkISize::Make(800, 400); + return SkISize::Make(800, 800); } - void onDraw(SkCanvas* canvas) override { + void onDrawHelper(SkCanvas* canvas, int padLeft, int padTop, int padRight, int padBottom) { + canvas->save(); + int xDivs[5]; int yDivs[5]; - xDivs[0] = 0; - yDivs[0] = 0; + xDivs[0] = padLeft; + yDivs[0] = padTop; SkBitmap bitmap; - sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1); + sk_sp<SkImage> image = make_image(canvas, xDivs + 1, yDivs + 1, padLeft, padTop, + padRight, padBottom); image_to_bitmap(image.get(), &bitmap); const SkTSize<SkScalar> size[] = { @@ -115,6 +126,11 @@ protected: lattice.fYDivs = yDivs + 1; lattice.fFlags = nullptr; + SkIRect bounds = SkIRect::MakeLTRB(padLeft, padTop, + image->width() - padRight, image->height() - padBottom); + lattice.fBounds = (bounds == SkIRect::MakeWH(image->width(), image->height())) ? + nullptr : &bounds; + for (int iy = 0; iy < 2; ++iy) { for (int ix = 0; ix < 2; ++ix) { int i = ix * 2 + iy; @@ -149,6 +165,14 @@ protected: canvas->drawImageLattice(image.get(), lattice, r); } } + + canvas->restore(); + } + + void onDraw(SkCanvas* canvas) override { + this->onDrawHelper(canvas, 0, 0, 0, 0); + canvas->translate(0.0f, 400.0f); + this->onDrawHelper(canvas, 3, 7, 4, 11); } private: |