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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRSXform.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-07-24 17:20:45 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::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];
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), kTextPad);
|
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]);
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas->drawString(label, i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
|
|
|
|
font, paint);
|
2015-07-24 17:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-01-25 16:37:10 +00:00
|
|
|
gModes[i], SkSamplingOptions(), 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,
|
2021-01-25 16:37:10 +00:00
|
|
|
gModes[i], SkSamplingOptions(), 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:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kNumXferModes = 29;
|
|
|
|
inline static constexpr int kNumColors = 4;
|
|
|
|
inline static constexpr int kAtlasSize = 30;
|
|
|
|
inline static constexpr int kPad = 2;
|
|
|
|
inline static constexpr int kTextPad = 8;
|
2015-07-24 17:20:45 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-07-24 17:20:45 +00:00
|
|
|
};
|
|
|
|
DEF_GM( return new DrawAtlasColorsGM; )
|