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 "SkColorTable.h" | 11 #include "SkColorTable.h" |
12 #include "SkData.h" | 12 #include "SkData.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 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
18 #include "GrContext.h" | 18 #include "GrContext.h" |
19 #include "SkGr.h" | 19 #include "SkGr.h" |
20 #include "SkGrPriv.h" | 20 #include "SkGrPriv.h" |
21 #endif | 21 #endif |
22 | 22 |
23 class SkImage_Raster : public SkImage_Base { | 23 class SkImage_Raster : public SkImage_Base { |
24 public: | 24 public: |
25 static bool ValidArgs(const Info& info, size_t rowBytes, SkColorTable* ctabl
e, | 25 static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable, |
26 size_t* minSize) { | 26 size_t* minSize) { |
27 const int maxDimension = SK_MaxS32 >> 2; | 27 const int maxDimension = SK_MaxS32 >> 2; |
28 | 28 |
29 if (info.width() <= 0 || info.height() <= 0) { | 29 if (info.width() <= 0 || info.height() <= 0) { |
30 return false; | 30 return false; |
31 } | 31 } |
32 if (info.width() > maxDimension || info.height() > maxDimension) { | 32 if (info.width() > maxDimension || info.height() > maxDimension) { |
33 return false; | 33 return false; |
34 } | 34 } |
35 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { | 35 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { |
36 return false; | 36 return false; |
37 } | 37 } |
38 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { | 38 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { |
39 return false; | 39 return false; |
40 } | 40 } |
41 | 41 |
42 if (kUnknown_SkColorType == info.colorType()) { | 42 if (kUnknown_SkColorType == info.colorType()) { |
43 return false; | 43 return false; |
44 } | 44 } |
45 | 45 |
46 const bool needsCT = kIndex_8_SkColorType == info.colorType(); | 46 const bool needsCT = kIndex_8_SkColorType == info.colorType(); |
47 const bool hasCT = nullptr != ctable; | 47 if (needsCT != hasColorTable) { |
48 if (needsCT != hasCT) { | |
49 return false; | 48 return false; |
50 } | 49 } |
51 | 50 |
52 if (rowBytes < SkImageMinRowBytes(info)) { | 51 if (rowBytes < SkImageMinRowBytes(info)) { |
53 return false; | 52 return false; |
54 } | 53 } |
55 | 54 |
56 size_t size = info.getSafeSize(rowBytes); | 55 size_t size = info.getSafeSize(rowBytes); |
57 if (0 == size) { | 56 if (0 == size) { |
58 return false; | 57 return false; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca
lar(-subset.y()), | 210 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca
lar(-subset.y()), |
212 nullptr); | 211 nullptr); |
213 return surface->newImageSnapshot(); | 212 return surface->newImageSnapshot(); |
214 } | 213 } |
215 | 214 |
216 /////////////////////////////////////////////////////////////////////////////// | 215 /////////////////////////////////////////////////////////////////////////////// |
217 | 216 |
218 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes, | 217 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes, |
219 SkColorTable* ctable) { | 218 SkColorTable* ctable) { |
220 size_t size; | 219 size_t size; |
221 if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable, &size) || !pixels) { | 220 if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable != nullptr, &size) ||
!pixels) { |
222 return nullptr; | 221 return nullptr; |
223 } | 222 } |
224 | 223 |
225 // Here we actually make a copy of the caller's pixel data | 224 // Here we actually make a copy of the caller's pixel data |
226 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size)); | 225 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size)); |
227 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); | 226 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); |
228 } | 227 } |
229 | 228 |
230 | 229 |
231 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { | 230 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro
wBytes) { |
232 size_t size; | 231 size_t size; |
233 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !data) { | 232 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) { |
234 return nullptr; | 233 return nullptr; |
235 } | 234 } |
236 | 235 |
237 // did they give us enough data? | 236 // did they give us enough data? |
238 if (data->size() < size) { | 237 if (data->size() < size) { |
239 return nullptr; | 238 return nullptr; |
240 } | 239 } |
241 | 240 |
242 SkColorTable* ctable = nullptr; | 241 SkColorTable* ctable = nullptr; |
243 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); | 242 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); |
244 } | 243 } |
245 | 244 |
246 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes, | 245 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes, |
247 RasterReleaseProc proc, ReleaseContext ctx) { | 246 RasterReleaseProc proc, ReleaseContext ctx) { |
248 size_t size; | 247 size_t size; |
249 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, &size) || !pixels) { | 248 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !pixels) { |
250 return nullptr; | 249 return nullptr; |
251 } | 250 } |
252 | 251 |
253 SkColorTable* ctable = nullptr; | 252 SkColorTable* ctable = nullptr; |
254 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx)); | 253 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx)); |
255 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); | 254 return new SkImage_Raster(info, data, rowBytes, ctable, nullptr); |
256 } | 255 } |
257 | 256 |
258 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, | 257 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
259 const SkIPoint& pixelRefOrigin, size_t rowBytes, | 258 const SkIPoint& pixelRefOrigin, size_t rowBytes, |
260 const SkSurfaceProps* props) { | 259 const SkSurfaceProps* props) { |
261 if (!SkImage_Raster::ValidArgs(info, rowBytes, nullptr, nullptr)) { | 260 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) { |
262 return nullptr; | 261 return nullptr; |
263 } | 262 } |
264 return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes, props); | 263 return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes, props); |
265 } | 264 } |
266 | 265 |
267 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr
ops, | 266 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr
ops, |
268 ForceCopyMode forceCopy) { | 267 ForceCopyMode forceCopy) { |
269 SkASSERT(nullptr == bm.getTexture()); | 268 SkASSERT(nullptr == bm.getTexture()); |
270 | 269 |
271 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), nullptr, nullptr))
{ | 270 bool hasColorTable = false; |
| 271 if (kIndex_8_SkColorType == bm.colorType()) { |
| 272 SkAutoLockPixels autoLockPixels(bm); |
| 273 hasColorTable = bm.getColorTable() != nullptr; |
| 274 } |
| 275 |
| 276 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null
ptr)) { |
272 return nullptr; | 277 return nullptr; |
273 } | 278 } |
274 | 279 |
275 SkImage* image = nullptr; | 280 SkImage* image = nullptr; |
276 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { | 281 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { |
277 SkBitmap tmp(bm); | 282 SkBitmap tmp(bm); |
278 tmp.lockPixels(); | 283 tmp.lockPixels(); |
279 if (tmp.getPixels()) { | 284 if (tmp.getPixels()) { |
280 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB
ytes(), | 285 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB
ytes(), |
281 tmp.getColorTable()); | 286 tmp.getColorTable()); |
(...skipping 24 matching lines...) Expand all Loading... |
306 // pixelref since the caller might call setImmutable() themselves | 311 // pixelref since the caller might call setImmutable() themselves |
307 // (thus changing our state). | 312 // (thus changing our state). |
308 if (fBitmap.isImmutable()) { | 313 if (fBitmap.isImmutable()) { |
309 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); | 314 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); |
310 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); | 315 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); |
311 return true; | 316 return true; |
312 } | 317 } |
313 } | 318 } |
314 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); | 319 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); |
315 } | 320 } |
OLD | NEW |