| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sky/engine/core/frame/ImageBitmap.h" | |
| 6 | |
| 7 #include "sky/engine/core/html/ImageData.h" | |
| 8 #include "sky/engine/platform/graphics/BitmapImage.h" | |
| 9 #include "sky/engine/platform/graphics/GraphicsContext.h" | |
| 10 #include "sky/engine/platform/graphics/ImageBuffer.h" | |
| 11 #include "sky/engine/wtf/RefPtr.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 static inline IntRect normalizeRect(const IntRect& rect) | |
| 16 { | |
| 17 return IntRect(std::min(rect.x(), rect.maxX()), | |
| 18 std::min(rect.y(), rect.maxY()), | |
| 19 std::max(rect.width(), -rect.width()), | |
| 20 std::max(rect.height(), -rect.height())); | |
| 21 } | |
| 22 | |
| 23 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect) | |
| 24 { | |
| 25 IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cro
pRect); | |
| 26 if (!intersectRect.width() || !intersectRect.height()) | |
| 27 return nullptr; | |
| 28 | |
| 29 SkBitmap cropped; | |
| 30 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, inters
ectRect); | |
| 31 return BitmapImage::create(NativeImageSkia::create(cropped)); | |
| 32 } | |
| 33 | |
| 34 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) | |
| 35 : m_imageElement(image) | |
| 36 , m_bitmap(nullptr) | |
| 37 , m_cropRect(cropRect) | |
| 38 { | |
| 39 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image
->height())); | |
| 40 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | |
| 41 m_bitmapOffset = srcRect.location(); | |
| 42 | |
| 43 if (!srcRect.width() || !srcRect.height()) | |
| 44 m_imageElement = nullptr; | |
| 45 else | |
| 46 m_imageElement->addClient(this); | |
| 47 | |
| 48 } | |
| 49 | |
| 50 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) | |
| 51 : m_imageElement(nullptr) | |
| 52 , m_cropRect(cropRect) | |
| 53 , m_bitmapOffset(IntPoint()) | |
| 54 { | |
| 55 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | |
| 56 | |
| 57 OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size()); | |
| 58 if (!buf) | |
| 59 return; | |
| 60 if (srcRect.width() > 0 && srcRect.height() > 0) | |
| 61 buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, In
tPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y()))); | |
| 62 | |
| 63 m_bitmap = buf->copyImage(DontCopyBackingStore); | |
| 64 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | |
| 65 } | |
| 66 | |
| 67 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) | |
| 68 : m_imageElement(bitmap->imageElement()) | |
| 69 , m_bitmap(nullptr) | |
| 70 , m_cropRect(cropRect) | |
| 71 , m_bitmapOffset(IntPoint()) | |
| 72 { | |
| 73 IntRect oldBitmapRect = bitmap->bitmapRect(); | |
| 74 IntRect srcRect = intersection(cropRect, oldBitmapRect); | |
| 75 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()
), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size()); | |
| 76 | |
| 77 if (m_imageElement) { | |
| 78 m_imageElement->addClient(this); | |
| 79 m_bitmapOffset = srcRect.location(); | |
| 80 } else if (bitmap->bitmapImage()) { | |
| 81 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR
ect.y() - oldBitmapRect.y()), cropRect.size()); | |
| 82 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) | |
| 87 : m_imageElement(nullptr) | |
| 88 , m_cropRect(cropRect) | |
| 89 { | |
| 90 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()))
; | |
| 91 m_bitmap = cropImage(image, cropRect); | |
| 92 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro
pRect.y())), srcRect.size()); | |
| 93 } | |
| 94 | |
| 95 ImageBitmap::~ImageBitmap() | |
| 96 { | |
| 97 #if !ENABLE(OILPAN) | |
| 98 if (m_imageElement) | |
| 99 m_imageElement->removeClient(this); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe
ct& cropRect) | |
| 104 { | |
| 105 IntRect normalizedCropRect = normalizeRect(cropRect); | |
| 106 return adoptRef(new ImageBitmap(image, normalizedCropRect)); | |
| 107 } | |
| 108 | |
| 109 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& crop
Rect) | |
| 110 { | |
| 111 IntRect normalizedCropRect = normalizeRect(cropRect); | |
| 112 return adoptRef(new ImageBitmap(data, normalizedCropRect)); | |
| 113 } | |
| 114 | |
| 115 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect&
cropRect) | |
| 116 { | |
| 117 IntRect normalizedCropRect = normalizeRect(cropRect); | |
| 118 return adoptRef(new ImageBitmap(bitmap, normalizedCropRect)); | |
| 119 } | |
| 120 | |
| 121 PassRefPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRec
t) | |
| 122 { | |
| 123 IntRect normalizedCropRect = normalizeRect(cropRect); | |
| 124 return adoptRef(new ImageBitmap(image, normalizedCropRect)); | |
| 125 } | |
| 126 | |
| 127 void ImageBitmap::notifyImageSourceChanged() | |
| 128 { | |
| 129 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect); | |
| 130 m_bitmapOffset = IntPoint(); | |
| 131 m_imageElement = nullptr; | |
| 132 } | |
| 133 | |
| 134 PassRefPtr<Image> ImageBitmap::bitmapImage() const | |
| 135 { | |
| 136 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect
.height()) && (!m_imageElement || !m_bitmap)); | |
| 137 if (m_imageElement) | |
| 138 return m_imageElement->cachedImage()->image(); | |
| 139 return m_bitmap; | |
| 140 } | |
| 141 | |
| 142 } | |
| OLD | NEW |