aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/ninepatchstretch.cpp
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2015-06-25 12:32:03 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-06-25 12:32:03 -0700
commit4c21dc5ddf3b482293ed34eead876d8d61a662c3 (patch)
tree390454c66e28c94a8a59b8d5ca881ef4b94e9fe2 /gm/ninepatchstretch.cpp
parentb4022965a280dd1ed64d6103dd29e2189abe6e00 (diff)
add drawImageNine
this also exposes nine-patch drawing directly to devices, and creates a shared iterator for unrolling a nine-patch into single rect->rect draws. BUG=skia: Review URL: https://codereview.chromium.org/1211583003
Diffstat (limited to 'gm/ninepatchstretch.cpp')
-rw-r--r--gm/ninepatchstretch.cpp69
1 files changed, 42 insertions, 27 deletions
diff --git a/gm/ninepatchstretch.cpp b/gm/ninepatchstretch.cpp
index 5031480aa5..267c41587b 100644
--- a/gm/ninepatchstretch.cpp
+++ b/gm/ninepatchstretch.cpp
@@ -6,15 +6,24 @@
*/
#include "gm.h"
+#include "SkSurface.h"
-static void make_bitmap(SkBitmap* bitmap, SkIRect* center) {
+static SkSurface* make_surface(SkCanvas* root, int N) {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
+ SkSurface* surface = root->newSurface(info);
+ if (!surface) {
+ surface = SkSurface::NewRaster(info);
+ }
+ return surface;
+}
+
+static SkImage* make_image(SkCanvas* root, SkIRect* center) {
const int kFixed = 28;
const int kStretchy = 8;
const int kSize = 2*kFixed + kStretchy;
- bitmap->allocN32Pixels(kSize, kSize);
- SkCanvas canvas(*bitmap);
- canvas.clear(SK_ColorTRANSPARENT);
+ SkAutoTUnref<SkSurface> surface(make_surface(root, kSize));
+ SkCanvas* canvas = surface->getCanvas();
SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
const SkScalar strokeWidth = SkIntToScalar(6);
@@ -26,39 +35,48 @@ static void make_bitmap(SkBitmap* bitmap, SkIRect* center) {
paint.setAntiAlias(true);
paint.setColor(0xFFFF0000);
- canvas.drawRoundRect(r, radius, radius, paint);
+ canvas->drawRoundRect(r, radius, radius, paint);
r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
paint.setColor(0x8800FF00);
- canvas.drawRect(r, paint);
+ canvas->drawRect(r, paint);
r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
paint.setColor(0x880000FF);
- canvas.drawRect(r, paint);
+ canvas->drawRect(r, paint);
+
+ return surface->newImageSnapshot();
}
-namespace skiagm {
+static void image_to_bitmap(const SkImage* image, SkBitmap* bm) {
+ SkImageInfo info = SkImageInfo::MakeN32Premul(image->width(), image->height());
+ bm->allocPixels(info);
+ image->readPixels(info, bm->getPixels(), bm->rowBytes(), 0, 0);
+}
-class NinePatchStretchGM : public GM {
+class NinePatchStretchGM : public skiagm::GM {
public:
- SkBitmap fBM;
+ SkAutoTUnref<SkImage> fImage;
+ SkBitmap fBitmap;
+ SkIRect fCenter;
NinePatchStretchGM() {}
protected:
- virtual SkString onShortName() {
+ SkString onShortName() override {
return SkString("ninepatch-stretch");
}
- virtual SkISize onISize() {
- return SkISize::Make(400, 400);
+ SkISize onISize() override {
+ return SkISize::Make(760, 400);
}
- virtual void onDraw(SkCanvas* canvas) {
- SkBitmap bm;
- SkIRect center;
- make_bitmap(&bm, &center);
+ void onDraw(SkCanvas* canvas) override {
+ if (NULL == fBitmap.pixelRef()) {
+ fImage.reset(make_image(canvas, &fCenter));
+ image_to_bitmap(fImage, &fBitmap);
+ }
// amount of bm that should not be stretched (unless we have to)
- const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
+ const SkScalar fixed = SkIntToScalar(fBitmap.width() - fCenter.width());
const SkTSize<SkScalar> size[] = {
{ fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
@@ -67,7 +85,7 @@ protected:
{ fixed * 4, fixed * 4 }
};
- canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
+ canvas->drawBitmap(fBitmap, 10, 10, NULL);
SkScalar x = SkIntToScalar(100);
SkScalar y = SkIntToScalar(100);
@@ -80,18 +98,15 @@ protected:
int i = ix * 2 + iy;
SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
size[i].width(), size[i].height());
- canvas->drawBitmapNine(bm, center, r, &paint);
+ canvas->drawBitmapNine(fBitmap, fCenter, r, &paint);
+ canvas->drawImageNine(fImage, fCenter, r.makeOffset(360, 0), &paint);
+
}
}
}
private:
- typedef GM INHERITED;
+ typedef skiagm::GM INHERITED;
};
+DEF_GM( return new NinePatchStretchGM; )
-//////////////////////////////////////////////////////////////////////////////
-
-static GM* MyFactory(void*) { return new NinePatchStretchGM; }
-static GMRegistry reg(MyFactory);
-
-}