Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: tests/SkImageTest.cpp

Issue 960783003: Add image as a draw type that can be filtered Base URL: https://skia.googlesource.com/skia.git@skimage-filters-04-snapshot-devices-and-use-snapshots-in-skcanvas
Patch Set: rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkCanvas.cpp ('k') | tools/bench_pictures_main.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
9 #include "SkImagePriv.h" 11 #include "SkImagePriv.h"
10 #include "Test.h" 12 #include "Test.h"
11 13
12 static const int gWidth = 20; 14 static const int gWidth = 20;
13 static const int gHeight = 20; 15 static const int gHeight = 20;
14 16
15 // Tests that SkNewImageFromBitmap obeys pixelref origin. 17 // Tests that SkNewImageFromBitmap obeys pixelref origin.
16 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) { 18 DEF_TEST(SkImageFromBitmap_extractSubset, reporter) {
17 SkAutoTUnref<SkImage> image; 19 SkAutoTUnref<SkImage> image;
18 { 20 {
(...skipping 21 matching lines...) Expand all
40 uint32_t pixel = 0; 42 uint32_t pixel = 0;
41 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul _SkAlphaType); 43 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul _SkAlphaType);
42 canvas.readPixels(info, &pixel, 4, 0, 0); 44 canvas.readPixels(info, &pixel, 4, 0, 0);
43 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); 45 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
44 canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6); 46 canvas.readPixels(info, &pixel, 4, gWidth - 6, gWidth - 6);
45 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); 47 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
46 48
47 canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5); 49 canvas.readPixels(info, &pixel, 4, gWidth - 5, gWidth - 5);
48 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT); 50 REPORTER_ASSERT(reporter, pixel == SK_ColorTRANSPARENT);
49 } 51 }
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.
169 SkBitmap bitmap;
170 bitmap.allocN32Pixels(10, 10);
171 bitmap.eraseColor(SK_ColorRED);
172 OnceTestLooper drawLooper;
173 SkCanvas canvas(bitmap);
174
175 SkBitmap imageBitmap;
176 imageBitmap.allocN32Pixels(1, 1);
177 imageBitmap.eraseColor(SK_ColorGREEN);
178 SkAutoTUnref<SkImage> image(SkNewImageFromBitmap(imageBitmap, true, NULL));
179 SkPaint p;
180 p.setLooper(&drawLooper);
181 canvas.drawImage(image, 0, 0, &p);
182
183 uint32_t pixel;
184 SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul _SkAlphaType);
185 canvas.readPixels(info, &pixel, 4, 0, 1);
186 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
187 canvas.readPixels(info, &pixel, 4, 0, 0);
188 REPORTER_ASSERT(reporter, pixel == SK_ColorRED);
189 canvas.readPixels(info, &pixel, 4, 0, 2);
190 REPORTER_ASSERT(reporter, pixel == SK_ColorRED);
191 }
192
OLDNEW
« no previous file with comments | « src/core/SkCanvas.cpp ('k') | tools/bench_pictures_main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698