2017-07-19 21:25:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkImage.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2017-07-19 21:25:38 +00:00
|
|
|
|
|
|
|
static SkBitmap make_alpha_image(int w, int h) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::MakeA8(w, h));
|
|
|
|
bm.eraseARGB(10, 0, 0 , 0);
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = y; x < bm.width(); ++x) {
|
|
|
|
*bm.getAddr8(x, y) = 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.setImmutable();
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkColorFilter> make_color_filter() {
|
2019-04-30 16:18:54 +00:00
|
|
|
float colorMatrix[20] = {
|
2017-07-19 21:25:38 +00:00
|
|
|
1, 0, 0, 0, 0,
|
|
|
|
0, 1, 0, 0, 0,
|
|
|
|
0, 0, 0.5, 0.5, 0,
|
|
|
|
0, 0, 0.5, 0.5, 0}; // mix G and A.
|
2019-04-30 16:18:54 +00:00
|
|
|
return SkColorFilters::Matrix(colorMatrix);
|
2017-07-19 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) {
|
2020-12-23 15:11:33 +00:00
|
|
|
auto image = make_alpha_image(96, 96).asImage();
|
2017-07-19 21:25:38 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
paint.setColorFilter(make_color_filter());
|
2018-03-14 17:01:17 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f));
|
2017-07-19 21:25:38 +00:00
|
|
|
canvas->drawImage(image.get(), 16, 16, &paint);
|
|
|
|
|
|
|
|
paint.setColorFilter(nullptr);
|
2019-04-09 17:55:36 +00:00
|
|
|
paint.setShader(SkShaders::Color(SK_ColorCYAN));
|
2017-07-19 21:25:38 +00:00
|
|
|
canvas->drawImage(image.get(), 144, 16, &paint);
|
|
|
|
|
|
|
|
paint.setColorFilter(make_color_filter());
|
|
|
|
canvas->drawImage(image.get(), 16, 144, &paint);
|
|
|
|
|
|
|
|
paint.setMaskFilter(nullptr);
|
|
|
|
canvas->drawImage(image.get(), 144, 144, &paint);
|
|
|
|
}
|
2020-07-30 20:49:54 +00:00
|
|
|
|
|
|
|
// Created to demonstrate skbug.com/10556 - GPU backend was failing to apply paint alpha to
|
|
|
|
// alpha-only image shaders. The two boxes should look the same.
|
|
|
|
DEF_SIMPLE_GM(alpha_image_alpha_tint, canvas, 152, 80) {
|
|
|
|
canvas->clear(SK_ColorGRAY);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(SkImageInfo::MakeA8(64, 64));
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = 0; x < bm.width(); ++x) {
|
|
|
|
*bm.getAddr8(x, y) = y * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.setImmutable();
|
2020-12-23 15:11:33 +00:00
|
|
|
auto image = bm.asImage();
|
2020-07-30 20:49:54 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor4f({ 0, 1, 0, 0.5f });
|
|
|
|
|
|
|
|
canvas->translate(8, 8);
|
|
|
|
canvas->drawImage(image.get(), 0, 0, &paint);
|
|
|
|
|
|
|
|
canvas->translate(72, 0);
|
2020-12-08 18:16:56 +00:00
|
|
|
paint.setShader(image->makeShader(SkSamplingOptions()));
|
2020-07-30 20:49:54 +00:00
|
|
|
canvas->drawRect({ 0, 0, 64, 64 }, paint);
|
|
|
|
}
|