2012-11-15 13:46:47 +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"
|
|
|
|
#include "SkCanvas.h"
|
2012-11-15 15:56:38 +00:00
|
|
|
#include "SkGradientShader.h"
|
2012-11-15 17:24:23 +00:00
|
|
|
#include "SkSurface.h"
|
2012-11-15 13:46:47 +00:00
|
|
|
|
2012-11-16 02:31:03 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#endif
|
2012-11-16 02:23:10 +00:00
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
#define W SkIntToScalar(80)
|
2012-11-15 13:46:47 +00:00
|
|
|
#define H SkIntToScalar(60)
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
typedef void (*PaintProc)(SkPaint*);
|
|
|
|
|
2012-11-16 02:23:10 +00:00
|
|
|
static void identity_paintproc(SkPaint* paint) {
|
|
|
|
paint->setShader(NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
static void gradient_paintproc(SkPaint* paint) {
|
|
|
|
const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
const SkPoint pts[] = { { 0, 0 }, { W, H } };
|
|
|
|
SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL,
|
|
|
|
SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kClamp_TileMode);
|
|
|
|
paint->setShader(s)->unref();
|
|
|
|
}
|
|
|
|
|
2012-11-15 13:46:47 +00:00
|
|
|
typedef void (*Proc)(SkCanvas*, const SkPaint&);
|
|
|
|
|
|
|
|
static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
p.setStrokeWidth(0);
|
|
|
|
canvas->drawLine(0, 0, W, H, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
p.setStrokeWidth(H/5);
|
|
|
|
canvas->drawLine(0, 0, W, H, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
canvas->drawRect(SkRect::MakeWH(W, H), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
canvas->drawOval(SkRect::MakeWH(W, H), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
p.setTextSize(H/4);
|
2012-11-15 15:56:38 +00:00
|
|
|
canvas->drawText("Hamburge", 8, 0, H*2/3, p);
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class SrcModeGM : public skiagm::GM {
|
|
|
|
SkPath fPath;
|
|
|
|
public:
|
|
|
|
SrcModeGM() {
|
2012-11-15 17:24:23 +00:00
|
|
|
this->setBGColor(SK_ColorBLACK);
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("srcmode");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
2012-11-15 15:56:38 +00:00
|
|
|
return SkISize::Make(640, 760);
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 17:24:23 +00:00
|
|
|
void drawContent(SkCanvas* canvas) {
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(0x80FF0000);
|
|
|
|
|
|
|
|
const Proc procs[] = {
|
|
|
|
draw_hair, draw_thick, draw_rect, draw_oval, draw_text
|
|
|
|
};
|
|
|
|
|
|
|
|
const SkXfermode::Mode modes[] = {
|
|
|
|
SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
|
|
|
|
};
|
|
|
|
|
2012-11-15 15:56:38 +00:00
|
|
|
const PaintProc paintProcs[] = {
|
|
|
|
identity_paintproc, gradient_paintproc
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int aa = 0; aa <= 1; ++aa) {
|
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
2012-11-15 13:46:47 +00:00
|
|
|
canvas->save();
|
2012-11-15 15:56:38 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
|
|
|
|
paintProcs[i](&paint);
|
|
|
|
for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
|
|
|
|
paint.setXfermodeMode(modes[x]);
|
|
|
|
canvas->save();
|
|
|
|
for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
|
|
|
|
procs[y](canvas, paint);
|
|
|
|
canvas->translate(0, H * 5 / 4);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(W * 5 / 4, 0);
|
|
|
|
}
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2012-11-15 15:56:38 +00:00
|
|
|
canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
|
2012-11-15 13:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 13:41:45 +00:00
|
|
|
static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size,
|
|
|
|
bool skipGPU) {
|
2012-11-15 17:24:23 +00:00
|
|
|
SkImage::Info info = {
|
|
|
|
size.width(),
|
|
|
|
size.height(),
|
|
|
|
SkImage::kPMColor_ColorType,
|
2013-09-20 19:33:52 +00:00
|
|
|
kPremul_SkAlphaType
|
2012-11-15 17:24:23 +00:00
|
|
|
};
|
2012-11-16 02:31:03 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2013-08-29 11:54:56 +00:00
|
|
|
SkBaseDevice* dev = canvas->getDevice();
|
2012-11-16 13:41:45 +00:00
|
|
|
if (!skipGPU && dev->accessRenderTarget()) {
|
2012-11-16 02:23:10 +00:00
|
|
|
SkGpuDevice* gd = (SkGpuDevice*)dev;
|
|
|
|
GrContext* ctx = gd->context();
|
|
|
|
return SkSurface::NewRenderTarget(ctx, info, 0);
|
|
|
|
}
|
2012-11-16 02:31:03 +00:00
|
|
|
#endif
|
2012-11-15 17:24:23 +00:00
|
|
|
return SkSurface::NewRaster(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
2012-11-16 13:41:45 +00:00
|
|
|
SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(),
|
|
|
|
this->isCanvasDeferred()));
|
2012-11-15 17:24:23 +00:00
|
|
|
surf->getCanvas()->drawColor(SK_ColorWHITE);
|
|
|
|
this->drawContent(surf->getCanvas());
|
|
|
|
surf->draw(canvas, 0, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2012-11-15 13:46:47 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM(return new SrcModeGM;)
|