Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / ImageBitmap.cpp
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 "config.h"
6 #include "core/frame/ImageBitmap.h"
7
8 #include "core/html/HTMLCanvasElement.h"
9 #include "core/html/HTMLVideoElement.h"
10 #include "core/html/ImageData.h"
11 #include "core/html/canvas/CanvasRenderingContext.h"
12 #include "platform/graphics/BitmapImage.h"
13 #include "platform/graphics/GraphicsContext.h"
14 #include "platform/graphics/ImageBuffer.h"
15 #include "wtf/RefPtr.h"
16
17 namespace blink {
18
19 static inline IntRect normalizeRect(const IntRect& rect)
20 {
21     return IntRect(std::min(rect.x(), rect.maxX()),
22         std::min(rect.y(), rect.maxY()),
23         std::max(rect.width(), -rect.width()),
24         std::max(rect.height(), -rect.height()));
25 }
26
27 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect)
28 {
29     IntRect intersectRect = intersection(IntRect(IntPoint(), image->size()), cropRect);
30     if (!intersectRect.width() || !intersectRect.height())
31         return nullptr;
32
33     SkBitmap cropped;
34     image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, intersectRect);
35     return BitmapImage::create(NativeImageSkia::create(cropped));
36 }
37
38 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
39     : m_imageElement(image)
40     , m_bitmap(nullptr)
41     , m_cropRect(cropRect)
42 {
43     IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image->height()));
44     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
45     m_bitmapOffset = srcRect.location();
46
47     if (!srcRect.width() || !srcRect.height())
48         m_imageElement = nullptr;
49     else
50         m_imageElement->addClient(this);
51
52     ScriptWrappable::init(this);
53 }
54
55 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
56     : m_imageElement(nullptr)
57     , m_cropRect(cropRect)
58     , m_bitmapOffset(IntPoint())
59 {
60     IntSize playerSize;
61
62     if (video->webMediaPlayer())
63         playerSize = video->webMediaPlayer()->naturalSize();
64
65     IntRect videoRect = IntRect(IntPoint(), playerSize);
66     IntRect srcRect = intersection(cropRect, videoRect);
67     IntRect dstRect(IntPoint(), srcRect.size());
68
69     OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size());
70     if (!buf)
71         return;
72     GraphicsContext* c = buf->context();
73     c->clip(dstRect);
74     c->translate(-srcRect.x(), -srcRect.y());
75     video->paintCurrentFrameInContext(c, videoRect);
76     m_bitmap = buf->copyImage(DontCopyBackingStore);
77     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
78
79     ScriptWrappable::init(this);
80 }
81
82 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
83     : m_imageElement(nullptr)
84     , m_cropRect(cropRect)
85     , m_bitmapOffset(IntPoint())
86 {
87     CanvasRenderingContext* sourceContext = canvas->renderingContext();
88     if (sourceContext && sourceContext->is3d())
89         sourceContext->paintRenderingResultsToCanvas();
90
91     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()));
92     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
93     m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cropRect);
94
95     ScriptWrappable::init(this);
96 }
97
98 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
99     : m_imageElement(nullptr)
100     , m_cropRect(cropRect)
101     , m_bitmapOffset(IntPoint())
102 {
103     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
104
105     OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size());
106     if (!buf)
107         return;
108     if (srcRect.width() > 0 && srcRect.height() > 0)
109         buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
110
111     m_bitmap = buf->copyImage(DontCopyBackingStore);
112     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())),  srcRect.size());
113
114     ScriptWrappable::init(this);
115 }
116
117 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
118     : m_imageElement(bitmap->imageElement())
119     , m_bitmap(nullptr)
120     , m_cropRect(cropRect)
121     , m_bitmapOffset(IntPoint())
122 {
123     IntRect oldBitmapRect = bitmap->bitmapRect();
124     IntRect srcRect = intersection(cropRect, oldBitmapRect);
125     m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
126
127     if (m_imageElement) {
128         m_imageElement->addClient(this);
129         m_bitmapOffset = srcRect.location();
130     } else if (bitmap->bitmapImage()) {
131         IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropRect.y() - oldBitmapRect.y()), cropRect.size());
132         m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect);
133     }
134
135     ScriptWrappable::init(this);
136 }
137
138 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
139     : m_imageElement(nullptr)
140     , m_cropRect(cropRect)
141 {
142     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()));
143     m_bitmap = cropImage(image, cropRect);
144     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())),  srcRect.size());
145
146     ScriptWrappable::init(this);
147 }
148
149 ImageBitmap::~ImageBitmap()
150 {
151 #if !ENABLE(OILPAN)
152     if (m_imageElement)
153         m_imageElement->removeClient(this);
154 #endif
155 }
156
157 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
158 {
159     IntRect normalizedCropRect = normalizeRect(cropRect);
160     return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
161 }
162
163 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
164 {
165     IntRect normalizedCropRect = normalizeRect(cropRect);
166     return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect));
167 }
168
169 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropRect)
170 {
171     IntRect normalizedCropRect = normalizeRect(cropRect);
172     return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect));
173 }
174
175 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& cropRect)
176 {
177     IntRect normalizedCropRect = normalizeRect(cropRect);
178     return adoptRefWillBeNoop(new ImageBitmap(data, normalizedCropRect));
179 }
180
181 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
182 {
183     IntRect normalizedCropRect = normalizeRect(cropRect);
184     return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect));
185 }
186
187 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRect)
188 {
189     IntRect normalizedCropRect = normalizeRect(cropRect);
190     return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
191 }
192
193 void ImageBitmap::notifyImageSourceChanged()
194 {
195     m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
196     m_bitmapOffset = IntPoint();
197     m_imageElement = nullptr;
198 }
199
200 PassRefPtr<Image> ImageBitmap::bitmapImage() const
201 {
202     ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect.height()) && (!m_imageElement || !m_bitmap));
203     if (m_imageElement)
204         return m_imageElement->cachedImage()->image();
205     return m_bitmap;
206 }
207
208 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageMode, SourceImageStatus* status) const
209 {
210     *status = NormalSourceImageStatus;
211     return bitmapImage();
212 }
213
214 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
215 {
216     FloatRect intersectRect = intersection(m_bitmapRect, *srcRect);
217     FloatRect newSrcRect = intersectRect;
218     newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
219     FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location()), m_bitmapRect.size());
220     newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
221         dstRect->height() / srcRect->height() * intersectRect.height() / m_bitmapRect.height());
222     newDstRect.moveBy(dstRect->location());
223     *srcRect = newSrcRect;
224     *dstRect = newDstRect;
225 }
226
227 FloatSize ImageBitmap::sourceSize() const
228 {
229     return FloatSize(width(), height());
230 }
231
232 void ImageBitmap::trace(Visitor* visitor)
233 {
234     visitor->trace(m_imageElement);
235     ImageLoaderClient::trace(visitor);
236 }
237
238 }