OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/base/cursor/cursor_util.h" | 5 #include "ui/base/cursor/cursor_util.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "skia/ext/image_operations.h" | 8 #include "skia/ext/image_operations.h" |
9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/gfx/geometry/point_conversions.h" | 10 #include "ui/gfx/geometry/point_conversions.h" |
11 #include "ui/gfx/geometry/size_conversions.h" | 11 #include "ui/gfx/geometry/size_conversions.h" |
12 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
13 #include "ui/gfx/skbitmap_operations.h" | 13 #include "ui/gfx/skbitmap_operations.h" |
14 #include "ui/gfx/skia_util.h" | 14 #include "ui/gfx/skia_util.h" |
15 | 15 |
16 namespace ui { | 16 namespace ui { |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 // Converts the SkBitmap to use a different alpha type. Returns true if bitmap |
| 21 // was modified, otherwise returns false. |
| 22 bool ConvertSkBitmapAlphaType(SkBitmap* bitmap, SkAlphaType alpha_type) { |
| 23 if (bitmap->info().alphaType() == alpha_type) { |
| 24 return false; |
| 25 } |
| 26 |
| 27 // Copy the bitmap into a temporary buffer. This will convert alpha type. |
| 28 SkImageInfo image_info = |
| 29 SkImageInfo::MakeN32(bitmap->width(), bitmap->height(), alpha_type); |
| 30 std::vector<char> buffer(bitmap->getSize()); |
| 31 bitmap->readPixels(image_info, &buffer[0], image_info.minRowBytes(), 0, 0); |
| 32 // Read the temporary buffer back into the original bitmap. |
| 33 bitmap->reset(); |
| 34 bitmap->allocPixels(image_info); |
| 35 memcpy(bitmap->getPixels(), &buffer[0], buffer.size()); |
| 36 |
| 37 return true; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
18 void ScaleAndRotateCursorBitmapAndHotpoint(float scale, | 42 void ScaleAndRotateCursorBitmapAndHotpoint(float scale, |
19 gfx::Display::Rotation rotation, | 43 gfx::Display::Rotation rotation, |
20 SkBitmap* bitmap, | 44 SkBitmap* bitmap, |
21 gfx::Point* hotpoint) { | 45 gfx::Point* hotpoint) { |
| 46 // SkBitmapOperations::Rotate() needs the bitmap to have premultiplied alpha, |
| 47 // so convert bitmap alpha type if we are going to rotate. |
| 48 bool was_converted = false; |
| 49 if (rotation != gfx::Display::ROTATE_0 && |
| 50 bitmap->info().alphaType() == kUnpremul_SkAlphaType) { |
| 51 ConvertSkBitmapAlphaType(bitmap, kPremul_SkAlphaType); |
| 52 was_converted = true; |
| 53 } |
| 54 |
22 switch (rotation) { | 55 switch (rotation) { |
23 case gfx::Display::ROTATE_0: | 56 case gfx::Display::ROTATE_0: |
24 break; | 57 break; |
25 case gfx::Display::ROTATE_90: | 58 case gfx::Display::ROTATE_90: |
26 hotpoint->SetPoint(bitmap->height() - hotpoint->y(), hotpoint->x()); | 59 hotpoint->SetPoint(bitmap->height() - hotpoint->y(), hotpoint->x()); |
27 *bitmap = SkBitmapOperations::Rotate( | 60 *bitmap = SkBitmapOperations::Rotate( |
28 *bitmap, SkBitmapOperations::ROTATION_90_CW); | 61 *bitmap, SkBitmapOperations::ROTATION_90_CW); |
29 break; | 62 break; |
30 case gfx::Display::ROTATE_180: | 63 case gfx::Display::ROTATE_180: |
31 hotpoint->SetPoint( | 64 hotpoint->SetPoint( |
32 bitmap->width() - hotpoint->x(), bitmap->height() - hotpoint->y()); | 65 bitmap->width() - hotpoint->x(), bitmap->height() - hotpoint->y()); |
33 *bitmap = SkBitmapOperations::Rotate( | 66 *bitmap = SkBitmapOperations::Rotate( |
34 *bitmap, SkBitmapOperations::ROTATION_180_CW); | 67 *bitmap, SkBitmapOperations::ROTATION_180_CW); |
35 break; | 68 break; |
36 case gfx::Display::ROTATE_270: | 69 case gfx::Display::ROTATE_270: |
37 hotpoint->SetPoint(hotpoint->y(), bitmap->width() - hotpoint->x()); | 70 hotpoint->SetPoint(hotpoint->y(), bitmap->width() - hotpoint->x()); |
38 *bitmap = SkBitmapOperations::Rotate( | 71 *bitmap = SkBitmapOperations::Rotate( |
39 *bitmap, SkBitmapOperations::ROTATION_270_CW); | 72 *bitmap, SkBitmapOperations::ROTATION_270_CW); |
40 break; | 73 break; |
41 } | 74 } |
42 | 75 |
| 76 if (was_converted) { |
| 77 ConvertSkBitmapAlphaType(bitmap, kUnpremul_SkAlphaType); |
| 78 } |
| 79 |
43 if (scale < FLT_EPSILON) { | 80 if (scale < FLT_EPSILON) { |
44 NOTREACHED() << "Scale must be larger than 0."; | 81 NOTREACHED() << "Scale must be larger than 0."; |
45 scale = 1.0f; | 82 scale = 1.0f; |
46 } | 83 } |
47 | 84 |
48 if (scale == 1.0f) | 85 if (scale == 1.0f) |
49 return; | 86 return; |
50 | 87 |
51 gfx::Size scaled_size = gfx::ScaleToFlooredSize( | 88 gfx::Size scaled_size = gfx::ScaleToFlooredSize( |
52 gfx::Size(bitmap->width(), bitmap->height()), scale); | 89 gfx::Size(bitmap->width(), bitmap->height()), scale); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( | 138 SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap( |
102 bitmap, x_offset, 0, frame_width, frame_height); | 139 bitmap, x_offset, 0, frame_width, frame_height); |
103 DCHECK_EQ(frame_width, cropped.width()); | 140 DCHECK_EQ(frame_width, cropped.width()); |
104 DCHECK_EQ(frame_height, cropped.height()); | 141 DCHECK_EQ(frame_height, cropped.height()); |
105 | 142 |
106 (*bitmaps)[frame] = cropped; | 143 (*bitmaps)[frame] = cropped; |
107 } | 144 } |
108 } | 145 } |
109 | 146 |
110 } // namespace ui | 147 } // namespace ui |
OLD | NEW |