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) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 38 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 39 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 40 | |
| 41 | // Add the front layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 42 | (void)looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 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; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 47 | SkPaint* layerPaint = looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 48 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 49 | |
| 50 | FakeDevice device; |
| 51 | SkCanvas canvas(&device); |
| 52 | SkPaint paint; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 53 | SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 54 | looper->init(&canvas); |
| 55 | |
| 56 | // The back layer should come first. |
| 57 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 58 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 59 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 60 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 61 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 62 | paint.reset(); |
| 63 | |
| 64 | // Then the front layer. |
| 65 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 66 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 67 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 68 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 69 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 70 | |
| 71 | // Only two layers were added, so that should be the end. |
| 72 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 73 | } |
| 74 | |
| 75 | static void test_backToFront(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 76 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 77 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 78 | |
| 79 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 80 | (void)looperBuilder.addLayerOnTop(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 81 | |
| 82 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 83 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 84 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 85 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 86 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 87 | |
| 88 | FakeDevice device; |
| 89 | SkCanvas canvas(&device); |
| 90 | SkPaint paint; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 91 | SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 92 | looper->init(&canvas); |
| 93 | |
| 94 | // The back layer should come first. |
| 95 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 96 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 97 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 98 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 99 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 100 | paint.reset(); |
| 101 | |
| 102 | // Then the front layer. |
| 103 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 104 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 105 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 106 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 107 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 108 | |
| 109 | // Only two layers were added, so that should be the end. |
| 110 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 111 | } |
| 112 | |
| 113 | static void test_mixed(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 114 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 115 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 116 | |
| 117 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 118 | (void)looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 119 | |
| 120 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 121 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 122 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 123 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 124 | layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 125 | |
| 126 | FakeDevice device; |
| 127 | SkCanvas canvas(&device); |
| 128 | SkPaint paint; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame^] | 129 | SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 130 | looper->init(&canvas); |
| 131 | |
| 132 | // The back layer should come first. |
| 133 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 134 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 135 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 136 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 137 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 138 | paint.reset(); |
| 139 | |
| 140 | // Then the front layer. |
| 141 | REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); |
| 142 | REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrc_Mode)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 143 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 144 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 145 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 146 | |
| 147 | // Only two layers were added, so that should be the end. |
| 148 | REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); |
| 149 | } |
| 150 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 151 | DEF_TEST(LayerDrawLooper, reporter) { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 152 | test_frontToBack(reporter); |
| 153 | test_backToFront(reporter); |
| 154 | test_mixed(reporter); |
| 155 | } |