| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/core/frame/ImageBitmap.h" | |
| 32 | |
| 33 #include "sky/engine/core/dom/Document.h" | |
| 34 #include "sky/engine/core/fetch/ImageResource.h" | |
| 35 #include "sky/engine/core/fetch/MemoryCache.h" | |
| 36 #include "sky/engine/core/fetch/MockImageResourceClient.h" | |
| 37 #include "sky/engine/core/fetch/ResourcePtr.h" | |
| 38 #include "sky/engine/core/html/HTMLImageElement.h" | |
| 39 #include "sky/engine/platform/graphics/BitmapImage.h" | |
| 40 #include "sky/engine/platform/graphics/skia/NativeImageSkia.h" | |
| 41 #include "sky/engine/platform/heap/Handle.h" | |
| 42 #include "sky/engine/platform/network/ResourceRequest.h" | |
| 43 #include "sky/engine/wtf/OwnPtr.h" | |
| 44 #include "third_party/skia/include/core/SkPixelRef.h" | |
| 45 | |
| 46 #include <gtest/gtest.h> | |
| 47 | |
| 48 namespace blink { | |
| 49 | |
| 50 class ImageBitmapTest : public ::testing::Test { | |
| 51 protected: | |
| 52 virtual void SetUp() | |
| 53 { | |
| 54 m_bitmap.allocN32Pixels(10, 10); | |
| 55 m_bitmap.eraseColor(0xFFFFFFFF); | |
| 56 | |
| 57 m_bitmap2.allocN32Pixels(5, 5); | |
| 58 m_bitmap2.eraseColor(0xAAAAAAAA); | |
| 59 | |
| 60 // Save the global memory cache to restore it upon teardown. | |
| 61 m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create()
); | |
| 62 } | |
| 63 virtual void TearDown() | |
| 64 { | |
| 65 replaceMemoryCacheForTesting(m_globalMemoryCache.release()); | |
| 66 } | |
| 67 | |
| 68 SkBitmap m_bitmap, m_bitmap2; | |
| 69 OwnPtr<MemoryCache> m_globalMemoryCache; | |
| 70 }; | |
| 71 | |
| 72 // Verifies that the image resource held by an ImageBitmap is the same as the | |
| 73 // one held by the HTMLImageElement. | |
| 74 TEST_F(ImageBitmapTest, ImageResourceConsistency) | |
| 75 { | |
| 76 RefPtr<HTMLImageElement> imageElement = HTMLImageElement::create(*Document::
create().get()); | |
| 77 imageElement->setImageResource(new ImageResource(BitmapImage::create(NativeI
mageSkia::create(m_bitmap)).get())); | |
| 78 | |
| 79 RefPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageElement.get
(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height())); | |
| 80 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)); | |
| 81 RefPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageEleme
nt.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitmap.width(
), m_bitmap.height())); | |
| 82 RefPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageElemen
t.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width(), m_bitm
ap.height())); | |
| 83 | |
| 84 ASSERT_EQ(imageBitmapNoCrop->bitmapImage().get(), imageElement->cachedImage(
)->image()); | |
| 85 ASSERT_EQ(imageBitmapInteriorCrop->bitmapImage().get(), imageElement->cached
Image()->image()); | |
| 86 ASSERT_EQ(imageBitmapExteriorCrop->bitmapImage().get(), imageElement->cached
Image()->image()); | |
| 87 | |
| 88 RefPtr<Image> emptyImage = imageBitmapOutsideCrop->bitmapImage(); | |
| 89 ASSERT_NE(emptyImage.get(), imageElement->cachedImage()->image()); | |
| 90 } | |
| 91 | |
| 92 // Verifies that HTMLImageElements are given an elevated CacheLiveResourcePriori
ty when used to construct an ImageBitmap. | |
| 93 // ImageBitmaps that have crop rects outside of the bounds of the HTMLImageEleme
nt do not have elevated CacheLiveResourcePriority. | |
| 94 TEST_F(ImageBitmapTest, ImageBitmapLiveResourcePriority) | |
| 95 { | |
| 96 RefPtr<HTMLImageElement> imageNoCrop = HTMLImageElement::create(*Document::c
reate().get()); | |
| 97 ResourcePtr<ImageResource> cachedImageNoCrop = new ImageResource(ResourceReq
uest("http://foo.com/1"), BitmapImage::create(NativeImageSkia::create(m_bitmap))
.get()); | |
| 98 imageNoCrop->setImageResource(cachedImageNoCrop.get()); | |
| 99 | |
| 100 RefPtr<HTMLImageElement> imageInteriorCrop = HTMLImageElement::create(*Docum
ent::create().get()); | |
| 101 ResourcePtr<ImageResource> cachedImageInteriorCrop = new ImageResource(Resou
rceRequest("http://foo.com/2"), BitmapImage::create(NativeImageSkia::create(m_bi
tmap)).get()); | |
| 102 imageInteriorCrop->setImageResource(cachedImageInteriorCrop.get()); | |
| 103 | |
| 104 RefPtr<HTMLImageElement> imageExteriorCrop = HTMLImageElement::create(*Docum
ent::create().get()); | |
| 105 ResourcePtr<ImageResource> cachedImageExteriorCrop = new ImageResource(Resou
rceRequest("http://foo.com/3"), BitmapImage::create(NativeImageSkia::create(m_bi
tmap)).get()); | |
| 106 imageExteriorCrop->setImageResource(cachedImageExteriorCrop.get()); | |
| 107 | |
| 108 RefPtr<HTMLImageElement> imageOutsideCrop = HTMLImageElement::create(*Docume
nt::create().get()); | |
| 109 ResourcePtr<ImageResource> cachedImageOutsideCrop = new ImageResource(Resour
ceRequest("http://foo.com/4"), BitmapImage::create(NativeImageSkia::create(m_bit
map)).get()); | |
| 110 imageOutsideCrop->setImageResource(cachedImageOutsideCrop.get()); | |
| 111 | |
| 112 MockImageResourceClient mockClient1, mockClient2, mockClient3, mockClient4; | |
| 113 cachedImageNoCrop->addClient(&mockClient1); | |
| 114 cachedImageInteriorCrop->addClient(&mockClient2); | |
| 115 cachedImageExteriorCrop->addClient(&mockClient3); | |
| 116 cachedImageOutsideCrop->addClient(&mockClient4); | |
| 117 | |
| 118 memoryCache()->add(cachedImageNoCrop.get()); | |
| 119 memoryCache()->add(cachedImageInteriorCrop.get()); | |
| 120 memoryCache()->add(cachedImageExteriorCrop.get()); | |
| 121 memoryCache()->add(cachedImageOutsideCrop.get()); | |
| 122 memoryCache()->updateDecodedResource(cachedImageNoCrop.get(), UpdateForPrope
rtyChange); | |
| 123 memoryCache()->updateDecodedResource(cachedImageInteriorCrop.get(), UpdateFo
rPropertyChange); | |
| 124 memoryCache()->updateDecodedResource(cachedImageExteriorCrop.get(), UpdateFo
rPropertyChange); | |
| 125 memoryCache()->updateDecodedResource(cachedImageOutsideCrop.get(), UpdateFor
PropertyChange); | |
| 126 | |
| 127 // HTMLImageElements should default to CacheLiveResourcePriorityLow. | |
| 128 ASSERT_EQ(memoryCache()->priority(imageNoCrop->cachedImage()), MemoryCacheLi
veResourcePriorityLow); | |
| 129 ASSERT_EQ(memoryCache()->priority(imageInteriorCrop->cachedImage()), MemoryC
acheLiveResourcePriorityLow); | |
| 130 ASSERT_EQ(memoryCache()->priority(imageExteriorCrop->cachedImage()), MemoryC
acheLiveResourcePriorityLow); | |
| 131 ASSERT_EQ(memoryCache()->priority(imageOutsideCrop->cachedImage()), MemoryCa
cheLiveResourcePriorityLow); | |
| 132 | |
| 133 RefPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::create(imageInter
iorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.wid
th(), m_bitmap.height())); | |
| 134 { | |
| 135 RefPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageNoCrop.
get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height())); | |
| 136 RefPtr<ImageBitmap> imageBitmapInteriorCrop2 = ImageBitmap::create(image
InteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitma
p.width(), m_bitmap.height())); | |
| 137 RefPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageE
xteriorCrop.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitm
ap.width(), m_bitmap.height())); | |
| 138 RefPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageOu
tsideCrop.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width()
, m_bitmap.height())); | |
| 139 | |
| 140 // Images that are referenced by ImageBitmaps have CacheLiveResourcePrio
rityHigh. | |
| 141 ASSERT_EQ(memoryCache()->priority(imageNoCrop->cachedImage()), MemoryCac
heLiveResourcePriorityHigh); | |
| 142 ASSERT_EQ(memoryCache()->priority(imageInteriorCrop->cachedImage()), Mem
oryCacheLiveResourcePriorityHigh); | |
| 143 ASSERT_EQ(memoryCache()->priority(imageExteriorCrop->cachedImage()), Mem
oryCacheLiveResourcePriorityHigh); | |
| 144 | |
| 145 // ImageBitmaps that do not contain any of the source image do not eleva
te CacheLiveResourcePriority. | |
| 146 ASSERT_EQ(memoryCache()->priority(imageOutsideCrop->cachedImage()), Memo
ryCacheLiveResourcePriorityLow); | |
| 147 } | |
| 148 // CacheLiveResourcePriroity should return to CacheLiveResourcePriorityLow w
hen no ImageBitmaps reference the image. | |
| 149 ASSERT_EQ(memoryCache()->priority(imageNoCrop->cachedImage()), MemoryCacheLi
veResourcePriorityLow); | |
| 150 ASSERT_EQ(memoryCache()->priority(imageExteriorCrop->cachedImage()), MemoryC
acheLiveResourcePriorityLow); | |
| 151 ASSERT_EQ(memoryCache()->priority(imageOutsideCrop->cachedImage()), MemoryCa
cheLiveResourcePriorityLow); | |
| 152 | |
| 153 // There is still an ImageBitmap that references this image. | |
| 154 ASSERT_EQ(memoryCache()->priority(imageInteriorCrop->cachedImage()), MemoryC
acheLiveResourcePriorityHigh); | |
| 155 imageBitmapInteriorCrop = nullptr; | |
| 156 | |
| 157 cachedImageNoCrop->removeClient(&mockClient1); | |
| 158 cachedImageInteriorCrop->removeClient(&mockClient2); | |
| 159 cachedImageExteriorCrop->removeClient(&mockClient3); | |
| 160 cachedImageOutsideCrop->removeClient(&mockClient4); | |
| 161 } | |
| 162 | |
| 163 // Verifies that ImageBitmaps constructed from HTMLImageElements hold a referenc
e to the original Image if the HTMLImageElement src is changed. | |
| 164 TEST_F(ImageBitmapTest, ImageBitmapSourceChanged) | |
| 165 { | |
| 166 RefPtr<HTMLImageElement> image = HTMLImageElement::create(*Document::create(
).get()); | |
| 167 ResourcePtr<ImageResource> originalImageResource = new ImageResource(BitmapI
mage::create(NativeImageSkia::create(m_bitmap)).get()); | |
| 168 image->setImageResource(originalImageResource.get()); | |
| 169 | |
| 170 RefPtr<ImageBitmap> imageBitmap = ImageBitmap::create(image.get(), IntRect(0
, 0, m_bitmap.width(), m_bitmap.height())); | |
| 171 ASSERT_EQ(imageBitmap->bitmapImage().get(), originalImageResource->image()); | |
| 172 | |
| 173 ResourcePtr<ImageResource> newImageResource = new ImageResource(BitmapImage:
:create(NativeImageSkia::create(m_bitmap2)).get()); | |
| 174 image->setImageResource(newImageResource.get()); | |
| 175 | |
| 176 // The ImageBitmap should contain the same data as the original cached image
but should no longer hold a reference. | |
| 177 ASSERT_NE(imageBitmap->bitmapImage().get(), originalImageResource->image()); | |
| 178 ASSERT_EQ(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap()
.pixelRef()->pixels(), | |
| 179 originalImageResource->image()->nativeImageForCurrentFrame()->bitmap().p
ixelRef()->pixels()); | |
| 180 | |
| 181 ASSERT_NE(imageBitmap->bitmapImage().get(), newImageResource->image()); | |
| 182 ASSERT_NE(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap()
.pixelRef()->pixels(), | |
| 183 newImageResource->image()->nativeImageForCurrentFrame()->bitmap().pixelR
ef()->pixels()); | |
| 184 } | |
| 185 | |
| 186 } // namespace | |
| OLD | NEW |