Upstream version 11.40.277.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
53 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
54     : m_imageElement(nullptr)
55     , m_cropRect(cropRect)
56     , m_bitmapOffset(IntPoint())
57 {
58     IntSize playerSize;
59
60     if (video->webMediaPlayer())
61         playerSize = video->webMediaPlayer()->naturalSize();
62
63     IntRect videoRect = IntRect(IntPoint(), playerSize);
64     IntRect srcRect = intersection(cropRect, videoRect);
65     IntRect dstRect(IntPoint(), srcRect.size());
66
67     OwnPtr<ImageBuffer> buf = ImageBuffer::create(videoRect.size());
68     if (!buf)
69         return;
70     GraphicsContext* c = buf->context();
71     c->clip(dstRect);
72     c->translate(-srcRect.x(), -srcRect.y());
73     video->paintCurrentFrameInContext(c, videoRect);
74     m_bitmap = buf->copyImage(DontCopyBackingStore);
75     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
76 }
77
78 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
79     : m_imageElement(nullptr)
80     , m_cropRect(cropRect)
81     , m_bitmapOffset(IntPoint())
82 {
83     CanvasRenderingContext* sourceContext = canvas->renderingContext();
84     if (sourceContext && sourceContext->is3d())
85         sourceContext->paintRenderingResultsToCanvas();
86
87     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()));
88     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())), srcRect.size());
89     m_bitmap = cropImage(canvas->buffer()->copyImage(CopyBackingStore).get(), cropRect);
90 }
91
92 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
93     : m_imageElement(nullptr)
94     , m_cropRect(cropRect)
95     , m_bitmapOffset(IntPoint())
96 {
97     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
98
99     OwnPtr<ImageBuffer> buf = ImageBuffer::create(data->size());
100     if (!buf)
101         return;
102     if (srcRect.width() > 0 && srcRect.height() > 0)
103         buf->putByteArray(Premultiplied, data->data(), data->size(), srcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
104
105     m_bitmap = buf->copyImage(DontCopyBackingStore);
106     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())),  srcRect.size());
107 }
108
109 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
110     : m_imageElement(bitmap->imageElement())
111     , m_bitmap(nullptr)
112     , m_cropRect(cropRect)
113     , m_bitmapOffset(IntPoint())
114 {
115     IntRect oldBitmapRect = bitmap->bitmapRect();
116     IntRect srcRect = intersection(cropRect, oldBitmapRect);
117     m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x()), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
118
119     if (m_imageElement) {
120         m_imageElement->addClient(this);
121         m_bitmapOffset = srcRect.location();
122     } else if (bitmap->bitmapImage()) {
123         IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropRect.y() - oldBitmapRect.y()), cropRect.size());
124         m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect);
125     }
126 }
127
128 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
129     : m_imageElement(nullptr)
130     , m_cropRect(cropRect)
131 {
132     IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), image->size()));
133     m_bitmap = cropImage(image, cropRect);
134     m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRect.y())),  srcRect.size());
135 }
136
137 ImageBitmap::~ImageBitmap()
138 {
139 #if !ENABLE(OILPAN)
140     if (m_imageElement)
141         m_imageElement->removeClient(this);
142 #endif
143 }
144
145 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
146 {
147     IntRect normalizedCropRect = normalizeRect(cropRect);
148     return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
149 }
150
151 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
152 {
153     IntRect normalizedCropRect = normalizeRect(cropRect);
154     return adoptRefWillBeNoop(new ImageBitmap(video, normalizedCropRect));
155 }
156
157 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const IntRect& cropRect)
158 {
159     IntRect normalizedCropRect = normalizeRect(cropRect);
160     return adoptRefWillBeNoop(new ImageBitmap(canvas, normalizedCropRect));
161 }
162
163 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& cropRect)
164 {
165     IntRect normalizedCropRect = normalizeRect(cropRect);
166     return adoptRefWillBeNoop(new ImageBitmap(data, normalizedCropRect));
167 }
168
169 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
170 {
171     IntRect normalizedCropRect = normalizeRect(cropRect);
172     return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect));
173 }
174
175 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntRect& cropRect)
176 {
177     IntRect normalizedCropRect = normalizeRect(cropRect);
178     return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
179 }
180
181 void ImageBitmap::notifyImageSourceChanged()
182 {
183     m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
184     m_bitmapOffset = IntPoint();
185     m_imageElement = nullptr;
186 }
187
188 PassRefPtr<Image> ImageBitmap::bitmapImage() const
189 {
190     ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect.height()) && (!m_imageElement || !m_bitmap));
191     if (m_imageElement)
192         return m_imageElement->cachedImage()->image();
193     return m_bitmap;
194 }
195
196 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageMode, SourceImageStatus* status) const
197 {
198     *status = NormalSourceImageStatus;
199     return bitmapImage();
200 }
201
202 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
203 {
204     FloatRect intersectRect = intersection(m_bitmapRect, *srcRect);
205     FloatRect newSrcRect = intersectRect;
206     newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
207     FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location()), m_bitmapRect.size());
208     newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
209         dstRect->height() / srcRect->height() * intersectRect.height() / m_bitmapRect.height());
210     newDstRect.moveBy(dstRect->location());
211     *srcRect = newSrcRect;
212     *dstRect = newDstRect;
213 }
214
215 FloatSize ImageBitmap::sourceSize() const
216 {
217     return FloatSize(width(), height());
218 }
219
220 void ImageBitmap::trace(Visitor* visitor)
221 {
222     visitor->trace(m_imageElement);
223     ImageLoaderClient::trace(visitor);
224 }
225
226 } // namespace blink