gfx/skia/trunk/src/image/SkImagePriv.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkImagePriv.h"
michael@0 9 #include "SkCanvas.h"
michael@0 10 #include "SkPicture.h"
michael@0 11
michael@0 12 SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType colorType) {
michael@0 13 switch (colorType) {
michael@0 14 case kAlpha_8_SkColorType:
michael@0 15 return SkBitmap::kA8_Config;
michael@0 16
michael@0 17 case kARGB_4444_SkColorType:
michael@0 18 return SkBitmap::kARGB_4444_Config;
michael@0 19
michael@0 20 case kRGB_565_SkColorType:
michael@0 21 return SkBitmap::kRGB_565_Config;
michael@0 22
michael@0 23 case kPMColor_SkColorType:
michael@0 24 return SkBitmap::kARGB_8888_Config;
michael@0 25
michael@0 26 case kIndex_8_SkColorType:
michael@0 27 return SkBitmap::kIndex8_Config;
michael@0 28
michael@0 29 default:
michael@0 30 // break for unsupported colortypes
michael@0 31 break;
michael@0 32 }
michael@0 33 return SkBitmap::kNo_Config;
michael@0 34 }
michael@0 35
michael@0 36 SkBitmap::Config SkImageInfoToBitmapConfig(const SkImageInfo& info) {
michael@0 37 return SkColorTypeToBitmapConfig(info.fColorType);
michael@0 38 }
michael@0 39
michael@0 40 SkColorType SkBitmapConfigToColorType(SkBitmap::Config config) {
michael@0 41 static const SkColorType gCT[] = {
michael@0 42 kUnknown_SkColorType, // kNo_Config
michael@0 43 kAlpha_8_SkColorType, // kA8_Config
michael@0 44 kIndex_8_SkColorType, // kIndex8_Config
michael@0 45 kRGB_565_SkColorType, // kRGB_565_Config
michael@0 46 kARGB_4444_SkColorType, // kARGB_4444_Config
michael@0 47 kPMColor_SkColorType, // kARGB_8888_Config
michael@0 48 };
michael@0 49 SkASSERT((unsigned)config < SK_ARRAY_COUNT(gCT));
michael@0 50 return gCT[config];
michael@0 51 }
michael@0 52
michael@0 53 SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) {
michael@0 54 SkImageInfo info;
michael@0 55 if (!bm.asImageInfo(&info)) {
michael@0 56 return NULL;
michael@0 57 }
michael@0 58
michael@0 59 SkImage* image = NULL;
michael@0 60 if (canSharePixelRef || bm.isImmutable()) {
michael@0 61 image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes());
michael@0 62 } else {
michael@0 63 bm.lockPixels();
michael@0 64 if (bm.getPixels()) {
michael@0 65 image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes());
michael@0 66 }
michael@0 67 bm.unlockPixels();
michael@0 68 }
michael@0 69 return image;
michael@0 70 }
michael@0 71
michael@0 72 static bool needs_layer(const SkPaint& paint) {
michael@0 73 return 0xFF != paint.getAlpha() ||
michael@0 74 paint.getColorFilter() ||
michael@0 75 paint.getImageFilter() ||
michael@0 76 SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode);
michael@0 77 }
michael@0 78
michael@0 79 void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture,
michael@0 80 SkScalar x, SkScalar y, const SkPaint* paint) {
michael@0 81 int saveCount = canvas->getSaveCount();
michael@0 82
michael@0 83 if (paint && needs_layer(*paint)) {
michael@0 84 SkRect bounds;
michael@0 85 bounds.set(x, y,
michael@0 86 x + SkIntToScalar(picture->width()),
michael@0 87 y + SkIntToScalar(picture->height()));
michael@0 88 canvas->saveLayer(&bounds, paint);
michael@0 89 canvas->translate(x, y);
michael@0 90 } else if (x || y) {
michael@0 91 canvas->save();
michael@0 92 canvas->translate(x, y);
michael@0 93 }
michael@0 94
michael@0 95 canvas->drawPicture(*picture);
michael@0 96 canvas->restoreToCount(saveCount);
michael@0 97 }
michael@0 98
michael@0 99 void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture,
michael@0 100 const SkRect* src, const SkRect& dst, const SkPaint* paint) {
michael@0 101 int saveCount = canvas->getSaveCount();
michael@0 102
michael@0 103 SkMatrix matrix;
michael@0 104 SkRect tmpSrc;
michael@0 105
michael@0 106 if (NULL != src) {
michael@0 107 tmpSrc = *src;
michael@0 108 } else {
michael@0 109 tmpSrc.set(0, 0,
michael@0 110 SkIntToScalar(picture->width()),
michael@0 111 SkIntToScalar(picture->height()));
michael@0 112 }
michael@0 113
michael@0 114 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
michael@0 115 if (paint && needs_layer(*paint)) {
michael@0 116 canvas->saveLayer(&dst, paint);
michael@0 117 } else {
michael@0 118 canvas->save();
michael@0 119 }
michael@0 120 canvas->concat(matrix);
michael@0 121 if (!paint || !needs_layer(*paint)) {
michael@0 122 canvas->clipRect(tmpSrc);
michael@0 123 }
michael@0 124
michael@0 125 canvas->drawPicture(*picture);
michael@0 126 canvas->restoreToCount(saveCount);
michael@0 127 }

mercurial