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

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: Better method name 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));
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) 41 : m_cropRect(cropRect)
45 , m_bitmap(nullptr)
46 , m_cropRect(cropRect)
47 { 42 {
48 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image ->height())); 43 IntRect srcRect = intersection(cropRect, IntRect(0, 0, image->width(), image ->height()));
49 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); 44 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
50 m_bitmapOffset = srcRect.location(); 45 m_bitmapOffset = srcRect.location();
51 46
52 if (!srcRect.width() || !srcRect.height()) 47 if (!srcRect.width() || !srcRect.height())
53 m_imageElement = nullptr; 48 m_bitmap = nullptr;
54 else 49 else
55 m_imageElement->addClient(this); 50 m_bitmap = image->cachedImage()->image()->imageForCurrentFrame();
56 } 51 }
57 52
58 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) 53 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
59 : m_imageElement(nullptr) 54 : m_cropRect(cropRect)
60 , m_cropRect(cropRect)
61 , m_bitmapOffset(IntPoint()) 55 , m_bitmapOffset(IntPoint())
62 { 56 {
63 IntSize playerSize; 57 IntSize playerSize;
64 58
65 if (video->webMediaPlayer()) 59 if (video->webMediaPlayer())
66 playerSize = video->webMediaPlayer()->naturalSize(); 60 playerSize = video->webMediaPlayer()->naturalSize();
67 61
68 IntRect videoRect = IntRect(IntPoint(), playerSize); 62 IntRect videoRect = IntRect(IntPoint(), playerSize);
69 IntRect srcRect = intersection(cropRect, videoRect); 63 IntRect srcRect = intersection(cropRect, videoRect);
70 IntRect dstRect(IntPoint(), srcRect.size()); 64 IntRect dstRect(IntPoint(), srcRect.size());
71 65
72 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size()); 66 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size());
73 if (!buffer) 67 if (!buffer)
74 return; 68 return;
75 69
76 buffer->canvas()->clipRect(dstRect); 70 buffer->canvas()->clipRect(dstRect);
77 buffer->canvas()->translate(-srcRect.x(), -srcRect.y()); 71 buffer->canvas()->translate(-srcRect.x(), -srcRect.y());
78 72
79 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr); 73 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr);
80 m_bitmap = buffer->newImageSnapshot(); 74 m_bitmap = buffer->newSkImageSnapshot(PreferNoAcceleration);
81 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); 75 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
82 } 76 }
83 77
84 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) 78 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
85 : m_imageElement(nullptr) 79 : m_cropRect(cropRect)
86 , m_cropRect(cropRect)
87 , m_bitmapOffset(IntPoint()) 80 , m_bitmapOffset(IntPoint())
88 { 81 {
89 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvas->size()) ); 82 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()); 83 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
91 ASSERT(canvas->isPaintable()); 84 ASSERT(canvas->isPaintable());
92 m_bitmap = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration), cr opRect); 85 m_bitmap = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->im ageForCurrentFrame(), cropRect);
93 } 86 }
94 87
95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) 88 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
96 : m_imageElement(nullptr) 89 : m_cropRect(cropRect)
97 , m_cropRect(cropRect)
98 , m_bitmapOffset(IntPoint()) 90 , m_bitmapOffset(IntPoint())
99 { 91 {
100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 92 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
101 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels); 93 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels);
102 if (!buffer) 94 if (!buffer)
103 return; 95 return;
104 96
105 if (srcRect.width() > 0 && srcRect.height() > 0) 97 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()))); 98 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), s rcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
107 99
108 m_bitmap = buffer->newImageSnapshot(); 100 m_bitmap = buffer->newSkImageSnapshot(PreferNoAcceleration);
109 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); 101 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
110 } 102 }
111 103
112 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) 104 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
113 : m_imageElement(bitmap->imageElement()) 105 : m_cropRect(cropRect)
114 , m_bitmap(nullptr)
115 , m_cropRect(cropRect)
116 , m_bitmapOffset(IntPoint()) 106 , m_bitmapOffset(IntPoint())
117 { 107 {
118 IntRect oldBitmapRect = bitmap->bitmapRect(); 108 IntRect oldBitmapRect = bitmap->bitmapRect();
119 IntRect srcRect = intersection(cropRect, oldBitmapRect); 109 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()); 110 m_bitmapRect = IntRect(IntPoint(std::max(0, oldBitmapRect.x() - cropRect.x() ), std::max(0, oldBitmapRect.y() - cropRect.y())), srcRect.size());
121 111 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropRect. y() - oldBitmapRect.y()), cropRect.size());
122 if (m_imageElement) { 112 m_bitmap = cropImage(bitmap->bitmapData(), adjustedCropRect);
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 } 113 }
130 114
131 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) 115 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
132 : m_imageElement(nullptr) 116 : m_cropRect(cropRect)
133 , m_cropRect(cropRect)
134 { 117 {
135 IntRect srcRect = intersection(cropRect, image->rect()); 118 IntRect srcRect = intersection(cropRect, image->rect());
136 m_bitmap = cropImage(image, cropRect); 119 m_bitmap = cropImage(image->imageForCurrentFrame(), cropRect);
137 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size()); 120 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
138 } 121 }
139 122
140 ImageBitmap::~ImageBitmap() 123 ImageBitmap::~ImageBitmap()
141 { 124 {
142 #if !ENABLE(OILPAN)
143 if (m_imageElement)
144 m_imageElement->removeClient(this);
145 #endif
146 } 125 }
147 126
148 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect) 127 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
149 { 128 {
150 IntRect normalizedCropRect = normalizeRect(cropRect); 129 IntRect normalizedCropRect = normalizeRect(cropRect);
151 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 130 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
152 } 131 }
153 132
154 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect) 133 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
155 { 134 {
(...skipping 20 matching lines...) Expand all
176 } 155 }
177 156
178 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect) 157 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect)
179 { 158 {
180 IntRect normalizedCropRect = normalizeRect(cropRect); 159 IntRect normalizedCropRect = normalizeRect(cropRect);
181 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 160 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
182 } 161 }
183 162
184 void ImageBitmap::notifyImageSourceChanged() 163 void ImageBitmap::notifyImageSourceChanged()
185 { 164 {
186 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
187 m_bitmapOffset = IntPoint();
188 m_imageElement = nullptr;
189 } 165 }
190 166
191 PassRefPtr<Image> ImageBitmap::bitmapImage() const 167 PassRefPtr<SkImage> ImageBitmap::bitmapData() const
192 { 168 {
193 ASSERT((m_imageElement || m_bitmap || !m_bitmapRect.width() || !m_bitmapRect .height()) && (!m_imageElement || !m_bitmap)); 169 if (!m_bitmap || m_bitmapRect.width() < 0 || m_bitmapRect.height() < 0)
194 if (m_imageElement) 170 return nullptr;
195 return m_imageElement->cachedImage()->image();
196 return m_bitmap; 171 return m_bitmap;
197 } 172 }
198 173
199 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const 174 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const
200 { 175 {
201 *status = NormalSourceImageStatus; 176 *status = NormalSourceImageStatus;
202 return bitmapImage(); 177 return StaticBitmapImage::create(m_bitmap);
203 } 178 }
204 179
205 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const 180 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
206 { 181 {
207 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect); 182 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect);
208 FloatRect newSrcRect = intersectRect; 183 FloatRect newSrcRect = intersectRect;
209 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location()); 184 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
210 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), m_bitmapRect.size()); 185 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), m_bitmapRect.size());
211 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(), 186 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
212 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitma pRect.height()); 187 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitma pRect.height());
213 newDstRect.moveBy(dstRect->location()); 188 newDstRect.moveBy(dstRect->location());
214 *srcRect = newSrcRect; 189 *srcRect = newSrcRect;
215 *dstRect = newDstRect; 190 *dstRect = newDstRect;
216 } 191 }
217 192
218 FloatSize ImageBitmap::elementSize() const 193 FloatSize ImageBitmap::elementSize() const
219 { 194 {
220 return FloatSize(width(), height()); 195 return FloatSize(width(), height());
221 } 196 }
222 197
223 DEFINE_TRACE(ImageBitmap) 198 DEFINE_TRACE(ImageBitmap)
224 { 199 {
225 visitor->trace(m_imageElement);
226 ImageLoaderClient::trace(visitor); 200 ImageLoaderClient::trace(visitor);
227 } 201 }
228 202
229 } // namespace blink 203 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698