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

Side by Side Diff: Source/core/platform/DragImage.cpp

Issue 16715002: Refactor WebCore::DragImage. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 months 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 /* 1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "core/platform/DragImage.h" 27 #include "core/platform/DragImage.h"
28 28
29 #include "core/platform/graphics/BitmapImage.h"
30 #include "core/platform/graphics/Color.h"
31 #include "core/platform/graphics/FloatPoint.h"
32 #include "core/platform/graphics/FloatRect.h"
29 #include "core/platform/graphics/Font.h" 33 #include "core/platform/graphics/Font.h"
30 #include "core/platform/graphics/FontCache.h" 34 #include "core/platform/graphics/FontCache.h"
31 #include "core/platform/graphics/FontDescription.h" 35 #include "core/platform/graphics/FontDescription.h"
32 #include "core/platform/graphics/FontSelector.h" 36 #include "core/platform/graphics/FontMetrics.h"
33 #include "core/platform/graphics/GraphicsContext.h" 37 #include "core/platform/graphics/GraphicsContext.h"
38 #include "core/platform/graphics/Image.h"
34 #include "core/platform/graphics/ImageBuffer.h" 39 #include "core/platform/graphics/ImageBuffer.h"
40 #include "core/platform/graphics/IntPoint.h"
41 #include "core/platform/graphics/IntSize.h"
35 #include "core/platform/graphics/StringTruncator.h" 42 #include "core/platform/graphics/StringTruncator.h"
36 #include "core/platform/graphics/TextRun.h" 43 #include "core/platform/graphics/TextRun.h"
44 #include "core/platform/graphics/skia/NativeImageSkia.h"
45 #include "core/platform/graphics/transforms/AffineTransform.h"
46 #include "skia/ext/image_operations.h"
47 #include "third_party/skia/include/core/SkCanvas.h"
48 #include "third_party/skia/include/core/SkMatrix.h"
37 #include "weborigin/KURL.h" 49 #include "weborigin/KURL.h"
50 #include "wtf/PassOwnPtr.h"
51 #include "wtf/RefPtr.h"
52 #include "wtf/text/WTFString.h"
53
54 #include <algorithm>
38 55
39 namespace WebCore { 56 namespace WebCore {
40 57
41 const float kDragLabelBorderX = 4; 58 const float kDragLabelBorderX = 4;
42 // Keep border_y in synch with DragController::LinkDragBorderInset. 59 // Keep border_y in synch with DragController::LinkDragBorderInset.
43 const float kDragLabelBorderY = 2; 60 const float kDragLabelBorderY = 2;
44 const float kDragLabelRadius = 5; 61 const float kDragLabelRadius = 5;
45 const float kLabelBorderYOffset = 2; 62 const float kLabelBorderYOffset = 2;
46 63
47 const float kMinDragLabelWidthBeforeClip = 120; 64 const float kMinDragLabelWidthBeforeClip = 120;
48 const float kMaxDragLabelWidth = 300; 65 const float kMaxDragLabelWidth = 300;
49 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde rX); 66 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde rX);
50 67
51 const float kDragLinkLabelFontSize = 11; 68 const float kDragLinkLabelFontSize = 11;
52 const float kDragLinkUrlFontSize = 10; 69 const float kDragLinkUrlFontSize = 10;
53 70
71 PassOwnPtr<DragImage> DragImage::create(Image* image, RespectImageOrientationEnu m shouldRespectImageOrientation)
72 {
73 if (!image)
74 return PassOwnPtr<DragImage>();
jamesr 2013/06/17 17:51:00 nullptr
jbroman 2013/06/17 18:52:10 Done.
75
76 RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame();
77 if (!bitmap)
78 return PassOwnPtr<DragImage>();
79
80 if (image->isBitmapImage()) {
81 ImageOrientation orientation = DefaultImageOrientation;
82 BitmapImage* bitmapImage = static_cast<BitmapImage*>(image);
83 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientati on();
84
85 if (shouldRespectImageOrientation == RespectImageOrientation)
86 orientation = bitmapImage->currentFrameOrientation();
87
88 if (orientation != DefaultImageOrientation) {
89 FloatRect destRect(FloatPoint(), sizeRespectingOrientation);
90 if (orientation.usesWidthAsHeight())
91 destRect = destRect.transposedRect();
92
93 SkBitmap skBitmap;
94 skBitmap.setConfig(
95 SkBitmap::kARGB_8888_Config, sizeRespectingOrientation.width(), sizeRespectingOrientation.height());
96 if (!skBitmap.allocPixels())
97 return PassOwnPtr<DragImage>();
98
99 SkCanvas canvas(skBitmap);
100 canvas.concat(orientation.transformFromDefault(sizeRespectingOrienta tion));
101 canvas.drawBitmapRect(bitmap->bitmap(), 0, destRect);
102
103 return adoptPtr(new DragImage(skBitmap, bitmap->resolutionScale()));
104 }
105 }
106
107 SkBitmap skBitmap;
108 if (!bitmap->bitmap().copyTo(&skBitmap, SkBitmap::kARGB_8888_Config))
109 return PassOwnPtr<DragImage>();
110 return adoptPtr(new DragImage(skBitmap, bitmap->resolutionScale()));
111 }
112
54 static Font dragLabelFont(int size, FontWeight fontWeight, FontRenderingMode ren deringMode) 113 static Font dragLabelFont(int size, FontWeight fontWeight, FontRenderingMode ren deringMode)
55 { 114 {
56 FontDescription description; 115 FontDescription description;
57 description.setWeight(fontWeight); 116 description.setWeight(fontWeight);
58
59 description.setSpecifiedSize(size); 117 description.setSpecifiedSize(size);
60 description.setComputedSize(size); 118 description.setComputedSize(size);
61 description.setRenderingMode(renderingMode); 119 description.setRenderingMode(renderingMode);
62 Font result(description, 0, 0); 120 Font result(description, 0, 0);
63 result.update(0); 121 result.update(0);
64 return result; 122 return result;
65 } 123 }
66 124
67 DragImageRef fitDragImageToMaxSize(DragImageRef image, const IntSize& srcSize, c onst IntSize& size) 125 PassOwnPtr<DragImage> DragImage::create(const KURL& url, const String& inLabel, FontRenderingMode renderingMode, float deviceScaleFactor)
68 {
69 float heightResizeRatio = 0.0f;
70 float widthResizeRatio = 0.0f;
71 float resizeRatio = -1.0f;
72 IntSize originalSize = dragImageSize(image);
73
74 if (srcSize.width() > size.width()) {
75 widthResizeRatio = size.width() / (float)srcSize.width();
76 resizeRatio = widthResizeRatio;
77 }
78
79 if (srcSize.height() > size.height()) {
80 heightResizeRatio = size.height() / (float)srcSize.height();
81 if ((resizeRatio < 0.0f) || (resizeRatio > heightResizeRatio))
82 resizeRatio = heightResizeRatio;
83 }
84
85 if (srcSize == originalSize)
86 return resizeRatio > 0.0f ? scaleDragImage(image, FloatSize(resizeRatio, resizeRatio)) : image;
87
88 // The image was scaled in the webpage so at minimum we must account for tha t scaling
89 float scalex = srcSize.width() / (float)originalSize.width();
90 float scaley = srcSize.height() / (float)originalSize.height();
91 if (resizeRatio > 0.0f) {
92 scalex *= resizeRatio;
93 scaley *= resizeRatio;
94 }
95
96 return scaleDragImage(image, FloatSize(scalex, scaley));
97 }
98
99 DragImageRef createDragImageForSelection(DragImageRef image, float dragImageAlph a)
100 {
101 if (image)
102 image = dissolveDragImageToFraction(image, dragImageAlpha);
103 return image;
104 }
105
106 DragImageRef createDragImageForLink(const KURL& url, const String& inLabel, Font RenderingMode renderingMode, float deviceScaleFactor)
107 { 126 {
108 const Font labelFont = dragLabelFont(kDragLinkLabelFontSize, FontWeightBold, renderingMode); 127 const Font labelFont = dragLabelFont(kDragLinkLabelFontSize, FontWeightBold, renderingMode);
109 const Font urlFont = dragLabelFont(kDragLinkUrlFontSize, FontWeightNormal, r enderingMode); 128 const Font urlFont = dragLabelFont(kDragLinkUrlFontSize, FontWeightNormal, r enderingMode);
110 FontCachePurgePreventer fontCachePurgePreventer; 129 FontCachePurgePreventer fontCachePurgePreventer;
111 130
112 bool drawURLString = true; 131 bool drawURLString = true;
113 bool clipURLString = false; 132 bool clipURLString = false;
114 bool clipLabelString = false; 133 bool clipLabelString = false;
115 134
116 String urlString = url.string(); 135 String urlString = url.string();
(...skipping 26 matching lines...) Expand all
143 } else 162 } else
144 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width() ) + kDragLabelBorderX * 2); 163 imageSize.setWidth(std::max(labelSize.width(), urlStringSize.width() ) + kDragLabelBorderX * 2);
145 } 164 }
146 165
147 // We now know how big the image needs to be, so we create and 166 // We now know how big the image needs to be, so we create and
148 // fill the background 167 // fill the background
149 IntSize scaledImageSize = imageSize; 168 IntSize scaledImageSize = imageSize;
150 scaledImageSize.scale(deviceScaleFactor); 169 scaledImageSize.scale(deviceScaleFactor);
151 OwnPtr<ImageBuffer> buffer(ImageBuffer::create(scaledImageSize, deviceScaleF actor)); 170 OwnPtr<ImageBuffer> buffer(ImageBuffer::create(scaledImageSize, deviceScaleF actor));
152 if (!buffer) 171 if (!buffer)
153 return 0; 172 return PassOwnPtr<DragImage>();
154 173
155 const float DragLabelRadius = 5; 174 const float DragLabelRadius = 5;
156 const IntSize radii(DragLabelRadius, DragLabelRadius); 175 const IntSize radii(DragLabelRadius, DragLabelRadius);
157 IntRect rect(IntPoint(), imageSize); 176 IntRect rect(IntPoint(), imageSize);
158 const Color backgroundColor(140, 140, 140); 177 const Color backgroundColor(140, 140, 140);
159 buffer->context()->fillRoundedRect(rect, radii, radii, radii, radii, backgro undColor); 178 buffer->context()->fillRoundedRect(rect, radii, radii, radii, radii, backgro undColor);
160 179
161 // Draw the text 180 // Draw the text
162 if (drawURLString) { 181 if (drawURLString) {
163 if (clipURLString) 182 if (clipURLString)
164 urlString = StringTruncator::centerTruncate(urlString, imageSize.wid th() - (kDragLabelBorderX * 2.0f), urlFont, StringTruncator::EnableRoundingHacks ); 183 urlString = StringTruncator::centerTruncate(urlString, imageSize.wid th() - (kDragLabelBorderX * 2.0f), urlFont, StringTruncator::EnableRoundingHacks );
165 IntPoint textPos(kDragLabelBorderX, imageSize.height() - (kLabelBorderYO ffset + urlFont.fontMetrics().descent())); 184 IntPoint textPos(kDragLabelBorderX, imageSize.height() - (kLabelBorderYO ffset + urlFont.fontMetrics().descent()));
166 TextRun textRun(urlString); 185 TextRun textRun(urlString);
167 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos) ; 186 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos) ;
168 } 187 }
169 188
170 if (clipLabelString) 189 if (clipLabelString)
171 label = StringTruncator::rightTruncate(label, imageSize.width() - (kDrag LabelBorderX * 2.0f), labelFont, StringTruncator::EnableRoundingHacks); 190 label = StringTruncator::rightTruncate(label, imageSize.width() - (kDrag LabelBorderX * 2.0f), labelFont, StringTruncator::EnableRoundingHacks);
172 191
173 IntPoint textPos(kDragLabelBorderX, kDragLabelBorderY + labelFont.pixelSize( )); 192 IntPoint textPos(kDragLabelBorderX, kDragLabelBorderY + labelFont.pixelSize( ));
174 TextRun textRun(label); 193 TextRun textRun(label);
175 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos); 194 buffer->context()->drawText(urlFont, TextRunPaintInfo(textRun), textPos);
176 195
177 RefPtr<Image> image = buffer->copyImage(); 196 RefPtr<Image> image = buffer->copyImage();
178 return createDragImageFromImage(image.get()); 197 return DragImage::create(image.get());
198 }
199
200 DragImage::DragImage(const SkBitmap& bitmap, float resolutionScale)
201 : m_bitmap(bitmap)
202 , m_resolutionScale(resolutionScale)
203 {
204 }
205
206 DragImage::~DragImage()
207 {
208 }
209
210 void DragImage::fitToMaxSize(const IntSize& srcSize, const IntSize& maxSize)
211 {
212 float heightResizeRatio = 0.0f;
213 float widthResizeRatio = 0.0f;
214 float resizeRatio = -1.0f;
215 IntSize originalSize = size();
216
217 if (srcSize.width() > maxSize.width()) {
218 widthResizeRatio = maxSize.width() / (float)srcSize.width();
jamesr 2013/06/17 17:51:00 we pretty much never use c-style cases in Chromium
jbroman 2013/06/17 18:52:10 Agreed. This code is only this way because I modif
219 resizeRatio = widthResizeRatio;
220 }
221
222 if (srcSize.height() > maxSize.height()) {
223 heightResizeRatio = maxSize.height() / (float)srcSize.height();
224 if ((resizeRatio < 0.0f) || (resizeRatio > heightResizeRatio))
225 resizeRatio = heightResizeRatio;
226 }
227
228 if (srcSize == originalSize) {
229 if (resizeRatio > 0.0f)
230 scale(resizeRatio, resizeRatio);
231 return;
232 }
233
234 // The image was scaled in the webpage so at minimum we must account for tha t scaling
235 float scaleX = srcSize.width() / (float)originalSize.width();
236 float scaleY = srcSize.height() / (float)originalSize.height();
237 if (resizeRatio > 0.0f) {
238 scaleX *= resizeRatio;
239 scaleY *= resizeRatio;
240 }
241
242 scale(scaleX, scaleY);
243 }
244
245 void DragImage::scale(float scaleX, float scaleY)
246 {
247 int imageWidth = scaleX * m_bitmap.width();
248 int imageHeight = scaleY * m_bitmap.height();
249 m_bitmap = skia::ImageOperations::Resize(
250 m_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, imageWidth, imageHeigh t);
251 }
252
253 void DragImage::dissolveToFraction(float fraction)
254 {
255 m_bitmap.setIsOpaque(false);
256 SkAutoLockPixels lock(m_bitmap);
257
258 for (int row = 0; row < m_bitmap.height(); ++row) {
259 for (int column = 0; column < m_bitmap.width(); ++column) {
260 uint32_t* pixel = m_bitmap.getAddr32(column, row);
261 *pixel = SkPreMultiplyARGB(
262 SkColorGetA(*pixel) * fraction,
263 SkColorGetR(*pixel),
264 SkColorGetG(*pixel),
265 SkColorGetB(*pixel));
266 }
267 }
179 } 268 }
180 269
181 } // namespace WebCore 270 } // namespace WebCore
182
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698