2011-10-05 16:27:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkAAClip.h"
|
|
|
|
|
2011-10-27 16:58:46 +00:00
|
|
|
static void testop(const SkIRect& r0, const SkIRect& r1, SkRegion::Op op,
|
|
|
|
const SkIRect& expectedR) {
|
|
|
|
SkAAClip c0, c1, c2;
|
|
|
|
c0.setRect(r0);
|
|
|
|
c1.setRect(r1);
|
|
|
|
c2.op(c0, c1, op);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-01-07 19:54:40 +00:00
|
|
|
SkDEBUGCODE(SkIRect r2 = c2.getBounds());
|
2011-10-27 16:58:46 +00:00
|
|
|
SkASSERT(r2 == expectedR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkIRect r0;
|
|
|
|
SkIRect r1;
|
|
|
|
SkRegion::Op op;
|
|
|
|
SkIRect expectedR;
|
|
|
|
} gRec[] = {
|
|
|
|
{{ 1, 2, 9, 3 }, { -3, 2, 5, 11 }, SkRegion::kDifference_Op, { 5, 2, 9, 3 }},
|
|
|
|
{{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kDifference_Op, { 1, 11, 5, 13 }},
|
|
|
|
{{ 1, 10, 5, 13 }, { 1, 2, 5, 11 }, SkRegion::kReverseDifference_Op, { 1, 2, 5, 10 }},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void testop() {
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
|
|
|
|
testop(gRec[i].r0, gRec[i].r1, gRec[i].op, gRec[i].expectedR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
static void drawClip(SkCanvas* canvas, const SkAAClip& clip) {
|
|
|
|
SkMask mask;
|
|
|
|
SkBitmap bm;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
clip.copyToMask(&mask);
|
2011-10-24 12:19:46 +00:00
|
|
|
SkAutoMaskFreeImage amfi(mask.fImage);
|
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
bm.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(),
|
|
|
|
mask.fBounds.height()),
|
|
|
|
mask.fImage, mask.fRowBytes, NULL, NULL);
|
2011-10-05 16:27:44 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2011-10-24 21:09:40 +00:00
|
|
|
canvas->drawBitmap(bm,
|
|
|
|
SK_Scalar1 * mask.fBounds.fLeft,
|
|
|
|
SK_Scalar1 * mask.fBounds.fTop,
|
|
|
|
&paint);
|
2011-10-05 16:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class AAClipView : public SampleView {
|
|
|
|
public:
|
|
|
|
AAClipView() {
|
2011-10-27 16:58:46 +00:00
|
|
|
testop();
|
2011-10-05 16:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "AAClip");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
#if 1
|
|
|
|
SkAAClip aaclip;
|
|
|
|
SkPath path;
|
|
|
|
SkRect bounds;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
bounds.set(0, 0, 20, 20);
|
|
|
|
bounds.inset(SK_ScalarHalf, SK_ScalarHalf);
|
|
|
|
|
|
|
|
// path.addRect(bounds);
|
|
|
|
// path.addOval(bounds);
|
|
|
|
path.addRoundRect(bounds, 4, 4);
|
|
|
|
aaclip.setPath(path);
|
|
|
|
canvas->translate(30, 30);
|
|
|
|
drawClip(canvas, aaclip);
|
|
|
|
|
|
|
|
SkAAClip aaclip2;
|
|
|
|
path.offset(10, 10);
|
|
|
|
aaclip2.setPath(path);
|
|
|
|
canvas->translate(30, 0);
|
|
|
|
drawClip(canvas, aaclip2);
|
|
|
|
|
|
|
|
SkAAClip aaclip3;
|
|
|
|
aaclip3.op(aaclip, aaclip2, SkRegion::kIntersect_Op);
|
|
|
|
canvas->translate(30, 0);
|
|
|
|
drawClip(canvas, aaclip3);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
#endif
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
#if 0
|
|
|
|
SkRect r;
|
|
|
|
r.set(0, 0, this->width(), this->height());
|
|
|
|
r.inset(20, 20);
|
|
|
|
canvas->clipRect(r);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-05 16:27:44 +00:00
|
|
|
SkPath path;
|
|
|
|
path.addRect(r);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new AAClipView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|