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

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

Issue 14063015: Adding optimization to avoid image copy in SkSurface copy on write when content is discardable (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « src/image/SkSurface_Picture.cpp ('k') | src/utils/SkDeferredCanvas.cpp » ('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 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 "SkSurface_Base.h" 8 #include "SkSurface_Base.h"
9 #include "SkImagePriv.h" 9 #include "SkImagePriv.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkDevice.h" 11 #include "SkDevice.h"
12 #include "SkMallocPixelRef.h" 12 #include "SkMallocPixelRef.h"
13 13
14 static const size_t kIgnoreRowBytesValue = (size_t)~0; 14 static const size_t kIgnoreRowBytesValue = (size_t)~0;
15 15
16 class SkSurface_Raster : public SkSurface_Base { 16 class SkSurface_Raster : public SkSurface_Base {
17 public: 17 public:
18 static bool Valid(const SkImage::Info&, size_t rb = kIgnoreRowBytesValue); 18 static bool Valid(const SkImage::Info&, size_t rb = kIgnoreRowBytesValue);
19 19
20 SkSurface_Raster(const SkImage::Info&, void*, size_t rb); 20 SkSurface_Raster(const SkImage::Info&, void*, size_t rb);
21 SkSurface_Raster(const SkImage::Info&, SkPixelRef*, size_t rb); 21 SkSurface_Raster(const SkImage::Info&, SkPixelRef*, size_t rb);
22 22
23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE; 23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
24 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE; 24 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE;
25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; 25 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, 26 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
27 const SkPaint*) SK_OVERRIDE; 27 const SkPaint*) SK_OVERRIDE;
28 virtual void onCopyOnWrite() SK_OVERRIDE; 28 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
29 29
30 private: 30 private:
31 SkBitmap fBitmap; 31 SkBitmap fBitmap;
32 bool fWeOwnThePixels; 32 bool fWeOwnThePixels;
33 33
34 typedef SkSurface_Base INHERITED; 34 typedef SkSurface_Base INHERITED;
35 }; 35 };
36 36
37 /////////////////////////////////////////////////////////////////////////////// 37 ///////////////////////////////////////////////////////////////////////////////
38 38
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 117
118 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, 118 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
119 const SkPaint* paint) { 119 const SkPaint* paint) {
120 canvas->drawBitmap(fBitmap, x, y, paint); 120 canvas->drawBitmap(fBitmap, x, y, paint);
121 } 121 }
122 122
123 SkImage* SkSurface_Raster::onNewImageSnapshot() { 123 SkImage* SkSurface_Raster::onNewImageSnapshot() {
124 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); 124 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels);
125 } 125 }
126 126
127 void SkSurface_Raster::onCopyOnWrite() { 127 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
128 // are we sharing pixelrefs with the image? 128 // are we sharing pixelrefs with the image?
129 SkASSERT(NULL !=this->getCachedImage()); 129 SkASSERT(NULL != this->getCachedImage());
130 if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) { 130 if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) {
131 SkASSERT(fWeOwnThePixels); 131 SkASSERT(fWeOwnThePixels);
132 SkBitmap prev(fBitmap); 132 if (kDiscard_ContentChangeMode == mode) {
133 prev.deepCopyTo(&fBitmap, prev.config()); 133 fBitmap.setPixelRef(NULL, 0);
134 fBitmap.allocPixels();
135 } else {
136 SkBitmap prev(fBitmap);
137 prev.deepCopyTo(&fBitmap, prev.config());
138 }
134 // Now fBitmap is a deep copy of itself (and therefore different from 139 // Now fBitmap is a deep copy of itself (and therefore different from
135 // what is being used by the image. Next we update the canvas to use 140 // what is being used by the image. Next we update the canvas to use
136 // this as its backend, so we can't modify the image's pixels anymore. 141 // this as its backend, so we can't modify the image's pixels anymore.
137 SkASSERT(NULL != this->getCachedCanvas()); 142 SkASSERT(NULL != this->getCachedCanvas());
138 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa ce(fBitmap); 143 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa ce(fBitmap);
139 } 144 }
140 } 145 }
141 146
142 /////////////////////////////////////////////////////////////////////////////// 147 ///////////////////////////////////////////////////////////////////////////////
143 148
(...skipping 22 matching lines...) Expand all
166 171
167 size_t size = (size_t)size64; 172 size_t size = (size_t)size64;
168 void* pixels = sk_malloc_throw(size); 173 void* pixels = sk_malloc_throw(size);
169 if (NULL == pixels) { 174 if (NULL == pixels) {
170 return NULL; 175 return NULL;
171 } 176 }
172 177
173 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL , true))); 178 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL , true)));
174 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); 179 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes));
175 } 180 }
OLDNEW
« no previous file with comments | « src/image/SkSurface_Picture.cpp ('k') | src/utils/SkDeferredCanvas.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698