OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, 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 26 matching lines...) Expand all Loading... |
37 #include "core/html/HTMLImageElement.h" | 37 #include "core/html/HTMLImageElement.h" |
38 #include "core/html/HTMLVideoElement.h" | 38 #include "core/html/HTMLVideoElement.h" |
39 #include "core/html/ImageData.h" | 39 #include "core/html/ImageData.h" |
40 #include "core/loader/resource/ImageResourceContent.h" | 40 #include "core/loader/resource/ImageResourceContent.h" |
41 #include "platform/graphics/StaticBitmapImage.h" | 41 #include "platform/graphics/StaticBitmapImage.h" |
42 #include "platform/graphics/skia/SkiaUtils.h" | 42 #include "platform/graphics/skia/SkiaUtils.h" |
43 #include "platform/heap/Handle.h" | 43 #include "platform/heap/Handle.h" |
44 #include "platform/image-decoders/ImageDecoder.h" | 44 #include "platform/image-decoders/ImageDecoder.h" |
45 #include "platform/loader/fetch/MemoryCache.h" | 45 #include "platform/loader/fetch/MemoryCache.h" |
46 #include "platform/loader/fetch/ResourceRequest.h" | 46 #include "platform/loader/fetch/ResourceRequest.h" |
| 47 #include "platform/scheduler/test/fake_web_task_runner.h" |
47 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
48 #include "third_party/skia/include/core/SkCanvas.h" | 49 #include "third_party/skia/include/core/SkCanvas.h" |
49 #include "third_party/skia/include/core/SkColorSpaceXform.h" | 50 #include "third_party/skia/include/core/SkColorSpaceXform.h" |
50 #include "third_party/skia/include/core/SkImage.h" | 51 #include "third_party/skia/include/core/SkImage.h" |
51 #include "third_party/skia/include/core/SkSurface.h" | 52 #include "third_party/skia/include/core/SkSurface.h" |
52 #include "third_party/skia/include/core/SkSwizzle.h" | 53 #include "third_party/skia/include/core/SkSwizzle.h" |
53 | 54 |
54 namespace blink { | 55 namespace blink { |
55 | 56 |
56 class ExceptionState; | 57 class ExceptionState; |
(...skipping 13 matching lines...) Expand all Loading... |
70 global_memory_cache_ = ReplaceMemoryCacheForTesting(MemoryCache::Create()); | 71 global_memory_cache_ = ReplaceMemoryCacheForTesting(MemoryCache::Create()); |
71 | 72 |
72 // Save the state of experimental canvas features and color correct | 73 // Save the state of experimental canvas features and color correct |
73 // rendering flags to restore them on teardown. | 74 // rendering flags to restore them on teardown. |
74 experimental_canvas_features = | 75 experimental_canvas_features = |
75 RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); | 76 RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); |
76 color_correct_rendering = | 77 color_correct_rendering = |
77 RuntimeEnabledFeatures::colorCorrectRenderingEnabled(); | 78 RuntimeEnabledFeatures::colorCorrectRenderingEnabled(); |
78 color_correct_rendering_default_mode = | 79 color_correct_rendering_default_mode = |
79 RuntimeEnabledFeatures::colorCorrectRenderingDefaultModeEnabled(); | 80 RuntimeEnabledFeatures::colorCorrectRenderingDefaultModeEnabled(); |
| 81 |
| 82 task_runner_ = AdoptRef(new scheduler::FakeWebTaskRunner); |
80 } | 83 } |
81 virtual void TearDown() { | 84 virtual void TearDown() { |
82 // Garbage collection is required prior to switching out the | 85 // Garbage collection is required prior to switching out the |
83 // test's memory cache; image resources are released, evicting | 86 // test's memory cache; image resources are released, evicting |
84 // them from the cache. | 87 // them from the cache. |
85 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | 88 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, |
86 BlinkGC::kGCWithSweep, | 89 BlinkGC::kGCWithSweep, |
87 BlinkGC::kForcedGC); | 90 BlinkGC::kForcedGC); |
88 | 91 |
89 ReplaceMemoryCacheForTesting(global_memory_cache_.Release()); | 92 ReplaceMemoryCacheForTesting(global_memory_cache_.Release()); |
90 RuntimeEnabledFeatures::setExperimentalCanvasFeaturesEnabled( | 93 RuntimeEnabledFeatures::setExperimentalCanvasFeaturesEnabled( |
91 experimental_canvas_features); | 94 experimental_canvas_features); |
92 RuntimeEnabledFeatures::setColorCorrectRenderingEnabled( | 95 RuntimeEnabledFeatures::setColorCorrectRenderingEnabled( |
93 color_correct_rendering); | 96 color_correct_rendering); |
94 RuntimeEnabledFeatures::setColorCorrectRenderingDefaultModeEnabled( | 97 RuntimeEnabledFeatures::setColorCorrectRenderingDefaultModeEnabled( |
95 color_correct_rendering_default_mode); | 98 color_correct_rendering_default_mode); |
96 } | 99 } |
97 | 100 |
98 sk_sp<SkImage> image_, image2_; | 101 sk_sp<SkImage> image_, image2_; |
99 Persistent<MemoryCache> global_memory_cache_; | 102 Persistent<MemoryCache> global_memory_cache_; |
100 bool experimental_canvas_features; | 103 bool experimental_canvas_features; |
101 bool color_correct_rendering; | 104 bool color_correct_rendering; |
102 bool color_correct_rendering_default_mode; | 105 bool color_correct_rendering_default_mode; |
| 106 RefPtr<scheduler::FakeWebTaskRunner> task_runner_; |
103 }; | 107 }; |
104 | 108 |
105 TEST_F(ImageBitmapTest, ImageResourceConsistency) { | 109 TEST_F(ImageBitmapTest, ImageResourceConsistency) { |
106 const ImageBitmapOptions default_options; | 110 const ImageBitmapOptions default_options; |
107 HTMLImageElement* image_element = | 111 HTMLImageElement* image_element = |
108 HTMLImageElement::Create(*Document::Create()); | 112 HTMLImageElement::Create(*Document::Create()); |
109 ImageResourceContent* image = | 113 ImageResourceContent* image = ImageResourceContent::Create( |
110 ImageResourceContent::Create(StaticBitmapImage::Create(image_).Get()); | 114 task_runner_, StaticBitmapImage::Create(image_).Get()); |
111 image_element->SetImageResource(image); | 115 image_element->SetImageResource(image); |
112 | 116 |
113 Optional<IntRect> crop_rect = | 117 Optional<IntRect> crop_rect = |
114 IntRect(0, 0, image_->width(), image_->height()); | 118 IntRect(0, 0, image_->width(), image_->height()); |
115 ImageBitmap* image_bitmap_no_crop = | 119 ImageBitmap* image_bitmap_no_crop = |
116 ImageBitmap::Create(image_element, crop_rect, | 120 ImageBitmap::Create(image_element, crop_rect, |
117 &(image_element->GetDocument()), default_options); | 121 &(image_element->GetDocument()), default_options); |
118 crop_rect = IntRect(image_->width() / 2, image_->height() / 2, | 122 crop_rect = IntRect(image_->width() / 2, image_->height() / 2, |
119 image_->width() / 2, image_->height() / 2); | 123 image_->width() / 2, image_->height() / 2); |
120 ImageBitmap* image_bitmap_interior_crop = | 124 ImageBitmap* image_bitmap_interior_crop = |
(...skipping 20 matching lines...) Expand all Loading... |
141 RefPtr<StaticBitmapImage> empty_image = | 145 RefPtr<StaticBitmapImage> empty_image = |
142 image_bitmap_outside_crop->BitmapImage(); | 146 image_bitmap_outside_crop->BitmapImage(); |
143 ASSERT_NE(empty_image->ImageForCurrentFrame(), | 147 ASSERT_NE(empty_image->ImageForCurrentFrame(), |
144 image_element->CachedImage()->GetImage()->ImageForCurrentFrame()); | 148 image_element->CachedImage()->GetImage()->ImageForCurrentFrame()); |
145 } | 149 } |
146 | 150 |
147 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a | 151 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a |
148 // reference to the original Image if the HTMLImageElement src is changed. | 152 // reference to the original Image if the HTMLImageElement src is changed. |
149 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) { | 153 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) { |
150 HTMLImageElement* image = HTMLImageElement::Create(*Document::Create()); | 154 HTMLImageElement* image = HTMLImageElement::Create(*Document::Create()); |
151 ImageResourceContent* original_image_resource = | 155 ImageResourceContent* original_image_resource = ImageResourceContent::Create( |
152 ImageResourceContent::Create(StaticBitmapImage::Create(image_).Get()); | 156 task_runner_, StaticBitmapImage::Create(image_).Get()); |
153 image->SetImageResource(original_image_resource); | 157 image->SetImageResource(original_image_resource); |
154 | 158 |
155 const ImageBitmapOptions default_options; | 159 const ImageBitmapOptions default_options; |
156 Optional<IntRect> crop_rect = | 160 Optional<IntRect> crop_rect = |
157 IntRect(0, 0, image_->width(), image_->height()); | 161 IntRect(0, 0, image_->width(), image_->height()); |
158 ImageBitmap* image_bitmap = ImageBitmap::Create( | 162 ImageBitmap* image_bitmap = ImageBitmap::Create( |
159 image, crop_rect, &(image->GetDocument()), default_options); | 163 image, crop_rect, &(image->GetDocument()), default_options); |
160 // As we are applying color space conversion for the "default" mode, | 164 // As we are applying color space conversion for the "default" mode, |
161 // this verifies that the color corrected image is not the same as the | 165 // this verifies that the color corrected image is not the same as the |
162 // source. | 166 // source. |
163 ASSERT_NE(image_bitmap->BitmapImage()->ImageForCurrentFrame(), | 167 ASSERT_NE(image_bitmap->BitmapImage()->ImageForCurrentFrame(), |
164 original_image_resource->GetImage()->ImageForCurrentFrame()); | 168 original_image_resource->GetImage()->ImageForCurrentFrame()); |
165 | 169 |
166 ImageResourceContent* new_image_resource = | 170 ImageResourceContent* new_image_resource = ImageResourceContent::Create( |
167 ImageResourceContent::Create(StaticBitmapImage::Create(image2_).Get()); | 171 task_runner_, StaticBitmapImage::Create(image2_).Get()); |
168 image->SetImageResource(new_image_resource); | 172 image->SetImageResource(new_image_resource); |
169 | 173 |
170 { | 174 { |
171 ASSERT_NE(image_bitmap->BitmapImage()->ImageForCurrentFrame(), | 175 ASSERT_NE(image_bitmap->BitmapImage()->ImageForCurrentFrame(), |
172 original_image_resource->GetImage()->ImageForCurrentFrame()); | 176 original_image_resource->GetImage()->ImageForCurrentFrame()); |
173 SkImage* image1 = image_bitmap->BitmapImage()->ImageForCurrentFrame().get(); | 177 SkImage* image1 = image_bitmap->BitmapImage()->ImageForCurrentFrame().get(); |
174 ASSERT_NE(image1, nullptr); | 178 ASSERT_NE(image1, nullptr); |
175 SkImage* image2 = | 179 SkImage* image2 = |
176 original_image_resource->GetImage()->ImageForCurrentFrame().get(); | 180 original_image_resource->GetImage()->ImageForCurrentFrame().get(); |
177 ASSERT_NE(image2, nullptr); | 181 ASSERT_NE(image2, nullptr); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 SkImageInfo::MakeN32Premul(10, 10, src_rgb_color_space); | 235 SkImageInfo::MakeN32Premul(10, 10, src_rgb_color_space); |
232 sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info)); | 236 sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info)); |
233 surface->getCanvas()->drawCircle(5, 5, 5, p); | 237 surface->getCanvas()->drawCircle(5, 5, 5, p); |
234 sk_sp<SkImage> image = surface->makeImageSnapshot(); | 238 sk_sp<SkImage> image = surface->makeImageSnapshot(); |
235 | 239 |
236 std::unique_ptr<uint8_t[]> src_pixel( | 240 std::unique_ptr<uint8_t[]> src_pixel( |
237 new uint8_t[raster_image_info.bytesPerPixel()]()); | 241 new uint8_t[raster_image_info.bytesPerPixel()]()); |
238 image->readPixels(raster_image_info.makeWH(1, 1), src_pixel.get(), | 242 image->readPixels(raster_image_info.makeWH(1, 1), src_pixel.get(), |
239 image->width() * raster_image_info.bytesPerPixel(), 5, 5); | 243 image->width() * raster_image_info.bytesPerPixel(), 5, 5); |
240 | 244 |
241 ImageResourceContent* original_image_resource = | 245 ImageResourceContent* original_image_resource = ImageResourceContent::Create( |
242 ImageResourceContent::Create(StaticBitmapImage::Create(image).Get()); | 246 task_runner_, StaticBitmapImage::Create(image).Get()); |
243 image_element->SetImageResource(original_image_resource); | 247 image_element->SetImageResource(original_image_resource); |
244 | 248 |
245 Optional<IntRect> crop_rect = IntRect(0, 0, image->width(), image->height()); | 249 Optional<IntRect> crop_rect = IntRect(0, 0, image->width(), image->height()); |
246 | 250 |
247 // Create and test the ImageBitmap objects. | 251 // Create and test the ImageBitmap objects. |
248 // We don't check "none" color space conversion as it requires the encoded | 252 // We don't check "none" color space conversion as it requires the encoded |
249 // data in a format readable by ImageDecoder. Furthermore, the code path for | 253 // data in a format readable by ImageDecoder. Furthermore, the code path for |
250 // "none" color space conversion is not affected by this CL. | 254 // "none" color space conversion is not affected by this CL. |
251 | 255 |
252 sk_sp<SkColorSpace> color_space = nullptr; | 256 sk_sp<SkColorSpace> color_space = nullptr; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 SkImageInfo::MakeN32Premul(10, 10, src_rgb_color_space); | 332 SkImageInfo::MakeN32Premul(10, 10, src_rgb_color_space); |
329 sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info)); | 333 sk_sp<SkSurface> surface(SkSurface::MakeRaster(raster_image_info)); |
330 surface->getCanvas()->drawCircle(5, 5, 5, p); | 334 surface->getCanvas()->drawCircle(5, 5, 5, p); |
331 sk_sp<SkImage> image = surface->makeImageSnapshot(); | 335 sk_sp<SkImage> image = surface->makeImageSnapshot(); |
332 | 336 |
333 std::unique_ptr<uint8_t[]> src_pixel( | 337 std::unique_ptr<uint8_t[]> src_pixel( |
334 new uint8_t[raster_image_info.bytesPerPixel()]()); | 338 new uint8_t[raster_image_info.bytesPerPixel()]()); |
335 image->readPixels(raster_image_info.makeWH(1, 1), src_pixel.get(), | 339 image->readPixels(raster_image_info.makeWH(1, 1), src_pixel.get(), |
336 image->width() * raster_image_info.bytesPerPixel(), 5, 5); | 340 image->width() * raster_image_info.bytesPerPixel(), 5, 5); |
337 | 341 |
338 ImageResourceContent* source_image_resource = | 342 ImageResourceContent* source_image_resource = ImageResourceContent::Create( |
339 ImageResourceContent::Create(StaticBitmapImage::Create(image).Get()); | 343 task_runner_, StaticBitmapImage::Create(image).Get()); |
340 image_element->SetImageResource(source_image_resource); | 344 image_element->SetImageResource(source_image_resource); |
341 | 345 |
342 Optional<IntRect> crop_rect = IntRect(0, 0, image->width(), image->height()); | 346 Optional<IntRect> crop_rect = IntRect(0, 0, image->width(), image->height()); |
343 ImageBitmapOptions options = | 347 ImageBitmapOptions options = |
344 PrepareBitmapOptionsAndSetRuntimeFlags(ColorSpaceConversion::SRGB); | 348 PrepareBitmapOptionsAndSetRuntimeFlags(ColorSpaceConversion::SRGB); |
345 ImageBitmap* source_image_bitmap = ImageBitmap::Create( | 349 ImageBitmap* source_image_bitmap = ImageBitmap::Create( |
346 image_element, crop_rect, &(image_element->GetDocument()), options); | 350 image_element, crop_rect, &(image_element->GetDocument()), options); |
347 | 351 |
348 sk_sp<SkColorSpace> color_space = nullptr; | 352 sk_sp<SkColorSpace> color_space = nullptr; |
349 SkColorType color_type = SkColorType::kN32_SkColorType; | 353 SkColorType color_type = SkColorType::kN32_SkColorType; |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 color_space_xform->apply(color_format, transformed_pixel.get(), | 565 color_space_xform->apply(color_format, transformed_pixel.get(), |
562 color_format32, src_pixel.get(), 1, | 566 color_format32, src_pixel.get(), 1, |
563 SkAlphaType::kUnpremul_SkAlphaType); | 567 SkAlphaType::kUnpremul_SkAlphaType); |
564 int compare = std::memcmp(converted_pixel.get(), transformed_pixel.get(), | 568 int compare = std::memcmp(converted_pixel.get(), transformed_pixel.get(), |
565 image_info.bytesPerPixel()); | 569 image_info.bytesPerPixel()); |
566 ASSERT_EQ(compare, 0); | 570 ASSERT_EQ(compare, 0); |
567 } | 571 } |
568 } | 572 } |
569 | 573 |
570 } // namespace blink | 574 } // namespace blink |
OLD | NEW |