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