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