OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImagePriv.h" | 10 #include "SkImagePriv.h" |
11 #include "SkImage_Base.h" | 11 #include "SkImage_Base.h" |
| 12 #include "SkReadPixelsRec.h" |
12 #include "SkSurface.h" | 13 #include "SkSurface.h" |
13 | 14 |
14 uint32_t SkImage::NextUniqueID() { | 15 uint32_t SkImage::NextUniqueID() { |
15 static int32_t gUniqueID; | 16 static int32_t gUniqueID; |
16 | 17 |
17 // never return 0; | 18 // never return 0; |
18 uint32_t id; | 19 uint32_t id; |
19 do { | 20 do { |
20 id = sk_atomic_inc(&gUniqueID) + 1; | 21 id = sk_atomic_inc(&gUniqueID) + 1; |
21 } while (0 == id); | 22 } while (0 == id); |
(...skipping 14 matching lines...) Expand all Loading... |
36 size_t rowBytesStorage; | 37 size_t rowBytesStorage; |
37 if (NULL == info) { | 38 if (NULL == info) { |
38 info = &infoStorage; | 39 info = &infoStorage; |
39 } | 40 } |
40 if (NULL == rowBytes) { | 41 if (NULL == rowBytes) { |
41 rowBytes = &rowBytesStorage; | 42 rowBytes = &rowBytesStorage; |
42 } | 43 } |
43 return as_IB(this)->onPeekPixels(info, rowBytes); | 44 return as_IB(this)->onPeekPixels(info, rowBytes); |
44 } | 45 } |
45 | 46 |
46 bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const { | 47 bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dst
RowBytes, |
47 if (NULL == bitmap) { | 48 int srcX, int srcY) const { |
| 49 SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 50 if (!rec.trim(this->width(), this->height())) { |
48 return false; | 51 return false; |
49 } | 52 } |
50 | 53 return as_IB(this)->onReadPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.
fX, rec.fY); |
51 SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); | |
52 | |
53 // trim against the bitmap, if its already been allocated | |
54 if (bitmap->pixelRef()) { | |
55 bounds.fRight = SkMin32(bounds.fRight, bitmap->width()); | |
56 bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height()); | |
57 if (bounds.isEmpty()) { | |
58 return false; | |
59 } | |
60 } | |
61 | |
62 if (subset && !bounds.intersect(*subset)) { | |
63 // perhaps we could return true + empty-bitmap? | |
64 return false; | |
65 } | |
66 return as_IB(this)->onReadPixels(bitmap, bounds); | |
67 } | 54 } |
68 | 55 |
69 GrTexture* SkImage::getTexture() { | 56 GrTexture* SkImage::getTexture() { |
70 return as_IB(this)->onGetTexture(); | 57 return as_IB(this)->onGetTexture(); |
71 } | 58 } |
72 | 59 |
73 SkShader* SkImage::newShader(SkShader::TileMode tileX, | 60 SkShader* SkImage::newShader(SkShader::TileMode tileX, |
74 SkShader::TileMode tileY, | 61 SkShader::TileMode tileY, |
75 const SkMatrix* localMatrix) const { | 62 const SkMatrix* localMatrix) const { |
76 return as_IB(this)->onNewShader(tileX, tileY, localMatrix); | 63 return as_IB(this)->onNewShader(tileX, tileY, localMatrix); |
(...skipping 23 matching lines...) Expand all Loading... |
100 case kRGB_565_SkColorType: | 87 case kRGB_565_SkColorType: |
101 return true; | 88 return true; |
102 case kAlpha_8_SkColorType: | 89 case kAlpha_8_SkColorType: |
103 return true; | 90 return true; |
104 default: | 91 default: |
105 break; | 92 break; |
106 } | 93 } |
107 return false; | 94 return false; |
108 } | 95 } |
109 | 96 |
110 bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const { | 97 bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, siz
e_t dstRowBytes, |
111 if (bitmap->pixelRef()) { | 98 int srcX, int srcY) const { |
112 const SkImageInfo info = bitmap->info(); | 99 if (!raster_canvas_supports(dstInfo)) { |
113 if (kUnknown_SkColorType == info.colorType()) { | 100 return false; |
114 return false; | |
115 } | |
116 if (!raster_canvas_supports(info)) { | |
117 return false; | |
118 } | |
119 } else { | |
120 SkBitmap tmp; | |
121 if (!tmp.tryAllocN32Pixels(subset.width(), subset.height())) { | |
122 return false; | |
123 } | |
124 *bitmap = tmp; | |
125 } | 101 } |
126 | 102 |
127 SkRect srcR, dstR; | 103 SkBitmap bm; |
128 srcR.set(subset); | 104 bm.installPixels(dstInfo, dstPixels, dstRowBytes); |
129 dstR = srcR; | 105 SkCanvas canvas(bm); |
130 dstR.offset(-dstR.left(), -dstR.top()); | |
131 | |
132 SkCanvas canvas(*bitmap); | |
133 | 106 |
134 SkPaint paint; | 107 SkPaint paint; |
135 paint.setXfermodeMode(SkXfermode::kClear_Mode); | 108 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
136 canvas.drawRect(dstR, paint); | 109 canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); |
137 | 110 |
138 canvas.drawImageRect(this, &srcR, dstR); | |
139 return true; | 111 return true; |
140 } | 112 } |
OLD | NEW |