Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: tools/SkBitmapRegionCanvas.cpp

Issue 1418093006: Refactor SkBitmapRegionDecoderInterface for Android (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Better comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/SkBitmapRegionCanvas.h ('k') | tools/SkBitmapRegionCodec.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkBitmapRegionCanvas.h" 8 #include "SkBitmapRegionCanvas.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkCodecPriv.h" 10 #include "SkCodecPriv.h"
11 #include "SkCodecTools.h" 11 #include "SkCodecTools.h"
12 12
13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder) 13 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkCodec* decoder)
14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height()) 14 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height())
15 , fDecoder(decoder) 15 , fDecoder(decoder)
16 {} 16 {}
17 17
18 /* 18 bool SkBitmapRegionCanvas::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* a llocator,
19 * Three differences from the Android version: 19 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
20 * Returns a Skia bitmap instead of an Android bitmap. 20 bool requireUnpremul) {
21 * Android version attempts to reuse a recycled bitmap.
22 * Removed the options object and used parameters for color type and
23 * sample size.
24 */
25 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int inputX, int inputY,
26 int inputWidth, int inputHeight,
27 int sampleSize,
28 SkColorType dstColorType) {
29 // Reject color types not supported by this method 21 // Reject color types not supported by this method
30 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) { 22 if (kIndex_8_SkColorType == dstColorType || kGray_8_SkColorType == dstColorT ype) {
31 SkCodecPrintf("Error: Color type not supported.\n"); 23 SkCodecPrintf("Error: Color type not supported.\n");
32 return nullptr; 24 return false;
33 } 25 }
34 26
27 // Reject requests for unpremultiplied alpha
28 if (requireUnpremul) {
29 SkCodecPrintf("Error: Alpha type not supported.\n");
30 return false;
31 }
32 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType();
33 if (kUnpremul_SkAlphaType == dstAlphaType) {
34 dstAlphaType = kPremul_SkAlphaType;
35 }
36
37 // FIXME: Can we add checks and support kIndex8 or unpremultiplied alpha in special cases?
38
35 // Fix the input sampleSize if necessary. 39 // Fix the input sampleSize if necessary.
36 if (sampleSize < 1) { 40 if (sampleSize < 1) {
37 sampleSize = 1; 41 sampleSize = 1;
38 } 42 }
39 43
40 // The size of the output bitmap is determined by the size of the 44 // The size of the output bitmap is determined by the size of the
41 // requested subset, not by the size of the intersection of the subset 45 // requested subset, not by the size of the intersection of the subset
42 // and the image dimensions. 46 // and the image dimensions.
43 // If inputX is negative, we will need to place decoded pixels into the 47 // If inputX is negative, we will need to place decoded pixels into the
44 // output bitmap starting at a left offset. Call this outX. 48 // output bitmap starting at a left offset. Call this outX.
45 // If outX is non-zero, subsetX must be zero. 49 // If outX is non-zero, subsetX must be zero.
46 // If inputY is negative, we will need to place decoded pixels into the 50 // If inputY is negative, we will need to place decoded pixels into the
47 // output bitmap starting at a top offset. Call this outY. 51 // output bitmap starting at a top offset. Call this outY.
48 // If outY is non-zero, subsetY must be zero. 52 // If outY is non-zero, subsetY must be zero.
49 int outX; 53 int outX;
50 int outY; 54 int outY;
51 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); 55 SkIRect subset = desiredSubset;
52 SubsetType type = adjust_subset_rect(fDecoder->getInfo().dimensions(), &subs et, &outX, &outY); 56 SubsetType type = adjust_subset_rect(fDecoder->getInfo().dimensions(), &subs et, &outX, &outY);
53 if (SubsetType::kOutside_SubsetType == type) { 57 if (SubsetType::kOutside_SubsetType == type) {
54 return nullptr; 58 return false;
55 } 59 }
56 60
57 // Create the image info for the decode 61 // Create the image info for the decode
58 SkAlphaType dstAlphaType = fDecoder->getInfo().alphaType();
59 if (kUnpremul_SkAlphaType == dstAlphaType) {
60 dstAlphaType = kPremul_SkAlphaType;
61 }
62 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(), 62 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(),
63 dstColorType, dstAlphaType); 63 dstColorType, dstAlphaType);
64 64
65 // Start the scanline decoder 65 // Start the scanline decoder
66 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo); 66 SkCodec::Result r = fDecoder->startScanlineDecode(decodeInfo);
67 if (SkCodec::kSuccess != r) { 67 if (SkCodec::kSuccess != r) {
68 SkCodecPrintf("Error: Could not start scanline decoder.\n"); 68 SkCodecPrintf("Error: Could not start scanline decoder.\n");
69 return nullptr; 69 return false;
70 } 70 }
71 71
72 // Allocate a bitmap for the unscaled decode 72 // Allocate a bitmap for the unscaled decode
73 SkBitmap tmp; 73 SkBitmap tmp;
74 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), subset.height()); 74 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), subset.height());
75 if (!tmp.tryAllocPixels(tmpInfo)) { 75 if (!tmp.tryAllocPixels(tmpInfo)) {
76 SkCodecPrintf("Error: Could not allocate pixels.\n"); 76 SkCodecPrintf("Error: Could not allocate pixels.\n");
77 return nullptr; 77 return false;
78 } 78 }
79 79
80 // Skip the unneeded rows 80 // Skip the unneeded rows
81 if (!fDecoder->skipScanlines(subset.y())) { 81 if (!fDecoder->skipScanlines(subset.y())) {
82 SkCodecPrintf("Error: Failed to skip scanlines.\n"); 82 SkCodecPrintf("Error: Failed to skip scanlines.\n");
83 return nullptr; 83 return false;
84 } 84 }
85 85
86 // Decode the necessary rows 86 // Decode the necessary rows
87 fDecoder->getScanlines(tmp.getAddr(0, 0), subset.height(), tmp.rowBytes()); 87 fDecoder->getScanlines(tmp.getAddr(0, 0), subset.height(), tmp.rowBytes());
88 88
89 // Calculate the size of the output 89 // Calculate the size of the output
90 const int outWidth = get_scaled_dimension(inputWidth, sampleSize); 90 const int outWidth = get_scaled_dimension(desiredSubset.width(), sampleSize) ;
91 const int outHeight = get_scaled_dimension(inputHeight, sampleSize); 91 const int outHeight = get_scaled_dimension(desiredSubset.height(), sampleSiz e);
92 92
93 // Initialize the destination bitmap 93 // Initialize the destination bitmap
94 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap());
95 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight); 94 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight);
96 if (!bitmap->tryAllocPixels(dstInfo)) { 95 bitmap->setInfo(dstInfo, dstInfo.minRowBytes());
96 if (!bitmap->tryAllocPixels(allocator, nullptr)) {
97 SkCodecPrintf("Error: Could not allocate pixels.\n"); 97 SkCodecPrintf("Error: Could not allocate pixels.\n");
98 return nullptr; 98 return false;
99 } 99 }
100 100
101 // Zero the bitmap if the region is not completely within the image. 101 // Zero the bitmap if the region is not completely within the image.
102 // TODO (msarett): Can we make this faster by implementing it to only 102 // TODO (msarett): Can we make this faster by implementing it to only
103 // zero parts of the image that we won't overwrite with 103 // zero parts of the image that we won't overwrite with
104 // pixels? 104 // pixels?
105 // TODO (msarett): This could be skipped if memory is zero initialized. 105 // TODO (msarett): This could be skipped if memory is zero initialized.
106 // This would matter if this code is moved to Android and 106 // This would matter if this code is moved to Android and
107 // uses Android bitmaps. 107 // uses Android bitmaps.
108 if (SubsetType::kPartiallyInside_SubsetType == type) { 108 if (SubsetType::kPartiallyInside_SubsetType == type) {
109 bitmap->eraseColor(0); 109 bitmap->eraseColor(0);
110 } 110 }
111 111
112 // Use a canvas to crop and scale to the destination bitmap 112 // Use a canvas to crop and scale to the destination bitmap
113 SkCanvas canvas(*bitmap); 113 SkCanvas canvas(*bitmap);
114 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats? 114 // TODO (msarett): Maybe we can take advantage of the fact that SkRect uses floats?
115 SkRect src = SkRect::MakeXYWH((SkScalar) subset.x(), (SkScalar) 0, 115 SkRect src = SkRect::MakeXYWH((SkScalar) subset.x(), (SkScalar) 0,
116 (SkScalar) subset.width(), (SkScalar) subset.height()); 116 (SkScalar) subset.width(), (SkScalar) subset.height());
117 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize), 117 SkRect dst = SkRect::MakeXYWH((SkScalar) (outX / sampleSize), (SkScalar) (ou tY / sampleSize),
118 (SkScalar) get_scaled_dimension(subset.width(), sampleSize), 118 (SkScalar) get_scaled_dimension(subset.width(), sampleSize),
119 (SkScalar) get_scaled_dimension(subset.height(), sampleSize)); 119 (SkScalar) get_scaled_dimension(subset.height(), sampleSize));
120 SkPaint paint; 120 SkPaint paint;
121 // Overwrite the dst with the src pixels 121 // Overwrite the dst with the src pixels
122 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 122 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
123 // TODO (msarett): Test multiple filter qualities. kNone is the default. 123 // TODO (msarett): Test multiple filter qualities. kNone is the default.
124 canvas.drawBitmapRect(tmp, src, dst, &paint); 124 canvas.drawBitmapRect(tmp, src, dst, &paint);
125 125
126 return bitmap.detach(); 126 return true;
127 } 127 }
128 128
129 bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) { 129 bool SkBitmapRegionCanvas::conversionSupported(SkColorType colorType) {
130 // SkCanvas does not draw to these color types. 130 // SkCanvas does not draw to these color types.
131 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) { 131 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
132 return false; 132 return false;
133 } 133 }
134 134
135 // FIXME: Call virtual function when it lands. 135 // FIXME: Call virtual function when it lands.
136 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al phaType(), 136 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fDecoder->getInfo().al phaType(),
137 fDecoder->getInfo().profileType()); 137 fDecoder->getInfo().profileType());
138 return conversion_possible(info, fDecoder->getInfo()); 138 return conversion_possible(info, fDecoder->getInfo());
139 } 139 }
OLDNEW
« no previous file with comments | « tools/SkBitmapRegionCanvas.h ('k') | tools/SkBitmapRegionCodec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698