OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "core/frame/ImageBitmap.h" | 5 #include "core/frame/ImageBitmap.h" |
6 | 6 |
7 #include "core/html/HTMLCanvasElement.h" | 7 #include "core/html/HTMLCanvasElement.h" |
8 #include "core/html/HTMLVideoElement.h" | 8 #include "core/html/HTMLVideoElement.h" |
9 #include "core/html/ImageData.h" | 9 #include "core/html/ImageData.h" |
10 #include "platform/image-decoders/ImageDecoder.h" | |
10 #include "third_party/skia/include/core/SkSurface.h" | 11 #include "third_party/skia/include/core/SkSurface.h" |
11 #include "wtf/RefPtr.h" | 12 #include "wtf/RefPtr.h" |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 | 15 |
16 // The following two functions are helpers used in cropImage | |
15 static inline IntRect normalizeRect(const IntRect& rect) | 17 static inline IntRect normalizeRect(const IntRect& rect) |
16 { | 18 { |
17 return IntRect(std::min(rect.x(), rect.maxX()), | 19 return IntRect(std::min(rect.x(), rect.maxX()), |
18 std::min(rect.y(), rect.maxY()), | 20 std::min(rect.y(), rect.maxY()), |
19 std::max(rect.width(), -rect.width()), | 21 std::max(rect.width(), -rect.width()), |
20 std::max(rect.height(), -rect.height())); | 22 std::max(rect.height(), -rect.height())); |
21 } | 23 } |
22 | 24 |
23 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect) | 25 static bool frameIsValid(const SkBitmap& frameBitmap) |
26 { | |
27 return !frameBitmap.isNull() | |
28 && !frameBitmap.empty() | |
29 && frameBitmap.isImmutable() | |
30 && frameBitmap.colorType() == kN32_SkColorType; | |
31 } | |
32 | |
33 static PassRefPtr<StaticBitmapImage> cropImage(Image* image, const IntRect& crop Rect, const ImageBitmapOptions& options) | |
24 { | 34 { |
25 ASSERT(image); | 35 ASSERT(image); |
26 | 36 |
27 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); | 37 IntRect imgRect(IntPoint(), IntSize(image->width(), image->height())); |
28 const IntRect srcRect = intersection(imgRect, cropRect); | 38 const IntRect srcRect = intersection(imgRect, cropRect); |
29 | 39 |
40 RefPtr<SkImage> skiaImage = image->imageForCurrentFrame(); | |
41 // Attempt to get raw unpremultiplied image data. | |
42 if (((!options.premultiplyAlpha() && !skiaImage->isOpaque()) || !skiaImage) && image->data()) { | |
43 OwnPtr<ImageDecoder> decoder(ImageDecoder::create( | |
44 *(image->data()), ImageDecoder::AlphaNotPremultiplied, | |
45 ImageDecoder::GammaAndColorProfileIgnored)); | |
46 if (!decoder) | |
47 return nullptr; | |
48 decoder->setData(image->data(), true); | |
49 if (!decoder->frameCount()) | |
50 return nullptr; | |
51 ImageFrame* frame = decoder->frameBufferAtIndex(0); | |
52 if (!frame || frame->status() != ImageFrame::FrameComplete) | |
53 return nullptr; | |
54 SkBitmap bitmap = frame->bitmap(); | |
55 if (!frameIsValid(bitmap)) | |
56 return nullptr; | |
57 ASSERT(bitmap.isImmutable()); | |
58 skiaImage = adoptRef(SkImage::NewFromBitmap(bitmap)); | |
59 } | |
60 | |
30 if (cropRect == srcRect) | 61 if (cropRect == srcRect) |
31 return StaticBitmapImage::create(adoptRef(image->imageForCurrentFrame()- >newSubset(srcRect))); | 62 return StaticBitmapImage::create(adoptRef(skiaImage->newSubset(srcRect)) ); |
32 | 63 |
33 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect. width(), cropRect.height())); | 64 RefPtr<SkSurface> surface; |
65 if (options.premultiplyAlpha()) { | |
66 surface = adoptRef(SkSurface::NewRasterN32Premul(cropRect.width(), cropR ect.height())); | |
67 } else { | |
68 SkImageInfo info = SkImageInfo::MakeN32(cropRect.width(), cropRect.heigh t(), kUnpremul_SkAlphaType); | |
69 surface = adoptRef(SkSurface::NewRaster(info)); | |
70 } | |
34 | 71 |
35 if (srcRect.isEmpty()) | 72 if (srcRect.isEmpty()) |
36 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 73 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
37 | 74 |
38 SkScalar dstLeft = std::min(0, -cropRect.x()); | 75 SkScalar dstLeft = std::min(0, -cropRect.x()); |
39 SkScalar dstTop = std::min(0, -cropRect.y()); | 76 SkScalar dstTop = std::min(0, -cropRect.y()); |
40 if (cropRect.x() < 0) | 77 if (cropRect.x() < 0) |
41 dstLeft = -cropRect.x(); | 78 dstLeft = -cropRect.x(); |
42 if (cropRect.y() < 0) | 79 if (cropRect.y() < 0) |
43 dstTop = -cropRect.y(); | 80 dstTop = -cropRect.y(); |
44 surface->getCanvas()->drawImage(image->imageForCurrentFrame().get(), dstLeft , dstTop); | 81 |
82 surface->getCanvas()->drawImage(skiaImage.get(), dstLeft, dstTop); | |
xidachen
2016/01/20 02:29:07
This program could crash at this point. It is beca
| |
45 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); | 83 return StaticBitmapImage::create(adoptRef(surface->newImageSnapshot())); |
46 } | 84 } |
47 | 85 |
48 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 86 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
49 { | 87 { |
50 m_image = cropImage(image->cachedImage()->image(), cropRect); | 88 m_image = cropImage(image->cachedImage()->image(), cropRect, options); |
51 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); | 89 m_image->setOriginClean(!image->wouldTaintOrigin(document->securityOrigin()) ); |
52 } | 90 } |
53 | 91 |
54 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) | 92 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect, Docum ent* document, const ImageBitmapOptions& options) |
55 { | 93 { |
56 IntSize playerSize; | 94 IntSize playerSize; |
57 if (video->webMediaPlayer()) | 95 if (video->webMediaPlayer()) |
58 playerSize = video->webMediaPlayer()->naturalSize(); | 96 playerSize = video->webMediaPlayer()->naturalSize(); |
59 | 97 |
60 IntRect videoRect = IntRect(IntPoint(), playerSize); | 98 IntRect videoRect = IntRect(IntPoint(), playerSize); |
61 IntRect srcRect = intersection(cropRect, videoRect); | 99 IntRect srcRect = intersection(cropRect, videoRect); |
62 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); | 100 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); |
63 if (!buffer) | 101 if (!buffer) |
64 return; | 102 return; |
65 | 103 |
66 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); | 104 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y())); |
67 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); | 105 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr); |
68 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); | 106 if (options.premultiplyAlpha()) |
107 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); | |
108 else | |
109 m_image = cropImage(buffer->newImageSnapshot().get(), IntRect(IntPoint(0 , 0), cropRect.size()), options); | |
69 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); | 110 m_image->setOriginClean(!video->wouldTaintOrigin(document->securityOrigin()) ); |
70 } | 111 } |
71 | 112 |
72 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) | 113 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect, con st ImageBitmapOptions& options) |
73 { | 114 { |
74 ASSERT(canvas->isPaintable()); | 115 ASSERT(canvas->isPaintable()); |
75 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect); | 116 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration).get( ), cropRect, options); |
76 m_image->setOriginClean(canvas->originClean()); | 117 m_image->setOriginClean(canvas->originClean()); |
77 } | 118 } |
78 | 119 |
79 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) | 120 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect, const ImageBi tmapOptions& options) |
80 { | 121 { |
81 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); | 122 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); |
82 | 123 |
83 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); | 124 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels); |
84 if (!buffer) | 125 if (!buffer) |
85 return; | 126 return; |
86 | 127 |
87 if (srcRect.isEmpty()) { | 128 if (srcRect.isEmpty()) { |
88 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); | 129 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoA cceleration)); |
89 return; | 130 return; |
90 } | 131 } |
91 | 132 |
92 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y())); | 133 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y())); |
93 if (cropRect.x() < 0) | 134 if (cropRect.x() < 0) |
94 dstPoint.setX(-cropRect.x()); | 135 dstPoint.setX(-cropRect.x()); |
95 if (cropRect.y() < 0) | 136 if (cropRect.y() < 0) |
96 dstPoint.setY(-cropRect.y()); | 137 dstPoint.setY(-cropRect.y()); |
97 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); | 138 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), srcRe ct, dstPoint); |
98 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); | 139 m_image = StaticBitmapImage::create(buffer->newSkImageSnapshot(PreferNoAccel eration)); |
99 } | 140 } |
100 | 141 |
101 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) | 142 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect, const Ima geBitmapOptions& options) |
102 { | 143 { |
103 m_image = cropImage(bitmap->bitmapImage(), cropRect); | 144 m_image = cropImage(bitmap->bitmapImage(), cropRect, options); |
104 m_image->setOriginClean(bitmap->originClean()); | 145 m_image->setOriginClean(bitmap->originClean()); |
105 } | 146 } |
106 | 147 |
107 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) | 148 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image, const IntRect& cro pRect, const ImageBitmapOptions& options) |
108 { | 149 { |
109 m_image = cropImage(image.get(), cropRect); | 150 m_image = cropImage(image.get(), cropRect, options); |
110 m_image->setOriginClean(image->originClean()); | 151 m_image->setOriginClean(image->originClean()); |
111 } | 152 } |
112 | 153 |
113 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) | 154 ImageBitmap::ImageBitmap(PassRefPtr<StaticBitmapImage> image) |
114 { | 155 { |
115 m_image = image; | 156 m_image = image; |
116 } | 157 } |
117 | 158 |
118 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() | 159 PassRefPtr<StaticBitmapImage> ImageBitmap::transfer() |
119 { | 160 { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 { | 259 { |
219 return FloatSize(width(), height()); | 260 return FloatSize(width(), height()); |
220 } | 261 } |
221 | 262 |
222 DEFINE_TRACE(ImageBitmap) | 263 DEFINE_TRACE(ImageBitmap) |
223 { | 264 { |
224 ImageLoaderClient::trace(visitor); | 265 ImageLoaderClient::trace(visitor); |
225 } | 266 } |
226 | 267 |
227 } // namespace blink | 268 } // namespace blink |
OLD | NEW |