Chromium Code Reviews| Index: tests/CanvasStateTest.cpp |
| diff --git a/tests/CanvasStateTest.cpp b/tests/CanvasStateTest.cpp |
| index d8549f63eee10b498e0b581736b038db60749638..9f1612a3c1fb45cc87539d839225157225988ac4 100644 |
| --- a/tests/CanvasStateTest.cpp |
| +++ b/tests/CanvasStateTest.cpp |
| @@ -5,17 +5,37 @@ |
| * found in the LICENSE file. |
| */ |
| +#include "CanvasStateHelpers.h" |
| #include "SkCanvas.h" |
| #include "SkCanvasStateUtils.h" |
| +#include "SkCommandLineFlags.h" |
| #include "SkDrawFilter.h" |
| #include "SkError.h" |
| #include "SkPaint.h" |
| #include "SkRRect.h" |
| #include "SkRect.h" |
| +#include "SkTemplates.h" |
| #include "Test.h" |
| -static void test_complex_layers(skiatest::Reporter* reporter) { |
| +// dlopen and the library flag are only used for tests which require this flag. |
| #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
| +#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."); |
| + |
| + |
| +// Uses dlopen to open the desired library. |
| +static void* open_library(const char* library) { |
| + return dlopen(library, RTLD_LAZY | RTLD_LOCAL); |
|
djsollen
2014/07/22 15:06:34
I expected this function to check the library flag
scroggo
2014/07/22 17:10:10
Done.
|
| +} |
| +#endif |
|
djsollen
2014/07/22 15:06:34
why #endif followed by identical #ifdef? Remove b
scroggo
2014/07/22 17:10:10
Just for future proofing. I can imagine someone ad
|
| + |
| +#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
| +DEF_TEST(CanvasState_test_complex_layers, reporter) { |
| const int WIDTH = 400; |
| const int HEIGHT = 400; |
| const int SPACER = 10; |
| @@ -35,6 +55,27 @@ static void test_complex_layers(skiatest::Reporter* reporter) { |
| }; |
| REPORTER_ASSERT(reporter, sizeof(layerAlpha) == sizeof(flags)); |
| + bool (*drawFn)(SkCanvasState* state, float l, float t, |
| + float r, float b, int32_t s); |
| + void* handle; |
| + if (FLAGS_library.count() == 1) { |
| + handle = open_library(FLAGS_library[0]); |
| + REPORTER_ASSERT(reporter, handle); |
| + if (!handle) { |
| + return; |
| + } |
| + *(void**) (&drawFn) = dlsym(handle, "complex_layers_draw_from_canvas_state"); |
| + } else { |
| + drawFn = complex_layers_draw_from_canvas_state; |
| + handle = NULL; |
| + } |
| + |
| + SkAutoTCallIProc<void, dlclose> autoClose(handle); |
| + REPORTER_ASSERT(reporter, drawFn); |
| + if (!drawFn) { |
| + return; |
| + } |
| + |
| for (size_t i = 0; i < SK_ARRAY_COUNT(colorTypes); ++i) { |
| SkBitmap bitmaps[2]; |
| for (int j = 0; j < 2; ++j) { |
| @@ -50,27 +91,23 @@ static void test_complex_layers(skiatest::Reporter* reporter) { |
| // 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); |
| + // Capture from the first Skia. |
| + SkCanvasState* 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); |
| + // And draw to it in the second Skia. |
|
scroggo
2014/07/22 17:10:10
Originally I tried to write this test outside of S
djsollen
2014/07/22 17:24:42
Lets stick with the current version as the other w
|
| + bool success = complex_layers_draw_from_canvas_state(state, |
| + rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, SPACER); |
| + REPORTER_ASSERT(reporter, success); |
| - tmpCanvas->unref(); |
| - SkCanvasStateUtils::ReleaseCanvasState(state); |
| + // 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); |
| + } |
| canvas.restore(); |
| @@ -85,13 +122,13 @@ static void test_complex_layers(skiatest::Reporter* reporter) { |
| bitmaps[1].getPixels(), |
| bitmaps[0].getSize())); |
| } |
| -#endif |
| } |
| +#endif |
| //////////////////////////////////////////////////////////////////////////////// |
| -static void test_complex_clips(skiatest::Reporter* reporter) { |
| #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
| +DEF_TEST(CanvasState_test_complex_clips, reporter) { |
| const int WIDTH = 400; |
| const int HEIGHT = 400; |
| const int SPACER = 10; |
| @@ -124,6 +161,28 @@ static void test_complex_clips(skiatest::Reporter* reporter) { |
| }; |
| REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags)); |
| + 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); |
| + void* handle; |
| + if (FLAGS_library.count() == 1) { |
| + handle = open_library(FLAGS_library[0]); |
| + REPORTER_ASSERT(reporter, handle); |
| + if (!handle) { |
| + return; |
| + } |
| + *(void**) (&drawFn) = dlsym(handle, "complex_clips_draw_from_canvas_state"); |
| + } else { |
| + drawFn = complex_clips_draw_from_canvas_state; |
| + handle = NULL; |
| + } |
| + |
| + SkAutoTCallIProc<void, dlclose> autoClose(handle); |
| + REPORTER_ASSERT(reporter, drawFn); |
| + if (!drawFn) { |
| + return; |
| + } |
| + |
| SkBitmap bitmaps[2]; |
| for (int i = 0; i < 2; ++i) { |
| bitmaps[i].allocN32Pixels(WIDTH, HEIGHT); |
| @@ -138,27 +197,30 @@ static void test_complex_clips(skiatest::Reporter* reporter) { |
| SkRect layerBounds = SkRect::Make(layerRect); |
| canvas.saveLayerAlpha(&layerBounds, 128, flags[j]); |
| - SkCanvasState* state = NULL; |
| - SkCanvas* tmpCanvas = NULL; |
| if (i) { |
| - state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| + SkCanvasState* state = SkCanvasStateUtils::CaptureCanvasState(&canvas); |
| REPORTER_ASSERT(reporter, state); |
| - tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state); |
| - REPORTER_ASSERT(reporter, tmpCanvas); |
| - } else { |
| - tmpCanvas = SkRef(&canvas); |
| - } |
| - tmpCanvas->save(); |
| - tmpCanvas->clipRect(SkRect::Make(clipRect), clipOps[j]); |
| - tmpCanvas->drawColor(SK_ColorBLUE); |
| - tmpCanvas->restore(); |
| - |
| - tmpCanvas->clipRegion(localRegion, clipOps[j]); |
| - tmpCanvas->drawColor(SK_ColorBLUE); |
| + 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); |
| - tmpCanvas->unref(); |
| - SkCanvasStateUtils::ReleaseCanvasState(state); |
| + SkCanvasStateUtils::ReleaseCanvasState(state); |
| + } else { |
| + complex_clips_draw(&canvas, clipRect.fLeft, clipRect.fTop, |
| + clipRect.fRight, clipRect.fBottom, clipOps[j], |
| + localRegion); |
| + } |
| canvas.restore(); |
| @@ -173,8 +235,8 @@ static void test_complex_clips(skiatest::Reporter* reporter) { |
| REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(), |
| bitmaps[1].getPixels(), |
| bitmaps[0].getSize())); |
| -#endif |
| } |
| +#endif |
| //////////////////////////////////////////////////////////////////////////////// |
| @@ -183,7 +245,7 @@ public: |
| virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; } |
| }; |
| -static void test_draw_filters(skiatest::Reporter* reporter) { |
| +DEF_TEST(CanvasState_test_draw_filters, reporter) { |
| TestDrawFilter drawFilter; |
| SkBitmap bitmap; |
| bitmap.allocN32Pixels(10, 10); |
| @@ -208,7 +270,7 @@ static void test_draw_filters(skiatest::Reporter* reporter) { |
| // we need this function to prevent SkError from printing to stdout |
| static void error_callback(SkError code, void* ctx) {} |
| -static void test_soft_clips(skiatest::Reporter* reporter) { |
| +DEF_TEST(CanvasState_test_soft_clips, reporter) { |
| SkBitmap bitmap; |
| bitmap.allocN32Pixels(10, 10); |
| SkCanvas canvas(bitmap); |
| @@ -227,8 +289,8 @@ static void test_soft_clips(skiatest::Reporter* reporter) { |
| SkClearLastError(); |
| } |
| -static void test_saveLayer_clip(skiatest::Reporter* reporter) { |
| #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
| +DEF_TEST(CanvasState_test_saveLayer_clip, reporter) { |
| const int WIDTH = 100; |
| const int HEIGHT = 100; |
| const int LAYER_WIDTH = 50; |
| @@ -259,13 +321,5 @@ static void test_saveLayer_clip(skiatest::Reporter* reporter) { |
| REPORTER_ASSERT(reporter, clipStackBounds.height() == LAYER_HEIGHT); |
| canvas.restore(); |
| -#endif |
| -} |
| - |
| -DEF_TEST(CanvasState, reporter) { |
| - test_complex_layers(reporter); |
| - test_complex_clips(reporter); |
| - test_draw_filters(reporter); |
| - test_soft_clips(reporter); |
| - test_saveLayer_clip(reporter); |
| } |
| +#endif |