Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: Source/core/frame/ImageBitmapTest.cpp

Issue 190183003: Oilpan: move ImageBitmap to the oilpan heap. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Control image resource lifetimes in tests using explicit GCs Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 22 matching lines...) Expand all
33 33
34 #include "SkPixelRef.h" 34 #include "SkPixelRef.h"
35 #include "core/dom/Document.h" 35 #include "core/dom/Document.h"
36 #include "core/fetch/ImageResource.h" 36 #include "core/fetch/ImageResource.h"
37 #include "core/fetch/MemoryCache.h" 37 #include "core/fetch/MemoryCache.h"
38 #include "core/fetch/MockImageResourceClient.h" 38 #include "core/fetch/MockImageResourceClient.h"
39 #include "core/fetch/ResourcePtr.h" 39 #include "core/fetch/ResourcePtr.h"
40 #include "core/html/HTMLCanvasElement.h" 40 #include "core/html/HTMLCanvasElement.h"
41 #include "core/html/HTMLImageElement.h" 41 #include "core/html/HTMLImageElement.h"
42 #include "core/html/canvas/CanvasRenderingContext2D.h" 42 #include "core/html/canvas/CanvasRenderingContext2D.h"
43 #include "heap/Handle.h"
43 #include "platform/graphics/BitmapImage.h" 44 #include "platform/graphics/BitmapImage.h"
44 #include "platform/graphics/skia/NativeImageSkia.h" 45 #include "platform/graphics/skia/NativeImageSkia.h"
45 #include "platform/network/ResourceRequest.h" 46 #include "platform/network/ResourceRequest.h"
46 #include "wtf/OwnPtr.h" 47 #include "wtf/OwnPtr.h"
47 48
48 #include <gtest/gtest.h> 49 #include <gtest/gtest.h>
49 50
50 namespace WebCore { 51 namespace WebCore {
51 52
52 class ImageBitmapTest : public ::testing::Test { 53 class ImageBitmapTest : public ::testing::Test {
53 protected: 54 protected:
54 virtual void SetUp() 55 virtual void SetUp()
55 { 56 {
56 m_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); 57 m_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
57 m_bitmap.allocPixels(); 58 m_bitmap.allocPixels();
58 m_bitmap.eraseColor(0xFFFFFFFF); 59 m_bitmap.eraseColor(0xFFFFFFFF);
59 60
60 m_bitmap2.setConfig(SkBitmap::kARGB_8888_Config, 5, 5); 61 m_bitmap2.setConfig(SkBitmap::kARGB_8888_Config, 5, 5);
61 m_bitmap2.allocPixels(); 62 m_bitmap2.allocPixels();
62 m_bitmap2.eraseColor(0xAAAAAAAA); 63 m_bitmap2.eraseColor(0xAAAAAAAA);
63 64
64 // Save the global memory cache to restore it upon teardown. 65 // Save the global memory cache to restore it upon teardown.
65 m_globalMemoryCache = adoptPtr(memoryCache()); 66 m_globalMemoryCache = adoptPtr(memoryCache());
66 // Create the test memory cache instance and hook it in. 67 // Create the test memory cache instance and hook it in.
67 m_testingMemoryCache = adoptPtr(new MemoryCache()); 68 m_testingMemoryCache = adoptPtr(new MemoryCache());
68 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr()); 69 setMemoryCacheForTesting(m_testingMemoryCache.leakPtr());
69 } 70 }
70 virtual void TearDown() 71 virtual void TearDown()
71 { 72 {
73 // Garbage collection is required prior to switching out the
74 // test's memory cache; image resources are released, evicting
75 // them from the cache.
76 Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, Heap::ForcedFor Testing);
77
72 // Regain the ownership of testing memory cache, so that it will be 78 // Regain the ownership of testing memory cache, so that it will be
73 // destroyed. 79 // destroyed.
74 m_testingMemoryCache = adoptPtr(memoryCache()); 80 m_testingMemoryCache = adoptPtr(memoryCache());
75 // Yield the ownership of the global memory cache back. 81 // Yield the ownership of the global memory cache back.
76 setMemoryCacheForTesting(m_globalMemoryCache.leakPtr()); 82 setMemoryCacheForTesting(m_globalMemoryCache.leakPtr());
77 } 83 }
78 84
79 SkBitmap m_bitmap, m_bitmap2; 85 SkBitmap m_bitmap, m_bitmap2;
80 OwnPtr<MemoryCache> m_testingMemoryCache; 86 OwnPtr<MemoryCache> m_testingMemoryCache;
81 OwnPtr<MemoryCache> m_globalMemoryCache; 87 OwnPtr<MemoryCache> m_globalMemoryCache;
82 }; 88 };
83 89
84 // Verifies that the image resource held by an ImageBitmap is the same as the 90 // Verifies that the image resource held by an ImageBitmap is the same as the
85 // one held by the HTMLImageElement. 91 // one held by the HTMLImageElement.
86 TEST_F(ImageBitmapTest, ImageResourceConsistency) 92 TEST_F(ImageBitmapTest, ImageResourceConsistency)
87 { 93 {
88 RefPtr<HTMLImageElement> imageElement = HTMLImageElement::create(*Document:: create().get()); 94 RefPtr<HTMLImageElement> imageElement = HTMLImageElement::create(*Document:: create().get());
89 imageElement->setImageResource(new ImageResource(BitmapImage::create(NativeI mageSkia::create(m_bitmap)).get())); 95 imageElement->setImageResource(new ImageResource(BitmapImage::create(NativeI mageSkia::create(m_bitmap)).get()));
90 96
91 RefPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageElement.get (), IntRect(0, 0, m_bitmap.width(), m_bitmap.height())); 97 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imag eElement.get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
92 RefPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::create(imageEleme nt.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width() / 2, m_bitmap.height() / 2)); 98 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::creat e(imageElement.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bit map.width() / 2, m_bitmap.height() / 2));
93 RefPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageEleme nt.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitmap.width( ), m_bitmap.height())); 99 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::creat e(imageElement.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_b itmap.width(), m_bitmap.height()));
94 RefPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageElemen t.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width(), m_bitm ap.height())); 100 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create (imageElement.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.wid th(), m_bitmap.height()));
95 101
96 ASSERT_EQ(imageBitmapNoCrop->bitmapImage().get(), imageElement->cachedImage( )->image()); 102 ASSERT_EQ(imageBitmapNoCrop->bitmapImage().get(), imageElement->cachedImage( )->image());
97 ASSERT_EQ(imageBitmapInteriorCrop->bitmapImage().get(), imageElement->cached Image()->image()); 103 ASSERT_EQ(imageBitmapInteriorCrop->bitmapImage().get(), imageElement->cached Image()->image());
98 ASSERT_EQ(imageBitmapExteriorCrop->bitmapImage().get(), imageElement->cached Image()->image()); 104 ASSERT_EQ(imageBitmapExteriorCrop->bitmapImage().get(), imageElement->cached Image()->image());
99 105
100 RefPtr<Image> emptyImage = imageBitmapOutsideCrop->bitmapImage(); 106 RefPtr<Image> emptyImage = imageBitmapOutsideCrop->bitmapImage();
101 ASSERT_NE(emptyImage.get(), imageElement->cachedImage()->image()); 107 ASSERT_NE(emptyImage.get(), imageElement->cachedImage()->image());
102 } 108 }
103 109
104 // Verifies that HTMLImageElements are given an elevated CacheLiveResourcePriori ty when used to construct an ImageBitmap. 110 // Verifies that HTMLImageElements are given an elevated CacheLiveResourcePriori ty when used to construct an ImageBitmap.
(...skipping 30 matching lines...) Expand all
135 memoryCache()->insertInLiveDecodedResourcesList(cachedImageInteriorCrop.get( )); 141 memoryCache()->insertInLiveDecodedResourcesList(cachedImageInteriorCrop.get( ));
136 memoryCache()->insertInLiveDecodedResourcesList(cachedImageExteriorCrop.get( )); 142 memoryCache()->insertInLiveDecodedResourcesList(cachedImageExteriorCrop.get( ));
137 memoryCache()->insertInLiveDecodedResourcesList(cachedImageOutsideCrop.get() ); 143 memoryCache()->insertInLiveDecodedResourcesList(cachedImageOutsideCrop.get() );
138 144
139 // HTMLImageElements should default to CacheLiveResourcePriorityLow. 145 // HTMLImageElements should default to CacheLiveResourcePriorityLow.
140 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource: :CacheLiveResourcePriorityLow); 146 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource: :CacheLiveResourcePriorityLow);
141 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow); 147 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow);
142 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow); 148 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow);
143 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Reso urce::CacheLiveResourcePriorityLow); 149 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Reso urce::CacheLiveResourcePriorityLow);
144 150
145 RefPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::create(imageInter iorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.wid th(), m_bitmap.height())); 151 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::creat e(imageInteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
146 { 152 {
147 RefPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageNoCrop. get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height())); 153 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create( imageNoCrop.get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
148 RefPtr<ImageBitmap> imageBitmapInteriorCrop2 = ImageBitmap::create(image InteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitma p.width(), m_bitmap.height())); 154 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop2 = ImageBitmap:: create(imageInteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
149 RefPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageE xteriorCrop.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitm ap.width(), m_bitmap.height())); 155 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::c reate(imageExteriorCrop.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
150 RefPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageOu tsideCrop.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width() , m_bitmap.height())); 156 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::cr eate(imageOutsideCrop.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bi tmap.width(), m_bitmap.height()));
151 157
152 // Images that are referenced by ImageBitmaps have CacheLiveResourcePrio rityHigh. 158 // Images that are referenced by ImageBitmaps have CacheLiveResourcePrio rityHigh.
153 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resou rce::CacheLiveResourcePriorityHigh); 159 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resou rce::CacheLiveResourcePriorityHigh);
154 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh); 160 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
155 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh); 161 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
156 162
157 // ImageBitmaps that do not contain any of the source image do not eleva te CacheLiveResourcePriority. 163 // ImageBitmaps that do not contain any of the source image do not eleva te CacheLiveResourcePriority.
158 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow); 164 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
165
166 // Stub out references to the ImageBitmaps created and force a
167 // garbage collection to have the ImageBitmaps be collected and
168 // destructed.
169 imageBitmapNoCrop = nullptr;
170 imageBitmapInteriorCrop2 = nullptr;
171 imageBitmapExteriorCrop = nullptr;
172 imageBitmapOutsideCrop = nullptr;
173 Heap::collectGarbage(ThreadState::HeapPointersOnStack, Heap::ForcedForTe sting);
159 } 174 }
160 175
161 // CacheLiveResourcePriroity should return to CacheLiveResourcePriorityLow w hen no ImageBitmaps reference the image. 176 // CacheLiveResourcePriroity should return to CacheLiveResourcePriorityLow w hen no ImageBitmaps reference the image.
162 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource: :CacheLiveResourcePriorityLow); 177 ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource: :CacheLiveResourcePriorityLow);
163 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow); 178 ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityLow);
164 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Reso urce::CacheLiveResourcePriorityLow); 179 ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Reso urce::CacheLiveResourcePriorityLow);
165 180
166 // There is still an ImageBitmap that references this image. 181 // There is still an ImageBitmap that references this image.
167 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityHigh); 182 ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Res ource::CacheLiveResourcePriorityHigh);
183 imageBitmapInteriorCrop = nullptr;
168 } 184 }
169 185
170 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a referenc e to the original Image if the HTMLImageElement src is changed. 186 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a referenc e to the original Image if the HTMLImageElement src is changed.
171 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) 187 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged)
172 { 188 {
173 RefPtr<HTMLImageElement> image = HTMLImageElement::create(*Document::create( ).get()); 189 RefPtr<HTMLImageElement> image = HTMLImageElement::create(*Document::create( ).get());
174 ResourcePtr<ImageResource> originalImageResource = new ImageResource(BitmapI mage::create(NativeImageSkia::create(m_bitmap)).get()); 190 ResourcePtr<ImageResource> originalImageResource = new ImageResource(BitmapI mage::create(NativeImageSkia::create(m_bitmap)).get());
175 image->setImageResource(originalImageResource.get()); 191 image->setImageResource(originalImageResource.get());
176 192
177 RefPtr<ImageBitmap> imageBitmap = ImageBitmap::create(image.get(), IntRect(0 , 0, m_bitmap.width(), m_bitmap.height())); 193 RefPtrWillBeRawPtr<ImageBitmap> imageBitmap = ImageBitmap::create(image.get( ), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
178 ASSERT_EQ(imageBitmap->bitmapImage().get(), originalImageResource->image()); 194 ASSERT_EQ(imageBitmap->bitmapImage().get(), originalImageResource->image());
179 195
180 ResourcePtr<ImageResource> newImageResource = new ImageResource(BitmapImage: :create(NativeImageSkia::create(m_bitmap2)).get()); 196 ResourcePtr<ImageResource> newImageResource = new ImageResource(BitmapImage: :create(NativeImageSkia::create(m_bitmap2)).get());
181 image->setImageResource(newImageResource.get()); 197 image->setImageResource(newImageResource.get());
182 198
183 // The ImageBitmap should contain the same data as the original cached image but should no longer hold a reference. 199 // The ImageBitmap should contain the same data as the original cached image but should no longer hold a reference.
184 ASSERT_NE(imageBitmap->bitmapImage().get(), originalImageResource->image()); 200 ASSERT_NE(imageBitmap->bitmapImage().get(), originalImageResource->image());
185 ASSERT_EQ(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap() .pixelRef()->pixels(), 201 ASSERT_EQ(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap() .pixelRef()->pixels(),
186 originalImageResource->image()->nativeImageForCurrentFrame()->bitmap().p ixelRef()->pixels()); 202 originalImageResource->image()->nativeImageForCurrentFrame()->bitmap().p ixelRef()->pixels());
187 203
188 ASSERT_NE(imageBitmap->bitmapImage().get(), newImageResource->image()); 204 ASSERT_NE(imageBitmap->bitmapImage().get(), newImageResource->image());
189 ASSERT_NE(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap() .pixelRef()->pixels(), 205 ASSERT_NE(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap() .pixelRef()->pixels(),
190 newImageResource->image()->nativeImageForCurrentFrame()->bitmap().pixelR ef()->pixels()); 206 newImageResource->image()->nativeImageForCurrentFrame()->bitmap().pixelR ef()->pixels());
191 } 207 }
192 208
193 // Verifies that ImageBitmaps constructed from ImageBitmaps hold onto their own Image. 209 // Verifies that ImageBitmaps constructed from ImageBitmaps hold onto their own Image.
194 TEST_F(ImageBitmapTest, ImageResourceLifetime) 210 TEST_F(ImageBitmapTest, ImageResourceLifetime)
195 { 211 {
196 RefPtr<HTMLCanvasElement> canvasElement = HTMLCanvasElement::create(*Documen t::create().get()); 212 RefPtr<HTMLCanvasElement> canvasElement = HTMLCanvasElement::create(*Documen t::create().get());
197 canvasElement->setHeight(40); 213 canvasElement->setHeight(40);
198 canvasElement->setWidth(40); 214 canvasElement->setWidth(40);
199 RefPtr<ImageBitmap> imageBitmapDerived; 215 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapDerived;
200 { 216 {
201 RefPtr<ImageBitmap> imageBitmapFromCanvas = ImageBitmap::create(canvasEl ement.get(), IntRect(0, 0, canvasElement->width(), canvasElement->height())); 217 RefPtrWillBeRawPtr<ImageBitmap> imageBitmapFromCanvas = ImageBitmap::cre ate(canvasElement.get(), IntRect(0, 0, canvasElement->width(), canvasElement->he ight()));
202 imageBitmapDerived = ImageBitmap::create(imageBitmapFromCanvas.get(), In tRect(0, 0, 20, 20)); 218 imageBitmapDerived = ImageBitmap::create(imageBitmapFromCanvas.get(), In tRect(0, 0, 20, 20));
203 } 219 }
204 CanvasRenderingContext* context = canvasElement->getContext("2d"); 220 CanvasRenderingContext* context = canvasElement->getContext("2d");
205 TrackExceptionState exceptionState; 221 TrackExceptionState exceptionState;
206 toCanvasRenderingContext2D(context)->drawImage(imageBitmapDerived.get(), 0, 0, exceptionState); 222 toCanvasRenderingContext2D(context)->drawImage(imageBitmapDerived.get(), 0, 0, exceptionState);
207 } 223 }
208 224
209 } // namespace 225 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698