skia2/gm/drawatlascolor.cpp
mtklein dbfd7ab108 Replace a lot of 'static const' with 'constexpr' or 'const'.
'static const' means, there must be at most one of these, and initialize it at
compile time if possible or runtime if necessary.  This leads to unexpected
code execution, and TSAN* will complain about races on the guard variables.

Generally 'constexpr' or 'const' are better choices.  Neither can cause races:
they're either intialized at compile time (constexpr) or intialized each time
independently (const).

This CL prefers constexpr where possible, and uses const where not.  It even
prefers constexpr over const where they don't make a difference... I want to have
lots of examples of constexpr for people to see and mimic.

The scoped-to-class static has nothing to do with any of this, and is not changed.

* Not yet on the bots, which use an older TSAN.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2300623005

Review-Url: https://codereview.chromium.org/2300623005
2016-09-01 11:24:54 -07:00

177 lines
6.4 KiB
C++

/*
* 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
//
static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
const int kBlockSize = atlasSize/2;
SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
auto surface(caller->makeSurface(info));
if (nullptr == surface) {
surface = SkSurface::MakeRaster(info);
}
SkCanvas* canvas = surface->getCanvas();
SkPaint paint;
paint.setXfermode(SkXfermode::Make(SkXfermode::kSrc_Mode));
paint.setColor(SK_ColorWHITE);
SkRect r = SkRect::MakeXYWH(0, 0,
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
canvas->drawRect(r, paint);
paint.setColor(SK_ColorRED);
r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
canvas->drawRect(r, paint);
paint.setColor(SK_ColorGREEN);
r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
canvas->drawRect(r, paint);
paint.setColor(SK_ColorTRANSPARENT);
r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
canvas->drawRect(r, paint);
return surface->makeImageSnapshot();
}
// 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));
}
protected:
SkString onShortName() override {
return SkString("draw-atlas-colors");
}
SkISize onISize() override {
return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
}
void onDraw(SkCanvas* canvas) override {
const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
if (nullptr == fAtlas) {
fAtlas = make_atlas(canvas, kAtlasSize);
}
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);
sk_tool_utils::set_portable_typeface(&textP, nullptr);
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) {
canvas->save();
canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
SkIntToScalar(kTextPad+kPad));
// w/o a paint
canvas->drawAtlas(fAtlas.get(), xforms, rects, quadColors, numColors,
gModes[i].fMode, nullptr, nullptr);
canvas->translate(0.0f, numColors*(target.height()+kPad));
// w a paint
canvas->drawAtlas(fAtlas.get(), xforms, rects, quadColors, numColors,
gModes[i].fMode, nullptr, &paint);
canvas->restore();
}
}
private:
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;
sk_sp<SkImage> fAtlas;
typedef GM INHERITED;
};
DEF_GM( return new DrawAtlasColorsGM; )