Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: gm/lattice.cpp

Issue 2382893002: Add a src rect to drawImageLattice() API (Closed)
Patch Set: Simplify impl Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | include/core/SkCanvas.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/lattice.cpp
diff --git a/gm/lattice.cpp b/gm/lattice.cpp
index 919fb3588e7dde16ec073b487ec0a5e66abcc8ee..46c5a9498966a9dcf62545ec312958adad4d2f33 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:
« no previous file with comments | « no previous file | include/core/SkCanvas.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698