robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 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 | // |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 19 | static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) { |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 20 | const int kBlockSize = atlasSize/2; |
| 21 | |
| 22 | SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 23 | auto surface(caller->makeSurface(info)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 24 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 25 | surface = SkSurface::MakeRaster(info); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 26 | } |
| 27 | SkCanvas* canvas = surface->getCanvas(); |
| 28 | |
| 29 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 30 | paint.setBlendMode(SkBlendMode::kSrc); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 31 | |
| 32 | paint.setColor(SK_ColorWHITE); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 33 | SkRect r = SkRect::MakeXYWH(0, 0, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 34 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 35 | canvas->drawRect(r, paint); |
| 36 | |
| 37 | paint.setColor(SK_ColorRED); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 38 | r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 39 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 40 | canvas->drawRect(r, paint); |
| 41 | |
| 42 | paint.setColor(SK_ColorGREEN); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 43 | r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize), |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 44 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 45 | canvas->drawRect(r, paint); |
| 46 | |
| 47 | paint.setColor(SK_ColorTRANSPARENT); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 48 | r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize), |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 49 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 50 | canvas->drawRect(r, paint); |
| 51 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 52 | return surface->makeImageSnapshot(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 63 | protected: |
| 64 | SkString onShortName() override { |
| 65 | return SkString("draw-atlas-colors"); |
| 66 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 67 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 68 | SkISize onISize() override { |
| 69 | return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad, |
| 70 | 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad); |
| 71 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 73 | void onDraw(SkCanvas* canvas) override { |
| 74 | const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize)); |
| 75 | |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 76 | auto atlas = make_atlas(canvas, kAtlasSize); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 77 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 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, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | sk_tool_utils::set_portable_typeface(&textP, nullptr); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 138 | |
| 139 | for (int i = 0; i < numModes; ++i) { |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 140 | const char* label = SkBlendMode_Name(gModes[i]); |
| 141 | canvas->drawText(label, strlen(label), |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 142 | i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad), |
| 143 | textP); |
| 144 | } |
| 145 | |
| 146 | for (int i = 0; i < numModes; ++i) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 147 | canvas->save(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 148 | canvas->translate(SkIntToScalar(i*(target.height()+kPad)), |
| 149 | SkIntToScalar(kTextPad+kPad)); |
| 150 | // w/o a paint |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 151 | canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors, |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 152 | gModes[i], nullptr, nullptr); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 153 | canvas->translate(0.0f, numColors*(target.height()+kPad)); |
| 154 | // w a paint |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 155 | canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors, |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 156 | gModes[i], nullptr, &paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 157 | canvas->restore(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 160 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 161 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 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; |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 167 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 168 | typedef GM INHERITED; |
| 169 | }; |
| 170 | DEF_GM( return new DrawAtlasColorsGM; ) |