reed@google.com | 6997ebb | 2012-07-30 19:50:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 8 | #include "SkImagePriv.h" |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkPicture.h" |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 11 | |
| 12 | SkBitmap::Config SkImageInfoToBitmapConfig(const SkImage::Info& info, |
| 13 | bool* isOpaque) { |
| 14 | switch (info.fColorType) { |
| 15 | case SkImage::kAlpha_8_ColorType: |
| 16 | switch (info.fAlphaType) { |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 17 | case kIgnore_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 18 | // makes no sense |
| 19 | return SkBitmap::kNo_Config; |
| 20 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 21 | case kOpaque_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 22 | *isOpaque = true; |
| 23 | return SkBitmap::kA8_Config; |
| 24 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 25 | case kPremul_SkAlphaType: |
| 26 | case kUnpremul_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 27 | *isOpaque = false; |
| 28 | return SkBitmap::kA8_Config; |
| 29 | } |
| 30 | break; |
| 31 | |
| 32 | case SkImage::kRGB_565_ColorType: |
| 33 | // we ignore fAlpahType, though some would not make sense |
| 34 | *isOpaque = true; |
| 35 | return SkBitmap::kRGB_565_Config; |
| 36 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 37 | case SkImage::kPMColor_ColorType: |
| 38 | switch (info.fAlphaType) { |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 39 | case kIgnore_SkAlphaType: |
| 40 | case kUnpremul_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 41 | // not supported yet |
| 42 | return SkBitmap::kNo_Config; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 43 | case kOpaque_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 44 | *isOpaque = true; |
| 45 | return SkBitmap::kARGB_8888_Config; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 46 | case kPremul_SkAlphaType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 47 | *isOpaque = false; |
| 48 | return SkBitmap::kARGB_8888_Config; |
| 49 | } |
| 50 | break; |
skia.committer@gmail.com | 572a865 | 2013-09-21 07:01:53 +0000 | [diff] [blame^] | 51 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 52 | default: |
| 53 | // break for unsupported colortypes |
| 54 | break; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 55 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 56 | return SkBitmap::kNo_Config; |
| 57 | } |
| 58 | |
| 59 | int SkImageBytesPerPixel(SkImage::ColorType ct) { |
| 60 | static const uint8_t gColorTypeBytesPerPixel[] = { |
| 61 | 1, // kAlpha_8_ColorType |
| 62 | 2, // kRGB_565_ColorType |
| 63 | 4, // kRGBA_8888_ColorType |
| 64 | 4, // kBGRA_8888_ColorType |
| 65 | 4, // kPMColor_ColorType |
| 66 | }; |
| 67 | |
| 68 | SkASSERT((size_t)ct < SK_ARRAY_COUNT(gColorTypeBytesPerPixel)); |
| 69 | return gColorTypeBytesPerPixel[ct]; |
| 70 | } |
| 71 | |
| 72 | bool SkBitmapToImageInfo(const SkBitmap& bm, SkImage::Info* info) { |
| 73 | switch (bm.config()) { |
| 74 | case SkBitmap::kA8_Config: |
| 75 | info->fColorType = SkImage::kAlpha_8_ColorType; |
| 76 | break; |
| 77 | |
| 78 | case SkBitmap::kRGB_565_Config: |
| 79 | info->fColorType = SkImage::kRGB_565_ColorType; |
| 80 | break; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 81 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 82 | case SkBitmap::kARGB_8888_Config: |
| 83 | info->fColorType = SkImage::kPMColor_ColorType; |
| 84 | break; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 85 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 86 | default: |
| 87 | return false; |
| 88 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 89 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 90 | info->fWidth = bm.width(); |
| 91 | info->fHeight = bm.height(); |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 92 | info->fAlphaType = bm.isOpaque() ? kOpaque_SkAlphaType : |
| 93 | kPremul_SkAlphaType; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 97 | SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 98 | SkImage::Info info; |
| 99 | if (!SkBitmapToImageInfo(bm, &info)) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | SkImage* image = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 104 | if (canSharePixelRef || bm.isImmutable()) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 105 | image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes()); |
| 106 | } else { |
| 107 | bm.lockPixels(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 108 | if (bm.getPixels()) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 109 | image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes()); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 110 | } |
| 111 | bm.unlockPixels(); |
| 112 | } |
| 113 | return image; |
| 114 | } |
| 115 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 116 | static bool needs_layer(const SkPaint& paint) { |
| 117 | return 0xFF != paint.getAlpha() || |
| 118 | paint.getColorFilter() || |
| 119 | paint.getImageFilter() || |
| 120 | SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode); |
| 121 | } |
| 122 | |
| 123 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
| 124 | SkScalar x, SkScalar y, const SkPaint* paint) { |
| 125 | int saveCount = canvas->getSaveCount(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 126 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 127 | if (paint && needs_layer(*paint)) { |
| 128 | SkRect bounds; |
| 129 | bounds.set(x, y, |
| 130 | x + SkIntToScalar(picture->width()), |
| 131 | y + SkIntToScalar(picture->height())); |
| 132 | canvas->saveLayer(&bounds, paint); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 133 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 134 | } else if (x || y) { |
| 135 | canvas->save(); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 136 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 137 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 138 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 139 | canvas->drawPicture(*picture); |
| 140 | canvas->restoreToCount(saveCount); |
| 141 | } |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 142 | |
| 143 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 144 | const SkRect* src, const SkRect& dst, const SkPaint* paint) { |
| 145 | int saveCount = canvas->getSaveCount(); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 146 | |
| 147 | SkMatrix matrix; |
| 148 | SkRect tmpSrc; |
| 149 | |
| 150 | if (NULL != src) { |
| 151 | tmpSrc = *src; |
| 152 | } else { |
| 153 | tmpSrc.set(0, 0, |
| 154 | SkIntToScalar(picture->width()), |
| 155 | SkIntToScalar(picture->height())); |
| 156 | } |
| 157 | |
| 158 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 159 | if (paint && needs_layer(*paint)) { |
| 160 | canvas->saveLayer(&dst, paint); |
| 161 | } else { |
| 162 | canvas->save(); |
| 163 | } |
| 164 | canvas->concat(matrix); |
| 165 | if (!paint || !needs_layer(*paint)) { |
| 166 | canvas->clipRect(tmpSrc); |
| 167 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 168 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 169 | canvas->drawPicture(*picture); |
| 170 | canvas->restoreToCount(saveCount); |
| 171 | } |