OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapSource.h" | 8 #include "SkBitmapSource.h" |
| 9 #include "SkCanvas.h" |
9 #include "SkDevice.h" | 10 #include "SkDevice.h" |
10 #include "SkCanvas.h" | 11 #include "SkImagePriv.h" |
11 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkValidationUtils.h" |
12 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
13 #include "SkValidationUtils.h" | |
14 | 15 |
15 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) | 16 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) |
16 : INHERITED(0, 0) | 17 : INHERITED(0, 0) |
17 , fBitmap(bitmap) | 18 , fBitmap(bitmap) |
18 , fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()), | 19 , fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()), |
19 SkIntToScalar(bitmap.height()))) | 20 SkIntToScalar(bitmap.height()))) |
20 , fDstRect(fSrcRect) | 21 , fDstRect(fSrcRect) |
21 {} | 22 {} |
22 | 23 |
23 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, co
nst SkRect& dstRect) | 24 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, co
nst SkRect& dstRect) |
(...skipping 12 matching lines...) Expand all Loading... |
36 } | 37 } |
37 return SkBitmapSource::Create(bitmap, src, dst); | 38 return SkBitmapSource::Create(bitmap, src, dst); |
38 } | 39 } |
39 | 40 |
40 void SkBitmapSource::flatten(SkWriteBuffer& buffer) const { | 41 void SkBitmapSource::flatten(SkWriteBuffer& buffer) const { |
41 buffer.writeRect(fSrcRect); | 42 buffer.writeRect(fSrcRect); |
42 buffer.writeRect(fDstRect); | 43 buffer.writeRect(fDstRect); |
43 buffer.writeBitmap(fBitmap); | 44 buffer.writeBitmap(fBitmap); |
44 } | 45 } |
45 | 46 |
46 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const Context&
ctx, | 47 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkImage*, const Context&
ctx, |
47 SkBitmap* result, SkIPoint* offset) const { | 48 SkAutoTUnref<const SkImage>& result, SkIPoint
* offset) const { |
48 SkRect bounds, dstRect; | 49 SkRect bounds, dstRect; |
49 fBitmap.getBounds(&bounds); | 50 fBitmap.getBounds(&bounds); |
50 ctx.ctm().mapRect(&dstRect, fDstRect); | 51 ctx.ctm().mapRect(&dstRect, fDstRect); |
51 if (fSrcRect == bounds && dstRect == bounds) { | 52 if (fSrcRect == bounds && dstRect == bounds) { |
52 // No regions cropped out or resized; return entire bitmap. | 53 // No regions cropped out or resized; return entire bitmap. |
53 *result = fBitmap; | 54 SkImage* image = SkNewImageFromBitmap(fBitmap, NULL); |
| 55 if (NULL == image) { |
| 56 return false; |
| 57 } |
| 58 result.reset(image); |
54 offset->fX = offset->fY = 0; | 59 offset->fX = offset->fY = 0; |
55 return true; | 60 return true; |
56 } | 61 } |
57 | 62 |
58 const SkIRect dstIRect = dstRect.roundOut(); | 63 const SkIRect dstIRect = dstRect.roundOut(); |
59 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstI
Rect.height())); | 64 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstI
Rect.height())); |
60 if (NULL == device.get()) { | 65 if (NULL == device.get()) { |
61 return false; | 66 return false; |
62 } | 67 } |
63 | 68 |
64 SkCanvas canvas(device.get()); | 69 SkCanvas canvas(device.get()); |
65 SkPaint paint; | 70 SkPaint paint; |
66 | 71 |
67 // Subtract off the integer component of the translation (will be applied in
loc, below). | 72 // Subtract off the integer component of the translation (will be applied in
loc, below). |
68 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop)
); | 73 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop)
); |
69 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 74 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
70 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect ass
erts | 75 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect ass
erts |
71 // None filtering when it's translate-only | 76 // None filtering when it's translate-only |
72 paint.setFilterQuality( | 77 paint.setFilterQuality( |
73 fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.heig
ht() ? | 78 fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.heig
ht() ? |
74 kNone_SkFilterQuality : kHigh_SkFilterQuality); | 79 kNone_SkFilterQuality : kHigh_SkFilterQuality); |
75 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint); | 80 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint); |
76 | 81 |
77 *result = device.get()->accessBitmap(false); | 82 SkImage* image = device->newImageSnapshot(); |
| 83 if (NULL == image) { |
| 84 return false; |
| 85 } |
| 86 result.reset(image); |
| 87 |
78 offset->fX = dstIRect.fLeft; | 88 offset->fX = dstIRect.fLeft; |
79 offset->fY = dstIRect.fTop; | 89 offset->fY = dstIRect.fTop; |
80 return true; | 90 return true; |
81 } | 91 } |
82 | 92 |
83 void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const { | 93 void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const { |
84 *dst = fDstRect; | 94 *dst = fDstRect; |
85 } | 95 } |
86 | 96 |
87 #ifndef SK_IGNORE_TO_STRING | 97 #ifndef SK_IGNORE_TO_STRING |
88 void SkBitmapSource::toString(SkString* str) const { | 98 void SkBitmapSource::toString(SkString* str) const { |
89 str->appendf("SkBitmapSource: ("); | 99 str->appendf("SkBitmapSource: ("); |
90 str->appendf("src: (%f,%f,%f,%f) dst: (%f,%f,%f,%f) ", | 100 str->appendf("src: (%f,%f,%f,%f) dst: (%f,%f,%f,%f) ", |
91 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m, | 101 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m, |
92 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto
m); | 102 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto
m); |
93 str->appendf("bitmap: (%d,%d)", | 103 str->appendf("bitmap: (%d,%d)", |
94 fBitmap.width(), fBitmap.height()); | 104 fBitmap.width(), fBitmap.height()); |
95 str->append(")"); | 105 str->append(")"); |
96 } | 106 } |
97 #endif | 107 #endif |
OLD | NEW |