blob: 04c99fd968e2f521127f7e5547fa384ff385c9a1 [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"
21#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkBlurMask.h"
23#include "src/effects/SkEmbossMaskFilter.h"
reedcc0e3112014-09-10 10:20:24 -070024
reedcc0e3112014-09-10 10:20:24 -070025static SkBitmap make_bm() {
26 SkBitmap bm;
27 bm.allocN32Pixels(100, 100);
28
29 SkCanvas canvas(bm);
30 canvas.clear(0);
31 SkPaint paint;
32 paint.setAntiAlias(true);
33 canvas.drawCircle(50, 50, 50, paint);
34 return bm;
35}
36
37class EmbossGM : public skiagm::GM {
38public:
39 EmbossGM() {
40 }
41
42protected:
mtklein36352bf2015-03-25 18:17:31 -070043 SkString onShortName() override {
reedcc0e3112014-09-10 10:20:24 -070044 return SkString("emboss");
45 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 SkISize onISize() override {
reedcc0e3112014-09-10 10:20:24 -070048 return SkISize::Make(600, 120);
49 }
50
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
reedcc0e3112014-09-10 10:20:24 -070052 SkPaint paint;
53 SkBitmap bm = make_bm();
54 canvas->drawBitmap(bm, 10, 10, &paint);
Ben Wagner16622572017-11-10 16:24:50 -050055 canvas->translate(bm.width() + SkIntToScalar(10), 0);
Ben Wagner339b84e2017-11-10 16:24:50 -050056
57 paint.setMaskFilter(SkEmbossMaskFilter::Make(
58 SkBlurMask::ConvertRadiusToSigma(3),
59 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
Brian Osman3e31e992017-11-14 16:16:39 +000060 canvas->drawBitmap(bm, 10, 10, &paint);
Ben Wagner339b84e2017-11-10 16:24:50 -050061 canvas->translate(bm.width() + SkIntToScalar(10), 0);
reedcc0e3112014-09-10 10:20:24 -070062
63 // this combination of emboss+colorfilter used to crash -- so we exercise it to
64 // confirm that we have a fix.
Mike Reedb286bc22019-04-08 16:23:20 -040065 paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop));
Brian Osman3e31e992017-11-14 16:16:39 +000066 canvas->drawBitmap(bm, 10, 10, &paint);
Ben Wagner339b84e2017-11-10 16:24:50 -050067 canvas->translate(bm.width() + SkIntToScalar(10), 0);
68
69 paint.setAntiAlias(true);
70 paint.setStyle(SkPaint::kStroke_Style);
71 paint.setStrokeWidth(SkIntToScalar(10));
72 paint.setMaskFilter(SkEmbossMaskFilter::Make(
73 SkBlurMask::ConvertRadiusToSigma(4),
74 { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 }));
75 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040076 paint.setShader(SkShaders::Color(SK_ColorBLUE));
Ben Wagner339b84e2017-11-10 16:24:50 -050077 paint.setDither(true);
78 canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
79 SkIntToScalar(30), paint);
80 canvas->translate(SkIntToScalar(100), 0);
81
82 paint.setStyle(SkPaint::kFill_Style);
Hal Canary6ac0df82019-01-07 16:01:22 -050083 canvas->drawString("Hello", 0, 50, SkFont(nullptr, 50), paint);
Mike Klein278615a2019-10-29 14:15:19 -050084
85 paint.setShader(nullptr);
86 paint.setColor(SK_ColorGREEN);
87 canvas->drawString("World", 0, 100, SkFont(nullptr, 50), paint);
reedcc0e3112014-09-10 10:20:24 -070088 }
89
90private:
John Stiles7571f9e2020-09-02 22:42:33 -040091 using INHERITED = skiagm::GM;
reedcc0e3112014-09-10 10:20:24 -070092};
93
halcanary385fe4d2015-08-26 13:07:48 -070094DEF_GM(return new EmbossGM;)