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

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

Issue 1290133002: SkImage-ify DragImage (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: expectations Created 5 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « Source/platform/DragImage.h ('k') | Source/platform/DragImageTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 30 matching lines...) Expand all
41 #include "platform/graphics/ImageBuffer.h" 41 #include "platform/graphics/ImageBuffer.h"
42 #include "platform/graphics/paint/DrawingRecorder.h" 42 #include "platform/graphics/paint/DrawingRecorder.h"
43 #include "platform/graphics/paint/SkPictureBuilder.h" 43 #include "platform/graphics/paint/SkPictureBuilder.h"
44 #include "platform/text/BidiTextRun.h" 44 #include "platform/text/BidiTextRun.h"
45 #include "platform/text/StringTruncator.h" 45 #include "platform/text/StringTruncator.h"
46 #include "platform/text/TextRun.h" 46 #include "platform/text/TextRun.h"
47 #include "platform/transforms/AffineTransform.h" 47 #include "platform/transforms/AffineTransform.h"
48 #include "platform/weborigin/KURL.h" 48 #include "platform/weborigin/KURL.h"
49 #include "skia/ext/image_operations.h" 49 #include "skia/ext/image_operations.h"
50 #include "third_party/skia/include/core/SkCanvas.h" 50 #include "third_party/skia/include/core/SkCanvas.h"
51 #include "third_party/skia/include/core/SkColor.h" 51 #include "third_party/skia/include/core/SkImage.h"
52 #include "third_party/skia/include/core/SkMatrix.h" 52 #include "third_party/skia/include/core/SkMatrix.h"
53 #include "third_party/skia/include/core/SkSurface.h"
53 #include "wtf/PassOwnPtr.h" 54 #include "wtf/PassOwnPtr.h"
54 #include "wtf/RefPtr.h" 55 #include "wtf/RefPtr.h"
55 #include "wtf/text/WTFString.h" 56 #include "wtf/text/WTFString.h"
56 57
57 #include <algorithm> 58 #include <algorithm>
58 59
59 namespace blink { 60 namespace blink {
60 61
62 namespace {
63
61 const float kDragLabelBorderX = 4; 64 const float kDragLabelBorderX = 4;
62 // Keep border_y in synch with DragController::LinkDragBorderInset. 65 // Keep border_y in synch with DragController::LinkDragBorderInset.
63 const float kDragLabelBorderY = 2; 66 const float kDragLabelBorderY = 2;
64 const float kLabelBorderYOffset = 2; 67 const float kLabelBorderYOffset = 2;
65 68
66 const float kMaxDragLabelWidth = 300; 69 const float kMaxDragLabelWidth = 300;
67 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde rX); 70 const float kMaxDragLabelStringWidth = (kMaxDragLabelWidth - 2 * kDragLabelBorde rX);
68 71
69 const float kDragLinkLabelFontSize = 11; 72 const float kDragLinkLabelFontSize = 11;
70 const float kDragLinkUrlFontSize = 10; 73 const float kDragLinkUrlFontSize = 10;
71 74
72 PassOwnPtr<DragImage> DragImage::create(Image* image, RespectImageOrientationEnu m shouldRespectImageOrientation, float deviceScaleFactor, InterpolationQuality i nterpolationQuality) 75 PassRefPtr<SkImage> adjustedImage(PassRefPtr<SkImage> image, const IntSize& size ,
76 const AffineTransform& transform, float opacity, InterpolationQuality interp olationQuality)
77 {
78 if (transform.isIdentity() && opacity == 1) {
79 // Nothing to adjust, just use the original.
80 ASSERT(image->width() == size.width());
81 ASSERT(image->height() == size.height());
82 return image;
83 }
84
85 RefPtr<SkSurface> surface = adoptRef(SkSurface::NewRasterN32Premul(size.widt h(), size.height()));
86 if (!surface)
87 return nullptr;
88
89 SkPaint paint;
90 ASSERT(opacity >= 0 && opacity <= 1);
91 paint.setAlpha(opacity * 255);
92 paint.setFilterQuality(interpolationQuality == InterpolationNone
93 ? kNone_SkFilterQuality : kHigh_SkFilterQuality);
94
95 SkCanvas* canvas = surface->getCanvas();
96 canvas->clear(SK_ColorTRANSPARENT);
97 canvas->concat(affineTransformToSkMatrix(transform));
98 canvas->drawImage(image.get(), 0, 0, &paint);
99
100 return adoptRef(surface->newImageSnapshot());
101 }
102
103 } // anonymous namespace
104
105 FloatSize DragImage::clampedImageScale(const Image& image, const IntSize& size,
106 const IntSize& maxSize)
107 {
108 // Non-uniform scaling for size mapping.
109 FloatSize imageScale(
110 static_cast<float>(size.width()) / image.width(),
111 static_cast<float>(size.height()) / image.height());
112
113 // Uniform scaling for clamping.
114 const float clampScaleX = size.width() > maxSize.width()
115 ? static_cast<float>(maxSize.width()) / size.width() : 1;
116 const float clampScaleY = size.height() > maxSize.height()
117 ? static_cast<float>(maxSize.height()) / size.height() : 1;
118 imageScale.scale(std::min(clampScaleX, clampScaleY));
119
120 return imageScale;
121 }
122
123 PassOwnPtr<DragImage> DragImage::create(Image* image,
124 RespectImageOrientationEnum shouldRespectImageOrientation, float deviceScale Factor,
125 InterpolationQuality interpolationQuality, float opacity, const FloatSize& i mageScale)
73 { 126 {
74 if (!image) 127 if (!image)
75 return nullptr; 128 return nullptr;
76 129
77 SkBitmap bitmap; 130 RefPtr<SkImage> skImage = image->imageForCurrentFrame();
78 if (!image->deprecatedBitmapForCurrentFrame(&bitmap)) 131 if (!skImage)
79 return nullptr; 132 return nullptr;
80 133
81 if (image->isBitmapImage()) { 134 IntSize size = image->size();
82 ImageOrientation orientation = DefaultImageOrientation; 135 size.scale(imageScale.width(), imageScale.height());
136 if (size.isEmpty())
137 return nullptr;
138
139 AffineTransform transform;
140 transform.scaleNonUniform(imageScale.width(), imageScale.height());
141
142 if (shouldRespectImageOrientation == RespectImageOrientation && image->isBit mapImage()) {
83 BitmapImage* bitmapImage = toBitmapImage(image); 143 BitmapImage* bitmapImage = toBitmapImage(image);
84 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientati on(); 144 ImageOrientation orientation = bitmapImage->currentFrameOrientation();
85
86 if (shouldRespectImageOrientation == RespectImageOrientation)
87 orientation = bitmapImage->currentFrameOrientation();
88 145
89 if (orientation != DefaultImageOrientation) { 146 if (orientation != DefaultImageOrientation) {
90 FloatRect destRect(FloatPoint(), sizeRespectingOrientation); 147 size = bitmapImage->sizeRespectingOrientation();
91 if (orientation.usesWidthAsHeight()) 148 if (orientation.usesWidthAsHeight())
92 destRect = destRect.transposedRect(); 149 size.scale(imageScale.height(), imageScale.width());
150 else
151 size.scale(imageScale.width(), imageScale.height());
93 152
94 SkBitmap skBitmap; 153 transform *= orientation.transformFromDefault(size);
95 if (!skBitmap.tryAllocN32Pixels(sizeRespectingOrientation.width(), s izeRespectingOrientation.height()))
96 return nullptr;
97
98 skBitmap.eraseColor(SK_ColorTRANSPARENT);
99 SkCanvas canvas(skBitmap);
100 canvas.concat(affineTransformToSkMatrix(orientation.transformFromDef ault(sizeRespectingOrientation)));
101 canvas.drawBitmapRect(bitmap, destRect, nullptr);
102
103 return adoptPtr(new DragImage(skBitmap, deviceScaleFactor, interpola tionQuality));
104 } 154 }
105 } 155 }
106 156
107 SkBitmap skBitmap; 157 SkBitmap bm;
108 if (!bitmap.copyTo(&skBitmap, kN32_SkColorType)) 158 RefPtr<SkImage> resizedImage =
159 adjustedImage(skImage.release(), size, transform, opacity, interpolation Quality);
160 if (!resizedImage || !resizedImage->asLegacyBitmap(&bm, SkImage::kRO_LegacyB itmapMode))
109 return nullptr; 161 return nullptr;
110 return adoptPtr(new DragImage(skBitmap, deviceScaleFactor, interpolationQual ity)); 162
163 return adoptPtr(new DragImage(bm, deviceScaleFactor, interpolationQuality));
111 } 164 }
112 165
113 static Font deriveDragLabelFont(int size, FontWeight fontWeight, const FontDescr iption& systemFont) 166 static Font deriveDragLabelFont(int size, FontWeight fontWeight, const FontDescr iption& systemFont)
114 { 167 {
115 FontDescription description = systemFont; 168 FontDescription description = systemFont;
116 description.setWeight(fontWeight); 169 description.setWeight(fontWeight);
117 description.setSpecifiedSize(size); 170 description.setSpecifiedSize(size);
118 description.setComputedSize(size); 171 description.setComputedSize(size);
119 Font result(description); 172 Font result(description);
120 result.update(nullptr); 173 result.update(nullptr);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 : m_bitmap(bitmap) 265 : m_bitmap(bitmap)
213 , m_resolutionScale(resolutionScale) 266 , m_resolutionScale(resolutionScale)
214 , m_interpolationQuality(interpolationQuality) 267 , m_interpolationQuality(interpolationQuality)
215 { 268 {
216 } 269 }
217 270
218 DragImage::~DragImage() 271 DragImage::~DragImage()
219 { 272 {
220 } 273 }
221 274
222 void DragImage::fitToMaxSize(const IntSize& srcSize, const IntSize& maxSize)
223 {
224 float heightResizeRatio = 0.0f;
225 float widthResizeRatio = 0.0f;
226 float resizeRatio = -1.0f;
227 IntSize originalSize = size();
228
229 if (srcSize.width() > maxSize.width()) {
230 widthResizeRatio = maxSize.width() / static_cast<float>(srcSize.width()) ;
231 resizeRatio = widthResizeRatio;
232 }
233
234 if (srcSize.height() > maxSize.height()) {
235 heightResizeRatio = maxSize.height() / static_cast<float>(srcSize.height ());
236 if ((resizeRatio < 0.0f) || (resizeRatio > heightResizeRatio))
237 resizeRatio = heightResizeRatio;
238 }
239
240 if (srcSize == originalSize) {
241 if (resizeRatio > 0.0f)
242 scale(resizeRatio, resizeRatio);
243 return;
244 }
245
246 // The image was scaled in the webpage so at minimum we must account for tha t scaling
247 float scaleX = srcSize.width() / static_cast<float>(originalSize.width());
248 float scaleY = srcSize.height() / static_cast<float>(originalSize.height());
249 if (resizeRatio > 0.0f) {
250 scaleX *= resizeRatio;
251 scaleY *= resizeRatio;
252 }
253
254 scale(scaleX, scaleY);
255 }
256
257 void DragImage::scale(float scaleX, float scaleY) 275 void DragImage::scale(float scaleX, float scaleY)
258 { 276 {
259 skia::ImageOperations::ResizeMethod resizeMethod = m_interpolationQuality == InterpolationNone ? skia::ImageOperations::RESIZE_BOX : skia::ImageOperations:: RESIZE_LANCZOS3; 277 skia::ImageOperations::ResizeMethod resizeMethod = m_interpolationQuality == InterpolationNone ? skia::ImageOperations::RESIZE_BOX : skia::ImageOperations:: RESIZE_LANCZOS3;
260 int imageWidth = scaleX * m_bitmap.width(); 278 int imageWidth = scaleX * m_bitmap.width();
261 int imageHeight = scaleY * m_bitmap.height(); 279 int imageHeight = scaleY * m_bitmap.height();
262 m_bitmap = skia::ImageOperations::Resize(m_bitmap, resizeMethod, imageWidth, imageHeight); 280 m_bitmap = skia::ImageOperations::Resize(m_bitmap, resizeMethod, imageWidth, imageHeight);
263 } 281 }
264 282
265 void DragImage::dissolveToFraction(float fraction)
266 {
267 m_bitmap.setAlphaType(kPremul_SkAlphaType);
268 SkAutoLockPixels lock(m_bitmap);
269
270 for (int row = 0; row < m_bitmap.height(); ++row) {
271 for (int column = 0; column < m_bitmap.width(); ++column) {
272 uint32_t* pixel = m_bitmap.getAddr32(column, row);
273 *pixel = SkPreMultiplyARGB(
274 SkColorGetA(*pixel) * fraction,
275 SkColorGetR(*pixel),
276 SkColorGetG(*pixel),
277 SkColorGetB(*pixel));
278 }
279 }
280 }
281
282 } // namespace blink 283 } // namespace blink
OLDNEW
« no previous file with comments | « Source/platform/DragImage.h ('k') | Source/platform/DragImageTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698