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" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; | 87 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
88 | 88 |
89 // exposed for SkSurface_Raster via SkNewImageFromPixelRef | 89 // exposed for SkSurface_Raster via SkNewImageFromPixelRef |
90 SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size
_t rowBytes); | 90 SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size
_t rowBytes); |
91 | 91 |
92 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } | 92 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
93 | 93 |
94 bool isOpaque() const override; | 94 bool isOpaque() const override; |
95 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; | 95 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; |
96 | 96 |
97 SkImage_Raster(const SkBitmap& bm) | 97 SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable = false) |
98 : INHERITED(bm.width(), bm.height(), | 98 : INHERITED(bm.width(), bm.height(), |
99 is_not_subset(bm) ? bm.getGenerationID() | 99 is_not_subset(bm) ? bm.getGenerationID() |
100 : (uint32_t)kNeedNewImageUniqueID) | 100 : (uint32_t)kNeedNewImageUniqueID) |
101 , fBitmap(bm) | 101 , fBitmap(bm) |
102 { | 102 { |
103 if (bm.pixelRef()->isPreLocked()) { | 103 if (bm.pixelRef()->isPreLocked()) { |
104 // we only preemptively lock if there is no chance of triggering som
ething expensive | 104 // we only preemptively lock if there is no chance of triggering som
ething expensive |
105 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. | 105 // like a lazy decode or imagegenerator. PreLocked means it is flat
pixels already. |
106 fBitmap.lockPixels(); | 106 fBitmap.lockPixels(); |
107 } | 107 } |
108 SkASSERT(fBitmap.isImmutable()); | 108 SkASSERT(bitmapMayBeMutable || fBitmap.isImmutable()); |
109 } | 109 } |
110 | 110 |
111 bool onIsLazyGenerated() const override { | 111 bool onIsLazyGenerated() const override { |
112 return fBitmap.pixelRef() && fBitmap.pixelRef()->isLazyGenerated(); | 112 return fBitmap.pixelRef() && fBitmap.pixelRef()->isLazyGenerated(); |
113 } | 113 } |
114 | 114 |
115 private: | 115 private: |
116 SkBitmap fBitmap; | 116 SkBitmap fBitmap; |
117 | 117 |
118 typedef SkImage_Base INHERITED; | 118 typedef SkImage_Base INHERITED; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 if (kIndex_8_SkColorType == bm.colorType()) { | 257 if (kIndex_8_SkColorType == bm.colorType()) { |
258 SkAutoLockPixels autoLockPixels(bm); | 258 SkAutoLockPixels autoLockPixels(bm); |
259 hasColorTable = bm.getColorTable() != nullptr; | 259 hasColorTable = bm.getColorTable() != nullptr; |
260 } | 260 } |
261 | 261 |
262 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null
ptr)) { | 262 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null
ptr)) { |
263 return nullptr; | 263 return nullptr; |
264 } | 264 } |
265 | 265 |
266 sk_sp<SkImage> image; | 266 sk_sp<SkImage> image; |
267 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { | 267 if (kYes_ForceCopyMode == forceCopy || |
| 268 (!bm.isImmutable() && kNever_ForceCopyMode != forceCopy)) |
| 269 { |
268 SkBitmap tmp(bm); | 270 SkBitmap tmp(bm); |
269 tmp.lockPixels(); | 271 tmp.lockPixels(); |
270 SkPixmap pmap; | 272 SkPixmap pmap; |
271 if (tmp.getPixels() && tmp.peekPixels(&pmap)) { | 273 if (tmp.getPixels() && tmp.peekPixels(&pmap)) { |
272 image = SkImage::MakeRasterCopy(pmap); | 274 image = SkImage::MakeRasterCopy(pmap); |
273 } | 275 } |
274 } else { | 276 } else { |
275 image = sk_make_sp<SkImage_Raster>(bm); | 277 image = sk_make_sp<SkImage_Raster>(bm, kNever_ForceCopyMode == forceCopy
); |
276 } | 278 } |
277 return image; | 279 return image; |
278 } | 280 } |
279 | 281 |
280 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { | 282 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { |
281 return ((const SkImage_Raster*)image)->getPixelRef(); | 283 return ((const SkImage_Raster*)image)->getPixelRef(); |
282 } | 284 } |
283 | 285 |
284 bool SkImage_Raster::isOpaque() const { | 286 bool SkImage_Raster::isOpaque() const { |
285 return fBitmap.isOpaque(); | 287 return fBitmap.isOpaque(); |
286 } | 288 } |
287 | 289 |
288 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c
onst { | 290 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c
onst { |
289 if (kRO_LegacyBitmapMode == mode) { | 291 if (kRO_LegacyBitmapMode == mode) { |
290 // When we're a snapshot from a surface, our bitmap may not be marked im
mutable | 292 // When we're a snapshot from a surface, our bitmap may not be marked im
mutable |
291 // even though logically always we are, but in that case we can't physic
ally share our | 293 // even though logically always we are, but in that case we can't physic
ally share our |
292 // pixelref since the caller might call setImmutable() themselves | 294 // pixelref since the caller might call setImmutable() themselves |
293 // (thus changing our state). | 295 // (thus changing our state). |
294 if (fBitmap.isImmutable()) { | 296 if (fBitmap.isImmutable()) { |
295 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); | 297 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); |
296 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); | 298 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); |
297 return true; | 299 return true; |
298 } | 300 } |
299 } | 301 } |
300 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); | 302 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); |
301 } | 303 } |
OLD | NEW |