Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 10 | #include "include/core/SkBlendMode.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkImageInfo.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
| 19 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/utils/SkTextUtils.h" |
| 21 | #include "tools/ToolUtils.h" |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 22 | |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 23 | #include <initializer_list> |
| 24 | |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 25 | namespace skiagm { |
| 26 | |
| 27 | // This GM recreates the blend mode images from the Android documentation |
| 28 | class AndroidBlendModesGM : public GM { |
| 29 | public: |
| 30 | AndroidBlendModesGM() { |
| 31 | this->setBGColor(SK_ColorBLACK); |
| 32 | } |
| 33 | |
| 34 | protected: |
| 35 | SkString onShortName() override { |
| 36 | return SkString("androidblendmodes"); |
| 37 | } |
| 38 | |
| 39 | SkISize onISize() override { |
| 40 | return SkISize::Make(kNumCols * kBitmapSize, kNumRows * kBitmapSize); |
| 41 | } |
| 42 | |
| 43 | void onOnceBeforeDraw() override { |
| 44 | SkImageInfo ii = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize); |
| 45 | { |
| 46 | fCompositeSrc.allocPixels(ii); |
| 47 | SkCanvas tmp(fCompositeSrc); |
| 48 | tmp.clear(SK_ColorTRANSPARENT); |
| 49 | SkPaint p; |
| 50 | p.setAntiAlias(true); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 51 | p.setColor(ToolUtils::color_to_565(kBlue)); |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 52 | tmp.drawRect(SkRect::MakeLTRB(16, 96, 160, 240), p); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | fCompositeDst.allocPixels(ii); |
| 57 | SkCanvas tmp(fCompositeDst); |
| 58 | tmp.clear(SK_ColorTRANSPARENT); |
| 59 | SkPaint p; |
| 60 | p.setAntiAlias(true); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 61 | p.setColor(ToolUtils::color_to_565(kRed)); |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 62 | tmp.drawCircle(160, 95, 80, p); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void drawTile(SkCanvas* canvas, int xOffset, int yOffset, SkBlendMode mode) { |
| 67 | canvas->translate(xOffset, yOffset); |
| 68 | |
| 69 | canvas->clipRect(SkRect::MakeXYWH(0, 0, 256, 256)); |
| 70 | |
| 71 | canvas->saveLayer(nullptr, nullptr); |
| 72 | |
| 73 | SkPaint p; |
| 74 | canvas->drawBitmap(fCompositeDst, 0, 0, &p); |
| 75 | p.setBlendMode(mode); |
| 76 | canvas->drawBitmap(fCompositeSrc, 0, 0, &p); |
| 77 | } |
| 78 | |
| 79 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 80 | SkFont font(ToolUtils::create_portable_typeface()); |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 81 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 82 | ToolUtils::draw_checkerboard(canvas, kWhite, kGrey, 32); |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 83 | |
| 84 | int xOffset = 0, yOffset = 0; |
| 85 | |
| 86 | // Android doesn't expose all the blend modes |
| 87 | // Note that the Android documentation calls: |
| 88 | // Skia's kPlus, add |
| 89 | // Skia's kModulate, multiply |
| 90 | for (SkBlendMode mode : { SkBlendMode::kPlus /* add */, SkBlendMode::kClear, |
| 91 | SkBlendMode::kDarken, SkBlendMode::kDst, |
| 92 | SkBlendMode::kDstATop, SkBlendMode::kDstIn, |
| 93 | SkBlendMode::kDstOut, SkBlendMode::kDstOver, |
| 94 | SkBlendMode::kLighten, SkBlendMode::kModulate /* multiply */, |
| 95 | SkBlendMode::kOverlay, SkBlendMode::kScreen, |
| 96 | SkBlendMode::kSrc, SkBlendMode::kSrcATop, |
| 97 | SkBlendMode::kSrcIn, SkBlendMode::kSrcOut, |
| 98 | SkBlendMode::kSrcOver, SkBlendMode::kXor } ) { |
| 99 | |
| 100 | int saveCount = canvas->save(); |
| 101 | this->drawTile(canvas, xOffset, yOffset, mode); |
| 102 | canvas->restoreToCount(saveCount); |
| 103 | |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 104 | SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode), |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 105 | xOffset + kBitmapSize/2.0f, |
| 106 | yOffset + kBitmapSize, |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 107 | font, SkPaint(), SkTextUtils::kCenter_Align); |
Robert Phillips | aea785b | 2018-08-03 11:15:24 -0400 | [diff] [blame] | 108 | |
| 109 | xOffset += 256; |
| 110 | if (xOffset >= 1024) { |
| 111 | xOffset = 0; |
| 112 | yOffset += 256; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | static const int kBitmapSize = 256; |
| 119 | static const int kNumRows = 5; |
| 120 | static const int kNumCols = 4; |
| 121 | |
| 122 | static const SkColor kBlue = SkColorSetARGB(255, 22, 150, 243); |
| 123 | static const SkColor kRed = SkColorSetARGB(255, 233, 30, 99); |
| 124 | static const SkColor kWhite = SkColorSetARGB(255, 243, 243, 243); |
| 125 | static const SkColor kGrey = SkColorSetARGB(255, 222, 222, 222); |
| 126 | |
| 127 | SkBitmap fCompositeSrc; |
| 128 | SkBitmap fCompositeDst; |
| 129 | |
| 130 | typedef GM INHERITED; |
| 131 | }; |
| 132 | |
| 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | DEF_GM(return new AndroidBlendModesGM;) |
| 136 | } |