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