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-03-29 18:32:50 +00:00
|
|
|
paint.setXfermode(SkXfermode::Make(SkXfermode::kSrc_Mode));
|
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));
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fAtlas) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fAtlas = make_atlas(canvas, kAtlasSize);
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
SkXfermode::Mode fMode;
|
|
|
|
const char* fLabel;
|
|
|
|
} gModes[] = {
|
|
|
|
{ SkXfermode::kClear_Mode, "Clear" },
|
|
|
|
{ SkXfermode::kSrc_Mode, "Src" },
|
|
|
|
{ SkXfermode::kDst_Mode, "Dst" },
|
|
|
|
{ SkXfermode::kSrcOver_Mode, "SrcOver" },
|
|
|
|
{ SkXfermode::kDstOver_Mode, "DstOver" },
|
|
|
|
{ SkXfermode::kSrcIn_Mode, "SrcIn" },
|
|
|
|
{ SkXfermode::kDstIn_Mode, "DstIn" },
|
|
|
|
{ SkXfermode::kSrcOut_Mode, "SrcOut" },
|
|
|
|
{ SkXfermode::kDstOut_Mode, "DstOut" },
|
|
|
|
{ SkXfermode::kSrcATop_Mode, "SrcATop" },
|
|
|
|
{ SkXfermode::kDstATop_Mode, "DstATop" },
|
|
|
|
{ SkXfermode::kXor_Mode, "Xor" },
|
|
|
|
{ SkXfermode::kPlus_Mode, "Plus" },
|
|
|
|
{ SkXfermode::kModulate_Mode, "Mod" },
|
|
|
|
{ SkXfermode::kScreen_Mode, "Screen" },
|
|
|
|
{ SkXfermode::kOverlay_Mode, "Overlay" },
|
|
|
|
{ SkXfermode::kDarken_Mode, "Darken" },
|
|
|
|
{ SkXfermode::kLighten_Mode, "Lighten" },
|
|
|
|
{ SkXfermode::kColorDodge_Mode, "Dodge" },
|
|
|
|
{ SkXfermode::kColorBurn_Mode, "Burn" },
|
|
|
|
{ SkXfermode::kHardLight_Mode, "Hard" },
|
|
|
|
{ SkXfermode::kSoftLight_Mode, "Soft" },
|
|
|
|
{ SkXfermode::kDifference_Mode, "Diff" },
|
|
|
|
{ SkXfermode::kExclusion_Mode, "Exclusion" },
|
|
|
|
{ SkXfermode::kMultiply_Mode, "Multiply" },
|
|
|
|
{ SkXfermode::kHue_Mode, "Hue" },
|
|
|
|
{ SkXfermode::kSaturation_Mode, "Sat" },
|
|
|
|
{ SkXfermode::kColor_Mode, "Color" },
|
|
|
|
{ SkXfermode::kLuminosity_Mode, "Luminosity"},
|
|
|
|
};
|
|
|
|
|
|
|
|
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-03-17 17:51:11 +00:00
|
|
|
canvas->drawAtlas(fAtlas.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-03-17 17:51:11 +00:00
|
|
|
canvas->drawAtlas(fAtlas.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:
|
|
|
|
static const int kNumXferModes = 29;
|
|
|
|
static const int kNumColors = 4;
|
|
|
|
static const int kAtlasSize = 30;
|
|
|
|
static const int kPad = 2;
|
|
|
|
static const int kTextPad = 8;
|
|
|
|
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fAtlas;
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new DrawAtlasColorsGM; )
|