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 "SkImage_Base.h" | 8 #include "SkImage_Base.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkImageGenerator.h" | 12 #include "SkImageGenerator.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
14 #include "SkPixelRef.h" | 14 #include "SkPixelRef.h" |
15 #include "SkSurface.h" | 15 #include "SkSurface.h" |
16 | 16 |
17 class SkImage_Raster : public SkImage_Base { | 17 class SkImage_Raster : public SkImage_Base { |
18 public: | 18 public: |
19 static bool ValidArgs(const Info& info, size_t rowBytes) { | 19 static bool ValidArgs(const Info& info, size_t rowBytes, size_t* minSize) { |
20 const int maxDimension = SK_MaxS32 >> 2; | 20 const int maxDimension = SK_MaxS32 >> 2; |
21 const size_t kMaxPixelByteSize = SK_MaxS32; | |
22 | 21 |
23 if (info.width() <= 0 || info.height() <= 0) { | 22 if (info.width() <= 0 || info.height() <= 0) { |
24 return false; | 23 return false; |
25 } | 24 } |
26 if (info.width() > maxDimension || info.height() > maxDimension) { | 25 if (info.width() > maxDimension || info.height() > maxDimension) { |
27 return false; | 26 return false; |
28 } | 27 } |
29 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { | 28 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { |
30 return false; | 29 return false; |
31 } | 30 } |
32 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { | 31 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { |
33 return false; | 32 return false; |
34 } | 33 } |
35 | 34 |
36 if (kUnknown_SkColorType == info.colorType()) { | 35 if (kUnknown_SkColorType == info.colorType()) { |
37 return false; | 36 return false; |
38 } | 37 } |
39 | 38 |
40 // TODO: check colorspace | 39 // TODO: check colorspace |
41 | 40 |
42 if (rowBytes < SkImageMinRowBytes(info)) { | 41 if (rowBytes < SkImageMinRowBytes(info)) { |
43 return false; | 42 return false; |
44 } | 43 } |
45 | 44 |
46 int64_t size = (int64_t)info.height() * rowBytes; | 45 size_t size = info.getSafeSize(rowBytes); |
47 if (size > (int64_t)kMaxPixelByteSize) { | 46 if (0 == size) { |
48 return false; | 47 return false; |
49 } | 48 } |
| 49 |
| 50 if (minSize) { |
| 51 *minSize = size; |
| 52 } |
50 return true; | 53 return true; |
51 } | 54 } |
52 | 55 |
53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, const SkSurfaceProps*
); | 56 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, const SkSurfaceProps*
); |
54 virtual ~SkImage_Raster(); | 57 virtual ~SkImage_Raster(); |
55 | 58 |
56 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const ove
rride; | 59 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const ove
rride; |
57 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) con
st override; | 60 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) con
st override; |
58 const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; | 61 const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; |
59 bool getROPixels(SkBitmap*) const override; | 62 bool getROPixels(SkBitmap*) const override; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 142 } |
140 | 143 |
141 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { | 144 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { |
142 *dst = fBitmap; | 145 *dst = fBitmap; |
143 return true; | 146 return true; |
144 } | 147 } |
145 | 148 |
146 /////////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////////// |
147 | 150 |
148 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes) { | 151 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes) { |
149 if (!SkImage_Raster::ValidArgs(info, rowBytes) || !pixels) { | 152 size_t size; |
| 153 if (!SkImage_Raster::ValidArgs(info, rowBytes, &size) || !pixels) { |
150 return NULL; | 154 return NULL; |
151 } | 155 } |
152 | 156 |
153 // Here we actually make a copy of the caller's pixel data | 157 // Here we actually make a copy of the caller's pixel data |
154 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.height() * rowBytes)); | 158 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size)); |
155 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); | 159 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); |
156 } | 160 } |
157 | 161 |
158 | 162 |
159 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { | 163 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { |
160 if (!SkImage_Raster::ValidArgs(info, rowBytes) || !data) { | 164 size_t size; |
| 165 if (!SkImage_Raster::ValidArgs(info, rowBytes, &size) || !data) { |
161 return NULL; | 166 return NULL; |
162 } | 167 } |
163 | 168 |
164 // did they give us enough data? | 169 // did they give us enough data? |
165 size_t size = info.height() * rowBytes; | |
166 if (data->size() < size) { | 170 if (data->size() < size) { |
167 return NULL; | 171 return NULL; |
168 } | 172 } |
169 | 173 |
170 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); | 174 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); |
171 } | 175 } |
172 | 176 |
| 177 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes, |
| 178 RasterReleaseProc proc, ReleaseContext ctx) { |
| 179 size_t size; |
| 180 if (!SkImage_Raster::ValidArgs(info, rowBytes, &size) || !pixels) { |
| 181 return NULL; |
| 182 } |
| 183 |
| 184 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx)); |
| 185 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes, NULL)); |
| 186 } |
| 187 |
173 SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator) { | 188 SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator) { |
174 SkBitmap bitmap; | 189 SkBitmap bitmap; |
175 if (!SkInstallDiscardablePixelRef(generator, &bitmap)) { | 190 if (!SkInstallDiscardablePixelRef(generator, &bitmap)) { |
176 return NULL; | 191 return NULL; |
177 } | 192 } |
178 if (0 == bitmap.width() || 0 == bitmap.height()) { | 193 if (0 == bitmap.width() || 0 == bitmap.height()) { |
179 return NULL; | 194 return NULL; |
180 } | 195 } |
181 | 196 |
182 return SkNEW_ARGS(SkImage_Raster, (bitmap, NULL)); | 197 return SkNEW_ARGS(SkImage_Raster, (bitmap, NULL)); |
183 } | 198 } |
184 | 199 |
185 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, | 200 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
186 const SkIPoint& pixelRefOrigin, size_t rowBytes, | 201 const SkIPoint& pixelRefOrigin, size_t rowBytes, |
187 const SkSurfaceProps* props) { | 202 const SkSurfaceProps* props) { |
188 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { | 203 if (!SkImage_Raster::ValidArgs(info, rowBytes, NULL)) { |
189 return NULL; | 204 return NULL; |
190 } | 205 } |
191 return SkNEW_ARGS(SkImage_Raster, (info, pr, pixelRefOrigin, rowBytes, props
)); | 206 return SkNEW_ARGS(SkImage_Raster, (info, pr, pixelRefOrigin, rowBytes, props
)); |
192 } | 207 } |
193 | 208 |
194 SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef, | 209 SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef, |
195 const SkSurfaceProps* props) { | 210 const SkSurfaceProps* props) { |
196 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes())) { | 211 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), NULL)) { |
197 return NULL; | 212 return NULL; |
198 } | 213 } |
199 | 214 |
200 SkImage* image = NULL; | 215 SkImage* image = NULL; |
201 if (canSharePixelRef || bm.isImmutable()) { | 216 if (canSharePixelRef || bm.isImmutable()) { |
202 image = SkNEW_ARGS(SkImage_Raster, (bm, props)); | 217 image = SkNEW_ARGS(SkImage_Raster, (bm, props)); |
203 } else { | 218 } else { |
204 bm.lockPixels(); | 219 bm.lockPixels(); |
205 if (bm.getPixels()) { | 220 if (bm.getPixels()) { |
206 image = SkImage::NewRasterCopy(bm.info(), bm.getPixels(), bm.rowByte
s()); | 221 image = SkImage::NewRasterCopy(bm.info(), bm.getPixels(), bm.rowByte
s()); |
207 } | 222 } |
208 bm.unlockPixels(); | 223 bm.unlockPixels(); |
209 | 224 |
210 // we don't expose props to NewRasterCopy (need a private vers) so post-
init it here | 225 // we don't expose props to NewRasterCopy (need a private vers) so post-
init it here |
211 if (image && props) { | 226 if (image && props) { |
212 as_IB(image)->initWithProps(*props); | 227 as_IB(image)->initWithProps(*props); |
213 } | 228 } |
214 } | 229 } |
215 return image; | 230 return image; |
216 } | 231 } |
217 | 232 |
218 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { | 233 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { |
219 return ((const SkImage_Raster*)image)->getPixelRef(); | 234 return ((const SkImage_Raster*)image)->getPixelRef(); |
220 } | 235 } |
221 | 236 |
222 bool SkImage_Raster::isOpaque() const { | 237 bool SkImage_Raster::isOpaque() const { |
223 return fBitmap.isOpaque(); | 238 return fBitmap.isOpaque(); |
224 } | 239 } |
225 | 240 |
OLD | NEW |