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 uint32_t onGetFlags() const SK_OVERRIDE {
     48         return kSkipTiled_Flag;
     49     }
     50 
     51     virtual SkString onShortName() {
     52         return SkString("ninepatch-stretch");
     53     }
     54 
     55     virtual SkISize onISize() {
     56         return SkISize::Make(400, 400);
     57     }
     58 
     59     virtual void onDraw(SkCanvas* canvas) {
     60         SkBitmap bm;
     61         SkIRect center;
     62         make_bitmap(&bm, &center);
     63 
     64         // amount of bm that should not be stretched (unless we have to)
     65         const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
     66 
     67         const SkTSize<SkScalar> size[] = {
     68             { fixed * 4 / 5, fixed * 4 / 5 },   // shrink in both axes
     69             { fixed * 4 / 5, fixed * 4 },       // shrink in X
     70             { fixed * 4,     fixed * 4 / 5 },   // shrink in Y
     71             { fixed * 4,     fixed * 4 }
     72         };
     73 
     74         canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
     75 
     76         SkScalar x = SkIntToScalar(100);
     77         SkScalar y = SkIntToScalar(100);
     78 
     79         SkPaint paint;
     80         paint.setFilterLevel(SkPaint::kLow_FilterLevel);
     81 
     82         for (int iy = 0; iy < 2; ++iy) {
     83             for (int ix = 0; ix < 2; ++ix) {
     84                 int i = ix * 2 + iy;
     85                 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
     86                                             size[i].width(), size[i].height());
     87                 canvas->drawBitmapNine(bm, center, r, &paint);
     88             }
     89         }
     90     }
     91 
     92 private:
     93     typedef GM INHERITED;
     94 };
     95 
     96 //////////////////////////////////////////////////////////////////////////////
     97 
     98 static GM* MyFactory(void*) { return new NinePatchStretchGM; }
     99 static GMRegistry reg(MyFactory);
    100 
    101 }
    102