blob: af414db9fac91aae36149f3cf4d1673ab1e77923 [file] [log] [blame]
reedcc0e3112014-09-10 10:20:24 -07001/*
2 * Copyright 2014 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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorFilter.h"
14#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkMaskFilter.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
Mike Reed34a0c972021-01-25 17:49:32 -050021#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkBlurMask.h"
24#include "src/effects/SkEmbossMaskFilter.h"
reedcc0e3112014-09-10 10:20:24 -070025
Mike Reed34a0c972021-01-25 17:49:32 -050026static sk_sp<SkImage> make_bm() {
27 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
reedcc0e3112014-09-10 10:20:24 -070028
reedcc0e3112014-09-10 10:20:24 -070029 SkPaint paint;
30 paint.setAntiAlias(true);
Mike Reed34a0c972021-01-25 17:49:32 -050031 surf->getCanvas()->drawCircle(50, 50, 50, paint);
32 return surf->makeImageSnapshot();
reedcc0e3112014-09-10 10:20:24 -070033}
34
35class EmbossGM : public skiagm::GM {
36public:
37 EmbossGM() {
38 }
39
40protected:
mtklein36352bf2015-03-25 18:17:31 -070041 SkString onShortName() override {
reedcc0e3112014-09-10 10:20:24 -070042 return SkString("emboss");
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 SkISize onISize() override {
reedcc0e3112014-09-10 10:20:24 -070046 return SkISize::Make(600, 120);
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 void onDraw(SkCanvas* canvas) override {
reedcc0e3112014-09-10 10:20:24 -070050 SkPaint paint;
Mike Reed34a0c972021-01-25 17:49:32 -050051 auto img = make_bm();
Mike Reed039f1362021-01-27 21:21:08 -050052 canvas->drawImage(img, 10, 10);
Mike Reed34a0c972021-01-25 17:49:32 -050053 canvas->translate(img->width() + SkIntToScalar(10), 0);
Ben Wagner339b84e2017-11-10 16:24:50 -050054
55 paint.setMaskFilter(SkEmbossMaskFilter::Make(
56 SkBlurMask::ConvertRadiusToSigma(3),
57 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
Mike Reed039f1362021-01-27 21:21:08 -050058 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
Mike Reed34a0c972021-01-25 17:49:32 -050059 canvas->translate(img->width() + SkIntToScalar(10), 0);
reedcc0e3112014-09-10 10:20:24 -070060
61 // this combination of emboss+colorfilter used to crash -- so we exercise it to
62 // confirm that we have a fix.
Mike Reedb286bc22019-04-08 16:23:20 -040063 paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop));
Mike Reed039f1362021-01-27 21:21:08 -050064 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
Mike Reed34a0c972021-01-25 17:49:32 -050065 canvas->translate(img->width() + SkIntToScalar(10), 0);
Ben Wagner339b84e2017-11-10 16:24:50 -050066
67 paint.setAntiAlias(true);
68 paint.setStyle(SkPaint::kStroke_Style);
69 paint.setStrokeWidth(SkIntToScalar(10));
70 paint.setMaskFilter(SkEmbossMaskFilter::Make(
71 SkBlurMask::ConvertRadiusToSigma(4),
72 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
73 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040074 paint.setShader(SkShaders::Color(SK_ColorBLUE));
Ben Wagner339b84e2017-11-10 16:24:50 -050075 paint.setDither(true);
76 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
77 SkIntToScalar(30), paint);
78 canvas->translate(SkIntToScalar(100), 0);
79
80 paint.setStyle(SkPaint::kFill_Style);
Hal Canary6ac0df82019-01-07 16:01:22 -050081 canvas->drawString("Hello", 0, 50, SkFont(nullptr, 50), paint);
Mike Klein278615a2019-10-29 14:15:19 -050082
83 paint.setShader(nullptr);
84 paint.setColor(SK_ColorGREEN);
85 canvas->drawString("World", 0, 100, SkFont(nullptr, 50), paint);
reedcc0e3112014-09-10 10:20:24 -070086 }
87
88private:
John Stiles7571f9e2020-09-02 22:42:33 -040089 using INHERITED = skiagm::GM;
reedcc0e3112014-09-10 10:20:24 -070090};
91
halcanary385fe4d2015-08-26 13:07:48 -070092DEF_GM(return new EmbossGM;)