blob: a9927dd0740688b6cde294e5e6f8aa84d0736e84 [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"
Hal Canaryf828c1d2017-07-19 17:25:38 -040020
21static SkBitmap make_alpha_image(int w, int h) {
22 SkBitmap bm;
23 bm.allocPixels(SkImageInfo::MakeA8(w, h));
24 bm.eraseARGB(10, 0, 0 , 0);
25 for (int y = 0; y < bm.height(); ++y) {
26 for (int x = y; x < bm.width(); ++x) {
27 *bm.getAddr8(x, y) = 0xFF;
28 }
29 }
30 bm.setImmutable();
31 return bm;
32}
33
34static sk_sp<SkColorFilter> make_color_filter() {
Mike Reede869a1e2019-04-30 12:18:54 -040035 float colorMatrix[20] = {
Hal Canaryf828c1d2017-07-19 17:25:38 -040036 1, 0, 0, 0, 0,
37 0, 1, 0, 0, 0,
38 0, 0, 0.5, 0.5, 0,
39 0, 0, 0.5, 0.5, 0}; // mix G and A.
Mike Reede869a1e2019-04-30 12:18:54 -040040 return SkColorFilters::Matrix(colorMatrix);
Hal Canaryf828c1d2017-07-19 17:25:38 -040041}
42
43DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) {
Mike Reedac9f0c92020-12-23 10:11:33 -050044 auto image = make_alpha_image(96, 96).asImage();
Hal Canaryf828c1d2017-07-19 17:25:38 -040045 SkPaint paint;
46
47 paint.setColorFilter(make_color_filter());
Mike Reed1be1f8d2018-03-14 13:01:17 -040048 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f));
Hal Canaryf828c1d2017-07-19 17:25:38 -040049 canvas->drawImage(image.get(), 16, 16, &paint);
50
51 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040052 paint.setShader(SkShaders::Color(SK_ColorCYAN));
Hal Canaryf828c1d2017-07-19 17:25:38 -040053 canvas->drawImage(image.get(), 144, 16, &paint);
54
55 paint.setColorFilter(make_color_filter());
56 canvas->drawImage(image.get(), 16, 144, &paint);
57
58 paint.setMaskFilter(nullptr);
59 canvas->drawImage(image.get(), 144, 144, &paint);
60}
Brian Osmane7809c72020-07-30 16:49:54 -040061
62// Created to demonstrate skbug.com/10556 - GPU backend was failing to apply paint alpha to
63// alpha-only image shaders. The two boxes should look the same.
64DEF_SIMPLE_GM(alpha_image_alpha_tint, canvas, 152, 80) {
65 canvas->clear(SK_ColorGRAY);
66
67 SkBitmap bm;
68 bm.allocPixels(SkImageInfo::MakeA8(64, 64));
69 for (int y = 0; y < bm.height(); ++y) {
70 for (int x = 0; x < bm.width(); ++x) {
71 *bm.getAddr8(x, y) = y * 4;
72 }
73 }
74 bm.setImmutable();
Mike Reedac9f0c92020-12-23 10:11:33 -050075 auto image = bm.asImage();
Brian Osmane7809c72020-07-30 16:49:54 -040076
77 SkPaint paint;
78 paint.setColor4f({ 0, 1, 0, 0.5f });
79
80 canvas->translate(8, 8);
81 canvas->drawImage(image.get(), 0, 0, &paint);
82
83 canvas->translate(72, 0);
Mike Reed99c94462020-12-08 13:16:56 -050084 paint.setShader(image->makeShader(SkSamplingOptions()));
Brian Osmane7809c72020-07-30 16:49:54 -040085 canvas->drawRect({ 0, 0, 64, 64 }, paint);
86}