2012-12-17 21:48:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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"
|
2014-03-04 16:52:20 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-03-04 16:25:34 +00:00
|
|
|
#include "GrTest.h"
|
2014-03-04 16:52:20 +00:00
|
|
|
#include "effects/GrRRectEffect.h"
|
|
|
|
#endif
|
2014-03-04 16:25:34 +00:00
|
|
|
#include "SkDevice.h"
|
2012-12-17 21:48:19 +00:00
|
|
|
#include "SkRRect.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class RRectGM : public GM {
|
|
|
|
public:
|
2014-03-04 16:25:34 +00:00
|
|
|
enum Type {
|
|
|
|
kBW_Draw_Type,
|
|
|
|
kAA_Draw_Type,
|
|
|
|
kBW_Clip_Type,
|
|
|
|
kAA_Clip_Type,
|
|
|
|
kEffect_Type,
|
|
|
|
};
|
|
|
|
RRectGM(Type type) : fType(type) {
|
2012-12-17 21:48:19 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
this->setUpRRects();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-03-04 16:25:34 +00:00
|
|
|
SkString onShortName() SK_OVERRIDE {
|
2012-12-17 21:48:19 +00:00
|
|
|
SkString name("rrect");
|
2014-03-04 16:25:34 +00:00
|
|
|
switch (fType) {
|
|
|
|
case kBW_Draw_Type:
|
|
|
|
name.append("_draw_bw");
|
|
|
|
break;
|
|
|
|
case kAA_Draw_Type:
|
|
|
|
name.append("_draw_aa");
|
|
|
|
break;
|
|
|
|
case kBW_Clip_Type:
|
|
|
|
name.append("_clip_bw");
|
|
|
|
break;
|
|
|
|
case kAA_Clip_Type:
|
|
|
|
name.append("_clip_aa");
|
|
|
|
break;
|
|
|
|
case kEffect_Type:
|
|
|
|
name.append("_effect");
|
|
|
|
break;
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkISize onISize() SK_OVERRIDE { return SkISize::Make(kImageWidth, kImageHeight); }
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2015-01-31 15:51:14 +00:00
|
|
|
GrContext* context = NULL;
|
2014-03-04 16:52:20 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-03-12 18:28:35 +00:00
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-01-31 15:51:14 +00:00
|
|
|
context = rt ? rt->getContext() : NULL;
|
|
|
|
#endif
|
2014-03-04 16:25:34 +00:00
|
|
|
if (kEffect_Type == fType && NULL == context) {
|
2015-01-31 15:51:14 +00:00
|
|
|
this->drawGpuOnlyMessage(canvas);
|
2014-03-04 16:25:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2014-03-04 16:25:34 +00:00
|
|
|
SkPaint paint;
|
|
|
|
if (kAA_Draw_Type == fType) {
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
}
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2014-03-10 19:33:16 +00:00
|
|
|
static const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX),
|
|
|
|
SkIntToScalar(kTileY));
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
|
|
|
|
SkIntToScalar(kImageHeight));
|
|
|
|
#endif
|
2012-12-17 21:48:19 +00:00
|
|
|
|
2014-03-10 19:51:46 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-09-23 16:50:21 +00:00
|
|
|
int lastEdgeType = (kEffect_Type == fType) ? kLast_GrProcessorEdgeType: 0;
|
2014-03-10 19:51:46 +00:00
|
|
|
#else
|
|
|
|
int lastEdgeType = 0;
|
|
|
|
#endif
|
2014-03-10 19:33:16 +00:00
|
|
|
|
|
|
|
int y = 1;
|
2014-03-10 19:51:46 +00:00
|
|
|
for (int et = 0; et <= lastEdgeType; ++et) {
|
2014-03-10 19:33:16 +00:00
|
|
|
int x = 1;
|
|
|
|
for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
|
|
|
|
bool drew = true;
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds()));
|
|
|
|
SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
|
|
|
|
imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
|
|
|
|
#endif
|
2012-12-17 21:48:19 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
2014-03-04 16:25:34 +00:00
|
|
|
if (kEffect_Type == fType) {
|
2014-03-04 16:52:20 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-03-04 16:25:34 +00:00
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
|
|
|
if (NULL == tt.target()) {
|
|
|
|
SkDEBUGFAIL("Couldn't get Gr test target.");
|
|
|
|
return;
|
|
|
|
}
|
2015-01-22 18:16:09 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2014-03-10 19:33:16 +00:00
|
|
|
SkRRect rrect = fRRects[curRRect];
|
2014-03-04 16:25:34 +00:00
|
|
|
rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
|
2014-09-23 16:50:21 +00:00
|
|
|
GrPrimitiveEdgeType edgeType = (GrPrimitiveEdgeType) et;
|
|
|
|
SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType,
|
|
|
|
rrect));
|
|
|
|
if (fp) {
|
2015-01-22 18:16:09 +00:00
|
|
|
pipelineBuilder.addCoverageProcessor(fp);
|
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2014-03-04 16:25:34 +00:00
|
|
|
|
|
|
|
SkRect bounds = rrect.getBounds();
|
|
|
|
bounds.outset(2.f, 2.f);
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2015-02-23 22:44:57 +00:00
|
|
|
tt.target()->drawSimpleRect(&pipelineBuilder,
|
|
|
|
0xff000000,
|
|
|
|
SkMatrix::I(),
|
2014-12-29 23:10:07 +00:00
|
|
|
bounds);
|
2014-03-10 19:33:16 +00:00
|
|
|
} else {
|
|
|
|
drew = false;
|
2014-03-04 16:25:34 +00:00
|
|
|
}
|
2014-03-04 16:52:20 +00:00
|
|
|
#endif
|
2014-03-04 16:25:34 +00:00
|
|
|
} else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) {
|
|
|
|
bool aaClip = (kAA_Clip_Type == fType);
|
2014-03-10 19:33:16 +00:00
|
|
|
canvas->clipRRect(fRRects[curRRect], SkRegion::kReplace_Op, aaClip);
|
2012-12-17 21:48:19 +00:00
|
|
|
canvas->drawRect(kMaxTileBound, paint);
|
|
|
|
} else {
|
2014-03-10 19:33:16 +00:00
|
|
|
canvas->drawRRect(fRRects[curRRect], paint);
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2014-03-10 19:33:16 +00:00
|
|
|
if (drew) {
|
|
|
|
x = x + kTileX;
|
|
|
|
if (x > kImageWidth) {
|
|
|
|
x = 1;
|
|
|
|
y += kTileY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (x != 1) {
|
|
|
|
y += kTileY;
|
2012-12-17 21:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUpRRects() {
|
2012-12-18 02:03:03 +00:00
|
|
|
// each RRect must fit in a 0x0 -> (kTileX-2)x(kTileY-2) block. These will be tiled across
|
2012-12-17 21:48:19 +00:00
|
|
|
// the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
|
|
|
|
|
|
|
|
// simple cases
|
|
|
|
fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
|
|
|
|
fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
|
|
|
|
fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
|
2014-02-19 15:18:05 +00:00
|
|
|
fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
|
|
|
|
// small circular corners are an interesting test case for gpu clipping
|
|
|
|
fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
|
|
|
|
fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
|
|
|
|
fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
|
2012-12-17 21:48:19 +00:00
|
|
|
|
|
|
|
// The first complex case needs special handling since it is a square
|
|
|
|
fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
|
2012-12-24 13:56:17 +00:00
|
|
|
for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) {
|
2012-12-17 21:48:19 +00:00
|
|
|
fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-04 16:25:34 +00:00
|
|
|
Type fType;
|
2014-03-05 03:02:06 +00:00
|
|
|
|
2012-12-17 21:48:19 +00:00
|
|
|
static const int kImageWidth = 640;
|
|
|
|
static const int kImageHeight = 480;
|
|
|
|
|
|
|
|
static const int kTileX = 80;
|
|
|
|
static const int kTileY = 40;
|
|
|
|
|
2014-02-19 15:18:05 +00:00
|
|
|
static const int kNumSimpleCases = 7;
|
2014-03-24 14:53:09 +00:00
|
|
|
static const int kNumComplexCases = 35;
|
2012-12-17 21:58:02 +00:00
|
|
|
static const SkVector gRadii[kNumComplexCases][4];
|
2012-12-17 21:48:19 +00:00
|
|
|
|
|
|
|
static const int kNumRRects = kNumSimpleCases + kNumComplexCases;
|
|
|
|
SkRRect fRRects[kNumRRects];
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Radii for the various test cases. Order is UL, UR, LR, LL
|
|
|
|
const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
|
|
|
|
// a circle
|
|
|
|
{ { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
|
|
|
|
|
|
|
|
// odd ball cases
|
|
|
|
{ { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
|
|
|
|
{ { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
|
|
|
|
{ { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
|
|
|
|
|
|
|
|
// UL
|
|
|
|
{ { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// UR
|
|
|
|
{ { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// LR
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
|
|
|
|
|
|
|
|
// LL
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
|
|
|
|
|
|
|
|
// over-sized radii
|
|
|
|
{ { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
|
2014-02-21 22:41:56 +00:00
|
|
|
|
|
|
|
// circular corner tabs
|
|
|
|
{ { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
|
|
|
|
{ { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
|
|
|
|
{ { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
|
2014-03-11 15:54:51 +00:00
|
|
|
|
2014-03-24 14:53:09 +00:00
|
|
|
// small radius circular corner tabs
|
|
|
|
{ { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
|
|
|
|
{ { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
|
|
|
|
|
2014-03-11 15:54:51 +00:00
|
|
|
// single circular corner cases
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
|
|
|
|
{ { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
|
|
|
|
{ { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
|
|
|
|
{ { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
|
2014-03-13 18:01:05 +00:00
|
|
|
|
|
|
|
// nine patch elliptical
|
|
|
|
{ { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
|
|
|
|
{ { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
|
2014-03-24 14:53:09 +00:00
|
|
|
|
|
|
|
// nine patch elliptical, small radii
|
|
|
|
{ { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
|
|
|
|
{ { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
|
|
|
|
{ { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
|
|
|
|
{ { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
|
|
|
|
|
2012-12-17 21:48:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-04 16:25:34 +00:00
|
|
|
DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
|
|
|
|
DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
|
2014-03-04 16:52:20 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2014-03-04 16:25:34 +00:00
|
|
|
DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
|
2014-03-04 16:52:20 +00:00
|
|
|
#endif
|
2012-12-17 21:48:19 +00:00
|
|
|
|
|
|
|
}
|