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