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 #if SK_SUPPORT_GPU
     11 #include "SkGpuDevice.h"
     12 #else
     13 class GrContext;
     14 #endif
     15 
     16 static void make_bitmap(SkBitmap* bitmap, GrContext* ctx, SkIRect* center) {
     17     SkBaseDevice* dev;
     18 
     19     const int kFixed = 28;
     20     const int kStretchy = 8;
     21     const int kSize = 2*kFixed + kStretchy;
     22 
     23 #if SK_SUPPORT_GPU
     24     if (ctx) {
     25         dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, kSize, kSize);
     26         *bitmap = dev->accessBitmap(false);
     27     } else
     28 #endif
     29     {
     30         bitmap->setConfig(SkBitmap::kARGB_8888_Config, kSize, kSize);
     31         bitmap->allocPixels();
     32         dev = new SkBitmapDevice(*bitmap);
     33     }
     34 
     35     SkCanvas canvas(dev);
     36     dev->unref();
     37     canvas.clear(SK_ColorTRANSPARENT);
     38 
     39     SkRect r = SkRect::MakeWH(SkIntToScalar(kSize), SkIntToScalar(kSize));
     40     const SkScalar strokeWidth = SkIntToScalar(6);
     41     const SkScalar radius = SkIntToScalar(kFixed) - strokeWidth/2;
     42 
     43     center->setXYWH(kFixed, kFixed, kStretchy, kStretchy);
     44 
     45     SkPaint paint;
     46     paint.setAntiAlias(true);
     47 
     48     paint.setColor(0xFFFF0000);
     49     canvas.drawRoundRect(r, radius, radius, paint);
     50     r.setXYWH(SkIntToScalar(kFixed), 0, SkIntToScalar(kStretchy), SkIntToScalar(kSize));
     51     paint.setColor(0x8800FF00);
     52     canvas.drawRect(r, paint);
     53     r.setXYWH(0, SkIntToScalar(kFixed), SkIntToScalar(kSize), SkIntToScalar(kStretchy));
     54     paint.setColor(0x880000FF);
     55     canvas.drawRect(r, paint);
     56 }
     57 
     58 namespace skiagm {
     59 
     60 class NinePatchStretchGM : public GM {
     61 public:
     62     SkBitmap fBM;
     63 
     64     NinePatchStretchGM() {}
     65 
     66 protected:
     67     virtual SkString onShortName() {
     68         return SkString("ninepatch-stretch");
     69     }
     70 
     71     virtual SkISize onISize() {
     72         return make_isize(400, 400);
     73     }
     74 
     75     virtual void onDraw(SkCanvas* canvas) {
     76         SkBitmap bm;
     77         SkIRect center;
     78         make_bitmap(&bm, NULL /*SampleCode::GetGr()*/, &center);
     79 
     80         // amount of bm that should not be stretched (unless we have to)
     81         const SkScalar fixed = SkIntToScalar(bm.width() - center.width());
     82 
     83         const SkTSize<SkScalar> size[] = {
     84             { fixed * 4 / 5, fixed * 4 / 5 },   // shrink in both axes
     85             { fixed * 4 / 5, fixed * 4 },       // shrink in X
     86             { fixed * 4,     fixed * 4 / 5 },   // shrink in Y
     87             { fixed * 4,     fixed * 4 }
     88         };
     89 
     90         canvas->drawBitmap(bm, SkIntToScalar(10), SkIntToScalar(10), NULL);
     91 
     92         SkScalar x = SkIntToScalar(100);
     93         SkScalar y = SkIntToScalar(100);
     94 
     95         SkPaint paint;
     96         paint.setFilterLevel(SkPaint::kLow_FilterLevel);
     97 
     98         for (int iy = 0; iy < 2; ++iy) {
     99             for (int ix = 0; ix < 2; ++ix) {
    100                 int i = ix * 2 + iy;
    101                 SkRect r = SkRect::MakeXYWH(x + ix * fixed, y + iy * fixed,
    102                                             size[i].width(), size[i].height());
    103                 canvas->drawBitmapNine(bm, center, r, &paint);
    104             }
    105         }
    106     }
    107 
    108 private:
    109     typedef GM INHERITED;
    110 };
    111 
    112 //////////////////////////////////////////////////////////////////////////////
    113 
    114 static GM* MyFactory(void*) { return new NinePatchStretchGM; }
    115 static GMRegistry reg(MyFactory);
    116 
    117 }
    118