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"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-07-24 17:20:45 +00:00
|
|
|
#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);
|
2018-09-05 18:41:40 +00:00
|
|
|
auto surface(sk_tool_utils::makeSurface(caller, 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() {
|
2018-08-16 14:17:03 +00:00
|
|
|
this->setBGColor(0xFFCCCCCC);
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
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
|
|
|
|
2017-03-15 16:19:37 +00:00
|
|
|
const SkBlendMode gModes[] = {
|
|
|
|
SkBlendMode::kClear,
|
|
|
|
SkBlendMode::kSrc,
|
|
|
|
SkBlendMode::kDst,
|
|
|
|
SkBlendMode::kSrcOver,
|
|
|
|
SkBlendMode::kDstOver,
|
|
|
|
SkBlendMode::kSrcIn,
|
|
|
|
SkBlendMode::kDstIn,
|
|
|
|
SkBlendMode::kSrcOut,
|
|
|
|
SkBlendMode::kDstOut,
|
|
|
|
SkBlendMode::kSrcATop,
|
|
|
|
SkBlendMode::kDstATop,
|
|
|
|
SkBlendMode::kXor,
|
|
|
|
SkBlendMode::kPlus,
|
|
|
|
SkBlendMode::kModulate,
|
|
|
|
SkBlendMode::kScreen,
|
|
|
|
SkBlendMode::kOverlay,
|
|
|
|
SkBlendMode::kDarken,
|
|
|
|
SkBlendMode::kLighten,
|
|
|
|
SkBlendMode::kColorDodge,
|
|
|
|
SkBlendMode::kColorBurn,
|
|
|
|
SkBlendMode::kHardLight,
|
|
|
|
SkBlendMode::kSoftLight,
|
|
|
|
SkBlendMode::kDifference,
|
|
|
|
SkBlendMode::kExclusion,
|
|
|
|
SkBlendMode::kMultiply,
|
|
|
|
SkBlendMode::kHue,
|
|
|
|
SkBlendMode::kSaturation,
|
|
|
|
SkBlendMode::kColor,
|
|
|
|
SkBlendMode::kLuminosity,
|
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) {
|
2017-03-15 16:19:37 +00:00
|
|
|
const char* label = SkBlendMode_Name(gModes[i]);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(label,
|
2015-07-24 17:20:45 +00:00
|
|
|
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,
|
2017-03-15 16:19:37 +00:00
|
|
|
gModes[i], 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,
|
2017-03-15 16:19:37 +00:00
|
|
|
gModes[i], 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; )
|