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" |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 18 | #include "SkSmallAllocator.h" |
Greg Daniel | e9d1b29 | 2016-11-23 16:47:38 +0000 | [diff] [blame] | 19 | #include "SkXfermode.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 20 | #include "Test.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 22 | static SkBitmap make_bm(int w, int h) { |
| 23 | SkBitmap bm; |
| 24 | bm.allocN32Pixels(w, h); |
| 25 | return bm; |
| 26 | } |
| 27 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 28 | // TODO: can this be derived from SkBaseDevice? |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 29 | class FakeDevice : public SkBitmapDevice { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 30 | public: |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 31 | FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry)) { |
| 32 | } |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 33 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 34 | void drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) override { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 35 | fLastMatrix = *draw.fMatrix; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 36 | this->INHERITED::drawRect(draw, r, paint); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | SkMatrix fLastMatrix; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 40 | |
| 41 | private: |
| 42 | typedef SkBitmapDevice INHERITED; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 45 | static void test_frontToBack(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 46 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 47 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 48 | |
| 49 | // Add the front layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 50 | (void)looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 51 | |
| 52 | // Add the back layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 53 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 54 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 55 | SkPaint* layerPaint = looperBuilder.addLayer(layerInfo); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 56 | layerPaint->setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 57 | |
| 58 | FakeDevice device; |
| 59 | SkCanvas canvas(&device); |
| 60 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 61 | auto looper(looperBuilder.detach()); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 62 | SkSmallAllocator<1, 32> allocator; |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 63 | SkDrawLooper::Context* context = allocator.createWithIniter( |
| 64 | looper->contextSize(), |
| 65 | [&](void* buffer) { |
| 66 | return looper->createContext(&canvas, buffer); |
| 67 | }); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 68 | |
| 69 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 72 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 73 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 74 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 75 | paint.reset(); |
| 76 | |
| 77 | // Then the front layer. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 80 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 81 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 82 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 83 | |
| 84 | // Only two layers were added, so that should be the end. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void test_backToFront(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 89 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 90 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 91 | |
| 92 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 93 | (void)looperBuilder.addLayerOnTop(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 94 | |
| 95 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 96 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 97 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 98 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 99 | layerPaint->setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 100 | |
| 101 | FakeDevice device; |
| 102 | SkCanvas canvas(&device); |
| 103 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 104 | auto looper(looperBuilder.detach()); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 105 | SkSmallAllocator<1, 32> allocator; |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 106 | SkDrawLooper::Context* context = allocator.createWithIniter( |
| 107 | looper->contextSize(), |
| 108 | [&](void* buffer) { |
| 109 | return looper->createContext(&canvas, buffer); |
| 110 | }); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 111 | |
| 112 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 114 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 115 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 116 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 117 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 118 | paint.reset(); |
| 119 | |
| 120 | // Then the front layer. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 123 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 124 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 125 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 126 | |
| 127 | // Only two layers were added, so that should be the end. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void test_mixed(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 132 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 133 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 134 | |
| 135 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 136 | (void)looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 137 | |
| 138 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 139 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 140 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 141 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 142 | layerPaint->setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 143 | |
| 144 | FakeDevice device; |
| 145 | SkCanvas canvas(&device); |
| 146 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 147 | sk_sp<SkDrawLooper> looper(looperBuilder.detach()); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 148 | SkSmallAllocator<1, 32> allocator; |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 149 | SkDrawLooper::Context* context = allocator.createWithIniter( |
| 150 | looper->contextSize(), |
| 151 | [&](void* buffer) { |
| 152 | return looper->createContext(&canvas, buffer); |
| 153 | }); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 154 | |
| 155 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 158 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 159 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 160 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 161 | paint.reset(); |
| 162 | |
| 163 | // Then the front layer. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 164 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 166 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 167 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 168 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 169 | |
| 170 | // Only two layers were added, so that should be the end. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 172 | } |
| 173 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 174 | DEF_TEST(LayerDrawLooper, reporter) { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 175 | test_frontToBack(reporter); |
| 176 | test_backToFront(reporter); |
| 177 | test_mixed(reporter); |
| 178 | } |