gfx/skia/trunk/src/image/SkImage.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 "SkBitmap.h"
michael@0 9 #include "SkCanvas.h"
michael@0 10 #include "SkImagePriv.h"
michael@0 11 #include "SkImage_Base.h"
michael@0 12
michael@0 13 static SkImage_Base* as_IB(SkImage* image) {
michael@0 14 return static_cast<SkImage_Base*>(image);
michael@0 15 }
michael@0 16
michael@0 17 static const SkImage_Base* as_IB(const SkImage* image) {
michael@0 18 return static_cast<const SkImage_Base*>(image);
michael@0 19 }
michael@0 20
michael@0 21 uint32_t SkImage::NextUniqueID() {
michael@0 22 static int32_t gUniqueID;
michael@0 23
michael@0 24 // never return 0;
michael@0 25 uint32_t id;
michael@0 26 do {
michael@0 27 id = sk_atomic_inc(&gUniqueID) + 1;
michael@0 28 } while (0 == id);
michael@0 29 return id;
michael@0 30 }
michael@0 31
michael@0 32 void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y,
michael@0 33 const SkPaint* paint) {
michael@0 34 as_IB(this)->onDraw(canvas, x, y, paint);
michael@0 35 }
michael@0 36
michael@0 37 void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
michael@0 38 const SkPaint* paint) {
michael@0 39 as_IB(this)->onDrawRectToRect(canvas, src, dst, paint);
michael@0 40 }
michael@0 41
michael@0 42 const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const {
michael@0 43 SkImageInfo infoStorage;
michael@0 44 size_t rowBytesStorage;
michael@0 45 if (NULL == info) {
michael@0 46 info = &infoStorage;
michael@0 47 }
michael@0 48 if (NULL == rowBytes) {
michael@0 49 rowBytes = &rowBytesStorage;
michael@0 50 }
michael@0 51 return as_IB(this)->onPeekPixels(info, rowBytes);
michael@0 52 }
michael@0 53
michael@0 54 bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const {
michael@0 55 if (NULL == bitmap) {
michael@0 56 return false;
michael@0 57 }
michael@0 58
michael@0 59 SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
michael@0 60
michael@0 61 // trim against the bitmap, if its already been allocated
michael@0 62 if (bitmap->pixelRef()) {
michael@0 63 bounds.fRight = SkMin32(bounds.fRight, bitmap->width());
michael@0 64 bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height());
michael@0 65 if (bounds.isEmpty()) {
michael@0 66 return false;
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 if (subset && !bounds.intersect(*subset)) {
michael@0 71 // perhaps we could return true + empty-bitmap?
michael@0 72 return false;
michael@0 73 }
michael@0 74 return as_IB(this)->onReadPixels(bitmap, bounds);
michael@0 75 }
michael@0 76
michael@0 77 GrTexture* SkImage::getTexture() {
michael@0 78 return as_IB(this)->onGetTexture();
michael@0 79 }
michael@0 80
michael@0 81 SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const {
michael@0 82 SkBitmap bm;
michael@0 83 if (as_IB(this)->getROPixels(&bm)) {
michael@0 84 return SkImageEncoder::EncodeData(bm, type, quality);
michael@0 85 }
michael@0 86 return NULL;
michael@0 87 }
michael@0 88
michael@0 89 ///////////////////////////////////////////////////////////////////////////////
michael@0 90
michael@0 91 static bool raster_canvas_supports(const SkImageInfo& info) {
michael@0 92 switch (info.fColorType) {
michael@0 93 case kPMColor_SkColorType:
michael@0 94 return kUnpremul_SkAlphaType != info.fAlphaType;
michael@0 95 case kRGB_565_SkColorType:
michael@0 96 return true;
michael@0 97 case kAlpha_8_SkColorType:
michael@0 98 return true;
michael@0 99 default:
michael@0 100 break;
michael@0 101 }
michael@0 102 return false;
michael@0 103 }
michael@0 104
michael@0 105 bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const {
michael@0 106 SkImageInfo info;
michael@0 107
michael@0 108 if (bitmap->pixelRef()) {
michael@0 109 if (!bitmap->asImageInfo(&info)) {
michael@0 110 return false;
michael@0 111 }
michael@0 112 if (!raster_canvas_supports(info)) {
michael@0 113 return false;
michael@0 114 }
michael@0 115 } else {
michael@0 116 SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(),
michael@0 117 subset.height());
michael@0 118 SkBitmap tmp;
michael@0 119 if (!tmp.allocPixels(info)) {
michael@0 120 return false;
michael@0 121 }
michael@0 122 *bitmap = tmp;
michael@0 123 }
michael@0 124
michael@0 125 SkRect srcR, dstR;
michael@0 126 srcR.set(subset);
michael@0 127 dstR = srcR;
michael@0 128 dstR.offset(-dstR.left(), -dstR.top());
michael@0 129
michael@0 130 SkCanvas canvas(*bitmap);
michael@0 131
michael@0 132 SkPaint paint;
michael@0 133 paint.setXfermodeMode(SkXfermode::kClear_Mode);
michael@0 134 canvas.drawRect(dstR, paint);
michael@0 135
michael@0 136 const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL);
michael@0 137 return true;
michael@0 138 }

mercurial