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

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: fix test case 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 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
29 { 29 {
30 ASSERT(image); 30 m_image = cropImage(image->cachedImage()->image()->imageForCurrentFrame(), c ropRect);
31
32 const SkIRect srcRect = intersection(image->rect(), cropRect);
33 if (srcRect.isEmpty())
34 return nullptr;
35
36 RefPtr<SkImage> skImage = image->imageForCurrentFrame();
37 if (!skImage)
38 return nullptr;
39
40 return StaticBitmapImage::create(adoptRef(skImage->newSubset(srcRect)));
41 }
42
43 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
44 : m_imageElement(image)
45 , m_bitmap(nullptr)
46 , m_cropRect(cropRect)
47 {
48 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());
50 m_bitmapOffset = srcRect.location();
51
52 if (!srcRect.width() || !srcRect.height())
53 m_imageElement = nullptr;
54 else
55 m_imageElement->addClient(this);
56 } 31 }
57 32
58 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect) 33 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
59 : m_imageElement(nullptr)
60 , m_cropRect(cropRect)
61 , m_bitmapOffset(IntPoint())
62 { 34 {
63 IntSize playerSize; 35 IntSize playerSize;
64
65 if (video->webMediaPlayer()) 36 if (video->webMediaPlayer())
66 playerSize = video->webMediaPlayer()->naturalSize(); 37 playerSize = video->webMediaPlayer()->naturalSize();
67 38
68 IntRect videoRect = IntRect(IntPoint(), playerSize); 39 IntRect videoRect = IntRect(IntPoint(), playerSize);
69 IntRect srcRect = intersection(cropRect, videoRect); 40 IntRect srcRect = intersection(cropRect, videoRect);
70 IntRect dstRect(IntPoint(), srcRect.size()); 41 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
71
72 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(videoRect.size());
73 if (!buffer) 42 if (!buffer)
74 return; 43 return;
75 44
76 buffer->canvas()->clipRect(dstRect); 45 IntPoint dstPoint = IntPoint(std::max(0, -cropRect.x()), std::max(0, -cropRe ct.y()));
77 buffer->canvas()->translate(-srcRect.x(), -srcRect.y()); 46 video->paintCurrentFrame(buffer->canvas(), IntRect(dstPoint, srcRect.size()) , nullptr);
Justin Novosad 2015/11/03 22:19:59 If the destination rectangle does not no cover the
xidachen 2015/11/04 12:59:27 Yes indeed. There is a fast/canvas/canvas-createIm
Justin Novosad 2015/11/04 15:12:37 Sounds like there is room for optimization. Pleas
78 47 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration);
79 video->paintCurrentFrame(buffer->canvas(), videoRect, nullptr);
80 m_bitmap = buffer->newImageSnapshot();
81 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
82 } 48 }
83 49
84 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect) 50 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
85 : m_imageElement(nullptr)
86 , m_cropRect(cropRect)
87 , m_bitmapOffset(IntPoint())
88 { 51 {
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()); 52 ASSERT(canvas->isPaintable());
92 m_bitmap = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration), cr opRect); 53 m_image = cropImage(canvas->copiedImage(BackBuffer, PreferAcceleration)->ima geForCurrentFrame(), cropRect);
93 } 54 }
94 55
95 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect) 56 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
96 : m_imageElement(nullptr)
97 , m_cropRect(cropRect)
98 , m_bitmapOffset(IntPoint())
99 { 57 {
100 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 58 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
101 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(data->size(), NonOpaque, Do NotInitializeImagePixels); 59 if (srcRect.isEmpty()) {
102 if (!buffer) 60 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpa que, DoNotInitializeImagePixels);
61 m_image = buffer->newSkImageSnapshot(PreferNoAcceleration);
103 return; 62 return;
104 63 }
105 if (srcRect.width() > 0 && srcRect.height() > 0) 64 m_image = cropImageHelper(cropRect, Unmultiplied, data->data()->data(), data ->size(), srcRect);
106 buffer->putByteArray(Unmultiplied, data->data()->data(), data->size(), s rcRect, IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRect.y())));
107
108 m_bitmap = buffer->newImageSnapshot();
109 m_bitmapRect = IntRect(IntPoint(std::max(0, -cropRect.x()), std::max(0, -cro pRect.y())), srcRect.size());
110 } 65 }
111 66
112 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect) 67 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 { 68 {
118 IntRect oldBitmapRect = bitmap->bitmapRect(); 69 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 } 70 }
130 71
131 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect) 72 ImageBitmap::ImageBitmap(Image* image, const IntRect& cropRect)
132 : m_imageElement(nullptr)
133 , m_cropRect(cropRect)
134 { 73 {
135 IntRect srcRect = intersection(cropRect, image->rect()); 74 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 } 75 }
139 76
140 ImageBitmap::~ImageBitmap() 77 ImageBitmap::~ImageBitmap()
141 { 78 {
142 #if !ENABLE(OILPAN)
143 if (m_imageElement)
144 m_imageElement->removeClient(this);
145 #endif
146 } 79 }
147 80
148 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect) 81 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRect& cropRect)
149 { 82 {
150 IntRect normalizedCropRect = normalizeRect(cropRect); 83 IntRect normalizedCropRect = normalizeRect(cropRect);
151 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 84 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
152 } 85 }
153 86
154 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect) 87 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRect& cropRect)
155 { 88 {
(...skipping 18 matching lines...) Expand all
174 IntRect normalizedCropRect = normalizeRect(cropRect); 107 IntRect normalizedCropRect = normalizeRect(cropRect);
175 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect)); 108 return adoptRefWillBeNoop(new ImageBitmap(bitmap, normalizedCropRect));
176 } 109 }
177 110
178 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect) 111 PassRefPtrWillBeRawPtr<ImageBitmap> ImageBitmap::create(Image* image, const IntR ect& cropRect)
179 { 112 {
180 IntRect normalizedCropRect = normalizeRect(cropRect); 113 IntRect normalizedCropRect = normalizeRect(cropRect);
181 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect)); 114 return adoptRefWillBeNoop(new ImageBitmap(image, normalizedCropRect));
182 } 115 }
183 116
117 PassRefPtr<SkImage> ImageBitmap::cropImageHelper(const IntRect& cropRect, Multip ly multiplied, const unsigned char* src, const IntSize& srcSize, const IntRect& srcRect)
118 {
119 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpaque, DoNotInitializeImagePixels);
120 if (!buffer)
121 return nullptr;
122
123 IntPoint dstPoint = IntPoint(std::min(0, -cropRect.x()), std::min(0, -cropRe ct.y()));
124 if (cropRect.x() < 0)
125 dstPoint.setX(-cropRect.x());
126 if (cropRect.y() < 0)
127 dstPoint.setY(-cropRect.y());
128 buffer->putByteArray(multiplied, src, srcSize, srcRect, dstPoint);
129 return buffer->newSkImageSnapshot(PreferNoAcceleration);
130 }
131
132 PassRefPtr<SkImage> ImageBitmap::cropImage(PassRefPtr<SkImage> image, const IntR ect& cropRect)
133 {
134 ASSERT(image);
135
136 IntRect imgRect = IntRect(IntPoint(), IntSize(image->width(), image->height( )));
137 const IntRect srcRect = intersection(imgRect, cropRect);
138
139 if (cropRect == srcRect)
140 return adoptRef(image->newSubset(srcRect));
141
142 if (srcRect.isEmpty()) {
143 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(cropRect.size(), NonOpa que, DoNotInitializeImagePixels);
144 return buffer->newSkImageSnapshot(PreferNoAcceleration);
145 }
146
147 SkImageInfo srcInfo = SkImageInfo::Make(image->width(), image->height(), kRG BA_8888_SkColorType, kPremul_SkAlphaType);
148 unsigned char* dstPixels = (unsigned char*)malloc(image->width() * image->he ight() * 4 * sizeof(unsigned char*));
Justin Novosad 2015/11/03 22:19:59 This buffer appears to be leaked. Also, it seems u
xidachen 2015/11/04 12:59:27 This is awesome. I have fixed the code accordingly
Justin Novosad 2015/11/04 15:12:37 Okay, but you have not re-uploaded the CL.
149 size_t dstRowBytes = image->width() * 4;
150 image->readPixels(srcInfo, dstPixels, dstRowBytes, 0, 0);
151 return cropImageHelper(cropRect, Premultiplied, dstPixels, IntSize(image->wi dth(), image->height()), srcRect);
152 }
153
184 void ImageBitmap::notifyImageSourceChanged() 154 void ImageBitmap::notifyImageSourceChanged()
185 { 155 {
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 } 156 }
198 157
199 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const 158 PassRefPtr<Image> ImageBitmap::getSourceImageForCanvas(SourceImageStatus* status , AccelerationHint) const
200 { 159 {
201 *status = NormalSourceImageStatus; 160 *status = NormalSourceImageStatus;
202 return bitmapImage(); 161 return StaticBitmapImage::create(m_image);
Justin Novosad 2015/11/03 22:20:00 Is this what we want to do when m_image is null?
xidachen 2015/11/04 12:59:27 Done.
203 } 162 }
204 163
205 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const 164 void ImageBitmap::adjustDrawRects(FloatRect* srcRect, FloatRect* dstRect) const
206 { 165 {
207 FloatRect intersectRect = intersection(m_bitmapRect, *srcRect); 166
208 FloatRect newSrcRect = intersectRect;
209 newSrcRect.move(m_bitmapOffset - m_bitmapRect.location());
210 FloatRect newDstRect(FloatPoint(intersectRect.location() - srcRect->location ()), m_bitmapRect.size());
211 newDstRect.scale(dstRect->width() / srcRect->width() * intersectRect.width() / m_bitmapRect.width(),
212 dstRect->height() / srcRect->height() * intersectRect.height() / m_bitma pRect.height());
213 newDstRect.moveBy(dstRect->location());
214 *srcRect = newSrcRect;
215 *dstRect = newDstRect;
216 } 167 }
217 168
218 FloatSize ImageBitmap::elementSize() const 169 FloatSize ImageBitmap::elementSize() const
219 { 170 {
220 return FloatSize(width(), height()); 171 return FloatSize(width(), height());
221 } 172 }
222 173
223 DEFINE_TRACE(ImageBitmap) 174 DEFINE_TRACE(ImageBitmap)
224 { 175 {
225 visitor->trace(m_imageElement);
226 ImageLoaderClient::trace(visitor); 176 ImageLoaderClient::trace(visitor);
227 } 177 }
228 178
229 } // namespace blink 179 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/frame/ImageBitmap.h ('k') | third_party/WebKit/Source/core/frame/ImageBitmapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698