| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
| 9 #include "SkColor.h" | |
| 10 #include "SkDrawFilter.h" | |
| 11 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
| 12 #include "Test.h" | 10 #include "Test.h" |
| 13 | 11 |
| 14 static const int gWidth = 20; | 12 static const int gWidth = 20; |
| 15 static const int gHeight = 20; | 13 static const int gHeight = 20; |
| 16 | 14 |
| 17 // Tests that SkNewImageFromBitmap obeys pixelref origin. | 15 // Tests that SkNewImageFromBitmap obeys pixelref origin. |
| 18 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) { | 16 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) { |
| 19 SkAutoTUnref<SkImage> image; | 17 SkAutoTUnref<SkImage> image; |
| 20 { | 18 { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 42 uint32_t pixel = 0; | 40 uint32_t pixel = 0; |
| 43 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | 41 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 44 canvas.readPixels(info, &pixel, 4, 0, 0); | 42 canvas.readPixels(info, &pixel, 4, 0, 0); |
| 45 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | 43 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 46 canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6); | 44 canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6); |
| 47 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | 45 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 48 | 46 |
| 49 canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5); | 47 canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5); |
| 50 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT); | 48 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT); |
| 51 } | 49 } |
| 52 | |
| 53 namespace { | |
| 54 class TestDrawFilterImage : public SkDrawFilter { | |
| 55 public: | |
| 56 TestDrawFilterImage() | |
| 57 : fFilteredImage(0) | |
| 58 , fFilteredOthers(0) | |
| 59 , fPreventImages(true) | |
| 60 , fPreventOthers(true) { | |
| 61 } | |
| 62 | |
| 63 bool filter(SkPaint*, Type type) SK_OVERRIDE { | |
| 64 if (type == SkDrawFilter::kImage_Type) { | |
| 65 if (fPreventImages) { | |
| 66 fFilteredImage++; | |
| 67 return false; | |
| 68 } | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 if (fPreventOthers) { | |
| 73 fFilteredOthers++; | |
| 74 return false; | |
| 75 } | |
| 76 return true; | |
| 77 } | |
| 78 int fFilteredImage; | |
| 79 int fFilteredOthers; | |
| 80 bool fPreventImages; | |
| 81 bool fPreventOthers; | |
| 82 }; | |
| 83 } | |
| 84 | |
| 85 DEF_TEST(SkCanvas_test_draw_filter_image, reporter) { | |
| 86 SkBitmap bitmap; | |
| 87 bitmap.allocN32Pixels(1, 1); | |
| 88 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
| 89 TestDrawFilterImage drawFilter; | |
| 90 SkCanvas canvas(bitmap); | |
| 91 | |
| 92 SkBitmap imageBitmap; | |
| 93 imageBitmap.allocN32Pixels(1, 1); | |
| 94 imageBitmap.eraseColor(SK_ColorGREEN); | |
| 95 SkAutoTUnref<SkImage> image(SkNewImageFromBitmap(imageBitmap, true, NULL)); | |
| 96 canvas.drawImage(image, 0, 0); | |
| 97 | |
| 98 uint32_t pixel; | |
| 99 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | |
| 100 canvas.readPixels(info, &pixel, 4, 0, 0); | |
| 101 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | |
| 102 | |
| 103 canvas.setDrawFilter(&drawFilter); | |
| 104 imageBitmap.eraseColor(SK_ColorRED); | |
| 105 image.reset(SkNewImageFromBitmap(imageBitmap, true, NULL)); | |
| 106 canvas.drawImage(image, 0, 0); | |
| 107 canvas.readPixels(info, &pixel, 4, 0, 0); | |
| 108 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | |
| 109 REPORTER_ASSERT(reporter, drawFilter.fFilteredImage == 1); | |
| 110 REPORTER_ASSERT(reporter, drawFilter.fFilteredOthers == 0); | |
| 111 | |
| 112 // Document a strange case: filtering everything but the images does not wor
k as | |
| 113 // expected. Images go through, but no pixels appear. (This due to SkCanvas:
:drawImage() using | |
| 114 // SkCanvas::drawBitmap() instead of non-existing SkBaseDevice::drawImage())
. | |
| 115 drawFilter.fFilteredImage = 0; | |
| 116 drawFilter.fPreventImages = false; | |
| 117 | |
| 118 canvas.drawImage(image, 0, 0); | |
| 119 canvas.readPixels(info, &pixel, 4, 0, 0); | |
| 120 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | |
| 121 REPORTER_ASSERT(reporter, drawFilter.fFilteredImage == 0); | |
| 122 REPORTER_ASSERT(reporter, drawFilter.fFilteredOthers == 1); | |
| 123 } | |
| 124 | |
| 125 namespace { | |
| 126 /* | |
| 127 * Subclass of looper that just draws once with one pixel offset. | |
| 128 */ | |
| 129 class OnceTestLooper : public SkDrawLooper { | |
| 130 public: | |
| 131 OnceTestLooper() { } | |
| 132 | |
| 133 SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVER
RIDE { | |
| 134 return SkNEW_PLACEMENT(storage, OnceTestLooperContext()); | |
| 135 } | |
| 136 | |
| 137 size_t contextSize() const SK_OVERRIDE { return sizeof(OnceTestLooperContext
); } | |
| 138 | |
| 139 #ifndef SK_IGNORE_TO_STRING | |
| 140 void toString(SkString* str) const SK_OVERRIDE { | |
| 141 str->append("OnceTestLooper:"); | |
| 142 } | |
| 143 #endif | |
| 144 | |
| 145 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(OnceTestLooper); | |
| 146 | |
| 147 private: | |
| 148 class OnceTestLooperContext : public SkDrawLooper::Context { | |
| 149 public: | |
| 150 OnceTestLooperContext() : fCount(0) {} | |
| 151 virtual ~OnceTestLooperContext() {} | |
| 152 | |
| 153 bool next(SkCanvas* canvas, SkPaint*) SK_OVERRIDE { | |
| 154 SkASSERT(fCount <= 1); | |
| 155 canvas->translate(0, 1); | |
| 156 return fCount++ < 1; | |
| 157 } | |
| 158 private: | |
| 159 unsigned fCount; | |
| 160 }; | |
| 161 }; | |
| 162 | |
| 163 SkFlattenable* OnceTestLooper::CreateProc(SkReadBuffer&) { return SkNEW(OnceTest
Looper); } | |
| 164 } | |
| 165 | |
| 166 DEF_TEST(SkCanvas_test_draw_looper_image, reporter) { | |
| 167 // Test that drawImage loops with the looper the correct way. At the time of
writing, this was | |
| 168 // tricky because drawImage was implemented with drawBitmap. The drawBitmap
uses applies the | |
| 169 // looper. | |
| 170 SkBitmap bitmap; | |
| 171 bitmap.allocN32Pixels(10, 10); | |
| 172 bitmap.eraseColor(SK_ColorRED); | |
| 173 OnceTestLooper drawLooper; | |
| 174 SkCanvas canvas(bitmap); | |
| 175 | |
| 176 SkBitmap imageBitmap; | |
| 177 imageBitmap.allocN32Pixels(1, 1); | |
| 178 imageBitmap.eraseColor(SK_ColorGREEN); | |
| 179 SkAutoTUnref<SkImage> image(SkNewImageFromBitmap(imageBitmap, true, NULL)); | |
| 180 SkPaint p; | |
| 181 p.setLooper(&drawLooper); | |
| 182 canvas.drawImage(image, 0, 0, &p); | |
| 183 | |
| 184 uint32_t pixel; | |
| 185 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | |
| 186 canvas.readPixels(info, &pixel, 4, 0, 1); | |
| 187 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); | |
| 188 canvas.readPixels(info, &pixel, 4, 0, 0); | |
| 189 REPORTER_ASSERT(reporter, pixel == SK_ColorRED); | |
| 190 canvas.readPixels(info, &pixel, 4, 0, 2); | |
| 191 REPORTER_ASSERT(reporter, pixel == SK_ColorRED); | |
| 192 } | |
| 193 | |
| OLD | NEW |