[chromium] Drag image for image elements should be scaled with device scale factor.
[WebKit-https.git] / Source / WebCore / platform / chromium / DragImageChromiumSkia.cpp
1 /*
2  * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "DragImage.h"
33
34 #include "Image.h"
35 #include "NativeImageSkia.h"
36 #include "NotImplemented.h"
37 #include "SkBitmap.h"
38
39 #include "skia/ext/image_operations.h"
40
41 #include <wtf/RefPtr.h>
42
43 namespace WebCore {
44
45 IntSize dragImageSize(DragImageRef image)
46 {
47     if (!image)
48         return IntSize();
49
50     return IntSize(image->bitmap->width(), image->bitmap->height());
51 }
52
53 void deleteDragImage(DragImageRef image)
54 {
55     delete image->bitmap;
56     delete image;
57 }
58
59 DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
60 {
61     if (!image)
62         return 0;
63
64     int imageWidth = scale.width() * image->bitmap->width();
65     int imageHeight = scale.height() * image->bitmap->height();
66     SkBitmap* scaledImage = new SkBitmap(
67         skia::ImageOperations::Resize(*image->bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
68                                       imageWidth, imageHeight));
69     delete image->bitmap;
70     image->bitmap = scaledImage;
71     return image;
72 }
73
74 DragImageRef dissolveDragImageToFraction(DragImageRef image, float fraction)
75 {
76     if (!image)
77         return 0;
78
79     image->bitmap->setIsOpaque(false);
80     image->bitmap->lockPixels();
81
82     for (int row = 0; row < image->bitmap->height(); ++row) {
83         for (int column = 0; column < image->bitmap->width(); ++column) {
84             uint32_t* pixel = image->bitmap->getAddr32(column, row);
85             *pixel = SkPreMultiplyARGB(SkColorGetA(*pixel) * fraction,
86                                        SkColorGetR(*pixel),
87                                        SkColorGetG(*pixel),
88                                        SkColorGetB(*pixel));
89         }
90     }
91
92     image->bitmap->unlockPixels();
93
94     return image;
95 }
96
97 DragImageRef createDragImageFromImage(Image* image, RespectImageOrientationEnum)
98 {
99     if (!image)
100         return 0;
101
102     NativeImageSkia* bitmap = image->nativeImageForCurrentFrame();
103     if (!bitmap)
104         return 0;
105
106     SkBitmap* dragImage = new SkBitmap();
107     bitmap->bitmap().copyTo(dragImage, SkBitmap::kARGB_8888_Config);
108     DragImageChromium* dragImageChromium = new DragImageChromium;
109     dragImageChromium->bitmap = dragImage;
110     dragImageChromium->resolutionScale = bitmap->resolutionScale();
111     return dragImageChromium;
112 }
113
114 DragImageRef createDragImageIconForCachedImage(CachedImage*)
115 {
116     notImplemented();
117     return 0;
118 }
119
120 } // namespace WebCore