2012-06-29 17:28:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> make_shader(SkXfermode::Mode mode) {
|
2014-05-05 18:23:54 +00:00
|
|
|
SkPoint pts[2];
|
|
|
|
SkColor colors[2];
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(SkIntToScalar(100), 0);
|
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorBLUE;
|
2016-03-09 17:50:50 +00:00
|
|
|
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(0, SkIntToScalar(100));
|
|
|
|
colors[0] = SK_ColorBLACK;
|
|
|
|
colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
|
2016-03-09 17:50:50 +00:00
|
|
|
auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2016-03-29 18:32:50 +00:00
|
|
|
return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
|
|
|
|
SkXfermode::Make(mode));
|
2014-05-05 18:23:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 16:19:08 +00:00
|
|
|
class ComposeShaderGM : public skiagm::GM {
|
2012-06-29 17:28:50 +00:00
|
|
|
public:
|
2014-05-05 16:19:08 +00:00
|
|
|
ComposeShaderGM() {
|
2014-05-05 18:23:54 +00:00
|
|
|
fShader = make_shader(SkXfermode::kDstIn_Mode);
|
2012-06-29 17:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-06-29 17:28:50 +00:00
|
|
|
return SkString("composeshader");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-05-05 16:19:08 +00:00
|
|
|
return SkISize::Make(120, 120);
|
2012-06-29 17:28:50 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-06-29 17:28:50 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
|
|
|
|
paint.setShader(fShader);
|
|
|
|
canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
|
|
|
|
}
|
|
|
|
|
2014-05-05 16:19:08 +00:00
|
|
|
protected:
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
2014-05-05 16:19:08 +00:00
|
|
|
|
|
|
|
private:
|
2012-06-29 17:28:50 +00:00
|
|
|
typedef GM INHERITED ;
|
|
|
|
};
|
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
class ComposeShaderAlphaGM : public skiagm::GM {
|
2014-05-05 16:19:08 +00:00
|
|
|
public:
|
|
|
|
ComposeShaderAlphaGM() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-05-05 16:19:08 +00:00
|
|
|
return SkString("composeshader_alpha");
|
|
|
|
}
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2015-09-03 15:32:22 +00:00
|
|
|
return SkISize::Make(750, 220);
|
2014-05-05 16:19:08 +00:00
|
|
|
}
|
2014-05-05 19:37:22 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> shaders[] = {
|
|
|
|
make_shader(SkXfermode::kDstIn_Mode),
|
|
|
|
make_shader(SkXfermode::kSrcOver_Mode),
|
|
|
|
};
|
2014-05-05 18:23:54 +00:00
|
|
|
|
2014-05-05 16:19:08 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 16:19:08 +00:00
|
|
|
const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
|
|
|
|
canvas->save();
|
|
|
|
for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
|
|
|
|
paint.setAlpha(0xFF);
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-05-05 18:23:54 +00:00
|
|
|
canvas->drawRect(r, paint);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
paint.setAlpha(alpha);
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(shaders[y]);
|
2014-05-05 18:23:54 +00:00
|
|
|
canvas->drawRect(r, paint);
|
2014-05-06 03:06:53 +00:00
|
|
|
|
2014-05-05 18:23:54 +00:00
|
|
|
canvas->translate(r.width() + 5, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + 5);
|
2014-05-05 16:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-03 15:32:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED ;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// creates a square bitmap with red background and a green circle in the center
|
|
|
|
static void draw_color_bm(SkBitmap* bm, int length) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
|
|
|
|
bm->allocN32Pixels(length, length);
|
|
|
|
bm->eraseColor(SK_ColorRED);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
|
|
|
|
paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
|
|
|
|
static void draw_alpha8_bm(SkBitmap* bm, int length) {
|
|
|
|
SkPaint circlePaint;
|
|
|
|
circlePaint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
bm->allocPixels(SkImageInfo::MakeA8(length, length));
|
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
|
|
|
|
circlePaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a linear gradient shader
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> make_linear_gradient_shader(int length) {
|
2015-09-03 15:32:22 +00:00
|
|
|
SkPoint pts[2];
|
|
|
|
SkColor colors[2];
|
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(SkIntToScalar(length), 0);
|
|
|
|
colors[0] = SK_ColorBLUE;
|
|
|
|
colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2015-09-03 15:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ComposeShaderBitmapGM : public skiagm::GM {
|
|
|
|
public:
|
2016-01-08 21:48:43 +00:00
|
|
|
ComposeShaderBitmapGM() {}
|
2016-01-04 22:17:47 +00:00
|
|
|
|
2016-01-08 21:48:43 +00:00
|
|
|
protected:
|
2016-01-04 22:17:47 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-09-03 15:32:22 +00:00
|
|
|
draw_color_bm(&fColorBitmap, squareLength);
|
|
|
|
draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
|
|
|
|
SkMatrix s;
|
|
|
|
s.reset();
|
2016-03-09 17:50:50 +00:00
|
|
|
fColorBitmapShader = SkShader::MakeBitmapShader(fColorBitmap, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &s);
|
|
|
|
fAlpha8BitmapShader = SkShader::MakeBitmapShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kRepeat_TileMode, &s);
|
2015-09-03 15:32:22 +00:00
|
|
|
fLinearGradientShader = make_linear_gradient_shader(squareLength);
|
|
|
|
}
|
2016-01-04 22:17:47 +00:00
|
|
|
|
2015-09-03 15:32:22 +00:00
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("composeshader_bitmap");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-03-29 18:32:50 +00:00
|
|
|
auto xfer(SkXfermode::Make(SkXfermode::kDstOver_Mode));
|
2015-09-03 15:32:22 +00:00
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> shaders[] = {
|
|
|
|
// gradient should appear over color bitmap
|
|
|
|
SkShader::MakeComposeShader(fLinearGradientShader, fColorBitmapShader, xfer),
|
|
|
|
// gradient should appear over alpha8 bitmap colorized by the paint color
|
|
|
|
SkShader::MakeComposeShader(fLinearGradientShader, fAlpha8BitmapShader, xfer),
|
|
|
|
};
|
2015-09-03 15:32:22 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
|
|
|
|
|
|
|
const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
|
|
|
|
SkIntToScalar(squareLength));
|
|
|
|
|
|
|
|
for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
|
|
|
|
canvas->save();
|
|
|
|
for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
|
|
|
|
paint.setAlpha(alpha);
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(shaders[y]);
|
2015-09-03 15:32:22 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
canvas->translate(r.width() + 5, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + 5);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-03 15:32:22 +00:00
|
|
|
private:
|
2016-02-15 16:27:14 +00:00
|
|
|
/** This determines the length and width of the bitmaps used in the ComposeShaders. Values
|
2015-09-03 15:32:22 +00:00
|
|
|
* above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
|
|
|
|
* work in a release build. You can change this parameter and then compile a release build
|
|
|
|
* to have this GM draw larger bitmaps for easier visual inspection.
|
|
|
|
*/
|
|
|
|
static const int squareLength = 20;
|
|
|
|
|
|
|
|
SkBitmap fColorBitmap;
|
|
|
|
SkBitmap fAlpha8Bitmap;
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> fColorBitmapShader;
|
|
|
|
sk_sp<SkShader> fAlpha8BitmapShader;
|
|
|
|
sk_sp<SkShader> fLinearGradientShader;
|
2015-09-03 15:32:22 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
2014-05-05 16:19:08 +00:00
|
|
|
};
|
|
|
|
|
2012-06-29 17:28:50 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-05 16:19:08 +00:00
|
|
|
DEF_GM( return new ComposeShaderGM; )
|
|
|
|
DEF_GM( return new ComposeShaderAlphaGM; )
|
2015-09-03 15:32:22 +00:00
|
|
|
DEF_GM( return new ComposeShaderBitmapGM; )
|