2012-06-29 21:28:50 +04: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 "SkComposeShader.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
static SkShader* make_shader(SkXfermode::Mode mode) {
|
|
|
|
SkPoint pts[2];
|
|
|
|
SkColor colors[2];
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(SkIntToScalar(100), 0);
|
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorBLUE;
|
|
|
|
SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
|
|
|
|
SkShader::kClamp_TileMode));
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(0, SkIntToScalar(100));
|
|
|
|
colors[0] = SK_ColorBLACK;
|
|
|
|
colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
|
|
|
|
SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
|
|
|
|
SkShader::kClamp_TileMode));
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer));
|
|
|
|
}
|
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
class ComposeShaderGM : public skiagm::GM {
|
2012-06-29 21:28:50 +04:00
|
|
|
public:
|
2014-05-05 20:19:08 +04:00
|
|
|
ComposeShaderGM() {
|
2014-05-05 22:23:54 +04:00
|
|
|
fShader = make_shader(SkXfermode::kDstIn_Mode);
|
2012-06-29 21:28:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
virtual ~ComposeShaderGM() {
|
2012-06-29 21:28:50 +04:00
|
|
|
SkSafeUnref(fShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 17:20:45 +04:00
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2012-06-29 21:28:50 +04:00
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("composeshader");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
2014-05-05 20:19:08 +04:00
|
|
|
return SkISize::Make(120, 120);
|
2012-06-29 21:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
|
|
|
|
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 20:19:08 +04:00
|
|
|
protected:
|
2012-06-29 21:28:50 +04:00
|
|
|
SkShader* fShader;
|
2014-05-05 20:19:08 +04:00
|
|
|
|
|
|
|
private:
|
2012-06-29 21:28:50 +04:00
|
|
|
typedef GM INHERITED ;
|
|
|
|
};
|
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
class ComposeShaderAlphaGM : public skiagm::GM {
|
2014-05-05 20:19:08 +04:00
|
|
|
public:
|
|
|
|
ComposeShaderAlphaGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("composeshader_alpha");
|
|
|
|
}
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
2014-05-05 22:23:54 +04:00
|
|
|
return SkISize::Make(220, 750);
|
2014-05-05 20:19:08 +04:00
|
|
|
}
|
2014-05-05 23:37:22 +04:00
|
|
|
|
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
// we're only off by 1 bit per-component
|
|
|
|
return kSkipTiled_Flag;
|
|
|
|
}
|
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2014-05-05 22:23:54 +04:00
|
|
|
SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
|
|
|
|
SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
|
|
|
|
SkShader* shaders[] = { shader0.get(), shader1.get() };
|
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
|
|
|
|
SkShader* shader = shaders[y];
|
|
|
|
canvas->save();
|
|
|
|
for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
|
|
|
|
paint.setAlpha(0xFF);
|
|
|
|
paint.setShader(NULL);
|
|
|
|
canvas->drawRect(r, paint);
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
paint.setAlpha(alpha);
|
|
|
|
paint.setShader(shader);
|
|
|
|
canvas->drawRect(r, paint);
|
2014-05-06 07:06:53 +04:00
|
|
|
|
2014-05-05 22:23:54 +04:00
|
|
|
canvas->translate(r.width() + 5, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, r.height() + 5);
|
2014-05-05 20:19:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-29 21:28:50 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-05 20:19:08 +04:00
|
|
|
DEF_GM( return new ComposeShaderGM; )
|
|
|
|
DEF_GM( return new ComposeShaderAlphaGM; )
|