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 | |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 12 | SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType colorType) { |
| 13 | switch (colorType) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 14 | case kAlpha_8_SkColorType: |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 15 | return SkBitmap::kA8_Config; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 16 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 17 | case kARGB_4444_SkColorType: |
| 18 | return SkBitmap::kARGB_4444_Config; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 19 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 20 | case kRGB_565_SkColorType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 21 | return SkBitmap::kRGB_565_Config; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 22 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 23 | case kPMColor_SkColorType: |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 24 | return SkBitmap::kARGB_8888_Config; |
skia.committer@gmail.com | 572a865 | 2013-09-21 07:01:53 +0000 | [diff] [blame] | 25 | |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 26 | case kIndex_8_SkColorType: |
reed@google.com | 23be7a5 | 2013-11-12 16:11:07 +0000 | [diff] [blame] | 27 | return SkBitmap::kIndex8_Config; |
| 28 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 29 | default: |
| 30 | // break for unsupported colortypes |
| 31 | break; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 32 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 33 | return SkBitmap::kNo_Config; |
| 34 | } |
| 35 | |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 36 | SkBitmap::Config SkImageInfoToBitmapConfig(const SkImageInfo& info) { |
| 37 | return SkColorTypeToBitmapConfig(info.fColorType); |
| 38 | } |
| 39 | |
commit-bot@chromium.org | 61e96cd | 2014-02-11 18:21:45 +0000 | [diff] [blame] | 40 | SkColorType SkBitmapConfigToColorType(SkBitmap::Config config) { |
| 41 | static const SkColorType gCT[] = { |
| 42 | kUnknown_SkColorType, // kNo_Config |
| 43 | kAlpha_8_SkColorType, // kA8_Config |
| 44 | kIndex_8_SkColorType, // kIndex8_Config |
| 45 | kRGB_565_SkColorType, // kRGB_565_Config |
| 46 | kARGB_4444_SkColorType, // kARGB_4444_Config |
| 47 | kPMColor_SkColorType, // kARGB_8888_Config |
| 48 | }; |
| 49 | SkASSERT((unsigned)config < SK_ARRAY_COUNT(gCT)); |
| 50 | return gCT[config]; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 53 | SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 54 | SkImageInfo info; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 55 | if (!bm.asImageInfo(&info)) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | SkImage* image = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 60 | if (canSharePixelRef || bm.isImmutable()) { |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 61 | image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes()); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 62 | } else { |
| 63 | bm.lockPixels(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 64 | if (bm.getPixels()) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 65 | image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes()); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 66 | } |
| 67 | bm.unlockPixels(); |
| 68 | } |
| 69 | return image; |
| 70 | } |
| 71 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 72 | static bool needs_layer(const SkPaint& paint) { |
| 73 | return 0xFF != paint.getAlpha() || |
| 74 | paint.getColorFilter() || |
| 75 | paint.getImageFilter() || |
| 76 | SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode); |
| 77 | } |
| 78 | |
| 79 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
| 80 | SkScalar x, SkScalar y, const SkPaint* paint) { |
| 81 | int saveCount = canvas->getSaveCount(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 82 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 83 | if (paint && needs_layer(*paint)) { |
| 84 | SkRect bounds; |
| 85 | bounds.set(x, y, |
| 86 | x + SkIntToScalar(picture->width()), |
| 87 | y + SkIntToScalar(picture->height())); |
| 88 | canvas->saveLayer(&bounds, paint); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 89 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 90 | } else if (x || y) { |
| 91 | canvas->save(); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 92 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 93 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 94 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 95 | canvas->drawPicture(*picture); |
| 96 | canvas->restoreToCount(saveCount); |
| 97 | } |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 98 | |
| 99 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 100 | const SkRect* src, const SkRect& dst, const SkPaint* paint) { |
| 101 | int saveCount = canvas->getSaveCount(); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 102 | |
| 103 | SkMatrix matrix; |
| 104 | SkRect tmpSrc; |
| 105 | |
| 106 | if (NULL != src) { |
| 107 | tmpSrc = *src; |
| 108 | } else { |
| 109 | tmpSrc.set(0, 0, |
| 110 | SkIntToScalar(picture->width()), |
| 111 | SkIntToScalar(picture->height())); |
| 112 | } |
| 113 | |
| 114 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 115 | if (paint && needs_layer(*paint)) { |
| 116 | canvas->saveLayer(&dst, paint); |
| 117 | } else { |
| 118 | canvas->save(); |
| 119 | } |
| 120 | canvas->concat(matrix); |
| 121 | if (!paint || !needs_layer(*paint)) { |
| 122 | canvas->clipRect(tmpSrc); |
| 123 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 124 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 125 | canvas->drawPicture(*picture); |
| 126 | canvas->restoreToCount(saveCount); |
| 127 | } |