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.
|
|
|
|
*/
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
#include "CanvasStateHelpers.h"
|
2017-05-03 19:16:58 +00:00
|
|
|
#include "SkBitmap.h"
|
2018-03-13 18:41:10 +00:00
|
|
|
#include "SkCanvasPriv.h"
|
2018-03-12 20:04:31 +00:00
|
|
|
#include "SkCanvasStateUtils.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkClipOpPriv.h"
|
|
|
|
#include "SkColor.h"
|
2014-07-22 19:38:55 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkImageInfo.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"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkTDArray.h"
|
2016-01-20 19:17:39 +00:00
|
|
|
#include "SkTLazy.h"
|
2018-03-12 17:46:21 +00:00
|
|
|
#include "SkTypes.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2018-03-12 17:46:21 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class SkCanvasState;
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
// dlopen and the library flag are only used for tests which require this flag.
|
2014-03-10 19:47:58 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2014-07-22 19:38:55 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
DEFINE_string(library, "", "Support library to use for CanvasState test. If empty (the default), "
|
|
|
|
"the test will be run without crossing a library boundary. Otherwise, "
|
|
|
|
"it is expected to be a full path to a shared library file, which will"
|
|
|
|
" be dynamically loaded. Functions from the library will be called to "
|
|
|
|
"test SkCanvasState. Instructions for generating the library are in "
|
|
|
|
"gyp/canvas_state_lib.gyp");
|
|
|
|
|
|
|
|
|
|
|
|
// This class calls dlopen on the library passed in to the command line flag library, and handles
|
|
|
|
// calling dlclose when it goes out of scope.
|
|
|
|
class OpenLibResult {
|
|
|
|
public:
|
|
|
|
// If the flag library was passed to this run of the test, attempt to open it using dlopen and
|
|
|
|
// report whether it succeeded.
|
|
|
|
OpenLibResult(skiatest::Reporter* reporter) {
|
|
|
|
if (FLAGS_library.count() == 1) {
|
|
|
|
fHandle = dlopen(FLAGS_library[0], RTLD_LAZY | RTLD_LOCAL);
|
2018-01-29 14:50:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, fHandle != nullptr, "Failed to open library!");
|
2014-07-22 19:38:55 +00:00
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
fHandle = nullptr;
|
2014-07-22 19:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically call dlclose when going out of scope.
|
|
|
|
~OpenLibResult() {
|
|
|
|
if (fHandle) {
|
|
|
|
dlclose(fHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pointer to the shared library object.
|
|
|
|
void* handle() { return fHandle; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* fHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(CanvasState_test_complex_layers, reporter) {
|
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[] = {
|
2014-04-11 17:15:40 +00:00
|
|
|
kRGB_565_SkColorType, kN32_SkColorType
|
2013-08-29 20:20:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int layerAlpha[] = { 255, 255, 0 };
|
2016-01-20 19:17:39 +00:00
|
|
|
const SkCanvas::SaveLayerFlags flags[] = {
|
2018-03-13 18:41:10 +00:00
|
|
|
static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
|
2016-01-20 19:17:39 +00:00
|
|
|
0,
|
2018-03-13 18:41:10 +00:00
|
|
|
static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
|
2013-08-29 20:20:40 +00:00
|
|
|
};
|
|
|
|
REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags));
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
bool (*drawFn)(SkCanvasState* state, float l, float t,
|
|
|
|
float r, float b, int32_t s);
|
|
|
|
|
|
|
|
OpenLibResult openLibResult(reporter);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (openLibResult.handle() != nullptr) {
|
2014-07-22 19:38:55 +00:00
|
|
|
*(void**) (&drawFn) = dlsym(openLibResult.handle(),
|
|
|
|
"complex_layers_draw_from_canvas_state");
|
|
|
|
} else {
|
|
|
|
drawFn = complex_layers_draw_from_canvas_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, drawFn);
|
|
|
|
if (!drawFn) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2014-07-09 14:26:40 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) {
|
2013-08-29 20:20:40 +00:00
|
|
|
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);
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(layerAlpha); ++k) {
|
2016-01-20 19:17:39 +00:00
|
|
|
SkTLazy<SkPaint> paint;
|
|
|
|
if (layerAlpha[k] != 0xFF) {
|
|
|
|
paint.init()->setAlpha(layerAlpha[k]);
|
|
|
|
}
|
|
|
|
|
2013-08-29 20:20:40 +00:00
|
|
|
// draw a rect within the layer's bounds and again outside the layer's bounds
|
2016-01-20 19:17:39 +00:00
|
|
|
canvas.saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull(), flags[k]));
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
if (j) {
|
2014-07-22 19:38:55 +00:00
|
|
|
// Capture from the first Skia.
|
|
|
|
SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
2013-08-29 20:20:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, state);
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
// And draw to it in the second Skia.
|
|
|
|
bool success = complex_layers_draw_from_canvas_state(state,
|
|
|
|
rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER);
|
|
|
|
REPORTER_ASSERT(reporter, success);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
// And release it in the *first* Skia.
|
|
|
|
SkCanvasStateUtils::ReleaseCanvasState(state);
|
|
|
|
} else {
|
|
|
|
// Draw in the first Skia.
|
|
|
|
complex_layers_draw(&canvas, rect.fLeft, rect.fTop,
|
|
|
|
rect.fRight, rect.fBottom, SPACER);
|
|
|
|
}
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
// translate the canvas for the next iteration
|
|
|
|
canvas.translate(0, 2*(rect.height() + SPACER));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we memcmp the two bitmaps
|
2017-10-09 17:09:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize());
|
2013-08-29 20:20:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
|
|
|
|
bitmaps[1].getPixels(),
|
2017-10-09 17:09:55 +00:00
|
|
|
bitmaps[0].computeByteSize()));
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 19:38:55 +00:00
|
|
|
#endif
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-10 19:47:58 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2014-07-22 19:38:55 +00:00
|
|
|
DEF_TEST(CanvasState_test_complex_clips, reporter) {
|
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,
|
|
|
|
};
|
2016-01-20 19:17:39 +00:00
|
|
|
const SkCanvas::SaveLayerFlags flags[] = {
|
2018-03-13 18:41:10 +00:00
|
|
|
static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
|
2016-01-20 19:17:39 +00:00
|
|
|
0,
|
2018-03-13 18:41:10 +00:00
|
|
|
static_cast<SkCanvas::SaveLayerFlags>(SkCanvasPriv::kDontClipToLayer_SaveLayerFlag),
|
2013-09-04 17:16:00 +00:00
|
|
|
};
|
|
|
|
REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
bool (*drawFn)(SkCanvasState* state, int32_t l, int32_t t,
|
|
|
|
int32_t r, int32_t b, int32_t clipOp,
|
|
|
|
int32_t regionRects, int32_t* rectCoords);
|
|
|
|
|
|
|
|
OpenLibResult openLibResult(reporter);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (openLibResult.handle() != nullptr) {
|
2014-07-22 19:38:55 +00:00
|
|
|
*(void**) (&drawFn) = dlsym(openLibResult.handle(),
|
|
|
|
"complex_clips_draw_from_canvas_state");
|
|
|
|
} else {
|
|
|
|
drawFn = complex_clips_draw_from_canvas_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, drawFn);
|
|
|
|
if (!drawFn) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-01-20 19:17:39 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(128);
|
2014-07-22 19:38:55 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(flags); ++j) {
|
2013-09-04 18:20:30 +00:00
|
|
|
SkRect layerBounds = SkRect::Make(layerRect);
|
2016-01-20 19:17:39 +00:00
|
|
|
canvas.saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint, flags[j]));
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
if (i) {
|
2014-07-22 19:38:55 +00:00
|
|
|
SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
2013-09-04 17:16:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, state);
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
SkRegion::Iterator iter(localRegion);
|
|
|
|
SkTDArray<int32_t> rectCoords;
|
|
|
|
for (; !iter.done(); iter.next()) {
|
|
|
|
const SkIRect& rect = iter.rect();
|
|
|
|
*rectCoords.append() = rect.fLeft;
|
|
|
|
*rectCoords.append() = rect.fTop;
|
|
|
|
*rectCoords.append() = rect.fRight;
|
|
|
|
*rectCoords.append() = rect.fBottom;
|
|
|
|
}
|
|
|
|
bool success = drawFn(state, clipRect.fLeft, clipRect.fTop,
|
|
|
|
clipRect.fRight, clipRect.fBottom, clipOps[j],
|
|
|
|
rectCoords.count() / 4, rectCoords.begin());
|
|
|
|
REPORTER_ASSERT(reporter, success);
|
2013-09-04 17:16:00 +00:00
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
SkCanvasStateUtils::ReleaseCanvasState(state);
|
|
|
|
} else {
|
|
|
|
complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop,
|
|
|
|
clipRect.fRight, clipRect.fBottom, clipOps[j],
|
|
|
|
localRegion);
|
|
|
|
}
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
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
|
2017-10-09 17:09:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, bitmaps[0].computeByteSize() == bitmaps[1].computeByteSize());
|
2013-09-04 17:16:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
|
|
|
|
bitmaps[1].getPixels(),
|
2017-10-09 17:09:55 +00:00
|
|
|
bitmaps[0].computeByteSize()));
|
2013-09-04 17:16:00 +00:00
|
|
|
}
|
2014-07-22 19:38:55 +00:00
|
|
|
#endif
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
DEF_TEST(CanvasState_test_soft_clips, 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));
|
|
|
|
|
2016-12-09 14:00:50 +00:00
|
|
|
canvas.clipRRect(roundRect, kIntersect_SkClipOp, true);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
|
|
|
|
REPORTER_ASSERT(reporter, !state);
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
DEF_TEST(CanvasState_test_saveLayer_clip, reporter) {
|
2017-01-19 16:36:41 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
|
2018-03-13 18:41:10 +00:00
|
|
|
static_assert(SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag ==
|
|
|
|
SkCanvasPriv::kDontClipToLayer_SaveLayerFlag, "");
|
2017-01-19 16:36:41 +00:00
|
|
|
#endif
|
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)));
|
|
|
|
|
2017-01-19 16:36:41 +00:00
|
|
|
SkIRect devClip;
|
|
|
|
// Check that saveLayer without the kClipToLayer_SaveFlag leaves the clip unchanged.
|
2018-03-13 18:41:10 +00:00
|
|
|
canvas.saveLayer(SkCanvas::SaveLayerRec(&bounds, nullptr,
|
|
|
|
(SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag));
|
2017-01-23 16:39:45 +00:00
|
|
|
devClip = canvas.getDeviceClipBounds();
|
2017-01-19 16:36:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isClipRect());
|
|
|
|
REPORTER_ASSERT(reporter, devClip.width() == WIDTH);
|
|
|
|
REPORTER_ASSERT(reporter, devClip.height() == HEIGHT);
|
2014-02-24 15:16:42 +00:00
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
// Check that saveLayer with the kClipToLayer_SaveFlag sets the clip
|
|
|
|
// stack to the layer bounds.
|
2016-01-20 19:17:39 +00:00
|
|
|
canvas.saveLayer(&bounds, nullptr);
|
2017-01-23 16:39:45 +00:00
|
|
|
devClip = canvas.getDeviceClipBounds();
|
2017-01-19 16:36:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, canvas.isClipRect());
|
|
|
|
REPORTER_ASSERT(reporter, devClip.width() == LAYER_WIDTH);
|
|
|
|
REPORTER_ASSERT(reporter, devClip.height() == LAYER_HEIGHT);
|
2014-02-24 15:16:42 +00:00
|
|
|
canvas.restore();
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|