| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapDevice.h" | 9 #include "SkBitmapDevice.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDraw.h" | 11 #include "SkDraw.h" |
| 12 #include "SkLayerDrawLooper.h" | 12 #include "SkLayerDrawLooper.h" |
| 13 #include "SkMatrix.h" | 13 #include "SkMatrix.h" |
| 14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
| 15 #include "SkRect.h" | 15 #include "SkRect.h" |
| 16 #include "SkRefCnt.h" | 16 #include "SkRefCnt.h" |
| 17 #include "SkScalar.h" | 17 #include "SkScalar.h" |
| 18 #include "SkSmallAllocator.h" |
| 18 #include "SkXfermode.h" | 19 #include "SkXfermode.h" |
| 19 #include "Test.h" | 20 #include "Test.h" |
| 20 | 21 |
| 21 static SkBitmap make_bm(int w, int h) { | 22 static SkBitmap make_bm(int w, int h) { |
| 22 SkBitmap bm; | 23 SkBitmap bm; |
| 23 bm.allocN32Pixels(w, h); | 24 bm.allocN32Pixels(w, h); |
| 24 return bm; | 25 return bm; |
| 25 } | 26 } |
| 26 | 27 |
| 27 class FakeDevice : public SkBitmapDevice { | 28 class FakeDevice : public SkBitmapDevice { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 // Add the back layer, with some layer info set. | 51 // Add the back layer, with some layer info set. |
| 51 layerInfo.fOffset.set(10.0f, 20.0f); | 52 layerInfo.fOffset.set(10.0f, 20.0f); |
| 52 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; | 53 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 53 SkPaint* layerPaint = looperBuilder.addLayer(layerInfo); | 54 SkPaint* layerPaint = looperBuilder.addLayer(layerInfo); |
| 54 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); | 55 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 55 | 56 |
| 56 FakeDevice device; | 57 FakeDevice device; |
| 57 SkCanvas canvas(&device); | 58 SkCanvas canvas(&device); |
| 58 SkPaint paint; | 59 SkPaint paint; |
| 59 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); | 60 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
| 60 looper->init(&canvas); | 61 SkSmallAllocator<1, 32> allocator; |
| 62 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize
()); |
| 63 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer); |
| 61 | 64 |
| 62 // The back layer should come first. | 65 // The back layer should come first. |
| 63 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 66 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 64 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); | 67 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); |
| 65 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 68 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 66 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); | 69 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 67 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); | 70 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
| 68 paint.reset(); | 71 paint.reset(); |
| 69 | 72 |
| 70 // Then the front layer. | 73 // Then the front layer. |
| 71 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 74 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 72 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); | 75 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); |
| 73 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 76 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 74 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); | 77 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 75 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); | 78 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
| 76 | 79 |
| 77 // Only two layers were added, so that should be the end. | 80 // Only two layers were added, so that should be the end. |
| 78 REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); | 81 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
| 79 } | 82 } |
| 80 | 83 |
| 81 static void test_backToFront(skiatest::Reporter* reporter) { | 84 static void test_backToFront(skiatest::Reporter* reporter) { |
| 82 SkLayerDrawLooper::Builder looperBuilder; | 85 SkLayerDrawLooper::Builder looperBuilder; |
| 83 SkLayerDrawLooper::LayerInfo layerInfo; | 86 SkLayerDrawLooper::LayerInfo layerInfo; |
| 84 | 87 |
| 85 // Add the back layer, with the defaults. | 88 // Add the back layer, with the defaults. |
| 86 (void)looperBuilder.addLayerOnTop(layerInfo); | 89 (void)looperBuilder.addLayerOnTop(layerInfo); |
| 87 | 90 |
| 88 // Add the front layer, with some layer info set. | 91 // Add the front layer, with some layer info set. |
| 89 layerInfo.fOffset.set(10.0f, 20.0f); | 92 layerInfo.fOffset.set(10.0f, 20.0f); |
| 90 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; | 93 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 91 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); | 94 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
| 92 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); | 95 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 93 | 96 |
| 94 FakeDevice device; | 97 FakeDevice device; |
| 95 SkCanvas canvas(&device); | 98 SkCanvas canvas(&device); |
| 96 SkPaint paint; | 99 SkPaint paint; |
| 97 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); | 100 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
| 98 looper->init(&canvas); | 101 SkSmallAllocator<1, 32> allocator; |
| 102 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize
()); |
| 103 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer); |
| 99 | 104 |
| 100 // The back layer should come first. | 105 // The back layer should come first. |
| 101 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 106 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 102 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); | 107 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); |
| 103 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 108 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 104 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); | 109 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 105 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); | 110 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
| 106 paint.reset(); | 111 paint.reset(); |
| 107 | 112 |
| 108 // Then the front layer. | 113 // Then the front layer. |
| 109 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 114 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 110 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); | 115 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); |
| 111 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 116 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 112 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); | 117 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 113 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); | 118 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
| 114 | 119 |
| 115 // Only two layers were added, so that should be the end. | 120 // Only two layers were added, so that should be the end. |
| 116 REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); | 121 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
| 117 } | 122 } |
| 118 | 123 |
| 119 static void test_mixed(skiatest::Reporter* reporter) { | 124 static void test_mixed(skiatest::Reporter* reporter) { |
| 120 SkLayerDrawLooper::Builder looperBuilder; | 125 SkLayerDrawLooper::Builder looperBuilder; |
| 121 SkLayerDrawLooper::LayerInfo layerInfo; | 126 SkLayerDrawLooper::LayerInfo layerInfo; |
| 122 | 127 |
| 123 // Add the back layer, with the defaults. | 128 // Add the back layer, with the defaults. |
| 124 (void)looperBuilder.addLayer(layerInfo); | 129 (void)looperBuilder.addLayer(layerInfo); |
| 125 | 130 |
| 126 // Add the front layer, with some layer info set. | 131 // Add the front layer, with some layer info set. |
| 127 layerInfo.fOffset.set(10.0f, 20.0f); | 132 layerInfo.fOffset.set(10.0f, 20.0f); |
| 128 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; | 133 layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit; |
| 129 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); | 134 SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo); |
| 130 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); | 135 layerPaint->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 131 | 136 |
| 132 FakeDevice device; | 137 FakeDevice device; |
| 133 SkCanvas canvas(&device); | 138 SkCanvas canvas(&device); |
| 134 SkPaint paint; | 139 SkPaint paint; |
| 135 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); | 140 SkAutoTUnref<SkLayerDrawLooper> looper(looperBuilder.detachLooper()); |
| 136 looper->init(&canvas); | 141 SkSmallAllocator<1, 32> allocator; |
| 142 void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize
()); |
| 143 SkDrawLooper::Context* context = looper->createContext(&canvas, buffer); |
| 137 | 144 |
| 138 // The back layer should come first. | 145 // The back layer should come first. |
| 139 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 146 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 140 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); | 147 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrcOver_Mode)); |
| 141 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 148 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 142 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); | 149 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX()); |
| 143 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); | 150 REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY()); |
| 144 paint.reset(); | 151 paint.reset(); |
| 145 | 152 |
| 146 // Then the front layer. | 153 // Then the front layer. |
| 147 REPORTER_ASSERT(reporter, looper->next(&canvas, &paint)); | 154 REPORTER_ASSERT(reporter, context->next(&canvas, &paint)); |
| 148 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); | 155 REPORTER_ASSERT(reporter, SkXfermode::IsMode(paint.getXfermode(), SkXfermode
::kSrc_Mode)); |
| 149 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); | 156 canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint); |
| 150 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); | 157 REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX()); |
| 151 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); | 158 REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY()); |
| 152 | 159 |
| 153 // Only two layers were added, so that should be the end. | 160 // Only two layers were added, so that should be the end. |
| 154 REPORTER_ASSERT(reporter, !looper->next(&canvas, &paint)); | 161 REPORTER_ASSERT(reporter, !context->next(&canvas, &paint)); |
| 155 } | 162 } |
| 156 | 163 |
| 157 DEF_TEST(LayerDrawLooper, reporter) { | 164 DEF_TEST(LayerDrawLooper, reporter) { |
| 158 test_frontToBack(reporter); | 165 test_frontToBack(reporter); |
| 159 test_backToFront(reporter); | 166 test_backToFront(reporter); |
| 160 test_mixed(reporter); | 167 test_mixed(reporter); |
| 161 } | 168 } |
| OLD | NEW |