OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #include <memory> | 28 #include <memory> |
29 #include "platform/CrossThreadFunctional.h" | 29 #include "platform/CrossThreadFunctional.h" |
30 #include "platform/SharedBuffer.h" | 30 #include "platform/SharedBuffer.h" |
31 #include "platform/WebTaskRunner.h" | 31 #include "platform/WebTaskRunner.h" |
32 #include "platform/graphics/ImageDecodingStore.h" | 32 #include "platform/graphics/ImageDecodingStore.h" |
33 #include "platform/graphics/ImageFrameGenerator.h" | 33 #include "platform/graphics/ImageFrameGenerator.h" |
34 #include "platform/graphics/paint/PaintCanvas.h" | 34 #include "platform/graphics/paint/PaintCanvas.h" |
35 #include "platform/graphics/paint/PaintRecord.h" | 35 #include "platform/graphics/paint/PaintRecord.h" |
36 #include "platform/graphics/paint/PaintRecorder.h" | 36 #include "platform/graphics/paint/PaintRecorder.h" |
37 #include "platform/graphics/paint/PaintSurface.h" | |
38 #include "platform/graphics/test/MockImageDecoder.h" | 37 #include "platform/graphics/test/MockImageDecoder.h" |
39 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
40 #include "public/platform/WebThread.h" | 39 #include "public/platform/WebThread.h" |
41 #include "public/platform/WebTraceLocation.h" | 40 #include "public/platform/WebTraceLocation.h" |
42 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
43 #include "third_party/skia/include/core/SkImage.h" | 42 #include "third_party/skia/include/core/SkImage.h" |
44 #include "third_party/skia/include/core/SkPixmap.h" | 43 #include "third_party/skia/include/core/SkPixmap.h" |
45 #include "third_party/skia/include/core/SkSurface.h" | 44 #include "third_party/skia/include/core/SkSurface.h" |
46 #include "wtf/PassRefPtr.h" | 45 #include "wtf/PassRefPtr.h" |
47 #include "wtf/PtrUtil.h" | 46 #include "wtf/PtrUtil.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 public MockImageDecoderClient { | 101 public MockImageDecoderClient { |
103 public: | 102 public: |
104 void SetUp() override { | 103 void SetUp() override { |
105 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); | 104 ImageDecodingStore::instance().setCacheLimitInBytes(1024 * 1024); |
106 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); | 105 m_data = SharedBuffer::create(whitePNG, sizeof(whitePNG)); |
107 m_frameCount = 1; | 106 m_frameCount = 1; |
108 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); | 107 std::unique_ptr<MockImageDecoder> decoder = MockImageDecoder::create(this); |
109 m_actualDecoder = decoder.get(); | 108 m_actualDecoder = decoder.get(); |
110 m_actualDecoder->setSize(1, 1); | 109 m_actualDecoder->setSize(1, 1); |
111 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); | 110 m_lazyDecoder = DeferredImageDecoder::createForTesting(std::move(decoder)); |
112 m_surface = PaintSurface::MakeRasterN32Premul(100, 100); | 111 m_bitmap.allocPixels(SkImageInfo::MakeN32Premul(100, 100)); |
113 ASSERT_TRUE(m_surface.get()); | 112 m_canvas = base::MakeUnique<cc::SkiaPaintCanvas>(m_bitmap); |
114 m_decodeRequestCount = 0; | 113 m_decodeRequestCount = 0; |
115 m_repetitionCount = cAnimationNone; | 114 m_repetitionCount = cAnimationNone; |
116 m_status = ImageFrame::FrameComplete; | 115 m_status = ImageFrame::FrameComplete; |
117 m_frameDuration = 0; | 116 m_frameDuration = 0; |
118 m_decodedSize = m_actualDecoder->size(); | 117 m_decodedSize = m_actualDecoder->size(); |
119 } | 118 } |
120 | 119 |
121 void TearDown() override { ImageDecodingStore::instance().clear(); } | 120 void TearDown() override { ImageDecodingStore::instance().clear(); } |
122 | 121 |
123 void decoderBeingDestroyed() override { m_actualDecoder = 0; } | 122 void decoderBeingDestroyed() override { m_actualDecoder = 0; } |
(...skipping 12 matching lines...) Expand all Loading... |
136 | 135 |
137 protected: | 136 protected: |
138 void useMockImageDecoderFactory() { | 137 void useMockImageDecoderFactory() { |
139 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( | 138 m_lazyDecoder->frameGenerator()->setImageDecoderFactory( |
140 MockImageDecoderFactory::create(this, m_decodedSize)); | 139 MockImageDecoderFactory::create(this, m_decodedSize)); |
141 } | 140 } |
142 | 141 |
143 // Don't own this but saves the pointer to query states. | 142 // Don't own this but saves the pointer to query states. |
144 MockImageDecoder* m_actualDecoder; | 143 MockImageDecoder* m_actualDecoder; |
145 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; | 144 std::unique_ptr<DeferredImageDecoder> m_lazyDecoder; |
146 sk_sp<PaintSurface> m_surface; | 145 SkBitmap m_bitmap; |
| 146 std::unique_ptr<cc::PaintCanvas> m_canvas; |
147 int m_decodeRequestCount; | 147 int m_decodeRequestCount; |
148 RefPtr<SharedBuffer> m_data; | 148 RefPtr<SharedBuffer> m_data; |
149 size_t m_frameCount; | 149 size_t m_frameCount; |
150 int m_repetitionCount; | 150 int m_repetitionCount; |
151 ImageFrame::Status m_status; | 151 ImageFrame::Status m_status; |
152 float m_frameDuration; | 152 float m_frameDuration; |
153 IntSize m_decodedSize; | 153 IntSize m_decodedSize; |
154 }; | 154 }; |
155 | 155 |
156 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecord) { | 156 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecord) { |
157 m_lazyDecoder->setData(m_data, true); | 157 m_lazyDecoder->setData(m_data, true); |
158 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 158 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
159 ASSERT_TRUE(image); | 159 ASSERT_TRUE(image); |
160 EXPECT_EQ(1, image->width()); | 160 EXPECT_EQ(1, image->width()); |
161 EXPECT_EQ(1, image->height()); | 161 EXPECT_EQ(1, image->height()); |
162 | 162 |
163 PaintRecorder recorder; | 163 PaintRecorder recorder; |
164 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); | 164 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); |
165 tempCanvas->drawImage(image, 0, 0); | 165 tempCanvas->drawImage(image, 0, 0); |
166 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); | 166 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); |
167 EXPECT_EQ(0, m_decodeRequestCount); | 167 EXPECT_EQ(0, m_decodeRequestCount); |
168 | 168 |
169 m_surface->getCanvas()->drawPicture(record); | 169 m_canvas->drawPicture(record); |
170 EXPECT_EQ(0, m_decodeRequestCount); | 170 EXPECT_EQ(0, m_decodeRequestCount); |
171 | 171 |
172 SkBitmap canvasBitmap; | 172 SkAutoLockPixels autoLock(m_bitmap); |
173 canvasBitmap.allocN32Pixels(100, 100); | 173 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), m_bitmap.getColor(0, 0)); |
174 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | |
175 SkAutoLockPixels autoLock(canvasBitmap); | |
176 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
177 } | 174 } |
178 | 175 |
179 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecordProgressive) { | 176 TEST_F(DeferredImageDecoderTest, drawIntoPaintRecordProgressive) { |
180 RefPtr<SharedBuffer> partialData = | 177 RefPtr<SharedBuffer> partialData = |
181 SharedBuffer::create(m_data->data(), m_data->size() - 10); | 178 SharedBuffer::create(m_data->data(), m_data->size() - 10); |
182 | 179 |
183 // Received only half the file. | 180 // Received only half the file. |
184 m_lazyDecoder->setData(partialData, false); | 181 m_lazyDecoder->setData(partialData, false); |
185 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 182 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
186 ASSERT_TRUE(image); | 183 ASSERT_TRUE(image); |
187 PaintRecorder recorder; | 184 PaintRecorder recorder; |
188 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); | 185 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); |
189 tempCanvas->drawImage(std::move(image), 0, 0); | 186 tempCanvas->drawImage(std::move(image), 0, 0); |
190 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); | 187 m_canvas->drawPicture(recorder.finishRecordingAsPicture()); |
191 | 188 |
192 // Fully received the file and draw the PaintRecord again. | 189 // Fully received the file and draw the PaintRecord again. |
193 m_lazyDecoder->setData(m_data, true); | 190 m_lazyDecoder->setData(m_data, true); |
194 image = m_lazyDecoder->createFrameAtIndex(0); | 191 image = m_lazyDecoder->createFrameAtIndex(0); |
195 ASSERT_TRUE(image); | 192 ASSERT_TRUE(image); |
196 tempCanvas = recorder.beginRecording(100, 100); | 193 tempCanvas = recorder.beginRecording(100, 100); |
197 tempCanvas->drawImage(std::move(image), 0, 0); | 194 tempCanvas->drawImage(std::move(image), 0, 0); |
198 m_surface->getCanvas()->drawPicture(recorder.finishRecordingAsPicture()); | 195 m_canvas->drawPicture(recorder.finishRecordingAsPicture()); |
199 | 196 |
200 SkBitmap canvasBitmap; | 197 SkAutoLockPixels autoLock(m_bitmap); |
201 canvasBitmap.allocN32Pixels(100, 100); | 198 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), m_bitmap.getColor(0, 0)); |
202 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | |
203 SkAutoLockPixels autoLock(canvasBitmap); | |
204 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
205 } | 199 } |
206 | 200 |
207 static void rasterizeMain(PaintCanvas* canvas, sk_sp<PaintRecord> record) { | 201 static void rasterizeMain(PaintCanvas* canvas, sk_sp<PaintRecord> record) { |
208 canvas->drawPicture(record); | 202 canvas->drawPicture(record); |
209 } | 203 } |
210 | 204 |
211 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { | 205 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) { |
212 m_lazyDecoder->setData(m_data, true); | 206 m_lazyDecoder->setData(m_data, true); |
213 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 207 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
214 ASSERT_TRUE(image); | 208 ASSERT_TRUE(image); |
215 EXPECT_EQ(1, image->width()); | 209 EXPECT_EQ(1, image->width()); |
216 EXPECT_EQ(1, image->height()); | 210 EXPECT_EQ(1, image->height()); |
217 | 211 |
218 PaintRecorder recorder; | 212 PaintRecorder recorder; |
219 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); | 213 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); |
220 tempCanvas->drawImage(std::move(image), 0, 0); | 214 tempCanvas->drawImage(std::move(image), 0, 0); |
221 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); | 215 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); |
222 EXPECT_EQ(0, m_decodeRequestCount); | 216 EXPECT_EQ(0, m_decodeRequestCount); |
223 | 217 |
224 // Create a thread to rasterize PaintRecord. | 218 // Create a thread to rasterize PaintRecord. |
225 std::unique_ptr<WebThread> thread = | 219 std::unique_ptr<WebThread> thread = |
226 WTF::wrapUnique(Platform::current()->createThread("RasterThread")); | 220 WTF::wrapUnique(Platform::current()->createThread("RasterThread")); |
227 thread->getWebTaskRunner()->postTask( | 221 thread->getWebTaskRunner()->postTask( |
228 BLINK_FROM_HERE, | 222 BLINK_FROM_HERE, |
229 crossThreadBind(&rasterizeMain, | 223 crossThreadBind(&rasterizeMain, crossThreadUnretained(m_canvas.get()), |
230 crossThreadUnretained(m_surface->getCanvas()), record)); | 224 record)); |
231 thread.reset(); | 225 thread.reset(); |
232 EXPECT_EQ(0, m_decodeRequestCount); | 226 EXPECT_EQ(0, m_decodeRequestCount); |
233 | 227 |
234 SkBitmap canvasBitmap; | 228 SkAutoLockPixels autoLock(m_bitmap); |
235 canvasBitmap.allocN32Pixels(100, 100); | 229 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), m_bitmap.getColor(0, 0)); |
236 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | |
237 SkAutoLockPixels autoLock(canvasBitmap); | |
238 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | |
239 } | 230 } |
240 | 231 |
241 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) { | 232 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) { |
242 m_status = ImageFrame::FramePartial; | 233 m_status = ImageFrame::FramePartial; |
243 m_lazyDecoder->setData(m_data, false); | 234 m_lazyDecoder->setData(m_data, false); |
244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 235 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
245 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); | 236 sk_sp<SkImage> image = m_lazyDecoder->createFrameAtIndex(0); |
246 ASSERT_TRUE(image); | 237 ASSERT_TRUE(image); |
247 unsigned firstId = image->uniqueID(); | 238 unsigned firstId = image->uniqueID(); |
248 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 239 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 EXPECT_EQ(m_decodedSize.height(), image->height()); | 303 EXPECT_EQ(m_decodedSize.height(), image->height()); |
313 | 304 |
314 useMockImageDecoderFactory(); | 305 useMockImageDecoderFactory(); |
315 | 306 |
316 // The following code should not fail any assert. | 307 // The following code should not fail any assert. |
317 PaintRecorder recorder; | 308 PaintRecorder recorder; |
318 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); | 309 PaintCanvas* tempCanvas = recorder.beginRecording(100, 100); |
319 tempCanvas->drawImage(std::move(image), 0, 0); | 310 tempCanvas->drawImage(std::move(image), 0, 0); |
320 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); | 311 sk_sp<PaintRecord> record = recorder.finishRecordingAsPicture(); |
321 EXPECT_EQ(0, m_decodeRequestCount); | 312 EXPECT_EQ(0, m_decodeRequestCount); |
322 m_surface->getCanvas()->drawPicture(record); | 313 m_canvas->drawPicture(record); |
323 EXPECT_EQ(1, m_decodeRequestCount); | 314 EXPECT_EQ(1, m_decodeRequestCount); |
324 } | 315 } |
325 | 316 |
326 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { | 317 TEST_F(DeferredImageDecoderTest, smallerFrameCount) { |
327 m_frameCount = 1; | 318 m_frameCount = 1; |
328 m_lazyDecoder->setData(m_data, false); | 319 m_lazyDecoder->setData(m_data, false); |
329 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 320 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
330 m_frameCount = 2; | 321 m_frameCount = 2; |
331 m_lazyDecoder->setData(m_data, false); | 322 m_lazyDecoder->setData(m_data, false); |
332 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 323 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 SharedBuffer::create(m_data->data(), m_data->size()); | 385 SharedBuffer::create(m_data->data(), m_data->size()); |
395 EXPECT_EQ(originalData->size(), m_data->size()); | 386 EXPECT_EQ(originalData->size(), m_data->size()); |
396 m_lazyDecoder->setData(originalData, false); | 387 m_lazyDecoder->setData(originalData, false); |
397 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); | 388 RefPtr<SharedBuffer> newData = m_lazyDecoder->data(); |
398 EXPECT_EQ(originalData->size(), newData->size()); | 389 EXPECT_EQ(originalData->size(), newData->size()); |
399 EXPECT_EQ( | 390 EXPECT_EQ( |
400 0, std::memcmp(originalData->data(), newData->data(), newData->size())); | 391 0, std::memcmp(originalData->data(), newData->data(), newData->size())); |
401 } | 392 } |
402 | 393 |
403 } // namespace blink | 394 } // namespace blink |
OLD | NEW |