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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-06 21:17:19 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2022-03-10 04:11:31 +00:00
|
|
|
#include "include/core/SkStream.h"
|
2019-09-03 17:23:43 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
|
|
|
#include "include/utils/SkCanvasStateUtils.h"
|
|
|
|
#include "src/core/SkCanvasPriv.h"
|
|
|
|
#include "src/core/SkTLazy.h"
|
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2018-03-12 17:46:21 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
class SkCanvasState;
|
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
// Uncomment to include tests of CanvasState across a library boundary. This will change how 'dm'
|
|
|
|
// is built so that the functions defined in CanvasStateHelpers do not exist inside 'dm', and are
|
|
|
|
// instead compiled as part of the 'canvas_state_lib' build target. This produces a shared library
|
|
|
|
// that must be passed to 'dm' using the --library flag when running.
|
|
|
|
// #define SK_TEST_CANVAS_STATE_CROSS_LIBRARY
|
|
|
|
|
|
|
|
// Must be included after SK_TEST_CANVAS_STATE_CROSS_LIBRARY is defined
|
|
|
|
#include "tests/CanvasStateHelpers.h"
|
|
|
|
|
|
|
|
// dlopen, the library flag and canvas state helpers are only used for tests which require this flag
|
|
|
|
#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
|
2014-07-22 19:38:55 +00:00
|
|
|
|
2019-03-21 16:31:36 +00:00
|
|
|
static DEFINE_string(library, "",
|
2020-12-04 17:58:35 +00:00
|
|
|
"Support library to use for CanvasState test. Must be provided when"
|
|
|
|
" SK_TEST_CANVAS_STATE_CROSS_LIBRARY to specify the dynamically loaded library"
|
|
|
|
" that receives the captured canvas state. Functions from the library will be"
|
|
|
|
" called to test SkCanvasState. The library is built from the canvas_state_lib"
|
|
|
|
" target");
|
2014-07-22 19:38:55 +00:00
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
#include "src/ports/SkOSLibrary.h"
|
2014-07-22 19:38:55 +00:00
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
// Automatically loads library passed to --library flag and closes it when it goes out of scope.
|
2014-07-22 19:38:55 +00:00
|
|
|
class OpenLibResult {
|
|
|
|
public:
|
|
|
|
OpenLibResult(skiatest::Reporter* reporter) {
|
|
|
|
if (FLAGS_library.count() == 1) {
|
2020-12-04 17:58:35 +00:00
|
|
|
fLibrary = SkLoadDynamicLibrary(FLAGS_library[0]);
|
|
|
|
REPORTER_ASSERT(reporter, fLibrary != nullptr, "Failed to open library!");
|
2014-07-22 19:38:55 +00:00
|
|
|
} else {
|
2020-12-04 17:58:35 +00:00
|
|
|
fLibrary = nullptr;
|
2014-07-22 19:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~OpenLibResult() {
|
2020-12-04 17:58:35 +00:00
|
|
|
if (fLibrary) {
|
|
|
|
SkFreeDynamicLibrary(fLibrary);
|
2014-07-22 19:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
// Load a function address from the library object, or null if the library had failed
|
|
|
|
void* procAddress(const char* funcName) {
|
|
|
|
if (fLibrary) {
|
|
|
|
return SkGetProcedureAddress(fLibrary, funcName);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-04 17:58:35 +00:00
|
|
|
void* fLibrary;
|
2014-07-22 19:38:55 +00:00
|
|
|
};
|
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
static void write_image(const SkImage* img, const char path[]) {
|
|
|
|
auto data = img->encodeToData();
|
|
|
|
SkFILEWStream(path).write(data->data(), data->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compare(skiatest::Reporter* reporter, SkImage* img0, SkImage* img1) {
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((false)) {
|
2019-09-03 17:23:43 +00:00
|
|
|
static int counter;
|
|
|
|
|
|
|
|
SkDebugf("---- counter %d\n", counter);
|
|
|
|
SkString name;
|
|
|
|
name.printf("no_capture_%d.png", counter);
|
|
|
|
write_image(img0, name.c_str());
|
|
|
|
name.printf("capture_%d.png", counter);
|
|
|
|
write_image(img1, name.c_str());
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPixmap pm[2];
|
|
|
|
REPORTER_ASSERT(reporter, img0->peekPixels(&pm[0]));
|
|
|
|
REPORTER_ASSERT(reporter, img1->peekPixels(&pm[1]));
|
|
|
|
// now we memcmp the two bitmaps
|
|
|
|
REPORTER_ASSERT(reporter, pm[0].computeByteSize() == pm[1].computeByteSize());
|
|
|
|
REPORTER_ASSERT(reporter, pm[0].rowBytes() == (size_t)pm[0].width() * pm[0].info().bytesPerPixel());
|
|
|
|
REPORTER_ASSERT(reporter, pm[1].rowBytes() == (size_t)pm[1].width() * pm[1].info().bytesPerPixel());
|
2020-12-04 17:58:35 +00:00
|
|
|
if (memcmp(pm[0].addr(0, 0), pm[1].addr(0, 0), pm[0].computeByteSize()) != 0) {
|
2019-09-03 17:23:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 19:38:55 +00:00
|
|
|
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 };
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
bool (*drawFn)(SkCanvasState* state, float l, float t,
|
|
|
|
float r, float b, int32_t s);
|
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
|
2014-07-22 19:38:55 +00:00
|
|
|
OpenLibResult openLibResult(reporter);
|
2020-12-04 17:58:35 +00:00
|
|
|
*(void**) (&drawFn) = openLibResult.procAddress("complex_layers_draw_from_canvas_state");
|
|
|
|
#else
|
|
|
|
drawFn = complex_layers_draw_from_canvas_state;
|
|
|
|
#endif
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
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) {
|
2019-09-03 17:23:43 +00:00
|
|
|
sk_sp<SkImage> images[2];
|
2013-08-29 20:20:40 +00:00
|
|
|
for (int j = 0; j < 2; ++j) {
|
2019-09-03 17:23:43 +00:00
|
|
|
auto surf = SkSurface::MakeRaster(SkImageInfo::Make(WIDTH, HEIGHT,
|
|
|
|
colorTypes[i],
|
|
|
|
kPremul_SkAlphaType));
|
|
|
|
SkCanvas* canvas = surf->getCanvas();
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
canvas->drawColor(SK_ColorRED);
|
2013-08-29 20:20:40 +00:00
|
|
|
|
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
|
2020-12-04 17:59:39 +00:00
|
|
|
canvas->saveLayer(SkCanvas::SaveLayerRec(&rect, paint.getMaybeNull()));
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
if (j) {
|
2014-07-22 19:38:55 +00:00
|
|
|
// Capture from the first Skia.
|
2019-09-03 17:23:43 +00:00
|
|
|
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.
|
2019-09-03 17:23:43 +00:00
|
|
|
complex_layers_draw(canvas, rect.fLeft, rect.fTop,
|
2014-07-22 19:38:55 +00:00
|
|
|
rect.fRight, rect.fBottom, SPACER);
|
|
|
|
}
|
2013-08-29 20:20:40 +00:00
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
canvas->restore();
|
2013-08-29 20:20:40 +00:00
|
|
|
|
|
|
|
// translate the canvas for the next iteration
|
2019-09-03 17:23:43 +00:00
|
|
|
canvas->translate(0, 2*(rect.height() + SPACER));
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
2019-09-03 17:23:43 +00:00
|
|
|
images[j] = surf->makeImageSnapshot();
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
compare(reporter, images[0].get(), images[1].get());
|
2013-08-29 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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,
|
2020-12-04 17:58:35 +00:00
|
|
|
SkRegion::kDifference_Op,
|
2013-09-04 17:16:00 +00:00
|
|
|
};
|
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);
|
|
|
|
|
2020-12-04 17:58:35 +00:00
|
|
|
#if defined(SK_TEST_CANVAS_STATE_CROSS_LIBRARY)
|
2014-07-22 19:38:55 +00:00
|
|
|
OpenLibResult openLibResult(reporter);
|
2020-12-04 17:58:35 +00:00
|
|
|
*(void**) (&drawFn) = openLibResult.procAddress("complex_clips_draw_from_canvas_state");
|
|
|
|
#else
|
|
|
|
drawFn = complex_clips_draw_from_canvas_state;
|
|
|
|
#endif
|
2014-07-22 19:38:55 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, drawFn);
|
|
|
|
if (!drawFn) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-04 17:16:00 +00:00
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
sk_sp<SkImage> images[2];
|
2013-09-04 17:16:00 +00:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
2019-09-03 17:23:43 +00:00
|
|
|
auto surf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT));
|
|
|
|
SkCanvas* canvas = surf->getCanvas();
|
2013-09-04 17:16:00 +00:00
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
canvas->drawColor(SK_ColorRED);
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
SkRegion localRegion = clipRegion;
|
|
|
|
|
2016-01-20 19:17:39 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(128);
|
2020-12-04 17:58:35 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(clipOps); ++j) {
|
2013-09-04 18:20:30 +00:00
|
|
|
SkRect layerBounds = SkRect::Make(layerRect);
|
2020-12-04 17:59:39 +00:00
|
|
|
canvas->saveLayer(SkCanvas::SaveLayerRec(&layerBounds, &paint));
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
if (i) {
|
2019-09-03 17:23:43 +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 {
|
2019-09-03 17:23:43 +00:00
|
|
|
complex_clips_draw(canvas, clipRect.fLeft, clipRect.fTop,
|
2014-07-22 19:38:55 +00:00
|
|
|
clipRect.fRight, clipRect.fBottom, clipOps[j],
|
|
|
|
localRegion);
|
|
|
|
}
|
2013-09-04 17:16:00 +00:00
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
canvas->restore();
|
2013-09-04 17:16:00 +00:00
|
|
|
|
|
|
|
// translate the canvas and region for the next iteration
|
2019-09-03 17:23:43 +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));
|
|
|
|
}
|
2019-09-03 17:23:43 +00:00
|
|
|
images[i] = surf->makeImageSnapshot();
|
2013-09-04 17:16:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 17:23:43 +00:00
|
|
|
compare(reporter, images[0].get(), images[1].get());
|
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));
|
|
|
|
|
2021-08-03 17:08:50 +00:00
|
|
|
canvas.clipRRect(roundRect, SkClipOp::kIntersect, 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) {
|
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)));
|
|
|
|
|
2020-12-04 17:59:39 +00:00
|
|
|
// Check that saveLayer sets the clip stack to the layer bounds.
|
2016-01-20 19:17:39 +00:00
|
|
|
canvas.saveLayer(&bounds, nullptr);
|
2020-12-04 17:59:39 +00:00
|
|
|
SkIRect 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
|
|
|
}
|