| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/playback/discardable_image_map.h" | 5 #include "cc/playback/discardable_image_map.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "cc/base/region.h" | 13 #include "cc/base/region.h" |
| 14 #include "cc/test/fake_content_layer_client.h" | 14 #include "cc/test/fake_content_layer_client.h" |
| 15 #include "cc/test/fake_recording_source.h" | 15 #include "cc/test/fake_recording_source.h" |
| 16 #include "cc/test/skia_common.h" | 16 #include "cc/test/skia_common.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/skia/include/core/SkCanvas.h" | 18 #include "third_party/skia/include/core/SkCanvas.h" |
| 19 #include "third_party/skia/include/core/SkGraphics.h" | 19 #include "third_party/skia/include/core/SkGraphics.h" |
| 20 #include "third_party/skia/include/core/SkImageGenerator.h" | 20 #include "third_party/skia/include/core/SkImageGenerator.h" |
| 21 #include "third_party/skia/include/core/SkRefCnt.h" | 21 #include "third_party/skia/include/core/SkRefCnt.h" |
| 22 #include "ui/gfx/geometry/rect.h" | 22 #include "ui/gfx/geometry/rect.h" |
| 23 #include "ui/gfx/skia_util.h" | 23 #include "ui/gfx/skia_util.h" |
| 24 | 24 |
| 25 namespace cc { | 25 namespace cc { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 struct PositionDrawImage { | 28 struct PositionScaleDrawImage { |
| 29 PositionDrawImage(sk_sp<const SkImage> image, const gfx::Rect& image_rect) | 29 PositionScaleDrawImage(sk_sp<const SkImage> image, |
| 30 : image(std::move(image)), image_rect(image_rect) {} | 30 const gfx::Rect& image_rect, |
| 31 const SkSize& scale) |
| 32 : image(std::move(image)), image_rect(image_rect), scale(scale) {} |
| 31 sk_sp<const SkImage> image; | 33 sk_sp<const SkImage> image; |
| 32 gfx::Rect image_rect; | 34 gfx::Rect image_rect; |
| 35 SkSize scale; |
| 33 }; | 36 }; |
| 34 | 37 |
| 35 } // namespace | 38 } // namespace |
| 36 | 39 |
| 37 class DiscardableImageMapTest : public testing::Test { | 40 class DiscardableImageMapTest : public testing::Test { |
| 38 public: | 41 public: |
| 39 std::vector<PositionDrawImage> GetDiscardableImagesInRect( | 42 std::vector<PositionScaleDrawImage> GetDiscardableImagesInRect( |
| 40 const DiscardableImageMap& image_map, | 43 const DiscardableImageMap& image_map, |
| 41 const gfx::Rect& rect) { | 44 const gfx::Rect& rect) { |
| 42 std::vector<DrawImage> draw_images; | 45 std::vector<DrawImage> draw_images; |
| 43 image_map.GetDiscardableImagesInRect(rect, gfx::SizeF(1.f, 1.f), | 46 image_map.GetDiscardableImagesInRect(rect, gfx::SizeF(1.f, 1.f), |
| 44 &draw_images); | 47 &draw_images); |
| 45 | 48 |
| 46 std::vector<size_t> indices; | 49 std::vector<size_t> indices; |
| 47 image_map.images_rtree_.Search(rect, &indices); | 50 image_map.images_rtree_.Search(rect, &indices); |
| 48 std::vector<PositionDrawImage> position_draw_images; | 51 std::vector<PositionScaleDrawImage> position_draw_images; |
| 49 for (size_t index : indices) { | 52 for (size_t index : indices) { |
| 50 position_draw_images.push_back( | 53 position_draw_images.push_back( |
| 51 PositionDrawImage(image_map.all_images_[index].first.image(), | 54 PositionScaleDrawImage(image_map.all_images_[index].first.image(), |
| 52 image_map.all_images_[index].second)); | 55 image_map.all_images_[index].second, |
| 56 image_map.all_images_[index].first.scale())); |
| 53 } | 57 } |
| 54 | 58 |
| 55 EXPECT_EQ(draw_images.size(), position_draw_images.size()); | 59 EXPECT_EQ(draw_images.size(), position_draw_images.size()); |
| 56 for (size_t i = 0; i < draw_images.size(); ++i) | 60 for (size_t i = 0; i < draw_images.size(); ++i) |
| 57 EXPECT_TRUE(draw_images[i].image() == position_draw_images[i].image); | 61 EXPECT_TRUE(draw_images[i].image() == position_draw_images[i].image); |
| 58 return position_draw_images; | 62 return position_draw_images; |
| 59 } | 63 } |
| 60 }; | 64 }; |
| 61 | 65 |
| 62 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectTest) { | 66 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectTest) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 93 | 97 |
| 94 DiscardableImageMap image_map; | 98 DiscardableImageMap image_map; |
| 95 { | 99 { |
| 96 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 100 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 97 visible_rect.size()); | 101 visible_rect.size()); |
| 98 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 102 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 99 } | 103 } |
| 100 | 104 |
| 101 for (int y = 0; y < 4; ++y) { | 105 for (int y = 0; y < 4; ++y) { |
| 102 for (int x = 0; x < 4; ++x) { | 106 for (int x = 0; x < 4; ++x) { |
| 103 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 107 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 104 image_map, gfx::Rect(x * 512, y * 512, 500, 500)); | 108 image_map, gfx::Rect(x * 512, y * 512, 500, 500)); |
| 105 if ((x + y) & 1) { | 109 if ((x + y) & 1) { |
| 106 EXPECT_EQ(1u, images.size()) << x << " " << y; | 110 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 107 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " | 111 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 108 << y; | 112 << y; |
| 109 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), | 113 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), |
| 110 images[0].image_rect); | 114 images[0].image_rect); |
| 111 EXPECT_EQ(Region(images[0].image_rect), | 115 EXPECT_EQ(Region(images[0].image_rect), |
| 112 image_map.GetRegionForImage(images[0].image->uniqueID())); | 116 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 113 } else { | 117 } else { |
| 114 EXPECT_EQ(0u, images.size()) << x << " " << y; | 118 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 115 } | 119 } |
| 116 } | 120 } |
| 117 } | 121 } |
| 118 | 122 |
| 119 // Capture 4 pixel refs. | 123 // Capture 4 pixel refs. |
| 120 std::vector<PositionDrawImage> images = | 124 std::vector<PositionScaleDrawImage> images = |
| 121 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); | 125 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); |
| 122 EXPECT_EQ(4u, images.size()); | 126 EXPECT_EQ(4u, images.size()); |
| 123 | 127 |
| 124 EXPECT_TRUE(images[0].image == discardable_image[1][2]); | 128 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 125 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); | 129 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); |
| 126 EXPECT_EQ(Region(images[0].image_rect), | 130 EXPECT_EQ(Region(images[0].image_rect), |
| 127 image_map.GetRegionForImage(images[0].image->uniqueID())); | 131 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 128 | 132 |
| 129 EXPECT_TRUE(images[1].image == discardable_image[2][1]); | 133 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| 130 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); | 134 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 184 |
| 181 DiscardableImageMap image_map; | 185 DiscardableImageMap image_map; |
| 182 { | 186 { |
| 183 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 187 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 184 layer_size); | 188 layer_size); |
| 185 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 189 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 186 } | 190 } |
| 187 | 191 |
| 188 for (int y = 0; y < 4; ++y) { | 192 for (int y = 0; y < 4; ++y) { |
| 189 for (int x = 0; x < 4; ++x) { | 193 for (int x = 0; x < 4; ++x) { |
| 190 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 194 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 191 image_map, gfx::Rect(1024 + x * 512, y * 512, 500, 500)); | 195 image_map, gfx::Rect(1024 + x * 512, y * 512, 500, 500)); |
| 192 if ((x + y) & 1) { | 196 if ((x + y) & 1) { |
| 193 EXPECT_EQ(1u, images.size()) << x << " " << y; | 197 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 194 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " | 198 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 195 << y; | 199 << y; |
| 196 EXPECT_EQ(gfx::Rect(1024 + x * 512 + 6, y * 512 + 6, 500, 500), | 200 EXPECT_EQ(gfx::Rect(1024 + x * 512 + 6, y * 512 + 6, 500, 500), |
| 197 images[0].image_rect); | 201 images[0].image_rect); |
| 198 EXPECT_EQ(Region(images[0].image_rect), | 202 EXPECT_EQ(Region(images[0].image_rect), |
| 199 image_map.GetRegionForImage(images[0].image->uniqueID())); | 203 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 200 } else { | 204 } else { |
| 201 EXPECT_EQ(0u, images.size()) << x << " " << y; | 205 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 202 } | 206 } |
| 203 } | 207 } |
| 204 } | 208 } |
| 205 // Capture 4 pixel refs. | 209 // Capture 4 pixel refs. |
| 206 { | 210 { |
| 207 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 211 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 208 image_map, gfx::Rect(1024 + 512, 512, 2048, 2048)); | 212 image_map, gfx::Rect(1024 + 512, 512, 2048, 2048)); |
| 209 EXPECT_EQ(4u, images.size()); | 213 EXPECT_EQ(4u, images.size()); |
| 210 | 214 |
| 211 EXPECT_TRUE(images[0].image == discardable_image[1][2]); | 215 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 212 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 512 + 6, 500, 500), | 216 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 512 + 6, 500, 500), |
| 213 images[0].image_rect); | 217 images[0].image_rect); |
| 214 EXPECT_EQ(Region(images[0].image_rect), | 218 EXPECT_EQ(Region(images[0].image_rect), |
| 215 image_map.GetRegionForImage(images[0].image->uniqueID())); | 219 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 216 | 220 |
| 217 EXPECT_TRUE(images[1].image == discardable_image[2][1]); | 221 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 228 | 232 |
| 229 EXPECT_TRUE(images[3].image == discardable_image[3][2]); | 233 EXPECT_TRUE(images[3].image == discardable_image[3][2]); |
| 230 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500), | 234 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500), |
| 231 images[3].image_rect); | 235 images[3].image_rect); |
| 232 EXPECT_EQ(Region(images[3].image_rect), | 236 EXPECT_EQ(Region(images[3].image_rect), |
| 233 image_map.GetRegionForImage(images[3].image->uniqueID())); | 237 image_map.GetRegionForImage(images[3].image->uniqueID())); |
| 234 } | 238 } |
| 235 | 239 |
| 236 // Non intersecting rects | 240 // Non intersecting rects |
| 237 { | 241 { |
| 238 std::vector<PositionDrawImage> images = | 242 std::vector<PositionScaleDrawImage> images = |
| 239 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1000, 1000)); | 243 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1000, 1000)); |
| 240 EXPECT_EQ(0u, images.size()); | 244 EXPECT_EQ(0u, images.size()); |
| 241 } | 245 } |
| 242 { | 246 { |
| 243 std::vector<PositionDrawImage> images = | 247 std::vector<PositionScaleDrawImage> images = |
| 244 GetDiscardableImagesInRect(image_map, gfx::Rect(3500, 0, 1000, 1000)); | 248 GetDiscardableImagesInRect(image_map, gfx::Rect(3500, 0, 1000, 1000)); |
| 245 EXPECT_EQ(0u, images.size()); | 249 EXPECT_EQ(0u, images.size()); |
| 246 } | 250 } |
| 247 { | 251 { |
| 248 std::vector<PositionDrawImage> images = | 252 std::vector<PositionScaleDrawImage> images = |
| 249 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 1100, 1000, 1000)); | 253 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 1100, 1000, 1000)); |
| 250 EXPECT_EQ(0u, images.size()); | 254 EXPECT_EQ(0u, images.size()); |
| 251 } | 255 } |
| 252 { | 256 { |
| 253 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 257 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 254 image_map, gfx::Rect(3500, 1100, 1000, 1000)); | 258 image_map, gfx::Rect(3500, 1100, 1000, 1000)); |
| 255 EXPECT_EQ(0u, images.size()); | 259 EXPECT_EQ(0u, images.size()); |
| 256 } | 260 } |
| 257 | 261 |
| 258 // Image not present in the list. | 262 // Image not present in the list. |
| 259 { | 263 { |
| 260 sk_sp<SkImage> image = CreateDiscardableImage(gfx::Size(500, 500)); | 264 sk_sp<SkImage> image = CreateDiscardableImage(gfx::Size(500, 500)); |
| 261 EXPECT_EQ(Region(), image_map.GetRegionForImage(image->uniqueID())); | 265 EXPECT_EQ(Region(), image_map.GetRegionForImage(image->uniqueID())); |
| 262 } | 266 } |
| 263 } | 267 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 300 |
| 297 DiscardableImageMap image_map; | 301 DiscardableImageMap image_map; |
| 298 { | 302 { |
| 299 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 303 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 300 visible_rect.size()); | 304 visible_rect.size()); |
| 301 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 305 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 302 } | 306 } |
| 303 | 307 |
| 304 for (int y = 0; y < 4; ++y) { | 308 for (int y = 0; y < 4; ++y) { |
| 305 for (int x = 0; x < 4; ++x) { | 309 for (int x = 0; x < 4; ++x) { |
| 306 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 310 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 307 image_map, gfx::Rect(x * 512 + 256, y * 512 + 256, 1, 1)); | 311 image_map, gfx::Rect(x * 512 + 256, y * 512 + 256, 1, 1)); |
| 308 if ((x + y) & 1) { | 312 if ((x + y) & 1) { |
| 309 EXPECT_EQ(1u, images.size()) << x << " " << y; | 313 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 310 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " | 314 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 311 << y; | 315 << y; |
| 312 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), | 316 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), |
| 313 images[0].image_rect); | 317 images[0].image_rect); |
| 314 EXPECT_EQ(Region(images[0].image_rect), | 318 EXPECT_EQ(Region(images[0].image_rect), |
| 315 image_map.GetRegionForImage(images[0].image->uniqueID())); | 319 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 316 } else { | 320 } else { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 334 scoped_refptr<DisplayItemList> display_list = | 338 scoped_refptr<DisplayItemList> display_list = |
| 335 content_layer_client.PaintContentsToDisplayList( | 339 content_layer_client.PaintContentsToDisplayList( |
| 336 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 340 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 337 | 341 |
| 338 DiscardableImageMap image_map; | 342 DiscardableImageMap image_map; |
| 339 { | 343 { |
| 340 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 344 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 341 visible_rect.size()); | 345 visible_rect.size()); |
| 342 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 346 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 343 } | 347 } |
| 344 std::vector<PositionDrawImage> images = | 348 std::vector<PositionScaleDrawImage> images = |
| 345 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 349 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 346 EXPECT_EQ(1u, images.size()); | 350 EXPECT_EQ(1u, images.size()); |
| 347 EXPECT_TRUE(images[0].image == discardable_image); | 351 EXPECT_TRUE(images[0].image == discardable_image); |
| 348 EXPECT_EQ(gfx::Rect(0, 0, 2048, 2048), images[0].image_rect); | 352 EXPECT_EQ(gfx::Rect(0, 0, 2048, 2048), images[0].image_rect); |
| 349 EXPECT_EQ(Region(images[0].image_rect), | 353 EXPECT_EQ(Region(images[0].image_rect), |
| 350 image_map.GetRegionForImage(images[0].image->uniqueID())); | 354 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 351 } | 355 } |
| 352 | 356 |
| 353 TEST_F(DiscardableImageMapTest, PaintDestroyedWhileImageIsDrawn) { | 357 TEST_F(DiscardableImageMapTest, PaintDestroyedWhileImageIsDrawn) { |
| 354 gfx::Rect visible_rect(2048, 2048); | 358 gfx::Rect visible_rect(2048, 2048); |
| 355 FakeContentLayerClient content_layer_client; | 359 FakeContentLayerClient content_layer_client; |
| 356 content_layer_client.set_bounds(visible_rect.size()); | 360 content_layer_client.set_bounds(visible_rect.size()); |
| 357 | 361 |
| 358 sk_sp<SkImage> discardable_image = CreateDiscardableImage(gfx::Size(10, 10)); | 362 sk_sp<SkImage> discardable_image = CreateDiscardableImage(gfx::Size(10, 10)); |
| 359 | 363 |
| 360 DiscardableImageMap image_map; | 364 DiscardableImageMap image_map; |
| 361 { | 365 { |
| 362 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 366 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 363 visible_rect.size()); | 367 visible_rect.size()); |
| 364 { | 368 { |
| 365 std::unique_ptr<SkPaint> paint(new SkPaint()); | 369 std::unique_ptr<SkPaint> paint(new SkPaint()); |
| 366 generator.canvas()->saveLayer(gfx::RectToSkRect(visible_rect), | 370 generator.canvas()->saveLayer(gfx::RectToSkRect(visible_rect), |
| 367 paint.get()); | 371 paint.get()); |
| 368 } | 372 } |
| 369 generator.canvas()->drawImage(discardable_image, 0, 0, nullptr); | 373 generator.canvas()->drawImage(discardable_image, 0, 0, nullptr); |
| 370 generator.canvas()->restore(); | 374 generator.canvas()->restore(); |
| 371 } | 375 } |
| 372 | 376 |
| 373 std::vector<PositionDrawImage> images = | 377 std::vector<PositionScaleDrawImage> images = |
| 374 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 378 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 375 EXPECT_EQ(1u, images.size()); | 379 EXPECT_EQ(1u, images.size()); |
| 376 EXPECT_TRUE(images[0].image == discardable_image); | 380 EXPECT_TRUE(images[0].image == discardable_image); |
| 377 } | 381 } |
| 378 | 382 |
| 379 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImage) { | 383 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImage) { |
| 380 gfx::Rect visible_rect(2048, 2048); | 384 gfx::Rect visible_rect(2048, 2048); |
| 381 FakeContentLayerClient content_layer_client; | 385 FakeContentLayerClient content_layer_client; |
| 382 content_layer_client.set_bounds(visible_rect.size()); | 386 content_layer_client.set_bounds(visible_rect.size()); |
| 383 | 387 |
| 384 int dimension = std::numeric_limits<int>::max(); | 388 int dimension = std::numeric_limits<int>::max(); |
| 385 sk_sp<SkImage> discardable_image = | 389 sk_sp<SkImage> discardable_image = |
| 386 CreateDiscardableImage(gfx::Size(dimension, dimension)); | 390 CreateDiscardableImage(gfx::Size(dimension, dimension)); |
| 387 SkPaint paint; | 391 SkPaint paint; |
| 388 content_layer_client.add_draw_image(discardable_image, gfx::Point(42, 42), | 392 content_layer_client.add_draw_image(discardable_image, gfx::Point(42, 42), |
| 389 paint); | 393 paint); |
| 390 | 394 |
| 391 scoped_refptr<DisplayItemList> display_list = | 395 scoped_refptr<DisplayItemList> display_list = |
| 392 content_layer_client.PaintContentsToDisplayList( | 396 content_layer_client.PaintContentsToDisplayList( |
| 393 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 397 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 394 | 398 |
| 395 DiscardableImageMap image_map; | 399 DiscardableImageMap image_map; |
| 396 { | 400 { |
| 397 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 401 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 398 visible_rect.size()); | 402 visible_rect.size()); |
| 399 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 403 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 400 } | 404 } |
| 401 std::vector<PositionDrawImage> images = | 405 std::vector<PositionScaleDrawImage> images = |
| 402 GetDiscardableImagesInRect(image_map, gfx::Rect(42, 42, 1, 1)); | 406 GetDiscardableImagesInRect(image_map, gfx::Rect(42, 42, 1, 1)); |
| 403 EXPECT_EQ(1u, images.size()); | 407 EXPECT_EQ(1u, images.size()); |
| 404 EXPECT_TRUE(images[0].image == discardable_image); | 408 EXPECT_TRUE(images[0].image == discardable_image); |
| 405 EXPECT_EQ(gfx::Rect(42, 42, 2006, 2006), images[0].image_rect); | 409 EXPECT_EQ(gfx::Rect(42, 42, 2006, 2006), images[0].image_rect); |
| 406 EXPECT_EQ(Region(images[0].image_rect), | 410 EXPECT_EQ(Region(images[0].image_rect), |
| 407 image_map.GetRegionForImage(images[0].image->uniqueID())); | 411 image_map.GetRegionForImage(images[0].image->uniqueID())); |
| 408 } | 412 } |
| 409 | 413 |
| 410 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImageMaxLayer) { | 414 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImageMaxLayer) { |
| 411 // At large values of integer x, x != static_cast<int>(static_cast<float>(x)). | 415 // At large values of integer x, x != static_cast<int>(static_cast<float>(x)). |
| (...skipping 24 matching lines...) Expand all Loading... |
| 436 DiscardableImageMap image_map; | 440 DiscardableImageMap image_map; |
| 437 { | 441 { |
| 438 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 442 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 439 visible_rect.size()); | 443 visible_rect.size()); |
| 440 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 444 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 441 } | 445 } |
| 442 | 446 |
| 443 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), | 447 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), |
| 444 image_map.GetRegionForImage(discardable_image->uniqueID())); | 448 image_map.GetRegionForImage(discardable_image->uniqueID())); |
| 445 | 449 |
| 446 std::vector<PositionDrawImage> images = | 450 std::vector<PositionScaleDrawImage> images = |
| 447 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 451 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 448 EXPECT_EQ(1u, images.size()); | 452 EXPECT_EQ(1u, images.size()); |
| 449 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); | 453 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); |
| 450 | 454 |
| 451 images = GetDiscardableImagesInRect(image_map, gfx::Rect(10000, 0, 1, 1)); | 455 images = GetDiscardableImagesInRect(image_map, gfx::Rect(10000, 0, 1, 1)); |
| 452 EXPECT_EQ(2u, images.size()); | 456 EXPECT_EQ(2u, images.size()); |
| 453 EXPECT_EQ(gfx::Rect(10000, 0, dimension - 10000, dimension), | 457 EXPECT_EQ(gfx::Rect(10000, 0, dimension - 10000, dimension), |
| 454 images[1].image_rect); | 458 images[1].image_rect); |
| 455 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); | 459 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); |
| 456 | 460 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 487 scoped_refptr<DisplayItemList> display_list = | 491 scoped_refptr<DisplayItemList> display_list = |
| 488 content_layer_client.PaintContentsToDisplayList( | 492 content_layer_client.PaintContentsToDisplayList( |
| 489 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 493 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 490 | 494 |
| 491 DiscardableImageMap image_map; | 495 DiscardableImageMap image_map; |
| 492 { | 496 { |
| 493 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 497 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 494 visible_rect.size()); | 498 visible_rect.size()); |
| 495 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 499 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 496 } | 500 } |
| 497 std::vector<PositionDrawImage> images = | 501 std::vector<PositionScaleDrawImage> images = |
| 498 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 502 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 499 EXPECT_EQ(1u, images.size()); | 503 EXPECT_EQ(1u, images.size()); |
| 500 EXPECT_EQ(gfx::Rect(0, 0, 90, 89), images[0].image_rect); | 504 EXPECT_EQ(gfx::Rect(0, 0, 90, 89), images[0].image_rect); |
| 501 | 505 |
| 502 images = GetDiscardableImagesInRect(image_map, gfx::Rect(999, 999, 1, 1)); | 506 images = GetDiscardableImagesInRect(image_map, gfx::Rect(999, 999, 1, 1)); |
| 503 EXPECT_EQ(1u, images.size()); | 507 EXPECT_EQ(1u, images.size()); |
| 504 EXPECT_EQ(gfx::Rect(950, 951, 50, 49), images[0].image_rect); | 508 EXPECT_EQ(gfx::Rect(950, 951, 50, 49), images[0].image_rect); |
| 505 | 509 |
| 506 images = GetDiscardableImagesInRect(image_map, gfx::Rect(0, 500, 1, 1)); | 510 images = GetDiscardableImagesInRect(image_map, gfx::Rect(0, 500, 1, 1)); |
| 507 EXPECT_EQ(1u, images.size()); | 511 EXPECT_EQ(1u, images.size()); |
| 508 EXPECT_EQ(gfx::Rect(0, 500, 1000, 100), images[0].image_rect); | 512 EXPECT_EQ(gfx::Rect(0, 500, 1000, 100), images[0].image_rect); |
| 509 | 513 |
| 510 Region discardable_image_region; | 514 Region discardable_image_region; |
| 511 discardable_image_region.Union(gfx::Rect(0, 0, 90, 89)); | 515 discardable_image_region.Union(gfx::Rect(0, 0, 90, 89)); |
| 512 discardable_image_region.Union(gfx::Rect(950, 951, 50, 49)); | 516 discardable_image_region.Union(gfx::Rect(950, 951, 50, 49)); |
| 513 EXPECT_EQ(discardable_image_region, | 517 EXPECT_EQ(discardable_image_region, |
| 514 image_map.GetRegionForImage(discardable_image->uniqueID())); | 518 image_map.GetRegionForImage(discardable_image->uniqueID())); |
| 515 | 519 |
| 516 EXPECT_EQ(Region(gfx::Rect(0, 500, 1000, 100)), | 520 EXPECT_EQ(Region(gfx::Rect(0, 500, 1000, 100)), |
| 517 image_map.GetRegionForImage(long_discardable_image->uniqueID())); | 521 image_map.GetRegionForImage(long_discardable_image->uniqueID())); |
| 518 } | 522 } |
| 519 | 523 |
| 524 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInShader) { |
| 525 gfx::Rect visible_rect(2048, 2048); |
| 526 FakeContentLayerClient content_layer_client; |
| 527 content_layer_client.set_bounds(visible_rect.size()); |
| 528 |
| 529 // Discardable pixel refs are found in the following grids: |
| 530 // |---|---|---|---| |
| 531 // | | x | | x | |
| 532 // |---|---|---|---| |
| 533 // | x | | x | | |
| 534 // |---|---|---|---| |
| 535 // | | x | | x | |
| 536 // |---|---|---|---| |
| 537 // | x | | x | | |
| 538 // |---|---|---|---| |
| 539 sk_sp<SkImage> discardable_image[4][4]; |
| 540 for (int y = 0; y < 4; ++y) { |
| 541 for (int x = 0; x < 4; ++x) { |
| 542 if ((x + y) & 1) { |
| 543 discardable_image[y][x] = CreateDiscardableImage(gfx::Size(500, 500)); |
| 544 SkMatrix scale = SkMatrix::MakeScale(x * 0.5f, y * 0.5f); |
| 545 SkPaint paint; |
| 546 paint.setShader(discardable_image[y][x]->makeShader( |
| 547 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &scale)); |
| 548 content_layer_client.add_draw_rect( |
| 549 gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), paint); |
| 550 } |
| 551 } |
| 552 } |
| 553 |
| 554 scoped_refptr<DisplayItemList> display_list = |
| 555 content_layer_client.PaintContentsToDisplayList( |
| 556 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 557 |
| 558 DiscardableImageMap image_map; |
| 559 { |
| 560 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 561 visible_rect.size()); |
| 562 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 563 } |
| 564 |
| 565 for (int y = 0; y < 4; ++y) { |
| 566 for (int x = 0; x < 4; ++x) { |
| 567 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 568 image_map, gfx::Rect(x * 512, y * 512, 500, 500)); |
| 569 if ((x + y) & 1) { |
| 570 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 571 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 572 << y; |
| 573 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), |
| 574 images[0].image_rect); |
| 575 EXPECT_EQ(x * 0.5f, images[0].scale.fWidth); |
| 576 EXPECT_EQ(y * 0.5f, images[0].scale.fHeight); |
| 577 } else { |
| 578 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 579 } |
| 580 } |
| 581 } |
| 582 |
| 583 // Capture 4 pixel refs. |
| 584 std::vector<PositionScaleDrawImage> images = |
| 585 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); |
| 586 EXPECT_EQ(4u, images.size()); |
| 587 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 588 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); |
| 589 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| 590 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); |
| 591 EXPECT_TRUE(images[2].image == discardable_image[2][3]); |
| 592 EXPECT_EQ(gfx::Rect(3 * 512 + 6, 2 * 512 + 6, 500, 500), |
| 593 images[2].image_rect); |
| 594 EXPECT_TRUE(images[3].image == discardable_image[3][2]); |
| 595 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 3 * 512 + 6, 500, 500), |
| 596 images[3].image_rect); |
| 597 } |
| 598 |
| 520 } // namespace cc | 599 } // namespace cc |
| OLD | NEW |