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 "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* onNewImageShapshot() SK_OVERRIDE; | 25 virtual SkImage* onNewImageShapshot() 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(SkImage*, SkCanvas*) SK_OVERRIDE; | 28 virtual void onCopyOnWrite(SkImage*, SkCanvas*, bool overwrite) 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 Loading... | |
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::onNewImageShapshot() { | 123 SkImage* SkSurface_Raster::onNewImageShapshot() { |
124 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); | 124 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels); |
125 } | 125 } |
126 | 126 |
127 void SkSurface_Raster::onCopyOnWrite(SkImage* image, SkCanvas* canvas) { | 127 void SkSurface_Raster::onCopyOnWrite(SkImage* image, SkCanvas* canvas, bool over write) { |
128 // are we sharing pixelrefs with the image? | 128 // are we sharing pixelrefs with the image? |
129 if (SkBitmapImageGetPixelRef(image) == fBitmap.pixelRef()) { | 129 if (SkBitmapImageGetPixelRef(image) == fBitmap.pixelRef()) { |
130 SkASSERT(fWeOwnThePixels); | 130 SkASSERT(fWeOwnThePixels); |
131 SkBitmap prev(fBitmap); | 131 if (!overwrite) { |
132 prev.deepCopyTo(&fBitmap, prev.config()); | 132 SkBitmap prev(fBitmap); |
133 prev.deepCopyTo(&fBitmap, prev.config()); | |
134 } else { | |
135 SkBitmap newBitmap; | |
136 newBitmap.setConfig(fBitmap.getConfig(), fBitmap.width(), fBitmap.he ight(), | |
137 fBitmap.rowBytes()); | |
138 newBitmap.allocPixels(); | |
139 fBitmap.swap(newBitmap); | |
140 } | |
133 // Now fBitmap is a deep copy of itself (and therefore different from | 141 // Now fBitmap is a deep copy of itself (and therefore different from |
Stephen White
2013/03/26 20:51:44
Nit: this comment is not strictly true anymore, s
| |
134 // what is being used by the image. Next we update the canvas to use | 142 // what is being used by the image. Next we update the canvas to use |
135 // this as its backend, so we can't modify the image's pixels anymore. | 143 // this as its backend, so we can't modify the image's pixels anymore. |
136 canvas->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); | 144 canvas->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); |
137 } | 145 } |
138 } | 146 } |
139 | 147 |
140 /////////////////////////////////////////////////////////////////////////////// | 148 /////////////////////////////////////////////////////////////////////////////// |
141 | 149 |
142 SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info, void* pixels, s ize_t rowBytes) { | 150 SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info, void* pixels, s ize_t rowBytes) { |
143 if (!SkSurface_Raster::Valid(info, rowBytes)) { | 151 if (!SkSurface_Raster::Valid(info, rowBytes)) { |
(...skipping 20 matching lines...) Expand all Loading... | |
164 | 172 |
165 size_t size = (size_t)size64; | 173 size_t size = (size_t)size64; |
166 void* pixels = sk_malloc_throw(size); | 174 void* pixels = sk_malloc_throw(size); |
167 if (NULL == pixels) { | 175 if (NULL == pixels) { |
168 return NULL; | 176 return NULL; |
169 } | 177 } |
170 | 178 |
171 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL , true))); | 179 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL , true))); |
172 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); | 180 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes)); |
173 } | 181 } |
OLD | NEW |