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 SkImageInfo&, size_t rb = kIgnoreRowBytesValue); | 18 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
19 | 19 |
20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb, | 20 SkSurface_Raster(const SkImageInfo&, void*, size_t rb, |
21 void (*releaseProc)(void* pixels, void* context), void* con
text, | 21 void (*releaseProc)(void* pixels, void* context), void* con
text, |
22 const SkSurfaceProps*); | 22 const SkSurfaceProps*); |
23 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); | 23 SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*); |
24 | 24 |
25 SkCanvas* onNewCanvas() override; | 25 SkCanvas* onNewCanvas() override; |
26 SkSurface* onNewSurface(const SkImageInfo&) override; | 26 SkSurface* onNewSurface(const SkImageInfo&) override; |
27 SkImage* onNewImageSnapshot(Budgeted) override; | 27 SkImage* onNewImageSnapshot(Budgeted, ForceCopyMode) override; |
28 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; | 28 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; |
29 void onCopyOnWrite(ContentChangeMode) override; | 29 void onCopyOnWrite(ContentChangeMode) override; |
30 void onRestoreBackingMutability() override; | 30 void onRestoreBackingMutability() override; |
31 | 31 |
32 private: | 32 private: |
33 SkBitmap fBitmap; | 33 SkBitmap fBitmap; |
34 size_t fRowBytes; | 34 size_t fRowBytes; |
35 bool fWeOwnThePixels; | 35 bool fWeOwnThePixels; |
36 | 36 |
37 typedef SkSurface_Base INHERITED; | 37 typedef SkSurface_Base INHERITED; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 112 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
113 return SkSurface::NewRaster(info, &this->props()); | 113 return SkSurface::NewRaster(info, &this->props()); |
114 } | 114 } |
115 | 115 |
116 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 116 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
117 const SkPaint* paint) { | 117 const SkPaint* paint) { |
118 canvas->drawBitmap(fBitmap, x, y, paint); | 118 canvas->drawBitmap(fBitmap, x, y, paint); |
119 } | 119 } |
120 | 120 |
121 SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) { | 121 SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted, ForceCopyMode forceCopyM
ode) { |
122 if (fWeOwnThePixels) { | 122 if (fWeOwnThePixels) { |
123 // SkImage_raster requires these pixels are immutable for its full lifet
ime. | 123 // SkImage_raster requires these pixels are immutable for its full lifet
ime. |
124 // We'll undo this via onRestoreBackingMutability() if we can avoid the
COW. | 124 // We'll undo this via onRestoreBackingMutability() if we can avoid the
COW. |
125 if (SkPixelRef* pr = fBitmap.pixelRef()) { | 125 if (SkPixelRef* pr = fBitmap.pixelRef()) { |
126 pr->setTemporarilyImmutable(); | 126 pr->setTemporarilyImmutable(); |
127 } | 127 } |
| 128 } else { |
| 129 forceCopyMode = kYes_ForceCopyMode; |
128 } | 130 } |
| 131 |
129 // Our pixels are in memory, so read access on the snapshot SkImage could be
cheap. | 132 // Our pixels are in memory, so read access on the snapshot SkImage could be
cheap. |
130 // Lock the shared pixel ref to ensure peekPixels() is usable. | 133 // Lock the shared pixel ref to ensure peekPixels() is usable. |
131 return SkNewImageFromRasterBitmap(fBitmap, | 134 return SkNewImageFromRasterBitmap(fBitmap, forceCopyMode); |
132 fWeOwnThePixels ? kNo_ForceCopyMode : kYes
_ForceCopyMode); | |
133 } | 135 } |
134 | 136 |
135 void SkSurface_Raster::onRestoreBackingMutability() { | 137 void SkSurface_Raster::onRestoreBackingMutability() { |
136 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. | 138 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. |
137 if (SkPixelRef* pr = fBitmap.pixelRef()) { | 139 if (SkPixelRef* pr = fBitmap.pixelRef()) { |
138 pr->restoreMutability(); | 140 pr->restoreMutability(); |
139 } | 141 } |
140 } | 142 } |
141 | 143 |
142 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { | 144 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
143 // are we sharing pixelrefs with the image? | 145 // are we sharing pixelrefs with the image? |
144 SkASSERT(this->getCachedImage(kNo_Budgeted)); | 146 SkAutoTUnref<SkImage> cached(this->refCachedImage(kNo_Budgeted, kNo_ForceUni
que)); |
145 if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.
pixelRef()) { | 147 SkASSERT(cached); |
| 148 if (SkBitmapImageGetPixelRef(cached) == fBitmap.pixelRef()) { |
146 SkASSERT(fWeOwnThePixels); | 149 SkASSERT(fWeOwnThePixels); |
147 if (kDiscard_ContentChangeMode == mode) { | 150 if (kDiscard_ContentChangeMode == mode) { |
148 fBitmap.allocPixels(); | 151 fBitmap.allocPixels(); |
149 } else { | 152 } else { |
150 SkBitmap prev(fBitmap); | 153 SkBitmap prev(fBitmap); |
151 fBitmap.allocPixels(); | 154 fBitmap.allocPixels(); |
152 prev.lockPixels(); | 155 prev.lockPixels(); |
153 SkASSERT(prev.info() == fBitmap.info()); | 156 SkASSERT(prev.info() == fBitmap.info()); |
154 SkASSERT(prev.rowBytes() == fBitmap.rowBytes()); | 157 SkASSERT(prev.rowBytes() == fBitmap.rowBytes()); |
155 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.getSafeSize())
; | 158 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.getSafeSize())
; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 202 } |
200 if (rowBytes) { | 203 if (rowBytes) { |
201 SkASSERT(pr->rowBytes() == rowBytes); | 204 SkASSERT(pr->rowBytes() == rowBytes); |
202 } | 205 } |
203 return new SkSurface_Raster(pr, props); | 206 return new SkSurface_Raster(pr, props); |
204 } | 207 } |
205 | 208 |
206 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 209 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
rops) { |
207 return NewRaster(info, 0, props); | 210 return NewRaster(info, 0, props); |
208 } | 211 } |
OLD | NEW |