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

Side by Side Diff: Source/platform/DragImageTest.cpp

Issue 1001703003: Take NativeImageSkia out behind the woodshed. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Make bitmapForCurrentFrame() return SkBitmap by value. 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 19 matching lines...) Expand all
30 30
31 #include "config.h" 31 #include "config.h"
32 32
33 #include "platform/DragImage.h" 33 #include "platform/DragImage.h"
34 34
35 #include "platform/fonts/FontDescription.h" 35 #include "platform/fonts/FontDescription.h"
36 #include "platform/fonts/FontTraits.h" 36 #include "platform/fonts/FontTraits.h"
37 #include "platform/geometry/IntSize.h" 37 #include "platform/geometry/IntSize.h"
38 #include "platform/graphics/BitmapImage.h" 38 #include "platform/graphics/BitmapImage.h"
39 #include "platform/graphics/Image.h" 39 #include "platform/graphics/Image.h"
40 #include "platform/graphics/skia/NativeImageSkia.h"
41 #include "platform/weborigin/KURL.h" 40 #include "platform/weborigin/KURL.h"
42 #include "third_party/skia/include/core/SkBitmap.h" 41 #include "third_party/skia/include/core/SkBitmap.h"
43 #include "third_party/skia/include/core/SkColor.h" 42 #include "third_party/skia/include/core/SkColor.h"
44 #include "third_party/skia/include/core/SkPixelRef.h" 43 #include "third_party/skia/include/core/SkPixelRef.h"
45 #include "wtf/OwnPtr.h" 44 #include "wtf/OwnPtr.h"
46 #include "wtf/PassOwnPtr.h" 45 #include "wtf/PassOwnPtr.h"
47 #include "wtf/PassRefPtr.h" 46 #include "wtf/PassRefPtr.h"
48 #include "wtf/RefPtr.h" 47 #include "wtf/RefPtr.h"
49 48
50 #include <gtest/gtest.h> 49 #include <gtest/gtest.h>
51 50
52 using namespace blink; 51 using namespace blink;
53 52
54 namespace { 53 namespace {
55 54
56 class TestImage : public Image { 55 class TestImage : public Image {
57 public: 56 public:
58 57
59 static PassRefPtr<TestImage> create(const IntSize& size) 58 static PassRefPtr<TestImage> create(const IntSize& size)
60 { 59 {
61 return adoptRef(new TestImage(size)); 60 return adoptRef(new TestImage(size));
62 } 61 }
63 62
64 explicit TestImage(const IntSize& size) 63 explicit TestImage(const IntSize& size)
65 : Image(0) 64 : Image(0)
66 , m_size(size) 65 , m_size(size)
67 { 66 {
68 SkBitmap bitmap; 67 m_bitmap.allocN32Pixels(size.width(), size.height());
69 bitmap.allocN32Pixels(size.width(), size.height()); 68 m_bitmap.eraseColor(SK_ColorTRANSPARENT);
70 bitmap.eraseColor(SK_ColorTRANSPARENT);
71 m_nativeImage = NativeImageSkia::create(bitmap);
72 } 69 }
73 70
74 virtual IntSize size() const override 71 virtual IntSize size() const override
75 { 72 {
76 return m_size; 73 return m_size;
77 } 74 }
78 75
79 virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() override 76 virtual SkBitmap bitmapForCurrentFrame() override
80 { 77 {
81 if (m_size.isZero()) 78 if (m_size.isZero())
82 return nullptr; 79 return SkBitmap();
f(malita) 2015/03/15 00:32:45 Prolly not worth touching in this CL, but I don't
Stephen White 2015/03/15 16:49:31 Acknowledged.
83 80
84 return m_nativeImage; 81 return m_bitmap;
85 } 82 }
86 83
87 // Stub implementations of pure virtual Image functions. 84 // Stub implementations of pure virtual Image functions.
88 virtual void destroyDecodedData(bool) override 85 virtual void destroyDecodedData(bool) override
89 { 86 {
90 } 87 }
91 88
92 virtual bool currentFrameKnownToBeOpaque() override 89 virtual bool currentFrameKnownToBeOpaque() override
93 { 90 {
94 return false; 91 return false;
95 } 92 }
96 93
97 void draw(GraphicsContext*, const FloatRect&, const FloatRect&, SkXfermode:: Mode, RespectImageOrientationEnum) override 94 void draw(GraphicsContext*, const FloatRect&, const FloatRect&, SkXfermode:: Mode, RespectImageOrientationEnum) override
98 { 95 {
99 } 96 }
100 97
101 private: 98 private:
102 99
103 IntSize m_size; 100 IntSize m_size;
104 101
105 RefPtr<NativeImageSkia> m_nativeImage; 102 SkBitmap m_bitmap;
106 }; 103 };
107 104
108 TEST(DragImageTest, NullHandling) 105 TEST(DragImageTest, NullHandling)
109 { 106 {
110 EXPECT_FALSE(DragImage::create(0)); 107 EXPECT_FALSE(DragImage::create(0));
111 108
112 RefPtr<TestImage> nullTestImage(TestImage::create(IntSize())); 109 RefPtr<TestImage> nullTestImage(TestImage::create(IntSize()));
113 EXPECT_FALSE(DragImage::create(nullTestImage.get())); 110 EXPECT_FALSE(DragImage::create(nullTestImage.get()));
114 } 111 }
115 112
116 TEST(DragImageTest, NonNullHandling) 113 TEST(DragImageTest, NonNullHandling)
117 { 114 {
118 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2))); 115 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
119 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get()); 116 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
120 ASSERT_TRUE(dragImage); 117 ASSERT_TRUE(dragImage);
121 118
122 dragImage->scale(0.5, 0.5); 119 dragImage->scale(0.5, 0.5);
123 IntSize size = dragImage->size(); 120 IntSize size = dragImage->size();
124 EXPECT_EQ(1, size.width()); 121 EXPECT_EQ(1, size.width());
125 EXPECT_EQ(1, size.height()); 122 EXPECT_EQ(1, size.height());
126 123
127 dragImage->dissolveToFraction(0.5); 124 dragImage->dissolveToFraction(0.5);
128 } 125 }
129 126
130 TEST(DragImageTest, CreateDragImage) 127 TEST(DragImageTest, CreateDragImage)
131 { 128 {
132 { 129 {
133 // Tests that the DrageImage implementation doesn't choke on null values 130 // Tests that the DrageImage implementation doesn't choke on null values
134 // of nativeImageForCurrentFrame(). 131 // of bitmapForCurrentFrame().
135 RefPtr<TestImage> testImage(TestImage::create(IntSize())); 132 RefPtr<TestImage> testImage(TestImage::create(IntSize()));
136 EXPECT_FALSE(DragImage::create(testImage.get())); 133 EXPECT_FALSE(DragImage::create(testImage.get()));
137 } 134 }
138 135
139 { 136 {
140 // Tests that the drag image is a deep copy. 137 // Tests that the drag image is a deep copy.
141 RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1))); 138 RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
142 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get()); 139 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
143 ASSERT_TRUE(dragImage); 140 ASSERT_TRUE(dragImage);
144 SkAutoLockPixels lock1(dragImage->bitmap()), lock2(testImage->nativeImag eForCurrentFrame()->bitmap()); 141 SkAutoLockPixels lock1(dragImage->bitmap()), lock2(testImage->bitmapForC urrentFrame());
145 EXPECT_NE(dragImage->bitmap().getPixels(), testImage->nativeImageForCurr entFrame()->bitmap().getPixels()); 142 EXPECT_NE(dragImage->bitmap().getPixels(), testImage->bitmapForCurrentFr ame().getPixels());
146 } 143 }
147 } 144 }
148 145
149 TEST(DragImageTest, TrimWhitespace) 146 TEST(DragImageTest, TrimWhitespace)
150 { 147 {
151 KURL url(ParsedURLString, "http://www.example.com/"); 148 KURL url(ParsedURLString, "http://www.example.com/");
152 String testLabel = " Example Example Example \n "; 149 String testLabel = " Example Example Example \n ";
153 String expectedLabel = "Example Example Example"; 150 String expectedLabel = "Example Example Example";
154 float deviceScaleFactor = 1.0f; 151 float deviceScaleFactor = 1.0f;
155 152
(...skipping 28 matching lines...) Expand all
184 // This test is mostly useful with MSAN builds, which can actually detect 181 // This test is mostly useful with MSAN builds, which can actually detect
185 // the use of uninitialized memory. 182 // the use of uninitialized memory.
186 183
187 // Create a BitmapImage which will fail to produce pixels, and hence not 184 // Create a BitmapImage which will fail to produce pixels, and hence not
188 // draw. 185 // draw.
189 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); 186 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
190 RefPtr<SkPixelRef> pixelRef = adoptRef(new InvalidPixelRef(info)); 187 RefPtr<SkPixelRef> pixelRef = adoptRef(new InvalidPixelRef(info));
191 SkBitmap invalidBitmap; 188 SkBitmap invalidBitmap;
192 invalidBitmap.setInfo(info); 189 invalidBitmap.setInfo(info);
193 invalidBitmap.setPixelRef(pixelRef.get()); 190 invalidBitmap.setPixelRef(pixelRef.get());
194 RefPtr<NativeImageSkia> nativeImage = NativeImageSkia::create(invalidBitmap) ; 191 RefPtr<BitmapImage> image = BitmapImage::createWithOrientationForTesting(inv alidBitmap, OriginRightTop);
195 RefPtr<BitmapImage> image = BitmapImage::createWithOrientationForTesting(nat iveImage, OriginRightTop);
196 192
197 // Create a DragImage from it. In MSAN builds, this will cause a failure if 193 // Create a DragImage from it. In MSAN builds, this will cause a failure if
198 // the pixel memory is not initialized, if we have to respect non-default 194 // the pixel memory is not initialized, if we have to respect non-default
199 // orientation. 195 // orientation.
200 OwnPtr<DragImage> dragImage = DragImage::create(image.get(), RespectImageOri entation); 196 OwnPtr<DragImage> dragImage = DragImage::create(image.get(), RespectImageOri entation);
201 197
202 // The DragImage should be fully transparent. 198 // The DragImage should be fully transparent.
203 SkBitmap dragImageBitmap = dragImage->bitmap(); 199 const SkBitmap& dragImageBitmap = dragImage->bitmap();
204 SkAutoLockPixels lock(dragImageBitmap); 200 SkAutoLockPixels lock(dragImageBitmap);
205 ASSERT_NE(nullptr, dragImageBitmap.getPixels()); 201 ASSERT_NE(nullptr, dragImageBitmap.getPixels());
206 for (int x = 0; x < dragImageBitmap.width(); x++) { 202 for (int x = 0; x < dragImageBitmap.width(); x++) {
207 for (int y = 0; y < dragImageBitmap.height(); y++) { 203 for (int y = 0; y < dragImageBitmap.height(); y++) {
208 int alpha = SkColorGetA(dragImageBitmap.getColor(x, y)); 204 int alpha = SkColorGetA(dragImageBitmap.getColor(x, y));
209 ASSERT_EQ(0, alpha); 205 ASSERT_EQ(0, alpha);
210 } 206 }
211 } 207 }
212 } 208 }
213 209
214 TEST(DragImageTest, InterpolationNone) 210 TEST(DragImageTest, InterpolationNone)
215 { 211 {
216 SkBitmap expectedBitmap; 212 SkBitmap expectedBitmap;
217 expectedBitmap.allocN32Pixels(4, 4); 213 expectedBitmap.allocN32Pixels(4, 4);
218 { 214 {
219 SkAutoLockPixels lock(expectedBitmap); 215 SkAutoLockPixels lock(expectedBitmap);
220 expectedBitmap.eraseArea(SkIRect::MakeXYWH(0, 0, 2, 2), 0xFFFFFFFF); 216 expectedBitmap.eraseArea(SkIRect::MakeXYWH(0, 0, 2, 2), 0xFFFFFFFF);
221 expectedBitmap.eraseArea(SkIRect::MakeXYWH(0, 2, 2, 2), 0xFF000000); 217 expectedBitmap.eraseArea(SkIRect::MakeXYWH(0, 2, 2, 2), 0xFF000000);
222 expectedBitmap.eraseArea(SkIRect::MakeXYWH(2, 0, 2, 2), 0xFF000000); 218 expectedBitmap.eraseArea(SkIRect::MakeXYWH(2, 0, 2, 2), 0xFF000000);
223 expectedBitmap.eraseArea(SkIRect::MakeXYWH(2, 2, 2, 2), 0xFFFFFFFF); 219 expectedBitmap.eraseArea(SkIRect::MakeXYWH(2, 2, 2, 2), 0xFFFFFFFF);
224 } 220 }
225 221
226 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2))); 222 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
227 const SkBitmap& testBitmap = testImage->nativeImageForCurrentFrame()->bitmap (); 223 SkBitmap testBitmap = testImage->bitmapForCurrentFrame();
228 { 224 {
229 SkAutoLockPixels lock(testBitmap); 225 SkAutoLockPixels lock(testBitmap);
230 testBitmap.eraseArea(SkIRect::MakeXYWH(0, 0, 1, 1), 0xFFFFFFFF); 226 testBitmap.eraseArea(SkIRect::MakeXYWH(0, 0, 1, 1), 0xFFFFFFFF);
231 testBitmap.eraseArea(SkIRect::MakeXYWH(0, 1, 1, 1), 0xFF000000); 227 testBitmap.eraseArea(SkIRect::MakeXYWH(0, 1, 1, 1), 0xFF000000);
232 testBitmap.eraseArea(SkIRect::MakeXYWH(1, 0, 1, 1), 0xFF000000); 228 testBitmap.eraseArea(SkIRect::MakeXYWH(1, 0, 1, 1), 0xFF000000);
233 testBitmap.eraseArea(SkIRect::MakeXYWH(1, 1, 1, 1), 0xFFFFFFFF); 229 testBitmap.eraseArea(SkIRect::MakeXYWH(1, 1, 1, 1), 0xFFFFFFFF);
234 } 230 }
235 231
236 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get(), DoNotRespec tImageOrientation, 1, InterpolationNone); 232 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get(), DoNotRespec tImageOrientation, 1, InterpolationNone);
237 ASSERT_TRUE(dragImage); 233 ASSERT_TRUE(dragImage);
238 dragImage->scale(2, 2); 234 dragImage->scale(2, 2);
239 const SkBitmap& dragBitmap = dragImage->bitmap(); 235 const SkBitmap& dragBitmap = dragImage->bitmap();
240 { 236 {
241 SkAutoLockPixels lock1(dragBitmap); 237 SkAutoLockPixels lock1(dragBitmap);
242 SkAutoLockPixels lock2(expectedBitmap); 238 SkAutoLockPixels lock2(expectedBitmap);
243 for (int x = 0; x < dragBitmap.width(); ++x) 239 for (int x = 0; x < dragBitmap.width(); ++x)
244 for (int y = 0; y < dragBitmap.height(); ++y) 240 for (int y = 0; y < dragBitmap.height(); ++y)
245 EXPECT_EQ(expectedBitmap.getColor(x, y), dragBitmap.getColor(x, y)); 241 EXPECT_EQ(expectedBitmap.getColor(x, y), dragBitmap.getColor(x, y));
246 } 242 }
247 } 243 }
248 244
249 } // anonymous namespace 245 } // anonymous namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698