remove xfermode from public api
[platform/upstream/libSkiaSharp.git] / gm / drawatlascolor.cpp
1 /*
2  * Copyright 2015 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 "SkCanvas.h"
10 #include "SkRSXform.h"
11 #include "SkSurface.h"
12
13 // Create a square atlas of:
14 //   opaque white  |     opaque red
15 //  ------------------------------------
16 //   opaque green  |  transparent black
17 //
18 static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
19     const int kBlockSize = atlasSize/2;
20
21     SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
22     auto surface(caller->makeSurface(info));
23     if (nullptr == surface) {
24         surface = SkSurface::MakeRaster(info);
25     }
26     SkCanvas* canvas = surface->getCanvas();
27
28     SkPaint paint;
29     paint.setBlendMode(SkBlendMode::kSrc);
30
31     paint.setColor(SK_ColorWHITE);
32     SkRect r = SkRect::MakeXYWH(0, 0,
33                                 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
34     canvas->drawRect(r, paint);
35
36     paint.setColor(SK_ColorRED);
37     r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
38                          SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
39     canvas->drawRect(r, paint);
40
41     paint.setColor(SK_ColorGREEN);
42     r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
43                          SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
44     canvas->drawRect(r, paint);
45
46     paint.setColor(SK_ColorTRANSPARENT);
47     r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
48                          SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
49     canvas->drawRect(r, paint);
50
51     return surface->makeImageSnapshot();
52 }
53
54 // This GM tests the drawAtlas API with colors, different xfer modes
55 // and transparency in the atlas image
56 class DrawAtlasColorsGM : public skiagm::GM {
57 public:
58     DrawAtlasColorsGM() {
59         this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
60     }
61
62 protected:
63     SkString onShortName() override {
64         return SkString("draw-atlas-colors");
65     }
66
67     SkISize onISize() override {
68         return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
69                              2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
70     }
71
72     void onDraw(SkCanvas* canvas) override {
73         const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
74
75         auto atlas = make_atlas(canvas, kAtlasSize);
76
77         const struct {
78             SkBlendMode fMode;
79             const char* fLabel;
80         } gModes[] = {
81             { SkBlendMode::kClear,      "Clear"     },
82             { SkBlendMode::kSrc,        "Src"       },
83             { SkBlendMode::kDst,        "Dst"       },
84             { SkBlendMode::kSrcOver,    "SrcOver"   },
85             { SkBlendMode::kDstOver,    "DstOver"   },
86             { SkBlendMode::kSrcIn,      "SrcIn"     },
87             { SkBlendMode::kDstIn,      "DstIn"     },
88             { SkBlendMode::kSrcOut,     "SrcOut"    },
89             { SkBlendMode::kDstOut,     "DstOut"    },
90             { SkBlendMode::kSrcATop,    "SrcATop"   },
91             { SkBlendMode::kDstATop,    "DstATop"   },
92             { SkBlendMode::kXor,        "Xor"       },
93             { SkBlendMode::kPlus,       "Plus"      },
94             { SkBlendMode::kModulate,   "Mod"       },
95             { SkBlendMode::kScreen,     "Screen"    },
96             { SkBlendMode::kOverlay,    "Overlay"   },
97             { SkBlendMode::kDarken,     "Darken"    },
98             { SkBlendMode::kLighten,    "Lighten"   },
99             { SkBlendMode::kColorDodge, "Dodge"     },
100             { SkBlendMode::kColorBurn,  "Burn"      },
101             { SkBlendMode::kHardLight,  "Hard"      },
102             { SkBlendMode::kSoftLight,  "Soft"      },
103             { SkBlendMode::kDifference, "Diff"      },
104             { SkBlendMode::kExclusion,  "Exclusion" },
105             { SkBlendMode::kMultiply,   "Multiply"  },
106             { SkBlendMode::kHue,        "Hue"       },
107             { SkBlendMode::kSaturation, "Sat"       },
108             { SkBlendMode::kColor,      "Color"     },
109             { SkBlendMode::kLuminosity, "Luminosity"},
110         };
111
112         SkColor gColors[] = {
113             SK_ColorWHITE,
114             SK_ColorRED,
115             0x88888888,         // transparent grey
116             0x88000088          // transparent blue
117         };
118
119         const int numModes = SK_ARRAY_COUNT(gModes);
120         SkASSERT(numModes == kNumXferModes);
121         const int numColors = SK_ARRAY_COUNT(gColors);
122         SkASSERT(numColors == kNumColors);
123         SkRSXform xforms[numColors];
124         SkRect rects[numColors];
125         SkColor quadColors[numColors];
126
127         SkPaint paint;
128         paint.setAntiAlias(true);
129
130         for (int i = 0; i < numColors; ++i) {
131             xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
132             rects[i] = target;
133             quadColors[i] = gColors[i];
134         }
135
136         SkPaint textP;
137         textP.setTextSize(SkIntToScalar(kTextPad));
138         textP.setAntiAlias(true);
139         sk_tool_utils::set_portable_typeface(&textP, nullptr);
140
141         for (int i = 0; i < numModes; ++i) {
142             canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
143                              i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
144                              textP);
145         }
146
147         for (int i = 0; i < numModes; ++i) {
148             canvas->save();
149             canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
150                               SkIntToScalar(kTextPad+kPad));
151             // w/o a paint
152             canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
153                               gModes[i].fMode, nullptr, nullptr);
154             canvas->translate(0.0f, numColors*(target.height()+kPad));
155             // w a paint
156             canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
157                               gModes[i].fMode, nullptr, &paint);
158             canvas->restore();
159         }
160     }
161
162 private:
163     static constexpr int kNumXferModes = 29;
164     static constexpr int kNumColors = 4;
165     static constexpr int kAtlasSize = 30;
166     static constexpr int kPad = 2;
167     static constexpr int kTextPad = 8;
168
169     typedef GM INHERITED;
170 };
171 DEF_GM( return new DrawAtlasColorsGM; )