blob: 9cb0419e3b66ba50bb42ce77f4a7d30e1cc118ce [file] [log] [blame]
Hal Canaryf828c1d2017-07-19 17:25:38 -04001/*
2 * Copyright 2017 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 Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlurTypes.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorFilter.h"
14#include "include/core/SkImage.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040015#include "include/core/SkImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkMaskFilter.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040017#include "include/core/SkPaint.h"
18#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040020#include "tools/DecodeUtils.h"
Brian Osman5589c882022-07-11 11:08:11 -040021#include "tools/Resources.h"
Hal Canaryf828c1d2017-07-19 17:25:38 -040022
23static SkBitmap make_alpha_image(int w, int h) {
24 SkBitmap bm;
25 bm.allocPixels(SkImageInfo::MakeA8(w, h));
26 bm.eraseARGB(10, 0, 0 , 0);
27 for (int y = 0; y < bm.height(); ++y) {
28 for (int x = y; x < bm.width(); ++x) {
29 *bm.getAddr8(x, y) = 0xFF;
30 }
31 }
32 bm.setImmutable();
33 return bm;
34}
35
36static sk_sp<SkColorFilter> make_color_filter() {
Mike Reede869a1e2019-04-30 12:18:54 -040037 float colorMatrix[20] = {
Hal Canaryf828c1d2017-07-19 17:25:38 -040038 1, 0, 0, 0, 0,
39 0, 1, 0, 0, 0,
40 0, 0, 0.5, 0.5, 0,
41 0, 0, 0.5, 0.5, 0}; // mix G and A.
Mike Reede869a1e2019-04-30 12:18:54 -040042 return SkColorFilters::Matrix(colorMatrix);
Hal Canaryf828c1d2017-07-19 17:25:38 -040043}
44
45DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) {
Mike Reedac9f0c92020-12-23 10:11:33 -050046 auto image = make_alpha_image(96, 96).asImage();
Hal Canaryf828c1d2017-07-19 17:25:38 -040047 SkPaint paint;
48
49 paint.setColorFilter(make_color_filter());
Mike Reed1be1f8d2018-03-14 13:01:17 -040050 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f));
Mike Reede02d7f82021-01-21 22:25:21 -050051 canvas->drawImage(image.get(), 16, 16, SkSamplingOptions(), &paint);
Hal Canaryf828c1d2017-07-19 17:25:38 -040052
53 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040054 paint.setShader(SkShaders::Color(SK_ColorCYAN));
Mike Reede02d7f82021-01-21 22:25:21 -050055 canvas->drawImage(image.get(), 144, 16, SkSamplingOptions(), &paint);
Hal Canaryf828c1d2017-07-19 17:25:38 -040056
57 paint.setColorFilter(make_color_filter());
Mike Reede02d7f82021-01-21 22:25:21 -050058 canvas->drawImage(image.get(), 16, 144, SkSamplingOptions(), &paint);
Hal Canaryf828c1d2017-07-19 17:25:38 -040059
60 paint.setMaskFilter(nullptr);
Mike Reede02d7f82021-01-21 22:25:21 -050061 canvas->drawImage(image.get(), 144, 144, SkSamplingOptions(), &paint);
Hal Canaryf828c1d2017-07-19 17:25:38 -040062}
Brian Osmane7809c72020-07-30 16:49:54 -040063
64// Created to demonstrate skbug.com/10556 - GPU backend was failing to apply paint alpha to
65// alpha-only image shaders. The two boxes should look the same.
66DEF_SIMPLE_GM(alpha_image_alpha_tint, canvas, 152, 80) {
67 canvas->clear(SK_ColorGRAY);
68
69 SkBitmap bm;
70 bm.allocPixels(SkImageInfo::MakeA8(64, 64));
71 for (int y = 0; y < bm.height(); ++y) {
72 for (int x = 0; x < bm.width(); ++x) {
73 *bm.getAddr8(x, y) = y * 4;
74 }
75 }
76 bm.setImmutable();
Mike Reedac9f0c92020-12-23 10:11:33 -050077 auto image = bm.asImage();
Brian Osmane7809c72020-07-30 16:49:54 -040078
79 SkPaint paint;
80 paint.setColor4f({ 0, 1, 0, 0.5f });
81
82 canvas->translate(8, 8);
Mike Reed07c5f522021-01-23 12:23:23 -050083 canvas->drawImage(image.get(), 0, 0, SkSamplingOptions(), &paint);
Brian Osmane7809c72020-07-30 16:49:54 -040084
85 canvas->translate(72, 0);
Mike Reed99c94462020-12-08 13:16:56 -050086 paint.setShader(image->makeShader(SkSamplingOptions()));
Brian Osmane7809c72020-07-30 16:49:54 -040087 canvas->drawRect({ 0, 0, 64, 64 }, paint);
88}
Brian Osman5589c882022-07-11 11:08:11 -040089
90#if defined(SK_SUPPORT_LEGACY_ALPHA_BITMAP_AS_COVERAGE)
91// For a long time, the CPU backend treated A8 bitmaps as coverage, rather than alpha. This was
92// inconsistent with the GPU backend (skbug.com/9692). When this was fixed, it altered behavior
93// for some Android apps (b/231400686). This GM verifies that our Android framework workaround
94// produces the old result (mandrill with a round-rect border).
95DEF_SIMPLE_GM(alpha_bitmap_is_coverage_ANDROID, canvas, 128, 128) {
96 SkBitmap maskBitmap;
97 maskBitmap.allocPixels(SkImageInfo::MakeA8(128, 128));
98 {
99 SkCanvas maskCanvas(maskBitmap);
100 maskCanvas.clear(SK_ColorWHITE);
101
102 SkPaint maskPaint;
103 maskPaint.setAntiAlias(true);
104 maskPaint.setColor(SK_ColorWHITE);
105 maskPaint.setBlendMode(SkBlendMode::kClear);
106 maskCanvas.drawRoundRect({0, 0, 128, 128}, 16, 16, maskPaint);
107 }
108
109 SkBitmap offscreenBitmap;
110 offscreenBitmap.allocN32Pixels(128, 128);
111 {
112 SkCanvas offscreenCanvas(offscreenBitmap);
Kevin Lubick8b741882023-10-06 11:41:38 -0400113 offscreenCanvas.drawImage(ToolUtils::GetResourceAsImage("images/mandrill_128.png"), 0, 0);
Brian Osman5589c882022-07-11 11:08:11 -0400114
115 SkPaint clearPaint;
116 clearPaint.setAntiAlias(true);
117 clearPaint.setBlendMode(SkBlendMode::kClear);
118 // At tip-of-tree (or at any time on the GPU backend), this draw produces full coverage,
119 // completely erasing the mandrill. With the workaround enabled, the alpha border is treated
120 // as coverage, so we only apply kClear to those pixels, just erasing the outer border.
121 offscreenCanvas.drawImage(maskBitmap.asImage(), 0, 0, SkSamplingOptions{}, &clearPaint);
122 }
123
124 canvas->drawImage(offscreenBitmap.asImage(), 0, 0);
125}
126#endif