2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkCamera.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkInterpolator.h"
|
|
|
|
#include "SkMaskFilter.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkKey.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include "SkDrawFilter.h"
|
|
|
|
|
|
|
|
static void make_paint(SkPaint* paint) {
|
|
|
|
SkColor colors[] = { 0, SK_ColorWHITE };
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint->setShader(s)->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
paint->setXfermodeMode(SkXfermode::kDstIn_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_layers(const char label[], SkCanvas* canvas) {
|
|
|
|
SkDebugf("Dump Layers(%s)\n", label);
|
|
|
|
|
|
|
|
SkCanvas::LayerIter iter(canvas, true);
|
|
|
|
int index = 0;
|
|
|
|
while (!iter.done()) {
|
|
|
|
const SkBitmap& bm = iter.device()->accessBitmap(false);
|
|
|
|
const SkIRect& clip = iter.clip().getBounds();
|
|
|
|
SkDebugf("Layer[%d] bitmap [%d %d] X=%d Y=%d clip=[%d %d %d %d] alpha=%d\n", index++,
|
|
|
|
bm.width(), bm.height(), iter.x(), iter.y(),
|
|
|
|
clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
|
|
|
|
iter.paint().getAlpha());
|
|
|
|
iter.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test drawing with strips of fading gradient above and below
|
|
|
|
static void test_fade(SkCanvas* canvas) {
|
|
|
|
SkAutoCanvasRestore ar(canvas, true);
|
|
|
|
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore(canvas, false);
|
|
|
|
|
|
|
|
// create the layers
|
|
|
|
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
canvas->clipRect(r);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
r.fBottom = SkIntToScalar(20);
|
|
|
|
canvas->saveLayer(&r, NULL, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag));
|
|
|
|
|
|
|
|
r.fTop = SkIntToScalar(80);
|
|
|
|
r.fBottom = SkIntToScalar(100);
|
|
|
|
canvas->saveLayer(&r, NULL, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
// now draw the "content"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
if (true) {
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
|
|
|
|
canvas->saveLayerAlpha(&r, 0x80);
|
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
canvas->drawOval(r, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
dump_layers("inside layer alpha", canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
} else {
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// return;
|
|
|
|
|
|
|
|
dump_layers("outside layer alpha", canvas);
|
|
|
|
|
|
|
|
// now apply an effect
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
make_paint(&paint);
|
|
|
|
r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20));
|
|
|
|
// SkDebugf("--------- draw top grad\n");
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
SkMatrix m;
|
|
|
|
SkShader* s = paint.getShader();
|
|
|
|
m.setScale(SK_Scalar1, -SK_Scalar1);
|
|
|
|
m.postTranslate(0, SkIntToScalar(100));
|
|
|
|
s->setLocalMatrix(m);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
r.fTop = SkIntToScalar(80);
|
|
|
|
r.fBottom = SkIntToScalar(100);
|
|
|
|
// SkDebugf("--------- draw bot grad\n");
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RedFilter : public SkDrawFilter {
|
|
|
|
public:
|
2012-09-13 13:25:30 +00:00
|
|
|
virtual void filter(SkPaint* p, SkDrawFilter::Type) SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
fColor = p->getColor();
|
|
|
|
if (fColor == SK_ColorRED) {
|
|
|
|
p->setColor(SK_ColorGREEN);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkColor fColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
class LayersView : public SkView {
|
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
LayersView() {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Layers");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void drawBG(SkCanvas* canvas) {
|
2009-05-22 14:39:03 +00:00
|
|
|
canvas->drawColor(SK_ColorGRAY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
this->drawBG(canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-05-22 14:39:03 +00:00
|
|
|
if (true) {
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2009-05-22 14:39:03 +00:00
|
|
|
SkPaint p;
|
|
|
|
canvas->saveLayer(&r, &p);
|
|
|
|
canvas->drawColor(0xFFFF0000);
|
2010-12-20 18:26:13 +00:00
|
|
|
p.setAlpha(0); // or 0
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2009-05-22 14:39:03 +00:00
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (false) {
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
p.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (true) {
|
|
|
|
canvas->saveLayer(&r, &p);
|
|
|
|
p.setColor(0xFFFF0000);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setColor(0xFF0000FF);
|
|
|
|
r.offset(SkIntToScalar(20), SkIntToScalar(50));
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setAlpha(0x88);
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(300), 0);
|
|
|
|
|
|
|
|
SkRect r;
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(60));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->saveLayer(&r, &p, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag));
|
|
|
|
// canvas->clipRect(r, SkRegion::kDifference_Op);
|
|
|
|
// canvas->clipRect(r, SkRegion::kIntersect_Op);
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
r.set(SkIntToScalar(0), SkIntToScalar(0),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
canvas->drawOval(r, p);
|
|
|
|
canvas->restore();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
//canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
test_fade(canvas);
|
|
|
|
return;
|
|
|
|
|
|
|
|
// canvas->setDrawFilter(new RedFilter)->unref();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect r;
|
|
|
|
SkPaint p;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(SkIntToScalar(220), SkIntToScalar(20));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(true);
|
|
|
|
r.set(SkIntToScalar(20), SkIntToScalar(20),
|
|
|
|
SkIntToScalar(220), SkIntToScalar(120));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setColor(SK_ColorBLUE);
|
|
|
|
// p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(8), SkBlurMaskFilter::kNormal_BlurStyle))->unref();
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
p.setMaskFilter(NULL);
|
|
|
|
|
|
|
|
SkRect bounds = r;
|
|
|
|
bounds.fBottom = bounds.centerY();
|
|
|
|
canvas->saveLayer(&bounds, NULL, SkCanvas::kARGB_NoClipLayer_SaveFlag);
|
|
|
|
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
canvas->drawOval(r, p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x80);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kDstIn_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawRect(bounds, p);
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
|
|
|
|
this->inval(NULL);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual bool onClick(Click* click) {
|
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
virtual bool handleKey(SkKey key) {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new LayersView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|
|
|
|
|