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