Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 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 #include "SkBitmapSource.h"
     11 
     12 // This GM exercises the SkBitmapSource ImageFilter class.
     13 
     14 class BitmapSourceGM : public skiagm::GM {
     15 public:
     16     BitmapSourceGM() {
     17     }
     18 
     19 protected:
     20     virtual SkString onShortName() SK_OVERRIDE {
     21         return SkString("bitmapsource");
     22     }
     23 
     24     void makeBitmap() {
     25         fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
     26         fBitmap.allocPixels();
     27         SkBitmapDevice device(fBitmap);
     28         SkCanvas canvas(&device);
     29         canvas.clear(0x00000000);
     30         SkPaint paint;
     31         paint.setAntiAlias(true);
     32         paint.setColor(0xFFFFFFFF);
     33         paint.setTextSize(SkIntToScalar(96));
     34         const char* str = "e";
     35         canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
     36     }
     37 
     38     virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(500, 150); }
     39 
     40     virtual void onOnceBeforeDraw() SK_OVERRIDE {
     41         this->makeBitmap();
     42     }
     43 
     44     static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) {
     45         SkPaint paint;
     46         paint.setImageFilter(filter);
     47         canvas->save();
     48         canvas->clipRect(clipRect);
     49         canvas->drawPaint(paint);
     50         canvas->restore();
     51     }
     52 
     53     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     54         canvas->clear(0x00000000);
     55         {
     56             SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
     57             SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
     58             SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
     59             SkRect bounds;
     60             fBitmap.getBounds(&bounds);
     61             SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap));
     62             SkAutoTUnref<SkImageFilter> bitmapSourceSrcRect(new SkBitmapSource(fBitmap, srcRect, srcRect));
     63             SkAutoTUnref<SkImageFilter> bitmapSourceSrcRectDstRect(new SkBitmapSource(fBitmap, srcRect, dstRect));
     64             SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(new SkBitmapSource(fBitmap, bounds, dstRect));
     65 
     66             // Draw an unscaled bitmap.
     67             fillRectFiltered(canvas, clipRect, bitmapSource);
     68             canvas->translate(SkIntToScalar(100), 0);
     69 
     70             // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
     71             fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect);
     72             canvas->translate(SkIntToScalar(100), 0);
     73 
     74             // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
     75             fillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect);
     76             canvas->translate(SkIntToScalar(100), 0);
     77 
     78             // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
     79             fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly);
     80             canvas->translate(SkIntToScalar(100), 0);
     81         }
     82     }
     83 
     84 private:
     85     SkBitmap fBitmap;
     86     typedef GM INHERITED;
     87 };
     88 
     89 ///////////////////////////////////////////////////////////////////////////////
     90 
     91 DEF_GM( return new BitmapSourceGM; )
     92