2013-08-29 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkBitmapDevice.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCanvasStateUtils.h"
|
|
|
|
#include "SkDrawFilter.h"
|
2013-09-18 20:15:12 +00:00
|
|
|
#include "SkError.h"
|
2013-08-29 20:20:40 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkRRect.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkRect.h"
|
|
|
|
#include "Test.h"
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
static void test_complex_layers(skiatest::Reporter* reporter) {
|
2014-03-10 19:47:58 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2013-08-29 20:20:40 +00:00
|
|
|
const int WIDTH = 400;
|
|
|
|
const int HEIGHT = 400;
|
|
|
|
const int SPACER = 10;
|
|
|
|
|
2013-08-29 20:36:22 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(SkIntToScalar(SPACER), SkIntToScalar(SPACER),
|
|
|
|
SkIntToScalar(WIDTH-(2*SPACER)),
|
|
|
|
SkIntToScalar((HEIGHT-(2*SPACER)) / 7));
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2014-02-13 22:00:04 +00:00
|
|
|
const SkColorType colorTypes[] = {
|
|
|
|
kRGB_565_SkColorType, kPMColor_SkColorType
|
2013-08-29 20:20:40 +00:00
|
|
|
};
|
2014-02-13 22:00:04 +00:00
|
|
|
const int configCount = sizeof(colorTypes) / sizeof(SkBitmap::Config);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
const int layerAlpha[] = { 255, 255, 0 };
|
|
|
|
const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag,
|
|
|
|
SkCanvas::kARGB_ClipLayer_SaveFlag,
|
|
|
|
SkCanvas::kARGB_NoClipLayer_SaveFlag
|
|
|
|
};
|
|
|
|
REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
|
|
|
|
const int layerCombinations = sizeof(layerAlpha) / sizeof(int);
|
|
|
|
|
|
|
|
for (int i = 0; i < configCount; ++i) {
|
|
|
|
SkBitmap bitmaps[2];
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
2014-02-13 22:00:04 +00:00
|
|
|
bitmaps[j].allocPixels(SkImageInfo::Make(WIDTH, HEIGHT,
|
|
|
|
colorTypes[i],
|
|
|
|
kPremul_SkAlphaType));
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bitmaps[j]);
|
|
|
|
|
|
|
|
canvas.drawColor(SK_ColorRED);
|
|
|
|
|
|
|
|
for (int k = 0; k < layerCombinations; ++k) {
|
|
|
|
// draw a rect within the layer's bounds and again outside the layer's bounds
|
|
|
|
canvas.saveLayerAlpha(&rect, layerAlpha[k], flags[k]);
|
|
|
|
|
|
|
|
SkCanvasState* state = NULL;
|
|
|
|
SkCanvas* tmpCanvas = NULL;
|
|
|
|
if (j) {
|
|
|
|
state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, state);
|
|
|
|
tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
|
|
|
|
REPORTER_ASSERT(reporter, tmpCanvas);
|
|
|
|
} else {
|
|
|
|
tmpCanvas = SkRef(&canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint bluePaint;
|
|
|
|
bluePaint.setColor(SK_ColorBLUE);
|
|
|
|
bluePaint.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
|
|
|
tmpCanvas->drawRect(rect, bluePaint);
|
|
|
|
tmpCanvas->translate(0, rect.height() + SPACER);
|
|
|
|
tmpCanvas->drawRect(rect, bluePaint);
|
|
|
|
|
|
|
|
tmpCanvas->unref();
|
|
|
|
SkCanvasStateUtils::ReleaseCanvasState(state);
|
|
|
|
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
// translate the canvas for the next iteration
|
|
|
|
canvas.translate(0, 2*(rect.height() + SPACER));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we memcmp the two bitmaps
|
|
|
|
REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
|
|
|
|
bitmaps[1].getPixels(),
|
|
|
|
bitmaps[0].getSize()));
|
|
|
|
}
|
2014-03-10 19:47:58 +00:00
|
|
|
#endif
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-04 17:16:00 +00:00
|
|
|
static void test_complex_clips(skiatest::Reporter* reporter) {
|
2014-03-10 19:47:58 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2013-09-04 17:16:00 +00:00
|
|
|
const int WIDTH = 400;
|
|
|
|
const int HEIGHT = 400;
|
2013-09-04 18:20:30 +00:00
|
|
|
const int SPACER = 10;
|
2013-09-04 17:16:00 +00:00
|
|
|
|
2013-09-04 18:20:30 +00:00
|
|
|
SkIRect layerRect = SkIRect::MakeWH(WIDTH, HEIGHT / 4);
|
2013-09-04 17:16:00 +00:00
|
|
|
layerRect.inset(2*SPACER, 2*SPACER);
|
|
|
|
|
2013-09-04 18:20:30 +00:00
|
|
|
SkIRect clipRect = layerRect;
|
2013-09-04 17:16:00 +00:00
|
|
|
clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
|
|
|
|
clipRect.outset(SPACER, SPACER);
|
|
|
|
|
2013-09-04 18:20:30 +00:00
|
|
|
SkIRect regionBounds = clipRect;
|
2013-09-04 17:16:00 +00:00
|
|
|
regionBounds.offset(clipRect.width() + (2*SPACER), 0);
|
|
|
|
|
|
|
|
SkIRect regionInterior = regionBounds;
|
|
|
|
regionInterior.inset(SPACER*3, SPACER*3);
|
|
|
|
|
|
|
|
SkRegion clipRegion;
|
|
|
|
clipRegion.setRect(regionBounds);
|
|
|
|
clipRegion.op(regionInterior, SkRegion::kDifference_Op);
|
|
|
|
|
|
|
|
|
|
|
|
const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
|
|
|
|
SkRegion::kIntersect_Op,
|
|
|
|
SkRegion::kReplace_Op,
|
|
|
|
};
|
|
|
|
const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag,
|
|
|
|
SkCanvas::kARGB_ClipLayer_SaveFlag,
|
|
|
|
SkCanvas::kARGB_NoClipLayer_SaveFlag,
|
|
|
|
};
|
|
|
|
REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
|
|
|
|
const int layerCombinations = sizeof(flags) / sizeof(SkCanvas::SaveFlags);
|
|
|
|
|
|
|
|
SkBitmap bitmaps[2];
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2014-02-13 22:00:04 +00:00
|
|
|
bitmaps[i].allocN32Pixels(WIDTH, HEIGHT);
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bitmaps[i]);
|
|
|
|
|
|
|
|
canvas.drawColor(SK_ColorRED);
|
|
|
|
|
|
|
|
SkRegion localRegion = clipRegion;
|
|
|
|
|
|
|
|
for (int j = 0; j < layerCombinations; ++j) {
|
2013-09-04 18:20:30 +00:00
|
|
|
SkRect layerBounds = SkRect::Make(layerRect);
|
|
|
|
canvas.saveLayerAlpha(&layerBounds, 128, flags[j]);
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
SkCanvasState* state = NULL;
|
|
|
|
SkCanvas* tmpCanvas = NULL;
|
|
|
|
if (i) {
|
|
|
|
state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, state);
|
|
|
|
tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
|
|
|
|
REPORTER_ASSERT(reporter, tmpCanvas);
|
|
|
|
} else {
|
|
|
|
tmpCanvas = SkRef(&canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpCanvas->save();
|
2013-09-04 18:20:30 +00:00
|
|
|
tmpCanvas->clipRect(SkRect::Make(clipRect), clipOps[j]);
|
2013-09-04 17:16:00 +00:00
|
|
|
tmpCanvas->drawColor(SK_ColorBLUE);
|
|
|
|
tmpCanvas->restore();
|
|
|
|
|
|
|
|
tmpCanvas->clipRegion(localRegion, clipOps[j]);
|
|
|
|
tmpCanvas->drawColor(SK_ColorBLUE);
|
|
|
|
|
|
|
|
tmpCanvas->unref();
|
|
|
|
SkCanvasStateUtils::ReleaseCanvasState(state);
|
|
|
|
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
// translate the canvas and region for the next iteration
|
2013-09-04 18:20:30 +00:00
|
|
|
canvas.translate(0, SkIntToScalar(2*(layerRect.height() + (SPACER))));
|
2013-09-04 17:16:00 +00:00
|
|
|
localRegion.translate(0, 2*(layerRect.height() + SPACER));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we memcmp the two bitmaps
|
|
|
|
REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
|
|
|
|
bitmaps[1].getPixels(),
|
|
|
|
bitmaps[0].getSize()));
|
2014-03-10 19:47:58 +00:00
|
|
|
#endif
|
2013-09-04 17:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-08-29 20:20:40 +00:00
|
|
|
class TestDrawFilter : public SkDrawFilter {
|
|
|
|
public:
|
|
|
|
virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_draw_filters(skiatest::Reporter* reporter) {
|
|
|
|
TestDrawFilter drawFilter;
|
2014-02-16 00:59:25 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(10, 10);
|
|
|
|
SkCanvas canvas(bitmap);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
canvas.setDrawFilter(&drawFilter);
|
|
|
|
|
|
|
|
SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, state);
|
|
|
|
SkCanvas* tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
|
|
|
|
REPORTER_ASSERT(reporter, tmpCanvas);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, NULL != canvas.getDrawFilter());
|
|
|
|
REPORTER_ASSERT(reporter, NULL == tmpCanvas->getDrawFilter());
|
|
|
|
|
|
|
|
tmpCanvas->unref();
|
|
|
|
SkCanvasStateUtils::ReleaseCanvasState(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-18 20:15:12 +00:00
|
|
|
// we need this function to prevent SkError from printing to stdout
|
|
|
|
static void error_callback(SkError code, void* ctx) {}
|
|
|
|
|
2013-08-29 20:20:40 +00:00
|
|
|
static void test_soft_clips(skiatest::Reporter* reporter) {
|
2014-02-16 00:59:25 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(10, 10);
|
|
|
|
SkCanvas canvas(bitmap);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
SkRRect roundRect;
|
|
|
|
roundRect.setOval(SkRect::MakeWH(5, 5));
|
|
|
|
|
|
|
|
canvas.clipRRect(roundRect, SkRegion::kIntersect_Op, true);
|
|
|
|
|
2013-09-18 20:15:12 +00:00
|
|
|
SkSetErrorCallback(error_callback, NULL);
|
|
|
|
|
2013-08-29 20:20:40 +00:00
|
|
|
SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, !state);
|
2013-09-18 20:15:12 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, kInvalidOperation_SkError == SkGetLastError());
|
|
|
|
SkClearLastError();
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 15:16:42 +00:00
|
|
|
static void test_saveLayer_clip(skiatest::Reporter* reporter) {
|
2014-03-10 19:47:58 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2014-02-24 15:16:42 +00:00
|
|
|
const int WIDTH = 100;
|
|
|
|
const int HEIGHT = 100;
|
|
|
|
const int LAYER_WIDTH = 50;
|
|
|
|
const int LAYER_HEIGHT = 50;
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(WIDTH, HEIGHT);
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
SkRect bounds = SkRect::MakeWH(SkIntToScalar(LAYER_WIDTH), SkIntToScalar(LAYER_HEIGHT));
|
|
|
|
canvas.clipRect(SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)));
|
|
|
|
|
|
|
|
// Check that saveLayer without the kClipToLayer_SaveFlag leaves the
|
|
|
|
// clip stack unchanged.
|
|
|
|
canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_NoClipLayer_SaveFlag);
|
|
|
|
SkRect clipStackBounds;
|
|
|
|
SkClipStack::BoundsType boundsType;
|
|
|
|
canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
|
|
|
|
REPORTER_ASSERT(reporter, clipStackBounds.width() == WIDTH);
|
|
|
|
REPORTER_ASSERT(reporter, clipStackBounds.height() == HEIGHT);
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
// Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
|
|
|
|
// stack to the layer bounds.
|
|
|
|
canvas.saveLayer(&bounds, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
|
|
|
|
canvas.getClipStack()->getBounds(&clipStackBounds, &boundsType);
|
|
|
|
REPORTER_ASSERT(reporter, clipStackBounds.width() == LAYER_WIDTH);
|
|
|
|
REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT);
|
|
|
|
|
|
|
|
canvas.restore();
|
2014-03-10 19:47:58 +00:00
|
|
|
#endif
|
2014-02-24 15:16:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(CanvasState, reporter) {
|
2013-08-29 20:20:40 +00:00
|
|
|
test_complex_layers(reporter);
|
2013-09-04 17:16:00 +00:00
|
|
|
test_complex_clips(reporter);
|
2013-08-29 20:20:40 +00:00
|
|
|
test_draw_filters(reporter);
|
|
|
|
test_soft_clips(reporter);
|
2014-02-24 15:16:42 +00:00
|
|
|
test_saveLayer_clip(reporter);
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|