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 | |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 8 | #include "SkArenaAlloc.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 10 | #include "SkBitmapDevice.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkDraw.h" |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 13 | #include "SkLayerDrawLooper.h" |
| 14 | #include "SkMatrix.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkRect.h" |
| 17 | #include "SkRefCnt.h" |
| 18 | #include "SkScalar.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 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame^] | 33 | void drawRect(const SkRect& r, const SkPaint& paint) override { |
| 34 | fLastMatrix = this->ctm(); |
| 35 | this->INHERITED::drawRect(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()); |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 61 | SkArenaAlloc alloc{48}; |
| 62 | SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 63 | |
| 64 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc); |
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, 10.0f == device.fLastMatrix.getTranslateX()); |
| 69 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 70 | paint.reset(); |
| 71 | |
| 72 | // Then the front layer. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 75 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 76 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 77 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 78 | |
| 79 | // 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] | 80 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void test_backToFront(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 84 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 85 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 86 | |
| 87 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 88 | (void)looperBuilder.addLayerOnTop(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 89 | |
| 90 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 91 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 92 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 93 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 94 | layerPaint->setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 95 | |
| 96 | FakeDevice device; |
| 97 | SkCanvas canvas(&device); |
| 98 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 99 | auto looper(looperBuilder.detach()); |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 100 | SkArenaAlloc alloc{48}; |
| 101 | SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 102 | |
| 103 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 106 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 107 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 108 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 109 | paint.reset(); |
| 110 | |
| 111 | // Then the front layer. |
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::kSrc); |
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, 10.0f == device.fLastMatrix.getTranslateX()); |
| 116 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 117 | |
| 118 | // 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] | 119 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void test_mixed(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 123 | SkLayerDrawLooper::Builder looperBuilder; |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 124 | SkLayerDrawLooper::LayerInfo layerInfo; |
| 125 | |
| 126 | // Add the back layer, with the defaults. |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 127 | (void)looperBuilder.addLayer(layerInfo); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 128 | |
| 129 | // Add the front layer, with some layer info set. |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 130 | layerInfo.fOffset.set(10.0f, 20.0f); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 131 | layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 132 | SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 133 | layerPaint->setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 134 | |
| 135 | FakeDevice device; |
| 136 | SkCanvas canvas(&device); |
| 137 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 138 | sk_sp<SkDrawLooper> looper(looperBuilder.detach()); |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 139 | SkArenaAlloc alloc{48}; |
| 140 | SkDrawLooper::Context* context = looper->makeContext(&canvas, &alloc); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 141 | |
| 142 | // The back layer should come first. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 145 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 146 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 147 | REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 148 | paint.reset(); |
| 149 | |
| 150 | // Then the front layer. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 152 | REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 153 | canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 154 | REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 155 | REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 156 | |
| 157 | // 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] | 158 | REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 159 | } |
| 160 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 161 | DEF_TEST(LayerDrawLooper, reporter) { |
commit-bot@chromium.org | 8f83825 | 2013-05-22 12:35:50 +0000 | [diff] [blame] | 162 | test_frontToBack(reporter); |
| 163 | test_backToFront(reporter); |
| 164 | test_mixed(reporter); |
| 165 | } |