skia2/gm/bigrrectaaeffect.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

130 lines
4.6 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"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrDrawContextPriv.h"
#include "SkRRect.h"
#include "batches/GrDrawBatch.h"
#include "batches/GrRectBatchFactory.h"
#include "effects/GrRRectEffect.h"
namespace skiagm {
///////////////////////////////////////////////////////////////////////////////
class BigRRectAAEffectGM : public GM {
public:
BigRRectAAEffectGM(const SkRRect& rrect, const char* name)
: fRRect(rrect)
, fName(name) {
this->setBGColor(sk_tool_utils::color_to_565(SK_ColorBLUE));
// Each test case draws the rrect with gaps around it.
fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap;
fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap;
// Add a pad between test cases.
fTestOffsetX = fTestWidth + kPad;
fTestOffsetY = fTestHeight + kPad;
// We draw two tests in x (fill and inv-fill) and pad around
// all four sides of the image.
fWidth = 2 * fTestOffsetX + kPad;
fHeight = fTestOffsetY + kPad;
}
protected:
SkString onShortName() override {
SkString name;
name.printf("big_rrect_%s_aa_effect", fName);
return name;
}
SkISize onISize() override { return SkISize::Make(fWidth, fHeight); }
void onDraw(SkCanvas* canvas) override {
GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
if (!drawContext) {
skiagm::GM::DrawGpuOnlyMessage(canvas);
return;
}
SkPaint paint;
int y = kPad;
int x = kPad;
constexpr GrPrimitiveEdgeType kEdgeTypes[] = {
kFillAA_GrProcessorEdgeType,
kInverseFillAA_GrProcessorEdgeType,
};
SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
canvas->save();
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
// Draw a background for the test case
SkPaint paint;
paint.setColor(SK_ColorWHITE);
canvas->drawRect(testBounds, paint);
GrPaint grPaint;
grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
SkRRect rrect = fRRect;
rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect));
SkASSERT(fp);
if (fp) {
grPaint.addCoverageFragmentProcessor(std::move(fp));
SkRect bounds = testBounds;
bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
SkAutoTUnref<GrDrawBatch> batch(
GrRectBatchFactory::CreateNonAAFill(0xff000000, SkMatrix::I(), bounds,
nullptr, nullptr));
drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
}
canvas->restore();
x = x + fTestOffsetX;
}
}
private:
// pad between test cases
static constexpr int kPad = 7;
// gap between rect for each case that is rendered and exterior of rrect
static constexpr int kGap = 3;
SkRRect fRRect;
int fWidth;
int fHeight;
int fTestWidth;
int fTestHeight;
int fTestOffsetX;
int fTestOffsetY;
const char* fName;
typedef GM INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
// This value is motivated by bug chromium:477684. It has to be large to cause overflow in
// the shader
constexpr int kSize = 700;
DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
// The next two have small linear segments between the corners
DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 10.f), "circular_corner"); )
DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 15.f), "elliptical_corner"); )
}
#endif