2015-07-24 17:20:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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 "SkRSXform.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
// Create a square atlas of:
|
|
|
|
// opaque white | opaque red
|
|
|
|
// ------------------------------------
|
|
|
|
// opaque green | transparent black
|
|
|
|
//
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
|
2015-07-24 17:20:45 +00:00
|
|
|
const int kBlockSize = atlasSize/2;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(caller->makeSurface(info));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == surface) {
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
2016-03-29 16:03:52 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH(0, 0,
|
2015-07-24 17:20:45 +00:00
|
|
|
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
2016-03-29 16:03:52 +00:00
|
|
|
r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
|
2015-07-24 17:20:45 +00:00
|
|
|
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2016-03-29 16:03:52 +00:00
|
|
|
r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
|
2015-07-24 17:20:45 +00:00
|
|
|
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorTRANSPARENT);
|
2016-03-29 16:03:52 +00:00
|
|
|
r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
|
2015-07-24 17:20:45 +00:00
|
|
|
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This GM tests the drawAtlas API with colors, different xfer modes
|
|
|
|
// and transparency in the atlas image
|
|
|
|
class DrawAtlasColorsGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
DrawAtlasColorsGM() {
|
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-24 17:20:45 +00:00
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("draw-atlas-colors");
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-24 17:20:45 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
|
|
|
|
2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-24 17:20:45 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
|
|
|
|
|
2016-09-29 20:43:49 +00:00
|
|
|
auto atlas = make_atlas(canvas, kAtlasSize);
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
const struct {
|
2016-10-28 19:42:34 +00:00
|
|
|
SkBlendMode fMode;
|
|
|
|
const char* fLabel;
|
2015-07-24 17:20:45 +00:00
|
|
|
} gModes[] = {
|
2016-10-28 19:42:34 +00:00
|
|
|
{ SkBlendMode::kClear, "Clear" },
|
|
|
|
{ SkBlendMode::kSrc, "Src" },
|
|
|
|
{ SkBlendMode::kDst, "Dst" },
|
|
|
|
{ SkBlendMode::kSrcOver, "SrcOver" },
|
|
|
|
{ SkBlendMode::kDstOver, "DstOver" },
|
|
|
|
{ SkBlendMode::kSrcIn, "SrcIn" },
|
|
|
|
{ SkBlendMode::kDstIn, "DstIn" },
|
|
|
|
{ SkBlendMode::kSrcOut, "SrcOut" },
|
|
|
|
{ SkBlendMode::kDstOut, "DstOut" },
|
|
|
|
{ SkBlendMode::kSrcATop, "SrcATop" },
|
|
|
|
{ SkBlendMode::kDstATop, "DstATop" },
|
|
|
|
{ SkBlendMode::kXor, "Xor" },
|
|
|
|
{ SkBlendMode::kPlus, "Plus" },
|
|
|
|
{ SkBlendMode::kModulate, "Mod" },
|
|
|
|
{ SkBlendMode::kScreen, "Screen" },
|
|
|
|
{ SkBlendMode::kOverlay, "Overlay" },
|
|
|
|
{ SkBlendMode::kDarken, "Darken" },
|
|
|
|
{ SkBlendMode::kLighten, "Lighten" },
|
|
|
|
{ SkBlendMode::kColorDodge, "Dodge" },
|
|
|
|
{ SkBlendMode::kColorBurn, "Burn" },
|
|
|
|
{ SkBlendMode::kHardLight, "Hard" },
|
|
|
|
{ SkBlendMode::kSoftLight, "Soft" },
|
|
|
|
{ SkBlendMode::kDifference, "Diff" },
|
|
|
|
{ SkBlendMode::kExclusion, "Exclusion" },
|
|
|
|
{ SkBlendMode::kMultiply, "Multiply" },
|
|
|
|
{ SkBlendMode::kHue, "Hue" },
|
|
|
|
{ SkBlendMode::kSaturation, "Sat" },
|
|
|
|
{ SkBlendMode::kColor, "Color" },
|
|
|
|
{ SkBlendMode::kLuminosity, "Luminosity"},
|
2015-07-24 17:20:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkColor gColors[] = {
|
|
|
|
SK_ColorWHITE,
|
|
|
|
SK_ColorRED,
|
|
|
|
0x88888888, // transparent grey
|
|
|
|
0x88000088 // transparent blue
|
|
|
|
};
|
|
|
|
|
|
|
|
const int numModes = SK_ARRAY_COUNT(gModes);
|
|
|
|
SkASSERT(numModes == kNumXferModes);
|
|
|
|
const int numColors = SK_ARRAY_COUNT(gColors);
|
|
|
|
SkASSERT(numColors == kNumColors);
|
|
|
|
SkRSXform xforms[numColors];
|
|
|
|
SkRect rects[numColors];
|
|
|
|
SkColor quadColors[numColors];
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
for (int i = 0; i < numColors; ++i) {
|
|
|
|
xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
|
|
|
|
rects[i] = target;
|
|
|
|
quadColors[i] = gColors[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint textP;
|
|
|
|
textP.setTextSize(SkIntToScalar(kTextPad));
|
|
|
|
textP.setAntiAlias(true);
|
2015-08-27 14:41:13 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&textP, nullptr);
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < numModes; ++i) {
|
|
|
|
canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
|
|
|
|
i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
|
|
|
|
textP);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numModes; ++i) {
|
2016-03-29 16:03:52 +00:00
|
|
|
canvas->save();
|
2015-07-24 17:20:45 +00:00
|
|
|
canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
|
|
|
|
SkIntToScalar(kTextPad+kPad));
|
|
|
|
// w/o a paint
|
2016-09-29 20:43:49 +00:00
|
|
|
canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
|
2015-08-27 14:41:13 +00:00
|
|
|
gModes[i].fMode, nullptr, nullptr);
|
2015-07-24 17:20:45 +00:00
|
|
|
canvas->translate(0.0f, numColors*(target.height()+kPad));
|
|
|
|
// w a paint
|
2016-09-29 20:43:49 +00:00
|
|
|
canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
|
2015-08-27 14:41:13 +00:00
|
|
|
gModes[i].fMode, nullptr, &paint);
|
2016-03-29 16:03:52 +00:00
|
|
|
canvas->restore();
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-07-24 17:20:45 +00:00
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kNumXferModes = 29;
|
|
|
|
static constexpr int kNumColors = 4;
|
|
|
|
static constexpr int kAtlasSize = 30;
|
|
|
|
static constexpr int kPad = 2;
|
|
|
|
static constexpr int kTextPad = 8;
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new DrawAtlasColorsGM; )
|