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 #include "platform/graphics/DeferredImageDecoder.h" | 27 #include "platform/graphics/DeferredImageDecoder.h" |
28 | 28 |
29 #include "SkBitmapDevice.h" | 29 #include "SkBitmapDevice.h" |
30 #include "SkCanvas.h" | 30 #include "SkCanvas.h" |
31 #include "SkPicture.h" | 31 #include "SkPicture.h" |
32 #include "SkPictureRecorder.h" | 32 #include "SkPictureRecorder.h" |
33 #include "SkSurface.h" | 33 #include "SkSurface.h" |
34 #include "platform/SharedBuffer.h" | 34 #include "platform/SharedBuffer.h" |
35 #include "platform/Task.h" | 35 #include "platform/Task.h" |
36 #include "platform/graphics/ImageDecodingStore.h" | 36 #include "platform/graphics/ImageDecodingStore.h" |
37 #include "platform/graphics/skia/NativeImageSkia.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 "wtf/PassRefPtr.h" | 41 #include "wtf/PassRefPtr.h" |
43 #include "wtf/RefPtr.h" | 42 #include "wtf/RefPtr.h" |
44 #include <gtest/gtest.h> | 43 #include <gtest/gtest.h> |
45 | 44 |
46 namespace blink { | 45 namespace blink { |
47 | 46 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 size_t m_frameCount; | 144 size_t m_frameCount; |
146 int m_repetitionCount; | 145 int m_repetitionCount; |
147 ImageFrame::Status m_status; | 146 ImageFrame::Status m_status; |
148 float m_frameDuration; | 147 float m_frameDuration; |
149 IntSize m_decodedSize; | 148 IntSize m_decodedSize; |
150 }; | 149 }; |
151 | 150 |
152 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) | 151 TEST_F(DeferredImageDecoderTest, drawIntoSkPicture) |
153 { | 152 { |
154 m_lazyDecoder->setData(*m_data, true); | 153 m_lazyDecoder->setData(*m_data, true); |
155 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); | 154 SkBitmap bitmap = m_lazyDecoder->createFrameAtIndex(0); |
156 EXPECT_EQ(1, image->bitmap().width()); | 155 EXPECT_EQ(1, bitmap.width()); |
157 EXPECT_EQ(1, image->bitmap().height()); | 156 EXPECT_EQ(1, bitmap.height()); |
158 EXPECT_FALSE(image->bitmap().isNull()); | 157 EXPECT_FALSE(bitmap.isNull()); |
159 EXPECT_TRUE(image->bitmap().isImmutable()); | 158 EXPECT_TRUE(bitmap.isImmutable()); |
160 | 159 |
161 SkPictureRecorder recorder; | 160 SkPictureRecorder recorder; |
162 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 161 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
163 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | 162 tempCanvas->drawBitmap(bitmap, 0, 0); |
164 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); | 163 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); |
165 EXPECT_EQ(0, m_frameBufferRequestCount); | 164 EXPECT_EQ(0, m_frameBufferRequestCount); |
166 | 165 |
167 m_surface->getCanvas()->drawPicture(picture.get()); | 166 m_surface->getCanvas()->drawPicture(picture.get()); |
168 EXPECT_EQ(0, m_frameBufferRequestCount); | 167 EXPECT_EQ(0, m_frameBufferRequestCount); |
169 | 168 |
170 SkBitmap canvasBitmap; | 169 SkBitmap canvasBitmap; |
171 canvasBitmap.allocN32Pixels(100, 100); | 170 canvasBitmap.allocN32Pixels(100, 100); |
172 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 171 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
173 SkAutoLockPixels autoLock(canvasBitmap); | 172 SkAutoLockPixels autoLock(canvasBitmap); |
174 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 173 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
175 } | 174 } |
176 | 175 |
177 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) | 176 TEST_F(DeferredImageDecoderTest, drawIntoSkPictureProgressive) |
178 { | 177 { |
179 RefPtr<SharedBuffer> partialData = SharedBuffer::create(m_data->data(), m_da
ta->size() - 10); | 178 RefPtr<SharedBuffer> partialData = SharedBuffer::create(m_data->data(), m_da
ta->size() - 10); |
180 | 179 |
181 // Received only half the file. | 180 // Received only half the file. |
182 m_lazyDecoder->setData(*partialData, false); | 181 m_lazyDecoder->setData(*partialData, false); |
183 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); | 182 SkBitmap bitmap = m_lazyDecoder->createFrameAtIndex(0); |
184 SkPictureRecorder recorder; | 183 SkPictureRecorder recorder; |
185 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 184 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
186 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | 185 tempCanvas->drawBitmap(bitmap, 0, 0); |
187 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); | 186 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); |
188 m_surface->getCanvas()->drawPicture(picture.get()); | 187 m_surface->getCanvas()->drawPicture(picture.get()); |
189 | 188 |
190 // Fully received the file and draw the SkPicture again. | 189 // Fully received the file and draw the SkPicture again. |
191 m_lazyDecoder->setData(*m_data, true); | 190 m_lazyDecoder->setData(*m_data, true); |
192 image = m_lazyDecoder->createFrameAtIndex(0); | 191 bitmap = m_lazyDecoder->createFrameAtIndex(0); |
193 tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 192 tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
194 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | 193 tempCanvas->drawBitmap(bitmap, 0, 0); |
195 picture = adoptRef(recorder.endRecording()); | 194 picture = adoptRef(recorder.endRecording()); |
196 m_surface->getCanvas()->drawPicture(picture.get()); | 195 m_surface->getCanvas()->drawPicture(picture.get()); |
197 | 196 |
198 SkBitmap canvasBitmap; | 197 SkBitmap canvasBitmap; |
199 canvasBitmap.allocN32Pixels(100, 100); | 198 canvasBitmap.allocN32Pixels(100, 100); |
200 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 199 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
201 SkAutoLockPixels autoLock(canvasBitmap); | 200 SkAutoLockPixels autoLock(canvasBitmap); |
202 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 201 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
203 } | 202 } |
204 | 203 |
205 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) | 204 static void rasterizeMain(SkCanvas* canvas, SkPicture* picture) |
206 { | 205 { |
207 canvas->drawPicture(picture); | 206 canvas->drawPicture(picture); |
208 } | 207 } |
209 | 208 |
210 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) | 209 TEST_F(DeferredImageDecoderTest, decodeOnOtherThread) |
211 { | 210 { |
212 m_lazyDecoder->setData(*m_data, true); | 211 m_lazyDecoder->setData(*m_data, true); |
213 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); | 212 SkBitmap bitmap = m_lazyDecoder->createFrameAtIndex(0); |
214 EXPECT_EQ(1, image->bitmap().width()); | 213 EXPECT_EQ(1, bitmap.width()); |
215 EXPECT_EQ(1, image->bitmap().height()); | 214 EXPECT_EQ(1, bitmap.height()); |
216 EXPECT_FALSE(image->bitmap().isNull()); | 215 EXPECT_FALSE(bitmap.isNull()); |
217 EXPECT_TRUE(image->bitmap().isImmutable()); | 216 EXPECT_TRUE(bitmap.isImmutable()); |
218 | 217 |
219 SkPictureRecorder recorder; | 218 SkPictureRecorder recorder; |
220 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 219 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
221 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | 220 tempCanvas->drawBitmap(bitmap, 0, 0); |
222 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); | 221 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); |
223 EXPECT_EQ(0, m_frameBufferRequestCount); | 222 EXPECT_EQ(0, m_frameBufferRequestCount); |
224 | 223 |
225 // Create a thread to rasterize SkPicture. | 224 // Create a thread to rasterize SkPicture. |
226 OwnPtr<WebThread> thread = adoptPtr(Platform::current()->createThread("Raste
rThread")); | 225 OwnPtr<WebThread> thread = adoptPtr(Platform::current()->createThread("Raste
rThread")); |
227 thread->postTask(FROM_HERE, new Task(WTF::bind(&rasterizeMain, m_surface->ge
tCanvas(), picture.get()))); | 226 thread->postTask(FROM_HERE, new Task(WTF::bind(&rasterizeMain, m_surface->ge
tCanvas(), picture.get()))); |
228 thread.clear(); | 227 thread.clear(); |
229 EXPECT_EQ(0, m_frameBufferRequestCount); | 228 EXPECT_EQ(0, m_frameBufferRequestCount); |
230 | 229 |
231 SkBitmap canvasBitmap; | 230 SkBitmap canvasBitmap; |
232 canvasBitmap.allocN32Pixels(100, 100); | 231 canvasBitmap.allocN32Pixels(100, 100); |
233 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); | 232 ASSERT_TRUE(m_surface->getCanvas()->readPixels(&canvasBitmap, 0, 0)); |
234 SkAutoLockPixels autoLock(canvasBitmap); | 233 SkAutoLockPixels autoLock(canvasBitmap); |
235 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); | 234 EXPECT_EQ(SkColorSetARGB(255, 255, 255, 255), canvasBitmap.getColor(0, 0)); |
236 } | 235 } |
237 | 236 |
238 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) | 237 TEST_F(DeferredImageDecoderTest, singleFrameImageLoading) |
239 { | 238 { |
240 m_status = ImageFrame::FramePartial; | 239 m_status = ImageFrame::FramePartial; |
241 m_lazyDecoder->setData(*m_data, false); | 240 m_lazyDecoder->setData(*m_data, false); |
242 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 241 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
243 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenerat
ionID(); | 242 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0).getGenerationID(); |
244 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 243 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
245 EXPECT_TRUE(m_actualDecoder); | 244 EXPECT_TRUE(m_actualDecoder); |
246 | 245 |
247 m_status = ImageFrame::FrameComplete; | 246 m_status = ImageFrame::FrameComplete; |
248 m_data->append(" ", 1); | 247 m_data->append(" ", 1); |
249 m_lazyDecoder->setData(*m_data, true); | 248 m_lazyDecoder->setData(*m_data, true); |
250 EXPECT_FALSE(m_actualDecoder); | 249 EXPECT_FALSE(m_actualDecoder); |
251 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 250 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
252 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenera
tionID(); | 251 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0).getGenerationID(); |
253 EXPECT_FALSE(m_frameBufferRequestCount); | 252 EXPECT_FALSE(m_frameBufferRequestCount); |
254 EXPECT_NE(firstId, secondId); | 253 EXPECT_NE(firstId, secondId); |
255 } | 254 } |
256 | 255 |
257 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading) | 256 TEST_F(DeferredImageDecoderTest, multiFrameImageLoading) |
258 { | 257 { |
259 m_repetitionCount = 10; | 258 m_repetitionCount = 10; |
260 m_frameCount = 1; | 259 m_frameCount = 1; |
261 m_frameDuration = 10; | 260 m_frameDuration = 10; |
262 m_status = ImageFrame::FramePartial; | 261 m_status = ImageFrame::FramePartial; |
263 m_lazyDecoder->setData(*m_data, false); | 262 m_lazyDecoder->setData(*m_data, false); |
264 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenerat
ionID(); | 263 unsigned firstId = m_lazyDecoder->createFrameAtIndex(0).getGenerationID(); |
265 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 264 EXPECT_FALSE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
266 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); | 265 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); |
267 | 266 |
268 m_frameCount = 2; | 267 m_frameCount = 2; |
269 m_frameDuration = 20; | 268 m_frameDuration = 20; |
270 m_status = ImageFrame::FrameComplete; | 269 m_status = ImageFrame::FrameComplete; |
271 m_data->append(" ", 1); | 270 m_data->append(" ", 1); |
272 m_lazyDecoder->setData(*m_data, false); | 271 m_lazyDecoder->setData(*m_data, false); |
273 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0)->bitmap().getGenera
tionID(); | 272 unsigned secondId = m_lazyDecoder->createFrameAtIndex(0).getGenerationID(); |
274 EXPECT_NE(firstId, secondId); | 273 EXPECT_NE(firstId, secondId); |
275 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 274 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
276 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); | 275 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); |
277 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); | 276 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); |
278 EXPECT_TRUE(m_actualDecoder); | 277 EXPECT_TRUE(m_actualDecoder); |
279 | 278 |
280 m_frameCount = 3; | 279 m_frameCount = 3; |
281 m_frameDuration = 30; | 280 m_frameDuration = 30; |
282 m_status = ImageFrame::FrameComplete; | 281 m_status = ImageFrame::FrameComplete; |
283 m_lazyDecoder->setData(*m_data, true); | 282 m_lazyDecoder->setData(*m_data, true); |
284 EXPECT_FALSE(m_actualDecoder); | 283 EXPECT_FALSE(m_actualDecoder); |
285 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); | 284 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(0)); |
286 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); | 285 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(1)); |
287 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2)); | 286 EXPECT_TRUE(m_lazyDecoder->frameIsCompleteAtIndex(2)); |
288 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); | 287 EXPECT_EQ(10.0f, m_lazyDecoder->frameDurationAtIndex(0)); |
289 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); | 288 EXPECT_EQ(20.0f, m_lazyDecoder->frameDurationAtIndex(1)); |
290 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2)); | 289 EXPECT_EQ(30.0f, m_lazyDecoder->frameDurationAtIndex(2)); |
291 EXPECT_EQ(10, m_lazyDecoder->repetitionCount()); | 290 EXPECT_EQ(10, m_lazyDecoder->repetitionCount()); |
292 } | 291 } |
293 | 292 |
294 TEST_F(DeferredImageDecoderTest, decodedSize) | 293 TEST_F(DeferredImageDecoderTest, decodedSize) |
295 { | 294 { |
296 m_decodedSize = IntSize(22, 33); | 295 m_decodedSize = IntSize(22, 33); |
297 m_lazyDecoder->setData(*m_data, true); | 296 m_lazyDecoder->setData(*m_data, true); |
298 RefPtr<NativeImageSkia> image = m_lazyDecoder->createFrameAtIndex(0); | 297 SkBitmap bitmap = m_lazyDecoder->createFrameAtIndex(0); |
299 EXPECT_EQ(m_decodedSize.width(), image->bitmap().width()); | 298 EXPECT_EQ(m_decodedSize.width(), bitmap.width()); |
300 EXPECT_EQ(m_decodedSize.height(), image->bitmap().height()); | 299 EXPECT_EQ(m_decodedSize.height(), bitmap.height()); |
301 EXPECT_FALSE(image->bitmap().isNull()); | 300 EXPECT_FALSE(bitmap.isNull()); |
302 EXPECT_TRUE(image->bitmap().isImmutable()); | 301 EXPECT_TRUE(bitmap.isImmutable()); |
303 | 302 |
304 useMockImageDecoderFactory(); | 303 useMockImageDecoderFactory(); |
305 | 304 |
306 // The following code should not fail any assert. | 305 // The following code should not fail any assert. |
307 SkPictureRecorder recorder; | 306 SkPictureRecorder recorder; |
308 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); | 307 SkCanvas* tempCanvas = recorder.beginRecording(100, 100, 0, 0); |
309 tempCanvas->drawBitmap(image->bitmap(), 0, 0); | 308 tempCanvas->drawBitmap(bitmap, 0, 0); |
310 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); | 309 RefPtr<SkPicture> picture = adoptRef(recorder.endRecording()); |
311 EXPECT_EQ(0, m_frameBufferRequestCount); | 310 EXPECT_EQ(0, m_frameBufferRequestCount); |
312 m_surface->getCanvas()->drawPicture(picture.get()); | 311 m_surface->getCanvas()->drawPicture(picture.get()); |
313 EXPECT_EQ(1, m_frameBufferRequestCount); | 312 EXPECT_EQ(1, m_frameBufferRequestCount); |
314 } | 313 } |
315 | 314 |
316 TEST_F(DeferredImageDecoderTest, smallerFrameCount) | 315 TEST_F(DeferredImageDecoderTest, smallerFrameCount) |
317 { | 316 { |
318 m_frameCount = 1; | 317 m_frameCount = 1; |
319 m_lazyDecoder->setData(*m_data, false); | 318 m_lazyDecoder->setData(*m_data, false); |
320 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 319 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
321 m_frameCount = 2; | 320 m_frameCount = 2; |
322 m_lazyDecoder->setData(*m_data, false); | 321 m_lazyDecoder->setData(*m_data, false); |
323 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 322 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
324 m_frameCount = 0; | 323 m_frameCount = 0; |
325 m_lazyDecoder->setData(*m_data, true); | 324 m_lazyDecoder->setData(*m_data, true); |
326 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); | 325 EXPECT_EQ(m_frameCount, m_lazyDecoder->frameCount()); |
327 } | 326 } |
328 | 327 |
329 } // namespace blink | 328 } // namespace blink |
OLD | NEW |