blob: f156b3e90d55fcf3d7d1facac8a17f7f50072f6b [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"
Kevin Lubickbca43ec2023-10-30 10:11:22 -040025#include "tools/fonts/FontToolUtils.h"
reedcc0e3112014-09-10 10:20:24 -070026
Mike Reed34a0c972021-01-25 17:49:32 -050027static sk_sp<SkImage> make_bm() {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040028 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
reedcc0e3112014-09-10 10:20:24 -070029
reedcc0e3112014-09-10 10:20:24 -070030 SkPaint paint;
31 paint.setAntiAlias(true);
Mike Reed34a0c972021-01-25 17:49:32 -050032 surf->getCanvas()->drawCircle(50, 50, 50, paint);
33 return surf->makeImageSnapshot();
reedcc0e3112014-09-10 10:20:24 -070034}
35
36class EmbossGM : public skiagm::GM {
37public:
38 EmbossGM() {
39 }
40
41protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000042 SkString getName() const override { return SkString("emboss"); }
reedcc0e3112014-09-10 10:20:24 -070043
Leandro Lovisolo8f023882023-08-15 21:13:52 +000044 SkISize getISize() override { return SkISize::Make(600, 120); }
reedcc0e3112014-09-10 10:20:24 -070045
mtklein36352bf2015-03-25 18:17:31 -070046 void onDraw(SkCanvas* canvas) override {
reedcc0e3112014-09-10 10:20:24 -070047 SkPaint paint;
Mike Reed34a0c972021-01-25 17:49:32 -050048 auto img = make_bm();
Mike Reed039f1362021-01-27 21:21:08 -050049 canvas->drawImage(img, 10, 10);
Mike Reed34a0c972021-01-25 17:49:32 -050050 canvas->translate(img->width() + SkIntToScalar(10), 0);
Ben Wagner339b84e2017-11-10 16:24:50 -050051
52 paint.setMaskFilter(SkEmbossMaskFilter::Make(
53 SkBlurMask::ConvertRadiusToSigma(3),
54 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
Mike Reed039f1362021-01-27 21:21:08 -050055 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
Mike Reed34a0c972021-01-25 17:49:32 -050056 canvas->translate(img->width() + SkIntToScalar(10), 0);
reedcc0e3112014-09-10 10:20:24 -070057
58 // this combination of emboss+colorfilter used to crash -- so we exercise it to
59 // confirm that we have a fix.
Mike Reedb286bc22019-04-08 16:23:20 -040060 paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop));
Mike Reed039f1362021-01-27 21:21:08 -050061 canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint);
Mike Reed34a0c972021-01-25 17:49:32 -050062 canvas->translate(img->width() + SkIntToScalar(10), 0);
Ben Wagner339b84e2017-11-10 16:24:50 -050063
64 paint.setAntiAlias(true);
65 paint.setStyle(SkPaint::kStroke_Style);
66 paint.setStrokeWidth(SkIntToScalar(10));
67 paint.setMaskFilter(SkEmbossMaskFilter::Make(
68 SkBlurMask::ConvertRadiusToSigma(4),
69 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
70 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040071 paint.setShader(SkShaders::Color(SK_ColorBLUE));
Ben Wagner339b84e2017-11-10 16:24:50 -050072 paint.setDither(true);
73 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
74 SkIntToScalar(30), paint);
75 canvas->translate(SkIntToScalar(100), 0);
76
Kevin Lubickbca43ec2023-10-30 10:11:22 -040077 SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 50);
Ben Wagner339b84e2017-11-10 16:24:50 -050078 paint.setStyle(SkPaint::kFill_Style);
Kevin Lubickbca43ec2023-10-30 10:11:22 -040079 canvas->drawString("Hello", 0, 50, font, paint);
Mike Klein278615a2019-10-29 14:15:19 -050080
81 paint.setShader(nullptr);
82 paint.setColor(SK_ColorGREEN);
Kevin Lubickbca43ec2023-10-30 10:11:22 -040083 canvas->drawString("World", 0, 100, font, paint);
reedcc0e3112014-09-10 10:20:24 -070084 }
85
86private:
John Stiles7571f9e2020-09-02 22:42:33 -040087 using INHERITED = skiagm::GM;
reedcc0e3112014-09-10 10:20:24 -070088};
89
halcanary385fe4d2015-08-26 13:07:48 -070090DEF_GM(return new EmbossGM;)