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); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 23 | auto surface(sk_tool_utils::makeSurface(caller, info)); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 24 | SkCanvas* canvas = surface->getCanvas(); |
| 25 | |
| 26 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 27 | paint.setBlendMode(SkBlendMode::kSrc); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 28 | |
| 29 | paint.setColor(SK_ColorWHITE); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 30 | SkRect r = SkRect::MakeXYWH(0, 0, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 31 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 32 | canvas->drawRect(r, paint); |
| 33 | |
| 34 | paint.setColor(SK_ColorRED); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 35 | r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 36 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 37 | canvas->drawRect(r, paint); |
| 38 | |
| 39 | paint.setColor(SK_ColorGREEN); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 40 | r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize), |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 41 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 42 | canvas->drawRect(r, paint); |
| 43 | |
| 44 | paint.setColor(SK_ColorTRANSPARENT); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 45 | r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize), |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 46 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 47 | canvas->drawRect(r, paint); |
| 48 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 49 | return surface->makeImageSnapshot(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // This GM tests the drawAtlas API with colors, different xfer modes |
| 53 | // and transparency in the atlas image |
| 54 | class DrawAtlasColorsGM : public skiagm::GM { |
| 55 | public: |
| 56 | DrawAtlasColorsGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 57 | this->setBGColor(0xFFCCCCCC); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 58 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 59 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 60 | protected: |
| 61 | SkString onShortName() override { |
| 62 | return SkString("draw-atlas-colors"); |
| 63 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 65 | SkISize onISize() override { |
| 66 | return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad, |
| 67 | 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad); |
| 68 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 69 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 70 | void onDraw(SkCanvas* canvas) override { |
| 71 | const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize)); |
| 72 | |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 73 | auto atlas = make_atlas(canvas, kAtlasSize); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 74 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 75 | const SkBlendMode gModes[] = { |
| 76 | SkBlendMode::kClear, |
| 77 | SkBlendMode::kSrc, |
| 78 | SkBlendMode::kDst, |
| 79 | SkBlendMode::kSrcOver, |
| 80 | SkBlendMode::kDstOver, |
| 81 | SkBlendMode::kSrcIn, |
| 82 | SkBlendMode::kDstIn, |
| 83 | SkBlendMode::kSrcOut, |
| 84 | SkBlendMode::kDstOut, |
| 85 | SkBlendMode::kSrcATop, |
| 86 | SkBlendMode::kDstATop, |
| 87 | SkBlendMode::kXor, |
| 88 | SkBlendMode::kPlus, |
| 89 | SkBlendMode::kModulate, |
| 90 | SkBlendMode::kScreen, |
| 91 | SkBlendMode::kOverlay, |
| 92 | SkBlendMode::kDarken, |
| 93 | SkBlendMode::kLighten, |
| 94 | SkBlendMode::kColorDodge, |
| 95 | SkBlendMode::kColorBurn, |
| 96 | SkBlendMode::kHardLight, |
| 97 | SkBlendMode::kSoftLight, |
| 98 | SkBlendMode::kDifference, |
| 99 | SkBlendMode::kExclusion, |
| 100 | SkBlendMode::kMultiply, |
| 101 | SkBlendMode::kHue, |
| 102 | SkBlendMode::kSaturation, |
| 103 | SkBlendMode::kColor, |
| 104 | SkBlendMode::kLuminosity, |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | SkColor gColors[] = { |
| 108 | SK_ColorWHITE, |
| 109 | SK_ColorRED, |
| 110 | 0x88888888, // transparent grey |
| 111 | 0x88000088 // transparent blue |
| 112 | }; |
| 113 | |
| 114 | const int numModes = SK_ARRAY_COUNT(gModes); |
| 115 | SkASSERT(numModes == kNumXferModes); |
| 116 | const int numColors = SK_ARRAY_COUNT(gColors); |
| 117 | SkASSERT(numColors == kNumColors); |
| 118 | SkRSXform xforms[numColors]; |
| 119 | SkRect rects[numColors]; |
| 120 | SkColor quadColors[numColors]; |
| 121 | |
| 122 | SkPaint paint; |
| 123 | paint.setAntiAlias(true); |
| 124 | |
| 125 | for (int i = 0; i < numColors; ++i) { |
| 126 | xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad)); |
| 127 | rects[i] = target; |
| 128 | quadColors[i] = gColors[i]; |
| 129 | } |
| 130 | |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 131 | SkFont font(sk_tool_utils::create_portable_typeface(), kTextPad); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 132 | |
| 133 | for (int i = 0; i < numModes; ++i) { |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 134 | const char* label = SkBlendMode_Name(gModes[i]); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 135 | canvas->drawString(label, i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad), |
| 136 | font, paint); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | for (int i = 0; i < numModes; ++i) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | canvas->save(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 141 | canvas->translate(SkIntToScalar(i*(target.height()+kPad)), |
| 142 | SkIntToScalar(kTextPad+kPad)); |
| 143 | // w/o a paint |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 144 | canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors, |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 145 | gModes[i], nullptr, nullptr); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 146 | canvas->translate(0.0f, numColors*(target.height()+kPad)); |
| 147 | // w a paint |
brianosman | 95e8d0a | 2016-09-29 13:43:49 -0700 | [diff] [blame] | 148 | canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors, |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 149 | gModes[i], nullptr, &paint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 150 | canvas->restore(); |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 153 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 154 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 155 | static constexpr int kNumXferModes = 29; |
| 156 | static constexpr int kNumColors = 4; |
| 157 | static constexpr int kAtlasSize = 30; |
| 158 | static constexpr int kPad = 2; |
| 159 | static constexpr int kTextPad = 8; |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 160 | |
robertphillips | 29ccdf8 | 2015-07-24 10:20:45 -0700 | [diff] [blame] | 161 | typedef GM INHERITED; |
| 162 | }; |
| 163 | DEF_GM( return new DrawAtlasColorsGM; ) |