1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ui/gfx/skia_util.h" 6 7 #include "third_party/skia/include/core/SkBitmap.h" 8 #include "third_party/skia/include/core/SkColorFilter.h" 9 #include "third_party/skia/include/core/SkColorPriv.h" 10 #include "third_party/skia/include/core/SkUnPreMultiply.h" 11 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" 12 #include "third_party/skia/include/effects/SkGradientShader.h" 13 #include "third_party/skia/include/effects/SkLayerDrawLooper.h" 14 #include "ui/gfx/image/image_skia_rep.h" 15 #include "ui/gfx/rect.h" 16 #include "ui/gfx/rect_f.h" 17 #include "ui/gfx/shadow_value.h" 18 #include "ui/gfx/transform.h" 19 20 namespace gfx { 21 22 SkRect RectToSkRect(const Rect& rect) { 23 SkRect r; 24 r.iset(rect.x(), rect.y(), rect.right(), rect.bottom()); 25 return r; 26 } 27 28 SkIRect RectToSkIRect(const Rect& rect) { 29 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); 30 } 31 32 Rect SkIRectToRect(const SkIRect& rect) { 33 return Rect(rect.x(), rect.y(), rect.width(), rect.height()); 34 } 35 36 SkRect RectFToSkRect(const RectF& rect) { 37 return SkRect::MakeXYWH(SkFloatToScalar(rect.x()), 38 SkFloatToScalar(rect.y()), 39 SkFloatToScalar(rect.width()), 40 SkFloatToScalar(rect.height())); 41 } 42 43 RectF SkRectToRectF(const SkRect& rect) { 44 return RectF(SkScalarToFloat(rect.x()), 45 SkScalarToFloat(rect.y()), 46 SkScalarToFloat(rect.width()), 47 SkScalarToFloat(rect.height())); 48 } 49 50 void TransformToFlattenedSkMatrix(const gfx::Transform& transform, 51 SkMatrix* flattened) { 52 // Convert from 4x4 to 3x3 by dropping the third row and column. 53 flattened->set(0, SkMScalarToScalar(transform.matrix().get(0, 0))); 54 flattened->set(1, SkMScalarToScalar(transform.matrix().get(0, 1))); 55 flattened->set(2, SkMScalarToScalar(transform.matrix().get(0, 3))); 56 flattened->set(3, SkMScalarToScalar(transform.matrix().get(1, 0))); 57 flattened->set(4, SkMScalarToScalar(transform.matrix().get(1, 1))); 58 flattened->set(5, SkMScalarToScalar(transform.matrix().get(1, 3))); 59 flattened->set(6, SkMScalarToScalar(transform.matrix().get(3, 0))); 60 flattened->set(7, SkMScalarToScalar(transform.matrix().get(3, 1))); 61 flattened->set(8, SkMScalarToScalar(transform.matrix().get(3, 3))); 62 } 63 64 skia::RefPtr<SkShader> CreateImageRepShader(const gfx::ImageSkiaRep& image_rep, 65 SkShader::TileMode tile_mode, 66 const SkMatrix& local_matrix) { 67 skia::RefPtr<SkShader> shader = skia::AdoptRef(SkShader::CreateBitmapShader( 68 image_rep.sk_bitmap(), tile_mode, tile_mode)); 69 SkScalar scale_x = local_matrix.getScaleX(); 70 SkScalar scale_y = local_matrix.getScaleY(); 71 SkScalar bitmap_scale = SkFloatToScalar(image_rep.scale()); 72 73 // Unscale matrix by |bitmap_scale| such that the bitmap is drawn at the 74 // correct density. 75 // Convert skew and translation to pixel coordinates. 76 // Thus, for |bitmap_scale| = 2: 77 // x scale = 2, x translation = 1 DIP, 78 // should be converted to 79 // x scale = 1, x translation = 2 pixels. 80 SkMatrix shader_scale = local_matrix; 81 shader_scale.preScale(bitmap_scale, bitmap_scale); 82 shader_scale.setScaleX(SkScalarDiv(scale_x, bitmap_scale)); 83 shader_scale.setScaleY(SkScalarDiv(scale_y, bitmap_scale)); 84 85 shader->setLocalMatrix(shader_scale); 86 return shader; 87 } 88 89 skia::RefPtr<SkShader> CreateGradientShader(int start_point, 90 int end_point, 91 SkColor start_color, 92 SkColor end_color) { 93 SkColor grad_colors[2] = { start_color, end_color}; 94 SkPoint grad_points[2]; 95 grad_points[0].iset(0, start_point); 96 grad_points[1].iset(0, end_point); 97 98 return skia::AdoptRef(SkGradientShader::CreateLinear( 99 grad_points, grad_colors, NULL, 2, SkShader::kRepeat_TileMode)); 100 } 101 102 skia::RefPtr<SkDrawLooper> CreateShadowDrawLooper( 103 const std::vector<ShadowValue>& shadows) { 104 if (shadows.empty()) 105 return skia::RefPtr<SkDrawLooper>(); 106 107 skia::RefPtr<SkLayerDrawLooper> looper = 108 skia::AdoptRef(new SkLayerDrawLooper); 109 110 looper->addLayer(); // top layer of the original. 111 112 SkLayerDrawLooper::LayerInfo layer_info; 113 layer_info.fPaintBits |= SkLayerDrawLooper::kMaskFilter_Bit; 114 layer_info.fPaintBits |= SkLayerDrawLooper::kColorFilter_Bit; 115 layer_info.fColorMode = SkXfermode::kSrc_Mode; 116 117 for (size_t i = 0; i < shadows.size(); ++i) { 118 const ShadowValue& shadow = shadows[i]; 119 120 layer_info.fOffset.set(SkIntToScalar(shadow.x()), 121 SkIntToScalar(shadow.y())); 122 123 // SkBlurMaskFilter's blur radius defines the range to extend the blur from 124 // original mask, which is half of blur amount as defined in ShadowValue. 125 skia::RefPtr<SkMaskFilter> blur_mask = skia::AdoptRef( 126 SkBlurMaskFilter::Create(SkDoubleToScalar(shadow.blur() / 2), 127 SkBlurMaskFilter::kNormal_BlurStyle, 128 SkBlurMaskFilter::kHighQuality_BlurFlag)); 129 skia::RefPtr<SkColorFilter> color_filter = skia::AdoptRef( 130 SkColorFilter::CreateModeFilter(shadow.color(), 131 SkXfermode::kSrcIn_Mode)); 132 133 SkPaint* paint = looper->addLayer(layer_info); 134 paint->setMaskFilter(blur_mask.get()); 135 paint->setColorFilter(color_filter.get()); 136 } 137 138 return looper; 139 } 140 141 bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { 142 void* addr1 = NULL; 143 void* addr2 = NULL; 144 size_t size1 = 0; 145 size_t size2 = 0; 146 147 bitmap1.lockPixels(); 148 addr1 = bitmap1.getAddr32(0, 0); 149 size1 = bitmap1.getSize(); 150 bitmap1.unlockPixels(); 151 152 bitmap2.lockPixels(); 153 addr2 = bitmap2.getAddr32(0, 0); 154 size2 = bitmap2.getSize(); 155 bitmap2.unlockPixels(); 156 157 return (size1 == size2) && (0 == memcmp(addr1, addr2, bitmap1.getSize())); 158 } 159 160 void ConvertSkiaToRGBA(const unsigned char* skia, 161 int pixel_width, 162 unsigned char* rgba) { 163 int total_length = pixel_width * 4; 164 for (int i = 0; i < total_length; i += 4) { 165 const uint32_t pixel_in = *reinterpret_cast<const uint32_t*>(&skia[i]); 166 167 // Pack the components here. 168 int alpha = SkGetPackedA32(pixel_in); 169 if (alpha != 0 && alpha != 255) { 170 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel_in); 171 rgba[i + 0] = SkColorGetR(unmultiplied); 172 rgba[i + 1] = SkColorGetG(unmultiplied); 173 rgba[i + 2] = SkColorGetB(unmultiplied); 174 rgba[i + 3] = alpha; 175 } else { 176 rgba[i + 0] = SkGetPackedR32(pixel_in); 177 rgba[i + 1] = SkGetPackedG32(pixel_in); 178 rgba[i + 2] = SkGetPackedB32(pixel_in); 179 rgba[i + 3] = alpha; 180 } 181 } 182 } 183 184 } // namespace gfx 185