blob: a044aad0a0da343ca3eed7d301500d3c3c808da6 [file] [log] [blame]
reed@google.com6997ebb2012-07-30 19:50:31 +00001/*
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.com889b09e2012-07-27 21:10:42 +00008#include "SkImagePriv.h"
mike@reedtribe.org70e35902012-07-29 20:38:16 +00009#include "SkCanvas.h"
10#include "SkPicture.h"
reed@google.com889b09e2012-07-27 21:10:42 +000011
halcanary@google.com3d50ea12014-01-02 13:15:13 +000012SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType colorType) {
13 switch (colorType) {
reed@google.com2bd8b812013-11-01 13:46:54 +000014 case kAlpha_8_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +000015 return SkBitmap::kA8_Config;
reed@google.com889b09e2012-07-27 21:10:42 +000016
reed@google.com9230ea22013-12-09 22:01:03 +000017 case kARGB_4444_SkColorType:
18 return SkBitmap::kARGB_4444_Config;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000019
reed@google.com2bd8b812013-11-01 13:46:54 +000020 case kRGB_565_SkColorType:
reed@google.com889b09e2012-07-27 21:10:42 +000021 return SkBitmap::kRGB_565_Config;
skia.committer@gmail.comcf0803b2013-12-10 07:02:03 +000022
reed@google.com2bd8b812013-11-01 13:46:54 +000023 case kPMColor_SkColorType:
reed@google.com383a6972013-10-21 14:00:07 +000024 return SkBitmap::kARGB_8888_Config;
skia.committer@gmail.com572a8652013-09-21 07:01:53 +000025
reed@google.com9230ea22013-12-09 22:01:03 +000026 case kIndex_8_SkColorType:
reed@google.com23be7a52013-11-12 16:11:07 +000027 return SkBitmap::kIndex8_Config;
28
reed@google.comd28ba802013-09-20 19:33:52 +000029 default:
30 // break for unsupported colortypes
31 break;
reed@google.com889b09e2012-07-27 21:10:42 +000032 }
reed@google.com889b09e2012-07-27 21:10:42 +000033 return SkBitmap::kNo_Config;
34}
35
halcanary@google.com3d50ea12014-01-02 13:15:13 +000036SkBitmap::Config SkImageInfoToBitmapConfig(const SkImageInfo& info) {
37 return SkColorTypeToBitmapConfig(info.fColorType);
38}
39
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000040SkColorType 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.com3d50ea12014-01-02 13:15:13 +000051}
52
reed@google.com97af1a62012-08-28 12:19:02 +000053SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) {
reed@google.com2bd8b812013-11-01 13:46:54 +000054 SkImageInfo info;
reed@google.com9230ea22013-12-09 22:01:03 +000055 if (!bm.asImageInfo(&info)) {
reed@google.com889b09e2012-07-27 21:10:42 +000056 return NULL;
57 }
58
59 SkImage* image = NULL;
reed@google.com97af1a62012-08-28 12:19:02 +000060 if (canSharePixelRef || bm.isImmutable()) {
commit-bot@chromium.orge13af712014-01-13 20:39:14 +000061 image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes());
reed@google.com889b09e2012-07-27 21:10:42 +000062 } else {
63 bm.lockPixels();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000064 if (bm.getPixels()) {
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000065 image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes());
reed@google.com889b09e2012-07-27 21:10:42 +000066 }
67 bm.unlockPixels();
68 }
69 return image;
70}
71
mike@reedtribe.org70e35902012-07-29 20:38:16 +000072static 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
79void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture,
80 SkScalar x, SkScalar y, const SkPaint* paint) {
81 int saveCount = canvas->getSaveCount();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
mike@reedtribe.org70e35902012-07-29 20:38:16 +000083 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.com9ea5a3b2012-07-30 21:03:46 +000089 canvas->translate(x, y);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000090 } else if (x || y) {
91 canvas->save();
reed@google.com9ea5a3b2012-07-30 21:03:46 +000092 canvas->translate(x, y);
mike@reedtribe.org70e35902012-07-29 20:38:16 +000093 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000094
mike@reedtribe.org70e35902012-07-29 20:38:16 +000095 canvas->drawPicture(*picture);
96 canvas->restoreToCount(saveCount);
97}
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000098
99void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture,
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000100 const SkRect* src, const SkRect& dst, const SkPaint* paint) {
101 int saveCount = canvas->getSaveCount();
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000102
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.com7f1af502013-07-24 07:01:12 +0000124
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000125 canvas->drawPicture(*picture);
126 canvas->restoreToCount(saveCount);
127}