Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "gm.h"
      9 
     10 static void make_bitmap(SkBitmap* bitmap, SkIRect* center) {
     11     const int kFixed = 28;
     12     const int kStretchy = 8;
     13     const int kSize = 2*kFixed + kStretchy;
     14 
     15     bitmap->allocN32Pixels(kSize, kSize);
     16     SkCanvas canvas(*bitmap);
     17     canvas.clear(SK_ColorTRANSPARENT);
     18 
     19     SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
     20     const SkScalar strokeWidth = SkIntToScalar(6);
     21     const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
     22 
     23     center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
     24 
     25     SkPaint paint;
     26     paint.setAntiAlias(true);
     27 
     28     paint.setColor(0xFFFF0000);
     29     canvas.drawRoundRect(r, radius, radius, paint);
     30     r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
     31     paint.setColor(0x8800FF00);
     32     canvas.drawRect(r, paint);
     33     r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
     34     paint.setColor(0x880000FF);
     35     canvas.drawRect(r, paint);
     36 }
     37 
     38 namespace skiagm {
     39 
     40 class NinePatchStretchGM : public GM {
     41 public:
     42     SkBitmap fBM;
     43 
     44     NinePatchStretchGM() {}
     45 
     46 protected:
     47     virtual SkString onShortName() {
     48         return SkString("ninepatch-stretch");
     49     }
     50 
     51     virtual SkISize onISize() {
     52         return SkISize::Make(400, 400);
     53     }
     54 
     55     virtual void onDraw(SkCanvas* canvas) {
     56         SkBitmap bm;
     57         SkIRect center;
     58         make_bitmap(&bm, &center);
     59 
     60         // amount of bm that should not be stretched (unless we have to)
     61         const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
     62 
     63         const SkTSize<SkScalar> size[] = {
     64             { fixed * 4 / 5, fixed * 4 / 5 },   // shrink in both axes
     65             { fixed * 4 / 5, fixed * 4 },       // shrink in X
     66             { fixed * 4,     fixed * 4 / 5 },   // shrink in Y
     67             { fixed * 4,     fixed * 4 }
     68         };
     69 
     70         canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
     71 
     72         SkScalar x = SkIntToScalar(100);
     73         SkScalar y = SkIntToScalar(100);
     74 
     75         SkPaint paint;
     76         paint.setFilterLevel(SkPaint::kLow_FilterLevel);
     77 
     78         for (int iy = 0; iy < 2; ++iy) {
     79             for (int ix = 0; ix < 2; ++ix) {
     80                 int i = ix * 2 + iy;
     81                 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
     82                                             size[i].width(), size[i].height());
     83                 canvas->drawBitmapNine(bm, center, r, &paint);
     84             }
     85         }
     86     }
     87 
     88 private:
     89     typedef GM INHERITED;
     90 };
     91 
     92 //////////////////////////////////////////////////////////////////////////////
     93 
     94 static GM* MyFactory(void*) { return new NinePatchStretchGM; }
     95 static GMRegistry reg(MyFactory);
     96 
     97 }
     98