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