Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: third_party/WebKit/Source/core/frame/ImageBitmap.cpp

Issue 1413583004: refractoring ImageBitmap class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up local vars Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "config.h" 5 #include "config.h"
6 #include "core/frame/ImageBitmap.h" 6 #include "core/frame/ImageBitmap.h"
7 7
8 #include "core/html/HTMLCanvasElement.h" 8 #include "core/html/HTMLCanvasElement.h"
9 #include "core/html/HTMLVideoElement.h" 9 #include "core/html/HTMLVideoElement.h"
10 #include "core/html/ImageData.h" 10 #include "core/html/ImageData.h"
11 #include "platform/graphics/GraphicsContext.h" 11 #include "platform/graphics/GraphicsContext.h"
12 #include "platform/graphics/ImageBuffer.h" 12 #include "platform/graphics/ImageBuffer.h"
13 #include "platform/graphics/StaticBitmapImage.h" 13 #include "platform/graphics/StaticBitmapImage.h"
14 #include "platform/graphics/paint/DrawingRecorder.h" 14 #include "platform/graphics/paint/DrawingRecorder.h"
15 #include "platform/graphics/paint/SkPictureBuilder.h" 15 #include "platform/graphics/paint/SkPictureBuilder.h"
16 #include "wtf/RefPtr.h" 16 #include "wtf/RefPtr.h"
17 17
18 namespace blink { 18 namespace blink {
19 19
20 static inline IntRect normalizeRect(const IntRect& rect) 20 static inline IntRect normalizeRect(const IntRect& rect)
21 { 21 {
22 return IntRect(std::min(rect.x(), rect.maxX()), 22 return IntRect(std::min(rect.x(), rect.maxX()),
23 std::min(rect.y(), rect.maxY()), 23 std::min(rect.y(), rect.maxY()),
24 std::max(rect.width(), -rect.width()), 24 std::max(rect.width(), -rect.width()),
25 std::max(rect.height(), -rect.height())); 25 std::max(rect.height(), -rect.height()));
26 } 26 }
27 27
28 static inline PassRefPtr<Image> cropImage(PassRefPtr<Image> image, const IntRect & cropRect) 28 static inline PassRefPtr<SkImage> cropImage(PassRefPtr<SkImage> image, const Int Rect& cropRect)
29 { 29 {
30 ASSERT(image); 30 ASSERT(image);
31 31
32 const SkIRect srcRect = intersection(image->rect(), cropRect); 32 IntRect imgRect = IntRect(IntPoint(), IntSize(image->width(), image->height( )));
33 const SkIRect srcRect = intersection(imgRect, cropRect);
33 if (srcRect.isEmpty()) 34 if (srcRect.isEmpty())
34 return nullptr; 35 return nullptr;
35 36
36 RefPtr<SkImage> skImage = image->imageForCurrentFrame(); 37 return adoptRef(image->newSubset(srcRect));
Justin Novosad 2015/11/02 15:53:19 Not so fast! In the case where the intersection op
37 if (!skImage)
38 return nullptr;
39
40 return StaticBitmapImage::create(adoptRef(skImage->newSubset(srcRect)));
41 } 38 }
42 39
43 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect) 40 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
44 : m_imageElement(image)
45 , m_bitmap(nullptr)
46 , m_cropRect(cropRect)
47 { 41 {
48 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image ->height())); 42 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c ropRect);
49 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
50 m_bitmapOffset = srcRect.location();
51
52 if (!srcRect.width() || !srcRect.height())
53 m_imageElement = nullptr;
54 else
55 m_imageElement->addClient(this);
56 } 43 }
57 44
58 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) 45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
59 : m_imageElement(nullptr)
60 , m_cropRect(cropRect)
61 , m_bitmapOffset(IntPoint())
62 { 46 {
63 IntSize playerSize; 47 IntSize playerSize;
64 48
65 if (video->webMediaPlayer()) 49 if (video->webMediaPlayer())
66 playerSize = video->webMediaPlayer()->naturalSize(); 50 playerSize = video->webMediaPlayer()->naturalSize();
67 51
68 IntRect videoRect = IntRect(IntPoint(), playerSize); 52 IntRect videoRect = IntRect(IntPoint(), playerSize);
69 IntRect srcRect = intersection(cropRect, videoRect); 53 IntRect srcRect = intersection(cropRect, videoRect);
70 IntRect dstRect(IntPoint(), srcRect.size()); 54 IntRect dstRect(IntPoint(), srcRect.size());
71 55
72 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size()); 56 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size());
73 if (!buffer) 57 if (!buffer)
74 return; 58 return;
75 59
76 buffer->canvas()->clipRect(dstRect); 60 buffer->canvas()->clipRect(dstRect);
77 buffer->canvas()->translate(-srcRect.x(), -srcRect.y()); 61 buffer->canvas()->translate(-srcRect.x(), -srcRect.y());
78 62
79 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr); 63 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr);
80 m_bitmap = buffer->newImageSnapshot(); 64 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration);
81 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
82 } 65 }
83 66
84 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) 67 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
85 : m_imageElement(nullptr)
86 , m_cropRect(cropRect)
87 , m_bitmapOffset(IntPoint())
88 { 68 {
89 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()) );
90 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
91 ASSERT(canvas->isPaintable()); 69 ASSERT(canvas->isPaintable());
92 m_bitmap = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration), cr opRect); 70 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima geForCurrentFrame(), cropRect);
93 } 71 }
94 72
95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) 73 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
96 : m_imageElement(nullptr)
97 , m_cropRect(cropRect)
98 , m_bitmapOffset(IntPoint())
99 { 74 {
100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 75 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
76 // TODO(xidachen): buffer should be with size of srcRect->size(), not data-> size().
101 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels); 77 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels);
102 if (!buffer) 78 if (!buffer)
103 return; 79 return;
104 80
105 if (srcRect.width() > 0 && srcRect.height() > 0) 81 if (srcRect.width() > 0 && srcRect.height() > 0)
106 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), s rcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y()))); 82 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), s rcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
107 83
108 m_bitmap = buffer->newImageSnapshot(); 84 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration);
109 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
110 } 85 }
111 86
112 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) 87 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
113 : m_imageElement(bitmap->imageElement())
114 , m_bitmap(nullptr)
115 , m_cropRect(cropRect)
116 , m_bitmapOffset(IntPoint())
117 { 88 {
118 IntRect oldBitmapRect = bitmap->bitmapRect(); 89 m_image = cropImage(bitmap->skImage(), cropRect);
119 IntRect srcRect = intersection(cropRect, oldBitmapRect);
120 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x() ), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
121
122 if (m_imageElement) {
123 m_imageElement->addClient(this);
124 m_bitmapOffset = srcRect.location();
125 } else if (bitmap->bitmapImage()) {
126 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR ect.y() - oldBitmapRect.y()), cropRect.size());
127 m_bitmap = cropImage(bitmap->bitmapImage(), adjustedCropRect);
128 }
129 } 90 }
130 91
131 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) 92 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
132 : m_imageElement(nullptr)
133 , m_cropRect(cropRect)
134 { 93 {
135 IntRect srcRect = intersection(cropRect, image->rect()); 94 m_image = cropImage(image->imageForCurrentFrame(), cropRect);
136 m_bitmap = cropImage(image, cropRect);
137 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
138 } 95 }
139 96
140 ImageBitmap::~ImageBitmap() 97 ImageBitmap::~ImageBitmap()
141 { 98 {
142 #if !ENABLE(OILPAN)
143 if (m_imageElement)
144 m_imageElement->removeClient(this);
145 #endif
146 } 99 }
147 100
148 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect) 101 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
149 { 102 {
150 IntRect normalizedCropRect = normalizeRect(cropRect); 103 IntRect normalizedCropRect = normalizeRect(cropRect);
151 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 104 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
152 } 105 }
153 106
154 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect) 107 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
155 { 108 {
(...skipping 20 matching lines...) Expand all
176 } 129 }
177 130
178 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect) 131 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect)
179 { 132 {
180 IntRect normalizedCropRect = normalizeRect(cropRect); 133 IntRect normalizedCropRect = normalizeRect(cropRect);
181 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 134 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
182 } 135 }
183 136
184 void ImageBitmap::notifyImageSourceChanged() 137 void ImageBitmap::notifyImageSourceChanged()
185 { 138 {
186 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
187 m_bitmapOffset = IntPoint();
188 m_imageElement = nullptr;
189 }
190
191 PassRefPtr<Image> ImageBitmap::bitmapImage() const
192 {
193 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect .height()) && (!m_imageElement || !m_bitmap));
194 if (m_imageElement)
195 return m_imageElement->cachedImage()->image();
196 return m_bitmap;
197 } 139 }
198 140
199 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const 141 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const
200 { 142 {
201 *status = NormalSourceImageStatus; 143 *status = NormalSourceImageStatus;
202 return bitmapImage(); 144 return StaticBitmapImage::create(m_image);
203 } 145 }
204 146
205 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const 147 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
206 { 148 {
207 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect); 149 FloatRect intersectRect = intersection(IntRect(0, 0, width(), height()), *sr cRect);
208 FloatRect newSrcRect = intersectRect; 150 FloatRect newSrcRect = intersectRect;
209 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location()); 151 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), size());
210 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), m_bitmapRect.size()); 152 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / width(),
211 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(), 153 dstRect->height() / srcRect->height() * intersectRect.height() / height( ));
212 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitma pRect.height());
213 newDstRect.moveBy(dstRect->location()); 154 newDstRect.moveBy(dstRect->location());
214 *srcRect = newSrcRect; 155 *srcRect = newSrcRect;
215 *dstRect = newDstRect; 156 *dstRect = newDstRect;
216 } 157 }
217 158
218 FloatSize ImageBitmap::elementSize() const 159 FloatSize ImageBitmap::elementSize() const
219 { 160 {
220 return FloatSize(width(), height()); 161 return FloatSize(width(), height());
221 } 162 }
222 163
223 DEFINE_TRACE(ImageBitmap) 164 DEFINE_TRACE(ImageBitmap)
224 { 165 {
225 visitor->trace(m_imageElement);
226 ImageLoaderClient::trace(visitor); 166 ImageLoaderClient::trace(visitor);
227 } 167 }
228 168
229 } // namespace blink 169 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698