OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBitmapRegionCodec.h" | 8 #include "SkBitmapRegionCodec.h" |
9 #include "SkAndroidCodec.h" | 9 #include "SkAndroidCodec.h" |
10 #include "SkCodecPriv.h" | 10 #include "SkCodecPriv.h" |
11 #include "SkCodecTools.h" | 11 #include "SkCodecTools.h" |
12 #include "SkPixelRef.h" | |
12 | 13 |
13 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) | 14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) |
14 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) | 15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) |
15 , fCodec(codec) | 16 , fCodec(codec) |
16 {} | 17 {} |
17 | 18 |
18 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* al locator, | 19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* al locator, |
19 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, | 20 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, |
20 bool requireUnpremul) { | 21 bool requireUnpremul) { |
21 | 22 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 scaledOutY = outY / sampleSize; | 87 scaledOutY = outY / sampleSize; |
87 // We need to be safe here because getSupportedSubset() may have modifie d the subset. | 88 // We need to be safe here because getSupportedSubset() may have modifie d the subset. |
88 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ()); | 89 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ()); |
89 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht()); | 90 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht()); |
90 const int scaledExtraX = extraX / sampleSize; | 91 const int scaledExtraX = extraX / sampleSize; |
91 const int scaledExtraY = extraY / sampleSize; | 92 const int scaledExtraY = extraY / sampleSize; |
92 scaledOutWidth += scaledOutX + scaledExtraX; | 93 scaledOutWidth += scaledOutX + scaledExtraX; |
93 scaledOutHeight += scaledOutY + scaledExtraY; | 94 scaledOutHeight += scaledOutY + scaledExtraY; |
94 } | 95 } |
95 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); | 96 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); |
96 bitmap->setInfo(outInfo, outInfo.minRowBytes()); | 97 bitmap->setInfo(outInfo); |
97 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { | 98 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { |
98 SkCodecPrintf("Error: Could not allocate pixels.\n"); | 99 SkCodecPrintf("Error: Could not allocate pixels.\n"); |
99 return false; | 100 return false; |
100 } | 101 } |
101 | 102 |
102 // Zero the bitmap if the region is not completely within the image. | 103 // Zero the bitmap if the region is not completely within the image. |
103 // TODO (msarett): Can we make this faster by implementing it to only | 104 // TODO (msarett): Can we make this faster by implementing it to only |
104 // zero parts of the image that we won't overwrite with | 105 // zero parts of the image that we won't overwrite with |
105 // pixels? | 106 // pixels? |
106 // TODO (msarett): This could be skipped if memory is zero initialized. | 107 // TODO (msarett): This could be skipped if memory is zero initialized. |
107 // This would matter if this code is moved to Android and | 108 // This would matter if this code is moved to Android and |
108 // uses Android bitmaps. | 109 // uses Android bitmaps. |
109 if (SubsetType::kPartiallyInside_SubsetType == type) { | 110 if (SubsetType::kPartiallyInside_SubsetType == type) { |
110 void* pixels = bitmap->getPixels(); | 111 void* pixels = bitmap->getPixels(); |
111 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes()); | 112 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes()); |
112 memset(pixels, 0, bytes); | 113 memset(pixels, 0, bytes); |
113 } | 114 } |
114 | 115 |
115 // Decode into the destination bitmap | 116 // Decode into the destination bitmap |
116 SkAndroidCodec::AndroidOptions options; | 117 SkAndroidCodec::AndroidOptions options; |
117 options.fSampleSize = sampleSize; | 118 options.fSampleSize = sampleSize; |
118 options.fSubset = ⊂ | 119 options.fSubset = ⊂ |
119 options.fColorPtr = colorPtr; | 120 options.fColorPtr = colorPtr; |
120 options.fColorCount = colorCountPtr; | 121 options.fColorCount = colorCountPtr; |
121 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); | 122 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); |
122 size_t rowBytes = bitmap->rowBytes(); | 123 |
124 // FIXME: skbug.com/4538 | |
125 // It is important that we use the rowBytes on the pixelRef. They may not b e | |
126 // set properly on the bitmap. | |
127 SkPixelRef* pr = SkRef(bitmap->pixelRef()); | |
scroggo
2015/11/05 22:45:56
I just want to double check that the ref counting
| |
128 size_t rowBytes = pr->rowBytes(); | |
129 bitmap->setInfo(outInfo, rowBytes); | |
scroggo
2015/11/05 22:45:56
Behind our backs, this decrements the refCount, so
| |
130 bitmap->setPixelRef(pr)->unref(); | |
scroggo
2015/11/05 22:45:56
setPixelRef increments it again (RC + 1) and then
msarett
2015/11/05 22:49:05
Looks correct to me as well.
| |
123 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options); | 131 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options); |
124 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { | 132 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { |
125 SkCodecPrintf("Error: Could not get pixels.\n"); | 133 SkCodecPrintf("Error: Could not get pixels.\n"); |
126 return false; | 134 return false; |
127 } | 135 } |
128 | 136 |
129 return true; | 137 return true; |
130 } | 138 } |
131 | 139 |
132 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { | 140 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { |
133 // FIXME: Call virtual function when it lands. | 141 // FIXME: Call virtual function when it lands. |
134 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), | 142 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), |
135 fCodec->getInfo().profileType()); | 143 fCodec->getInfo().profileType()); |
136 return conversion_possible(info, fCodec->getInfo()); | 144 return conversion_possible(info, fCodec->getInfo()); |
137 } | 145 } |
OLD | NEW |