2011-12-21 14:27:14 +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.
|
|
|
|
*/
|
|
|
|
|
2018-03-13 18:41:10 +00:00
|
|
|
#include "SkCanvasPriv.h"
|
2016-11-12 14:06:55 +00:00
|
|
|
#include "SkMakeUnique.h"
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "ToolUtils.h"
|
|
|
|
#include "gm.h"
|
2011-12-21 14:27:14 +00:00
|
|
|
|
2016-09-06 16:06:18 +00:00
|
|
|
static void do_draw(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2016-09-06 16:06:18 +00:00
|
|
|
paint.setColor(0x800000FF);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-13 18:41:10 +00:00
|
|
|
* Exercise SkCanvasPriv::kDontClipToLayer_SaveLayerFlag flag, which does not limit the clip to the
|
2016-09-06 16:06:18 +00:00
|
|
|
* layer's bounds. Thus when a draw occurs, it can (depending on "where" it is) draw into the layer
|
|
|
|
* and/or draw onto the surrounding portions of the canvas, or both.
|
|
|
|
*
|
|
|
|
* This GM has a 100x100 rectangle (r), which its going to draw. However first it creates a layer
|
|
|
|
* with this flag covering 1/2 of the rectangle (upper half). Then it draws the rect in SRC mode.
|
|
|
|
*
|
|
|
|
* The portion of the draw that intersects the layer should see the SRC draw, apply it to the layer
|
|
|
|
* and then during restore, it will SRC_OVER that layer onto the canvas (SRC_OVER since the layer
|
|
|
|
* has no paint, so it gets the default xfermode during restore).
|
|
|
|
*
|
2016-09-06 21:57:00 +00:00
|
|
|
* Note: when we talk about drawing directly into the "canvas", in fact we are drawing into an
|
|
|
|
* "outer" layer we created (filled with red). This is a testing detail, so that our final
|
|
|
|
* output image is itself opaque, otherwise we make it harder to view the GM as a PNG.
|
|
|
|
*
|
2016-09-06 16:06:18 +00:00
|
|
|
* The portion of the draw below the layer draws directly into the canvas. Since it is in SRC mode,
|
2016-09-06 21:57:00 +00:00
|
|
|
* it will write 0x80 to the canvas' alpha, overwriting the "red", which then gets blended with
|
|
|
|
* the GM's white background.
|
|
|
|
*
|
|
|
|
* The portion in the layer, will end up SRC_OVERing the 0x80 layer pixels onto the canvas' red
|
|
|
|
* pixels, making magenta.
|
2016-09-06 16:06:18 +00:00
|
|
|
*
|
2016-09-06 21:57:00 +00:00
|
|
|
* Thus the expected result is the upper half to be magenta 0xFF7F0080, and the lower half to be
|
|
|
|
* light blue 0xFF7F7FFF.
|
2016-09-06 16:06:18 +00:00
|
|
|
*/
|
|
|
|
DEF_SIMPLE_GM(dont_clip_to_layer, canvas, 120, 120) {
|
2016-09-06 21:57:00 +00:00
|
|
|
const SkRect r { 10, 10, 110, 110 };
|
|
|
|
|
|
|
|
// Wrap the entire test inside a red layer, so we don't punch the actual gm's alpha with
|
|
|
|
// kSrc_Mode, which makes it hard to view (we like our GMs to have opaque pixels).
|
|
|
|
canvas->saveLayer(&r, nullptr);
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
|
2017-02-14 15:12:34 +00:00
|
|
|
SkRect r0 = { 20, 20, 100, 55 };
|
|
|
|
SkRect r1 = { 20, 65, 100, 100 };
|
2016-09-06 16:06:18 +00:00
|
|
|
|
|
|
|
SkCanvas::SaveLayerRec rec;
|
|
|
|
rec.fPaint = nullptr;
|
|
|
|
rec.fBounds = &r0;
|
|
|
|
rec.fBackdrop = nullptr;
|
2018-03-13 18:41:10 +00:00
|
|
|
rec.fSaveLayerFlags = SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
|
2016-09-06 16:06:18 +00:00
|
|
|
canvas->saveLayer(rec);
|
2017-02-14 15:12:34 +00:00
|
|
|
rec.fBounds = &r1;
|
|
|
|
canvas->saveLayer(rec);
|
2016-09-06 16:06:18 +00:00
|
|
|
do_draw(canvas, r);
|
|
|
|
canvas->restore();
|
2017-04-24 17:59:54 +00:00
|
|
|
canvas->restore();
|
2016-09-06 21:57:00 +00:00
|
|
|
|
|
|
|
canvas->restore(); // red-layer
|
2016-09-06 16:06:18 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 14:27:14 +00:00
|
|
|
/** Draw a 2px border around the target, then red behind the target;
|
|
|
|
set the clip to match the target, then draw >> the target in blue.
|
|
|
|
*/
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
|
2011-12-21 14:27:14 +00:00
|
|
|
SkPaint borderPaint;
|
|
|
|
borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
|
|
|
|
borderPaint.setAntiAlias(true);
|
|
|
|
SkPaint backgroundPaint;
|
|
|
|
backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
|
|
|
|
backgroundPaint.setAntiAlias(true);
|
|
|
|
SkPaint foregroundPaint;
|
|
|
|
foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
|
|
|
|
foregroundPaint.setAntiAlias(true);
|
|
|
|
|
|
|
|
canvas->save();
|
2011-12-28 19:40:48 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
|
2011-12-21 14:27:14 +00:00
|
|
|
canvas->drawRect(target, borderPaint);
|
2011-12-28 19:40:48 +00:00
|
|
|
target.inset(SkIntToScalar(2), SkIntToScalar(2));
|
2011-12-21 14:27:14 +00:00
|
|
|
canvas->drawRect(target, backgroundPaint);
|
2016-09-21 18:15:07 +00:00
|
|
|
canvas->clipRect(target, true);
|
2011-12-28 19:40:48 +00:00
|
|
|
target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
|
2011-12-21 14:27:14 +00:00
|
|
|
canvas->drawRect(target, foregroundPaint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void draw_square(SkCanvas* canvas, int x, int y) {
|
2011-12-21 14:27:14 +00:00
|
|
|
SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
|
|
|
|
draw(canvas, target, x, y);
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void draw_column(SkCanvas* canvas, int x, int y) {
|
2011-12-21 14:27:14 +00:00
|
|
|
SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
|
|
|
|
draw(canvas, target, x, y);
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void draw_bar(SkCanvas* canvas, int x, int y) {
|
2011-12-21 14:27:14 +00:00
|
|
|
SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
|
|
|
|
draw(canvas, target, x, y);
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
static void draw_rect_tests(SkCanvas* canvas) {
|
2011-12-21 14:27:14 +00:00
|
|
|
draw_square(canvas, 10, 10);
|
|
|
|
draw_column(canvas, 30, 10);
|
|
|
|
draw_bar(canvas, 10, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test a set of clipping problems discovered while writing blitAntiRect,
|
|
|
|
and test all the code paths through the clipping blitters.
|
|
|
|
Each region should show as a blue center surrounded by a 2px green
|
|
|
|
border, with no red.
|
|
|
|
*/
|
2019-01-23 15:40:02 +00:00
|
|
|
DEF_SIMPLE_GM(aaclip, canvas, 240, 120) {
|
2011-12-21 14:27:14 +00:00
|
|
|
// Initial pixel-boundary-aligned draw
|
|
|
|
draw_rect_tests(canvas);
|
|
|
|
|
|
|
|
// Repeat 4x with .2, .4, .6, .8 px offsets
|
|
|
|
canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
|
2011-12-28 19:40:48 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2011-12-21 14:27:14 +00:00
|
|
|
draw_rect_tests(canvas);
|
|
|
|
|
|
|
|
canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
|
2011-12-28 19:40:48 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2011-12-21 14:27:14 +00:00
|
|
|
draw_rect_tests(canvas);
|
|
|
|
|
|
|
|
canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
|
2011-12-28 19:40:48 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2011-12-21 14:27:14 +00:00
|
|
|
draw_rect_tests(canvas);
|
|
|
|
|
|
|
|
canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
|
2011-12-28 19:40:48 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2011-12-21 14:27:14 +00:00
|
|
|
draw_rect_tests(canvas);
|
2019-01-23 15:40:02 +00:00
|
|
|
}
|
2014-04-22 20:24:33 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_MAC
|
|
|
|
|
2016-11-12 14:06:55 +00:00
|
|
|
static std::unique_ptr<SkCanvas> make_canvas(const SkBitmap& bm) {
|
2014-04-22 20:24:33 +00:00
|
|
|
const SkImageInfo& info = bm.info();
|
|
|
|
if (info.bytesPerPixel() == 4) {
|
2016-11-12 14:06:55 +00:00
|
|
|
return SkCanvas::MakeRasterDirectN32(info.width(), info.height(),
|
|
|
|
(SkPMColor*)bm.getPixels(),
|
|
|
|
bm.rowBytes());
|
2014-04-22 20:24:33 +00:00
|
|
|
} else {
|
2016-11-12 14:06:55 +00:00
|
|
|
return skstd::make_unique<SkCanvas>(bm);
|
2014-04-22 20:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkCGUtils.h"
|
|
|
|
static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(info);
|
|
|
|
|
|
|
|
if (info.isOpaque()) {
|
|
|
|
bm.eraseColor(SK_ColorGREEN);
|
|
|
|
} else {
|
|
|
|
bm.eraseColor(0);
|
|
|
|
}
|
2014-04-23 03:04:11 +00:00
|
|
|
|
2014-04-22 20:24:33 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
2016-11-12 14:06:55 +00:00
|
|
|
make_canvas(bm)->drawCircle(50, 50, 49, paint);
|
2014-04-22 20:24:33 +00:00
|
|
|
canvas->drawBitmap(bm, 10, 10);
|
2014-04-23 03:04:11 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
CGImageRef image = SkCreateCGImageRefWithColorspace(bm, nullptr);
|
2014-04-23 03:04:11 +00:00
|
|
|
|
2014-04-22 20:24:33 +00:00
|
|
|
SkBitmap bm2;
|
|
|
|
SkCreateBitmapFromCGImage(&bm2, image);
|
|
|
|
canvas->drawBitmap(bm2, 10, 120);
|
2016-12-21 17:01:12 +00:00
|
|
|
canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
|
|
|
|
|
|
|
|
CGImageRelease(image);
|
2014-04-22 20:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:40:02 +00:00
|
|
|
DEF_SIMPLE_GM(cgimage, canvas, 800, 250) {
|
2014-04-22 20:24:33 +00:00
|
|
|
const struct {
|
|
|
|
SkColorType fCT;
|
|
|
|
SkAlphaType fAT;
|
|
|
|
} rec[] = {
|
|
|
|
{ kRGB_565_SkColorType, kOpaque_SkAlphaType },
|
|
|
|
|
|
|
|
{ kRGBA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
|
|
|
|
{ kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
|
|
|
|
|
|
|
|
{ kBGRA_8888_SkColorType, kPremul_SkAlphaType },
|
|
|
|
{ kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
|
|
|
|
{ kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
|
|
|
|
};
|
2014-04-23 03:04:11 +00:00
|
|
|
|
2014-04-22 20:24:33 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
|
|
|
|
SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
|
|
|
|
test_image(canvas, info);
|
|
|
|
canvas->translate(info.width() + 10, 0);
|
|
|
|
}
|
2019-01-23 15:40:02 +00:00
|
|
|
}
|
2014-04-23 03:04:11 +00:00
|
|
|
|
2014-04-22 20:24:33 +00:00
|
|
|
#endif
|
2015-04-23 00:38:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-07 13:29:00 +00:00
|
|
|
// https://bug.skia.org/3716
|
2015-04-23 00:38:23 +00:00
|
|
|
class ClipCubicGM : public skiagm::GM {
|
|
|
|
const SkScalar W = 100;
|
|
|
|
const SkScalar H = 240;
|
|
|
|
|
|
|
|
SkPath fVPath, fHPath;
|
|
|
|
public:
|
|
|
|
ClipCubicGM() {
|
|
|
|
fVPath.moveTo(W, 0);
|
|
|
|
fVPath.cubicTo(W, H-10, 0, 10, 0, H);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-23 00:38:23 +00:00
|
|
|
SkMatrix pivot;
|
|
|
|
pivot.setRotate(90, W/2, H/2);
|
|
|
|
fVPath.transform(pivot, &fHPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("clipcubic");
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-23 00:38:23 +00:00
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(400, 410);
|
|
|
|
}
|
|
|
|
|
|
|
|
void doDraw(SkCanvas* canvas, const SkPath& path) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
paint.setColor(0xFFCCCCCC);
|
2015-04-23 00:38:23 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-23 00:38:23 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawAndClip(SkCanvas* canvas, const SkPath& path, SkScalar dx, SkScalar dy) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeXYWH(0, H/4, W, H/2);
|
|
|
|
SkPaint paint;
|
2019-03-20 16:12:10 +00:00
|
|
|
paint.setColor(ToolUtils::color_to_565(0xFF8888FF));
|
2015-04-23 00:38:23 +00:00
|
|
|
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
this->doDraw(canvas, path);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-23 00:38:23 +00:00
|
|
|
canvas->translate(dx, dy);
|
|
|
|
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->clipRect(r);
|
|
|
|
this->doDraw(canvas, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->translate(80, 10);
|
|
|
|
this->drawAndClip(canvas, fVPath, 200, 0);
|
|
|
|
canvas->translate(0, 200);
|
|
|
|
this->drawAndClip(canvas, fHPath, 200, 0);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-04-23 00:38:23 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ClipCubicGM;)
|