| 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 } else { | 115 } else { |
| 112 EXPECT_EQ(0u, images.size()) << x << " " << y; | 116 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 113 } | 117 } |
| 114 } | 118 } |
| 115 } | 119 } |
| 116 | 120 |
| 117 // Capture 4 pixel refs. | 121 // Capture 4 pixel refs. |
| 118 std::vector<PositionDrawImage> images = | 122 std::vector<PositionScaleDrawImage> images = |
| 119 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); | 123 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); |
| 120 EXPECT_EQ(4u, images.size()); | 124 EXPECT_EQ(4u, images.size()); |
| 121 EXPECT_TRUE(images[0].image == discardable_image[1][2]); | 125 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 122 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); | 126 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); |
| 123 EXPECT_TRUE(images[1].image == discardable_image[2][1]); | 127 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| 124 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); | 128 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); |
| 125 EXPECT_TRUE(images[2].image == discardable_image[2][3]); | 129 EXPECT_TRUE(images[2].image == discardable_image[2][3]); |
| 126 EXPECT_EQ(gfx::Rect(3 * 512 + 6, 2 * 512 + 6, 500, 500), | 130 EXPECT_EQ(gfx::Rect(3 * 512 + 6, 2 * 512 + 6, 500, 500), |
| 127 images[2].image_rect); | 131 images[2].image_rect); |
| 128 EXPECT_TRUE(images[3].image == discardable_image[3][2]); | 132 EXPECT_TRUE(images[3].image == discardable_image[3][2]); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 170 |
| 167 DiscardableImageMap image_map; | 171 DiscardableImageMap image_map; |
| 168 { | 172 { |
| 169 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 173 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 170 layer_size); | 174 layer_size); |
| 171 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 175 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 172 } | 176 } |
| 173 | 177 |
| 174 for (int y = 0; y < 4; ++y) { | 178 for (int y = 0; y < 4; ++y) { |
| 175 for (int x = 0; x < 4; ++x) { | 179 for (int x = 0; x < 4; ++x) { |
| 176 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 180 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 177 image_map, gfx::Rect(1024 + x * 512, y * 512, 500, 500)); | 181 image_map, gfx::Rect(1024 + x * 512, y * 512, 500, 500)); |
| 178 if ((x + y) & 1) { | 182 if ((x + y) & 1) { |
| 179 EXPECT_EQ(1u, images.size()) << x << " " << y; | 183 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 180 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " | 184 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 181 << y; | 185 << y; |
| 182 EXPECT_EQ(gfx::Rect(1024 + x * 512 + 6, y * 512 + 6, 500, 500), | 186 EXPECT_EQ(gfx::Rect(1024 + x * 512 + 6, y * 512 + 6, 500, 500), |
| 183 images[0].image_rect); | 187 images[0].image_rect); |
| 184 } else { | 188 } else { |
| 185 EXPECT_EQ(0u, images.size()) << x << " " << y; | 189 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 186 } | 190 } |
| 187 } | 191 } |
| 188 } | 192 } |
| 189 // Capture 4 pixel refs. | 193 // Capture 4 pixel refs. |
| 190 { | 194 { |
| 191 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 195 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 192 image_map, gfx::Rect(1024 + 512, 512, 2048, 2048)); | 196 image_map, gfx::Rect(1024 + 512, 512, 2048, 2048)); |
| 193 EXPECT_EQ(4u, images.size()); | 197 EXPECT_EQ(4u, images.size()); |
| 194 EXPECT_TRUE(images[0].image == discardable_image[1][2]); | 198 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 195 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 512 + 6, 500, 500), | 199 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 512 + 6, 500, 500), |
| 196 images[0].image_rect); | 200 images[0].image_rect); |
| 197 EXPECT_TRUE(images[1].image == discardable_image[2][1]); | 201 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| 198 EXPECT_EQ(gfx::Rect(1024 + 512 + 6, 2 * 512 + 6, 500, 500), | 202 EXPECT_EQ(gfx::Rect(1024 + 512 + 6, 2 * 512 + 6, 500, 500), |
| 199 images[1].image_rect); | 203 images[1].image_rect); |
| 200 EXPECT_TRUE(images[2].image == discardable_image[2][3]); | 204 EXPECT_TRUE(images[2].image == discardable_image[2][3]); |
| 201 EXPECT_EQ(gfx::Rect(1024 + 3 * 512 + 6, 2 * 512 + 6, 500, 500), | 205 EXPECT_EQ(gfx::Rect(1024 + 3 * 512 + 6, 2 * 512 + 6, 500, 500), |
| 202 images[2].image_rect); | 206 images[2].image_rect); |
| 203 EXPECT_TRUE(images[3].image == discardable_image[3][2]); | 207 EXPECT_TRUE(images[3].image == discardable_image[3][2]); |
| 204 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500), | 208 EXPECT_EQ(gfx::Rect(1024 + 2 * 512 + 6, 3 * 512 + 6, 500, 500), |
| 205 images[3].image_rect); | 209 images[3].image_rect); |
| 206 } | 210 } |
| 207 | 211 |
| 208 // Non intersecting rects | 212 // Non intersecting rects |
| 209 { | 213 { |
| 210 std::vector<PositionDrawImage> images = | 214 std::vector<PositionScaleDrawImage> images = |
| 211 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1000, 1000)); | 215 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1000, 1000)); |
| 212 EXPECT_EQ(0u, images.size()); | 216 EXPECT_EQ(0u, images.size()); |
| 213 } | 217 } |
| 214 { | 218 { |
| 215 std::vector<PositionDrawImage> images = | 219 std::vector<PositionScaleDrawImage> images = |
| 216 GetDiscardableImagesInRect(image_map, gfx::Rect(3500, 0, 1000, 1000)); | 220 GetDiscardableImagesInRect(image_map, gfx::Rect(3500, 0, 1000, 1000)); |
| 217 EXPECT_EQ(0u, images.size()); | 221 EXPECT_EQ(0u, images.size()); |
| 218 } | 222 } |
| 219 { | 223 { |
| 220 std::vector<PositionDrawImage> images = | 224 std::vector<PositionScaleDrawImage> images = |
| 221 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 1100, 1000, 1000)); | 225 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 1100, 1000, 1000)); |
| 222 EXPECT_EQ(0u, images.size()); | 226 EXPECT_EQ(0u, images.size()); |
| 223 } | 227 } |
| 224 { | 228 { |
| 225 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 229 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 226 image_map, gfx::Rect(3500, 1100, 1000, 1000)); | 230 image_map, gfx::Rect(3500, 1100, 1000, 1000)); |
| 227 EXPECT_EQ(0u, images.size()); | 231 EXPECT_EQ(0u, images.size()); |
| 228 } | 232 } |
| 229 } | 233 } |
| 230 | 234 |
| 231 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectOnePixelQuery) { | 235 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectOnePixelQuery) { |
| 232 gfx::Rect visible_rect(2048, 2048); | 236 gfx::Rect visible_rect(2048, 2048); |
| 233 FakeContentLayerClient content_layer_client; | 237 FakeContentLayerClient content_layer_client; |
| 234 content_layer_client.set_bounds(visible_rect.size()); | 238 content_layer_client.set_bounds(visible_rect.size()); |
| 235 | 239 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 262 | 266 |
| 263 DiscardableImageMap image_map; | 267 DiscardableImageMap image_map; |
| 264 { | 268 { |
| 265 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 269 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 266 visible_rect.size()); | 270 visible_rect.size()); |
| 267 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 271 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 268 } | 272 } |
| 269 | 273 |
| 270 for (int y = 0; y < 4; ++y) { | 274 for (int y = 0; y < 4; ++y) { |
| 271 for (int x = 0; x < 4; ++x) { | 275 for (int x = 0; x < 4; ++x) { |
| 272 std::vector<PositionDrawImage> images = GetDiscardableImagesInRect( | 276 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 273 image_map, gfx::Rect(x * 512 + 256, y * 512 + 256, 1, 1)); | 277 image_map, gfx::Rect(x * 512 + 256, y * 512 + 256, 1, 1)); |
| 274 if ((x + y) & 1) { | 278 if ((x + y) & 1) { |
| 275 EXPECT_EQ(1u, images.size()) << x << " " << y; | 279 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 276 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " | 280 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 277 << y; | 281 << y; |
| 278 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), | 282 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), |
| 279 images[0].image_rect); | 283 images[0].image_rect); |
| 280 } else { | 284 } else { |
| 281 EXPECT_EQ(0u, images.size()) << x << " " << y; | 285 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 282 } | 286 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 298 scoped_refptr<DisplayItemList> display_list = | 302 scoped_refptr<DisplayItemList> display_list = |
| 299 content_layer_client.PaintContentsToDisplayList( | 303 content_layer_client.PaintContentsToDisplayList( |
| 300 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 304 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 301 | 305 |
| 302 DiscardableImageMap image_map; | 306 DiscardableImageMap image_map; |
| 303 { | 307 { |
| 304 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 308 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 305 visible_rect.size()); | 309 visible_rect.size()); |
| 306 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); | 310 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 307 } | 311 } |
| 308 std::vector<PositionDrawImage> images = | 312 std::vector<PositionScaleDrawImage> images = |
| 309 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 313 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 310 EXPECT_EQ(1u, images.size()); | 314 EXPECT_EQ(1u, images.size()); |
| 311 EXPECT_TRUE(images[0].image == discardable_image); | 315 EXPECT_TRUE(images[0].image == discardable_image); |
| 312 EXPECT_EQ(gfx::Rect(0, 0, 2048, 2048), images[0].image_rect); | 316 EXPECT_EQ(gfx::Rect(0, 0, 2048, 2048), images[0].image_rect); |
| 313 } | 317 } |
| 314 | 318 |
| 315 TEST_F(DiscardableImageMapTest, PaintDestroyedWhileImageIsDrawn) { | 319 TEST_F(DiscardableImageMapTest, PaintDestroyedWhileImageIsDrawn) { |
| 316 gfx::Rect visible_rect(2048, 2048); | 320 gfx::Rect visible_rect(2048, 2048); |
| 317 FakeContentLayerClient content_layer_client; | 321 FakeContentLayerClient content_layer_client; |
| 318 content_layer_client.set_bounds(visible_rect.size()); | 322 content_layer_client.set_bounds(visible_rect.size()); |
| 319 | 323 |
| 320 sk_sp<SkImage> discardable_image = CreateDiscardableImage(gfx::Size(10, 10)); | 324 sk_sp<SkImage> discardable_image = CreateDiscardableImage(gfx::Size(10, 10)); |
| 321 | 325 |
| 322 DiscardableImageMap image_map; | 326 DiscardableImageMap image_map; |
| 323 { | 327 { |
| 324 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 328 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 325 visible_rect.size()); | 329 visible_rect.size()); |
| 326 { | 330 { |
| 327 std::unique_ptr<SkPaint> paint(new SkPaint()); | 331 std::unique_ptr<SkPaint> paint(new SkPaint()); |
| 328 generator.canvas()->saveLayer(gfx::RectToSkRect(visible_rect), | 332 generator.canvas()->saveLayer(gfx::RectToSkRect(visible_rect), |
| 329 paint.get()); | 333 paint.get()); |
| 330 } | 334 } |
| 331 generator.canvas()->drawImage(discardable_image, 0, 0, nullptr); | 335 generator.canvas()->drawImage(discardable_image, 0, 0, nullptr); |
| 332 generator.canvas()->restore(); | 336 generator.canvas()->restore(); |
| 333 } | 337 } |
| 334 | 338 |
| 335 std::vector<PositionDrawImage> images = | 339 std::vector<PositionScaleDrawImage> images = |
| 336 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 340 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 337 EXPECT_EQ(1u, images.size()); | 341 EXPECT_EQ(1u, images.size()); |
| 338 EXPECT_TRUE(images[0].image == discardable_image); | 342 EXPECT_TRUE(images[0].image == discardable_image); |
| 339 } | 343 } |
| 340 | 344 |
| 341 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImage) { | 345 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImage) { |
| 342 gfx::Rect visible_rect(2048, 2048); | 346 gfx::Rect visible_rect(2048, 2048); |
| 343 FakeContentLayerClient content_layer_client; | 347 FakeContentLayerClient content_layer_client; |
| 344 content_layer_client.set_bounds(visible_rect.size()); | 348 content_layer_client.set_bounds(visible_rect.size()); |
| 345 | 349 |
| 346 int dimension = std::numeric_limits<int>::max(); | 350 int dimension = std::numeric_limits<int>::max(); |
| 347 sk_sp<SkImage> discardable_image = | 351 sk_sp<SkImage> discardable_image = |
| 348 CreateDiscardableImage(gfx::Size(dimension, dimension)); | 352 CreateDiscardableImage(gfx::Size(dimension, dimension)); |
| 349 SkPaint paint; | 353 SkPaint paint; |
| 350 content_layer_client.add_draw_image(discardable_image, gfx::Point(42, 42), | 354 content_layer_client.add_draw_image(discardable_image, gfx::Point(42, 42), |
| 351 paint); | 355 paint); |
| 352 | 356 |
| 353 scoped_refptr<DisplayItemList> display_list = | 357 scoped_refptr<DisplayItemList> display_list = |
| 354 content_layer_client.PaintContentsToDisplayList( | 358 content_layer_client.PaintContentsToDisplayList( |
| 355 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 359 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 356 | 360 |
| 357 DiscardableImageMap image_map; | 361 DiscardableImageMap image_map; |
| 358 { | 362 { |
| 359 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 363 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 360 visible_rect.size()); | 364 visible_rect.size()); |
| 361 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 365 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 362 } | 366 } |
| 363 std::vector<PositionDrawImage> images = | 367 std::vector<PositionScaleDrawImage> images = |
| 364 GetDiscardableImagesInRect(image_map, gfx::Rect(42, 42, 1, 1)); | 368 GetDiscardableImagesInRect(image_map, gfx::Rect(42, 42, 1, 1)); |
| 365 EXPECT_EQ(1u, images.size()); | 369 EXPECT_EQ(1u, images.size()); |
| 366 EXPECT_TRUE(images[0].image == discardable_image); | 370 EXPECT_TRUE(images[0].image == discardable_image); |
| 367 EXPECT_EQ(gfx::Rect(42, 42, 2006, 2006), images[0].image_rect); | 371 EXPECT_EQ(gfx::Rect(42, 42, 2006, 2006), images[0].image_rect); |
| 368 } | 372 } |
| 369 | 373 |
| 370 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImageMaxLayer) { | 374 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInRectMaxImageMaxLayer) { |
| 371 // At large values of integer x, x != static_cast<int>(static_cast<float>(x)). | 375 // At large values of integer x, x != static_cast<int>(static_cast<float>(x)). |
| 372 // So, make sure the dimension can be converted back and forth for the | 376 // So, make sure the dimension can be converted back and forth for the |
| 373 // purposes of the unittest. Also, at near max int values, Skia seems to skip | 377 // purposes of the unittest. Also, at near max int values, Skia seems to skip |
| (...skipping 18 matching lines...) Expand all Loading... |
| 392 scoped_refptr<DisplayItemList> display_list = | 396 scoped_refptr<DisplayItemList> display_list = |
| 393 content_layer_client.PaintContentsToDisplayList( | 397 content_layer_client.PaintContentsToDisplayList( |
| 394 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 398 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 395 | 399 |
| 396 DiscardableImageMap image_map; | 400 DiscardableImageMap image_map; |
| 397 { | 401 { |
| 398 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 402 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 399 visible_rect.size()); | 403 visible_rect.size()); |
| 400 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 404 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 401 } | 405 } |
| 402 std::vector<PositionDrawImage> images = | 406 std::vector<PositionScaleDrawImage> images = |
| 403 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 407 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 404 EXPECT_EQ(1u, images.size()); | 408 EXPECT_EQ(1u, images.size()); |
| 405 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); | 409 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); |
| 406 | 410 |
| 407 images = GetDiscardableImagesInRect(image_map, gfx::Rect(10000, 0, 1, 1)); | 411 images = GetDiscardableImagesInRect(image_map, gfx::Rect(10000, 0, 1, 1)); |
| 408 EXPECT_EQ(2u, images.size()); | 412 EXPECT_EQ(2u, images.size()); |
| 409 EXPECT_EQ(gfx::Rect(10000, 0, dimension - 10000, dimension), | 413 EXPECT_EQ(gfx::Rect(10000, 0, dimension - 10000, dimension), |
| 410 images[1].image_rect); | 414 images[1].image_rect); |
| 411 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); | 415 EXPECT_EQ(gfx::Rect(0, 0, dimension, dimension), images[0].image_rect); |
| 412 | 416 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 443 scoped_refptr<DisplayItemList> display_list = | 447 scoped_refptr<DisplayItemList> display_list = |
| 444 content_layer_client.PaintContentsToDisplayList( | 448 content_layer_client.PaintContentsToDisplayList( |
| 445 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); | 449 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 446 | 450 |
| 447 DiscardableImageMap image_map; | 451 DiscardableImageMap image_map; |
| 448 { | 452 { |
| 449 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, | 453 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 450 visible_rect.size()); | 454 visible_rect.size()); |
| 451 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); | 455 display_list->Raster(generator.canvas(), nullptr, visible_rect, 1.f); |
| 452 } | 456 } |
| 453 std::vector<PositionDrawImage> images = | 457 std::vector<PositionScaleDrawImage> images = |
| 454 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); | 458 GetDiscardableImagesInRect(image_map, gfx::Rect(0, 0, 1, 1)); |
| 455 EXPECT_EQ(1u, images.size()); | 459 EXPECT_EQ(1u, images.size()); |
| 456 EXPECT_EQ(gfx::Rect(0, 0, 90, 89), images[0].image_rect); | 460 EXPECT_EQ(gfx::Rect(0, 0, 90, 89), images[0].image_rect); |
| 457 | 461 |
| 458 images = GetDiscardableImagesInRect(image_map, gfx::Rect(999, 999, 1, 1)); | 462 images = GetDiscardableImagesInRect(image_map, gfx::Rect(999, 999, 1, 1)); |
| 459 EXPECT_EQ(1u, images.size()); | 463 EXPECT_EQ(1u, images.size()); |
| 460 EXPECT_EQ(gfx::Rect(950, 951, 50, 49), images[0].image_rect); | 464 EXPECT_EQ(gfx::Rect(950, 951, 50, 49), images[0].image_rect); |
| 461 | 465 |
| 462 images = GetDiscardableImagesInRect(image_map, gfx::Rect(0, 500, 1, 1)); | 466 images = GetDiscardableImagesInRect(image_map, gfx::Rect(0, 500, 1, 1)); |
| 463 EXPECT_EQ(1u, images.size()); | 467 EXPECT_EQ(1u, images.size()); |
| 464 EXPECT_EQ(gfx::Rect(0, 500, 1000, 100), images[0].image_rect); | 468 EXPECT_EQ(gfx::Rect(0, 500, 1000, 100), images[0].image_rect); |
| 465 } | 469 } |
| 466 | 470 |
| 471 TEST_F(DiscardableImageMapTest, GetDiscardableImagesInShader) { |
| 472 gfx::Rect visible_rect(2048, 2048); |
| 473 FakeContentLayerClient content_layer_client; |
| 474 content_layer_client.set_bounds(visible_rect.size()); |
| 475 |
| 476 // Discardable pixel refs are found in the following grids: |
| 477 // |---|---|---|---| |
| 478 // | | x | | x | |
| 479 // |---|---|---|---| |
| 480 // | x | | x | | |
| 481 // |---|---|---|---| |
| 482 // | | x | | x | |
| 483 // |---|---|---|---| |
| 484 // | x | | x | | |
| 485 // |---|---|---|---| |
| 486 sk_sp<SkImage> discardable_image[4][4]; |
| 487 for (int y = 0; y < 4; ++y) { |
| 488 for (int x = 0; x < 4; ++x) { |
| 489 if ((x + y) & 1) { |
| 490 discardable_image[y][x] = CreateDiscardableImage(gfx::Size(500, 500)); |
| 491 SkMatrix scale = SkMatrix::MakeScale(x * 0.5f, y * 0.5f); |
| 492 SkPaint paint; |
| 493 paint.setShader(discardable_image[y][x]->makeShader( |
| 494 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &scale)); |
| 495 content_layer_client.add_draw_rect( |
| 496 gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), paint); |
| 497 } |
| 498 } |
| 499 } |
| 500 |
| 501 scoped_refptr<DisplayItemList> display_list = |
| 502 content_layer_client.PaintContentsToDisplayList( |
| 503 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL); |
| 504 |
| 505 DiscardableImageMap image_map; |
| 506 { |
| 507 DiscardableImageMap::ScopedMetadataGenerator generator(&image_map, |
| 508 visible_rect.size()); |
| 509 display_list->Raster(generator.canvas(), nullptr, gfx::Rect(), 1.f); |
| 510 } |
| 511 |
| 512 for (int y = 0; y < 4; ++y) { |
| 513 for (int x = 0; x < 4; ++x) { |
| 514 std::vector<PositionScaleDrawImage> images = GetDiscardableImagesInRect( |
| 515 image_map, gfx::Rect(x * 512, y * 512, 500, 500)); |
| 516 if ((x + y) & 1) { |
| 517 EXPECT_EQ(1u, images.size()) << x << " " << y; |
| 518 EXPECT_TRUE(images[0].image == discardable_image[y][x]) << x << " " |
| 519 << y; |
| 520 EXPECT_EQ(gfx::Rect(x * 512 + 6, y * 512 + 6, 500, 500), |
| 521 images[0].image_rect); |
| 522 EXPECT_EQ(x * 0.5f, images[0].scale.fWidth); |
| 523 EXPECT_EQ(y * 0.5f, images[0].scale.fHeight); |
| 524 } else { |
| 525 EXPECT_EQ(0u, images.size()) << x << " " << y; |
| 526 } |
| 527 } |
| 528 } |
| 529 |
| 530 // Capture 4 pixel refs. |
| 531 std::vector<PositionScaleDrawImage> images = |
| 532 GetDiscardableImagesInRect(image_map, gfx::Rect(512, 512, 2048, 2048)); |
| 533 EXPECT_EQ(4u, images.size()); |
| 534 EXPECT_TRUE(images[0].image == discardable_image[1][2]); |
| 535 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 512 + 6, 500, 500), images[0].image_rect); |
| 536 EXPECT_TRUE(images[1].image == discardable_image[2][1]); |
| 537 EXPECT_EQ(gfx::Rect(512 + 6, 2 * 512 + 6, 500, 500), images[1].image_rect); |
| 538 EXPECT_TRUE(images[2].image == discardable_image[2][3]); |
| 539 EXPECT_EQ(gfx::Rect(3 * 512 + 6, 2 * 512 + 6, 500, 500), |
| 540 images[2].image_rect); |
| 541 EXPECT_TRUE(images[3].image == discardable_image[3][2]); |
| 542 EXPECT_EQ(gfx::Rect(2 * 512 + 6, 3 * 512 + 6, 500, 500), |
| 543 images[3].image_rect); |
| 544 } |
| 545 |
| 467 } // namespace cc | 546 } // namespace cc |
| OLD | NEW |