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

Side by Side Diff: src/image/SkImage_Raster.cpp

Issue 1266143003: lock pixels in image when bitmap is immutable and not-lazy (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 4 months 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
OLDNEW
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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 74
75 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } 75 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
76 76
77 SkShader* onNewShader(SkShader::TileMode, 77 SkShader* onNewShader(SkShader::TileMode,
78 SkShader::TileMode, 78 SkShader::TileMode,
79 const SkMatrix* localMatrix) const override; 79 const SkMatrix* localMatrix) const override;
80 80
81 bool isOpaque() const override; 81 bool isOpaque() const override;
82 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; 82 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override;
83 83
84 SkImage_Raster(const SkBitmap& bm, const SkSurfaceProps* props, bool lockPix els = false) 84 SkImage_Raster(const SkBitmap& bm, const SkSurfaceProps* props)
85 : INHERITED(bm.width(), bm.height(), bm.getGenerationID(), props) 85 : INHERITED(bm.width(), bm.height(), bm.getGenerationID(), props)
86 , fBitmap(bm) { 86 , fBitmap(bm)
87 if (lockPixels) { 87 {
88 if (bm.pixelRef()->isPreLocked()) {
89 // we only preemptively lock if there is no chance of triggering som ething expensive
90 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already.
88 fBitmap.lockPixels(); 91 fBitmap.lockPixels();
89 } 92 }
90 SkASSERT(fBitmap.isImmutable()); 93 SkASSERT(fBitmap.isImmutable());
91 } 94 }
92 95
93 private: 96 private:
94 SkImage_Raster() : INHERITED(0, 0, fBitmap.getGenerationID(), NULL) { 97 SkImage_Raster() : INHERITED(0, 0, fBitmap.getGenerationID(), NULL) {
95 fBitmap.setImmutable(); 98 fBitmap.setImmutable();
96 } 99 }
97 100
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, 233 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
231 const SkIPoint& pixelRefOrigin, size_t rowBytes, 234 const SkIPoint& pixelRefOrigin, size_t rowBytes,
232 const SkSurfaceProps* props) { 235 const SkSurfaceProps* props) {
233 if (!SkImage_Raster::ValidArgs(info, rowBytes, NULL, NULL)) { 236 if (!SkImage_Raster::ValidArgs(info, rowBytes, NULL, NULL)) {
234 return NULL; 237 return NULL;
235 } 238 }
236 return SkNEW_ARGS(SkImage_Raster, (info, pr, pixelRefOrigin, rowBytes, props )); 239 return SkNEW_ARGS(SkImage_Raster, (info, pr, pixelRefOrigin, rowBytes, props ));
237 } 240 }
238 241
239 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr ops, 242 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, const SkSurfaceProps* pr ops,
240 SharedPixelRefMode mode, ForceCopyMode force Copy) { 243 ForceCopyMode forceCopy) {
241 SkASSERT(NULL == bm.getTexture()); 244 SkASSERT(NULL == bm.getTexture());
242 245
243 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), NULL, NULL)) { 246 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), NULL, NULL)) {
244 return NULL; 247 return NULL;
245 } 248 }
246 249
247 SkImage* image = NULL; 250 SkImage* image = NULL;
248 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { 251 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) {
249 SkBitmap tmp(bm); 252 SkBitmap tmp(bm);
250 tmp.lockPixels(); 253 tmp.lockPixels();
251 if (tmp.getPixels()) { 254 if (tmp.getPixels()) {
252 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB ytes(), 255 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB ytes(),
253 tmp.getColorTable()); 256 tmp.getColorTable());
254 } 257 }
255 258
256 // we don't expose props to NewRasterCopy (need a private vers) so post- init it here 259 // we don't expose props to NewRasterCopy (need a private vers) so post- init it here
257 if (image && props) { 260 if (image && props) {
258 as_IB(image)->initWithProps(*props); 261 as_IB(image)->initWithProps(*props);
259 } 262 }
260 } else { 263 } else {
261 image = SkNEW_ARGS(SkImage_Raster, (bm, props, kLocked_SharedPixelRefMod e == mode)); 264 image = SkNEW_ARGS(SkImage_Raster, (bm, props));
262 } 265 }
263 return image; 266 return image;
264 } 267 }
265 268
266 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { 269 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) {
267 return ((const SkImage_Raster*)image)->getPixelRef(); 270 return ((const SkImage_Raster*)image)->getPixelRef();
268 } 271 }
269 272
270 bool SkImage_Raster::isOpaque() const { 273 bool SkImage_Raster::isOpaque() const {
271 return fBitmap.isOpaque(); 274 return fBitmap.isOpaque();
272 } 275 }
273 276
274 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c onst { 277 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c onst {
275 if (kRO_LegacyBitmapMode == mode) { 278 if (kRO_LegacyBitmapMode == mode) {
276 // When we're a snapshot from a surface, our bitmap may not be marked im mutable 279 // When we're a snapshot from a surface, our bitmap may not be marked im mutable
277 // even though logically always we are, but in that case we can't physic ally share our 280 // even though logically always we are, but in that case we can't physic ally share our
278 // pixelref since the caller might call setImmutable() themselves 281 // pixelref since the caller might call setImmutable() themselves
279 // (thus changing our state). 282 // (thus changing our state).
280 if (fBitmap.isImmutable()) { 283 if (fBitmap.isImmutable()) {
281 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); 284 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
282 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); 285 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin());
283 return true; 286 return true;
284 } 287 }
285 } 288 }
286 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); 289 return this->INHERITED::onAsLegacyBitmap(bitmap, mode);
287 } 290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698