2018-08-03 15:15:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 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"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-04-30 17:44:26 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2018-08-03 15:15:24 +00:00
|
|
|
|
2019-04-30 17:44:26 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2018-08-03 15:15:24 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
// This GM recreates the blend mode images from the Android documentation
|
|
|
|
class AndroidBlendModesGM : public GM {
|
|
|
|
public:
|
|
|
|
AndroidBlendModesGM() {
|
|
|
|
this->setBGColor(SK_ColorBLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("androidblendmodes");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kNumCols * kBitmapSize, kNumRows * kBitmapSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkImageInfo ii = SkImageInfo::MakeN32Premul(kBitmapSize, kBitmapSize);
|
|
|
|
{
|
|
|
|
fCompositeSrc.allocPixels(ii);
|
|
|
|
SkCanvas tmp(fCompositeSrc);
|
|
|
|
tmp.clear(SK_ColorTRANSPARENT);
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
2019-03-20 16:12:10 +00:00
|
|
|
p.setColor(ToolUtils::color_to_565(kBlue));
|
2018-08-03 15:15:24 +00:00
|
|
|
tmp.drawRect(SkRect::MakeLTRB(16, 96, 160, 240), p);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
fCompositeDst.allocPixels(ii);
|
|
|
|
SkCanvas tmp(fCompositeDst);
|
|
|
|
tmp.clear(SK_ColorTRANSPARENT);
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
2019-03-20 16:12:10 +00:00
|
|
|
p.setColor(ToolUtils::color_to_565(kRed));
|
2018-08-03 15:15:24 +00:00
|
|
|
tmp.drawCircle(160, 95, 80, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawTile(SkCanvas* canvas, int xOffset, int yOffset, SkBlendMode mode) {
|
|
|
|
canvas->translate(xOffset, yOffset);
|
|
|
|
|
|
|
|
canvas->clipRect(SkRect::MakeXYWH(0, 0, 256, 256));
|
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
canvas->drawBitmap(fCompositeDst, 0, 0, &p);
|
|
|
|
p.setBlendMode(mode);
|
|
|
|
canvas->drawBitmap(fCompositeSrc, 0, 0, &p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2018-08-03 15:15:24 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, kWhite, kGrey, 32);
|
2018-08-03 15:15:24 +00:00
|
|
|
|
|
|
|
int xOffset = 0, yOffset = 0;
|
|
|
|
|
|
|
|
// Android doesn't expose all the blend modes
|
|
|
|
// Note that the Android documentation calls:
|
|
|
|
// Skia's kPlus, add
|
|
|
|
// Skia's kModulate, multiply
|
|
|
|
for (SkBlendMode mode : { SkBlendMode::kPlus /* add */, SkBlendMode::kClear,
|
|
|
|
SkBlendMode::kDarken, SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kDstATop, SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kDstOut, SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kLighten, SkBlendMode::kModulate /* multiply */,
|
|
|
|
SkBlendMode::kOverlay, SkBlendMode::kScreen,
|
|
|
|
SkBlendMode::kSrc, SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kSrcIn, SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kSrcOver, SkBlendMode::kXor } ) {
|
|
|
|
|
|
|
|
int saveCount = canvas->save();
|
|
|
|
this->drawTile(canvas, xOffset, yOffset, mode);
|
|
|
|
canvas->restoreToCount(saveCount);
|
|
|
|
|
2018-10-25 16:36:06 +00:00
|
|
|
SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode),
|
2018-08-03 15:15:24 +00:00
|
|
|
xOffset + kBitmapSize/2.0f,
|
|
|
|
yOffset + kBitmapSize,
|
2018-12-24 04:19:14 +00:00
|
|
|
font, SkPaint(), SkTextUtils::kCenter_Align);
|
2018-08-03 15:15:24 +00:00
|
|
|
|
|
|
|
xOffset += 256;
|
|
|
|
if (xOffset >= 1024) {
|
|
|
|
xOffset = 0;
|
|
|
|
yOffset += 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kBitmapSize = 256;
|
|
|
|
static const int kNumRows = 5;
|
|
|
|
static const int kNumCols = 4;
|
|
|
|
|
|
|
|
static const SkColor kBlue = SkColorSetARGB(255, 22, 150, 243);
|
|
|
|
static const SkColor kRed = SkColorSetARGB(255, 233, 30, 99);
|
|
|
|
static const SkColor kWhite = SkColorSetARGB(255, 243, 243, 243);
|
|
|
|
static const SkColor kGrey = SkColorSetARGB(255, 222, 222, 222);
|
|
|
|
|
|
|
|
SkBitmap fCompositeSrc;
|
|
|
|
SkBitmap fCompositeDst;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2018-08-03 15:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new AndroidBlendModesGM;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|