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 #include "SkArithmeticMode.h"
     10 #include "SkOffsetImageFilter.h"
     11 #include "SkXfermodeImageFilter.h"
     12 #include "SkBitmapSource.h"
     13 
     14 #define WIDTH 600
     15 #define HEIGHT 600
     16 #define MARGIN 12
     17 
     18 namespace skiagm {
     19 
     20 class XfermodeImageFilterGM : public GM {
     21 public:
     22     XfermodeImageFilterGM(){
     23         this->setBGColor(0xFF000000);
     24     }
     25 
     26 protected:
     27     virtual SkString onShortName() SK_OVERRIDE {
     28         return SkString("xfermodeimagefilter");
     29     }
     30 
     31     void make_bitmap() {
     32         fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
     33         fBitmap.allocPixels();
     34         SkBitmapDevice device(fBitmap);
     35         SkCanvas canvas(&device);
     36         canvas.clear(0x00000000);
     37         SkPaint paint;
     38         paint.setAntiAlias(true);
     39         paint.setColor(0xD000D000);
     40         paint.setTextSize(SkIntToScalar(96));
     41         const char* str = "e";
     42         canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint);
     43     }
     44 
     45     void make_checkerboard() {
     46         fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
     47         fCheckerboard.allocPixels();
     48         SkBitmapDevice device(fCheckerboard);
     49         SkCanvas canvas(&device);
     50         canvas.clear(0x00000000);
     51         SkPaint darkPaint;
     52         darkPaint.setColor(0xFF404040);
     53         SkPaint lightPaint;
     54         lightPaint.setColor(0xFFA0A0A0);
     55         for (int y = 0; y < 80; y += 16) {
     56           for (int x = 0; x < 80; x += 16) {
     57             canvas.save();
     58             canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
     59             canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
     60             canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
     61             canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
     62             canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
     63             canvas.restore();
     64           }
     65         }
     66     }
     67 
     68     virtual SkISize onISize() SK_OVERRIDE {
     69         return make_isize(WIDTH, HEIGHT);
     70     }
     71 
     72     static void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint,
     73                            int x, int y) {
     74         canvas->save();
     75         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     76         canvas->clipRect(SkRect::MakeWH(
     77             SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())));
     78         canvas->drawBitmap(bitmap, 0, 0, &paint);
     79         canvas->restore();
     80     }
     81 
     82     static void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint,
     83                           int x, int y) {
     84         canvas->save();
     85         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     86         canvas->clipRect(rect);
     87         canvas->drawPaint(paint);
     88         canvas->restore();
     89     }
     90 
     91     virtual void onOnceBeforeDraw() SK_OVERRIDE {
     92         make_bitmap();
     93         make_checkerboard();
     94     }
     95 
     96     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     97         canvas->clear(0x00000000);
     98         SkPaint paint;
     99 
    100         const struct {
    101             SkXfermode::Mode  fMode;
    102             const char*         fLabel;
    103         } gModes[] = {
    104             { SkXfermode::kClear_Mode,    "Clear"     },
    105             { SkXfermode::kSrc_Mode,      "Src"       },
    106             { SkXfermode::kDst_Mode,      "Dst"       },
    107             { SkXfermode::kSrcOver_Mode,  "SrcOver"   },
    108             { SkXfermode::kDstOver_Mode,  "DstOver"   },
    109             { SkXfermode::kSrcIn_Mode,    "SrcIn"     },
    110             { SkXfermode::kDstIn_Mode,    "DstIn"     },
    111             { SkXfermode::kSrcOut_Mode,   "SrcOut"    },
    112             { SkXfermode::kDstOut_Mode,   "DstOut"    },
    113             { SkXfermode::kSrcATop_Mode,  "SrcATop"   },
    114             { SkXfermode::kDstATop_Mode,  "DstATop"   },
    115             { SkXfermode::kXor_Mode,      "Xor"       },
    116 
    117             { SkXfermode::kPlus_Mode,         "Plus"          },
    118             { SkXfermode::kModulate_Mode,     "Modulate"      },
    119             { SkXfermode::kScreen_Mode,       "Screen"        },
    120             { SkXfermode::kOverlay_Mode,      "Overlay"       },
    121             { SkXfermode::kDarken_Mode,       "Darken"        },
    122             { SkXfermode::kLighten_Mode,      "Lighten"       },
    123             { SkXfermode::kColorDodge_Mode,   "ColorDodge"    },
    124             { SkXfermode::kColorBurn_Mode,    "ColorBurn"     },
    125             { SkXfermode::kHardLight_Mode,    "HardLight"     },
    126             { SkXfermode::kSoftLight_Mode,    "SoftLight"     },
    127             { SkXfermode::kDifference_Mode,   "Difference"    },
    128             { SkXfermode::kExclusion_Mode,    "Exclusion"     },
    129             { SkXfermode::kMultiply_Mode,     "Multiply"      },
    130             { SkXfermode::kHue_Mode,          "Hue"           },
    131             { SkXfermode::kSaturation_Mode,   "Saturation"    },
    132             { SkXfermode::kColor_Mode,        "Color"         },
    133             { SkXfermode::kLuminosity_Mode,   "Luminosity"    },
    134         };
    135 
    136         int x = 0, y = 0;
    137         SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
    138         for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
    139             SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode));
    140             SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(
    141                 SkXfermodeImageFilter, (mode, background)));
    142             paint.setImageFilter(filter);
    143             drawClippedBitmap(canvas, fBitmap, paint, x, y);
    144             x += fBitmap.width() + MARGIN;
    145             if (x + fBitmap.width() > WIDTH) {
    146                 x = 0;
    147                 y += fBitmap.height() + MARGIN;
    148             }
    149         }
    150         // Test arithmetic mode as image filter
    151         SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0));
    152         SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mode, background)));
    153         paint.setImageFilter(filter);
    154         drawClippedBitmap(canvas, fBitmap, paint, x, y);
    155         x += fBitmap.width() + MARGIN;
    156         if (x + fBitmap.width() > WIDTH) {
    157             x = 0;
    158             y += fBitmap.height() + MARGIN;
    159         }
    160         // Test NULL mode
    161         filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background)));
    162         paint.setImageFilter(filter);
    163         drawClippedBitmap(canvas, fBitmap, paint, x, y);
    164         x += fBitmap.width() + MARGIN;
    165         if (x + fBitmap.width() > WIDTH) {
    166             x = 0;
    167             y += fBitmap.height() + MARGIN;
    168         }
    169         SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4),
    170                                          SkIntToScalar(fBitmap.height() + 4));
    171         // Test offsets on SrcMode (uses fixed-function blend)
    172         SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitmap)));
    173         SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFilter,
    174             (SkIntToScalar(4), SkIntToScalar(-4), foreground)));
    175         SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFilter,
    176             (SkIntToScalar(4), SkIntToScalar(4), background)));
    177         mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
    178         filter.reset(SkNEW_ARGS(SkXfermodeImageFilter,
    179             (mode, offsetBackground, offsetForeground)));
    180         paint.setImageFilter(filter);
    181         drawClippedPaint(canvas, clipRect, paint, x, y);
    182         x += fBitmap.width() + MARGIN;
    183         if (x + fBitmap.width() > WIDTH) {
    184             x = 0;
    185             y += fBitmap.height() + MARGIN;
    186         }
    187         // Test offsets on Darken (uses shader blend)
    188         mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode));
    189         filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground, offsetForeground)));
    190         paint.setImageFilter(filter);
    191         drawClippedPaint(canvas, clipRect, paint, x, y);
    192         x += fBitmap.width() + MARGIN;
    193         if (x + fBitmap.width() > WIDTH) {
    194             x = 0;
    195             y += fBitmap.height() + MARGIN;
    196         }
    197         // Test cropping
    198         static const size_t nbSamples = 3;
    199         SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode,
    200                                                     SkXfermode::kSrcOver_Mode,
    201                                                     SkXfermode::kPlus_Mode};
    202         int offsets[nbSamples][4] = {{ 10,  10, -16, -16},
    203                                      { 10,  10,  10,  10},
    204                                      {-10, -10,  -6,  -6}};
    205         for (size_t i = 0; i < nbSamples; ++i) {
    206             SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0],
    207                                                  offsets[i][1],
    208                                                  fBitmap.width()  + offsets[i][2],
    209                                                  fBitmap.height() + offsets[i][3]);
    210             SkImageFilter::CropRect rect(SkRect::Make(cropRect));
    211             mode.reset(SkXfermode::Create(sampledModes[i]));
    212             filter.reset(SkNEW_ARGS(SkXfermodeImageFilter,
    213                                     (mode, offsetBackground, offsetForeground, &rect)));
    214             paint.setImageFilter(filter);
    215             drawClippedPaint(canvas, clipRect, paint, x, y);
    216             x += fBitmap.width() + MARGIN;
    217             if (x + fBitmap.width() > WIDTH) {
    218                 x = 0;
    219                 y += fBitmap.height() + MARGIN;
    220             }
    221         }
    222     }
    223 private:
    224     SkBitmap fBitmap, fCheckerboard;
    225     typedef GM INHERITED;
    226 };
    227 
    228 //////////////////////////////////////////////////////////////////////////////
    229 
    230 DEF_GM( return new XfermodeImageFilterGM; );
    231 
    232 }
    233