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 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 12 | SkBitmap::Config SkImageInfoToBitmapConfig(const SkImageInfo& info) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 13 | switch (info.fColorType) { |
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 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 36 | SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 37 | SkImageInfo info; |
reed@google.com | 9230ea2 | 2013-12-09 22:01:03 +0000 | [diff] [blame] | 38 | if (!bm.asImageInfo(&info)) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | SkImage* image = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 43 | if (canSharePixelRef || bm.isImmutable()) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 44 | image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes()); |
| 45 | } else { |
| 46 | bm.lockPixels(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 47 | if (bm.getPixels()) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 48 | image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes()); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 49 | } |
| 50 | bm.unlockPixels(); |
| 51 | } |
| 52 | return image; |
| 53 | } |
| 54 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 55 | static bool needs_layer(const SkPaint& paint) { |
| 56 | return 0xFF != paint.getAlpha() || |
| 57 | paint.getColorFilter() || |
| 58 | paint.getImageFilter() || |
| 59 | SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode); |
| 60 | } |
| 61 | |
| 62 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
| 63 | SkScalar x, SkScalar y, const SkPaint* paint) { |
| 64 | int saveCount = canvas->getSaveCount(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 65 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 66 | if (paint && needs_layer(*paint)) { |
| 67 | SkRect bounds; |
| 68 | bounds.set(x, y, |
| 69 | x + SkIntToScalar(picture->width()), |
| 70 | y + SkIntToScalar(picture->height())); |
| 71 | canvas->saveLayer(&bounds, paint); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 72 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 73 | } else if (x || y) { |
| 74 | canvas->save(); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 75 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 76 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 77 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 78 | canvas->drawPicture(*picture); |
| 79 | canvas->restoreToCount(saveCount); |
| 80 | } |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 81 | |
| 82 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 83 | const SkRect* src, const SkRect& dst, const SkPaint* paint) { |
| 84 | int saveCount = canvas->getSaveCount(); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 85 | |
| 86 | SkMatrix matrix; |
| 87 | SkRect tmpSrc; |
| 88 | |
| 89 | if (NULL != src) { |
| 90 | tmpSrc = *src; |
| 91 | } else { |
| 92 | tmpSrc.set(0, 0, |
| 93 | SkIntToScalar(picture->width()), |
| 94 | SkIntToScalar(picture->height())); |
| 95 | } |
| 96 | |
| 97 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 98 | if (paint && needs_layer(*paint)) { |
| 99 | canvas->saveLayer(&dst, paint); |
| 100 | } else { |
| 101 | canvas->save(); |
| 102 | } |
| 103 | canvas->concat(matrix); |
| 104 | if (!paint || !needs_layer(*paint)) { |
| 105 | canvas->clipRect(tmpSrc); |
| 106 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 108 | canvas->drawPicture(*picture); |
| 109 | canvas->restoreToCount(saveCount); |
| 110 | } |