commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 9 | #include "SkBitmapDevice.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkDraw.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 12 | #include "SkLayerDrawLooper.h" |
| 13 | #include "SkMatrix.h" |
| 14 | #include "SkPaint.h" |
| 15 | #include "SkRect.h" |
| 16 | #include "SkRefCnt.h" |
| 17 | #include "SkScalar.h" |
| 18 | #include "SkXfermode.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame^] | 19 | #include "Test.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 20 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 21 | class FakeDevice : public SkBitmapDevice { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 22 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 23 | FakeDevice() : SkBitmapDevice(SkBitmap::kARGB_8888_Config, 100, 100, false) { } |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 24 | |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 25 | virtual void drawRect(const SkDraw& draw, const SkRect& r, |
commit-bot@chromium.org | 3e2ea25 | 2013-07-23 11:28:45 +0000 | [diff] [blame] | 26 | const SkPaint& paint) SK_OVERRIDE { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 27 | fLastMatrix = *draw.fMatrix; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 28 | INHERITED::drawRect(draw, r, paint); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | SkMatrix fLastMatrix; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | typedef SkBitmapDevice INHERITED; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 37 | static void test_frontToBack(skiatest::Reporter* reporter) { |
| 38 | SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper)); |
| 39 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 40 | |
| 41 | // Add the front layer, with the defaults. |
| 42 | (void)looper->addLayer(layerInfo); |
| 43 | |
| 44 | // Add the back layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 45 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 46 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 47 | SkPaint* layerPaint = looper->addLayer(layerInfo); |
| 48 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 49 | |
| 50 | FakeDevice device; |
| 51 | SkCanvas canvas(&device); |
| 52 | SkPaint paint; |
| 53 | looper->init(&canvas); |
| 54 | |
| 55 | // The back layer should come first. |
| 56 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 57 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 58 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 59 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 60 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 61 | paint.reset(); |
| 62 | |
| 63 | // Then the front layer. |
| 64 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 65 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 66 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 67 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 68 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 69 | |
| 70 | // Only two layers were added, so that should be the end. |
| 71 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 72 | } |
| 73 | |
| 74 | static void test_backToFront(skiatest::Reporter* reporter) { |
| 75 | SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper)); |
| 76 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 77 | |
| 78 | // Add the back layer, with the defaults. |
| 79 | (void)looper->addLayerOnTop(layerInfo); |
| 80 | |
| 81 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 82 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 83 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 84 | SkPaint* layerPaint = looper->addLayerOnTop(layerInfo); |
| 85 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 86 | |
| 87 | FakeDevice device; |
| 88 | SkCanvas canvas(&device); |
| 89 | SkPaint paint; |
| 90 | looper->init(&canvas); |
| 91 | |
| 92 | // The back layer should come first. |
| 93 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 94 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 95 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 96 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 97 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 98 | paint.reset(); |
| 99 | |
| 100 | // Then the front layer. |
| 101 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 102 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 103 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 104 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 105 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 106 | |
| 107 | // Only two layers were added, so that should be the end. |
| 108 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 109 | } |
| 110 | |
| 111 | static void test_mixed(skiatest::Reporter* reporter) { |
| 112 | SkAutoTUnref<SkLayerDrawLooper> looper(SkNEW(SkLayerDrawLooper)); |
| 113 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 114 | |
| 115 | // Add the back layer, with the defaults. |
| 116 | (void)looper->addLayer(layerInfo); |
| 117 | |
| 118 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 119 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 120 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 121 | SkPaint* layerPaint = looper->addLayerOnTop(layerInfo); |
| 122 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 123 | |
| 124 | FakeDevice device; |
| 125 | SkCanvas canvas(&device); |
| 126 | SkPaint paint; |
| 127 | looper->init(&canvas); |
| 128 | |
| 129 | // The back layer should come first. |
| 130 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 131 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 132 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 133 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 134 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 135 | paint.reset(); |
| 136 | |
| 137 | // Then the front layer. |
| 138 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 139 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 140 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 141 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 142 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 143 | |
| 144 | // Only two layers were added, so that should be the end. |
| 145 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 146 | } |
| 147 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 148 | DEF_TEST(LayerDrawLooper, reporter) { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 149 | test_frontToBack(reporter); |
| 150 | test_backToFront(reporter); |
| 151 | test_mixed(reporter); |
| 152 | } |