aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/ninepatchstretch.cpp
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-09-06 19:23:41 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-09-06 19:23:41 +0000
commitb67052596f8abebbbc6399c682b20a7dffeeecef (patch)
tree81f890a2f6f608228bfe7a084a8d0da918860c21 /gm/ninepatchstretch.cpp
parentdca4aabd89a63dd8c43202dfcbab006f7d75ce2e (diff)
add gm test for ninepatch stretching
pdf output incorrect, so not checked in. git-svn-id: http://skia.googlecode.com/svn/trunk@2224 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'gm/ninepatchstretch.cpp')
-rw-r--r--gm/ninepatchstretch.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/gm/ninepatchstretch.cpp b/gm/ninepatchstretch.cpp
new file mode 100644
index 0000000000..dfe0b7cbca
--- /dev/null
+++ b/gm/ninepatchstretch.cpp
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+
+#include "SkGpuDevice.h"
+#include "SkNinePatch.h"
+
+static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
+ SkDevice* dev;
+ SkCanvas canvas;
+
+ const int kFixed = 28;
+ const int kStretchy = 8;
+ const int kSize = 2*kFixed + kStretchy;
+
+ if (ctx) {
+ dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
+ *bitmap = dev->accessBitmap(false);
+ } else {
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
+ bitmap->allocPixels();
+ dev = new SkDevice(*bitmap);
+ }
+
+ canvas.setDevice(dev)->unref();
+ canvas.clear(0);
+
+ SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
+ const SkScalar strokeWidth = SkIntToScalar(6);
+ const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
+
+ center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
+
+ SkPaint paint;
+ paint.setAntiAlias(true);
+
+ paint.setColor(0xFFFF0000);
+ canvas.drawRoundRect(r, radius, radius, paint);
+ r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
+ paint.setColor(0x8800FF00);
+ canvas.drawRect(r, paint);
+ r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
+ paint.setColor(0x880000FF);
+ canvas.drawRect(r, paint);
+}
+
+namespace skiagm {
+
+class NinePatchStretchGM : public GM {
+public:
+ SkBitmap fBM;
+
+ NinePatchStretchGM() {}
+
+protected:
+ virtual SkString onShortName() {
+ return SkString("ninepatch-stretch");
+ }
+
+ virtual SkISize onISize() {
+ return make_isize(400, 400);
+ }
+
+ static void drawNine(SkCanvas* canvas, const SkRect& dst, const SkBitmap& bm,
+ const SkIRect& center, const SkPaint* paint) {
+ SkIRect margin;
+ margin.set(center.fLeft, center.fTop, bm.width() - center.fRight,
+ bm.height() - center.fBottom);
+ SkNinePatch::DrawNine(canvas, dst, bm, margin, paint);
+ }
+
+ virtual void onDraw(SkCanvas* canvas) {
+ canvas->drawColor(SK_ColorBLACK);
+
+ SkBitmap bm;
+ SkIRect center;
+ make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
+
+ // amount of bm that should not be stretched (unless we have to)
+ const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
+
+ const SkTSize<SkScalar> size[] = {
+ { fixed * 4 / 5, fixed * 4 / 5 }, // shrink in both axes
+ { fixed * 4 / 5, fixed * 4 }, // shrink in X
+ { fixed * 4, fixed * 4 / 5 }, // shrink in Y
+ { fixed * 4, fixed * 4 }
+ };
+
+ canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
+
+ SkScalar x = SkIntToScalar(100);
+ SkScalar y = SkIntToScalar(100);
+
+ SkPaint paint;
+ paint.setFilterBitmap(true);
+
+ for (int iy = 0; iy < 2; ++iy) {
+ for (int ix = 0; ix < 2; ++ix) {
+ int i = ix * 2 + iy;
+ SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
+ size[i].width(), size[i].height());
+ drawNine(canvas, r, bm, center, &paint);
+ }
+ }
+ }
+
+private:
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+static GM* MyFactory(void*) { return new NinePatchStretchGM; }
+static GMRegistry reg(MyFactory);
+
+}
+
+
+