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) 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 | 31 |
31 private: | 32 private: |
32 SkBitmap fBitmap; | 33 SkBitmap fBitmap; |
33 bool fWeOwnThePixels; | 34 bool fWeOwnThePixels; |
34 | 35 |
35 typedef SkSurface_Base INHERITED; | 36 typedef SkSurface_Base INHERITED; |
36 }; | 37 }; |
37 | 38 |
38 /////////////////////////////////////////////////////////////////////////////// | 39 /////////////////////////////////////////////////////////////////////////////// |
39 | 40 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 112 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
112 return SkSurface::NewRaster(info, &this->props()); | 113 return SkSurface::NewRaster(info, &this->props()); |
113 } | 114 } |
114 | 115 |
115 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, | 116 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
116 const SkPaint* paint) { | 117 const SkPaint* paint) { |
117 canvas->drawBitmap(fBitmap, x, y, paint); | 118 canvas->drawBitmap(fBitmap, x, y, paint); |
118 } | 119 } |
119 | 120 |
120 SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) { | 121 SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) { |
| 122 if (fWeOwnThePixels) { |
| 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. |
| 125 if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 126 pr->setTemporarilyImmutable(); |
| 127 } |
| 128 } |
121 // 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. |
122 // Lock the shared pixel ref to ensure peekPixels() is usable. | 130 // Lock the shared pixel ref to ensure peekPixels() is usable. |
123 return SkNewImageFromRasterBitmap(fBitmap, fWeOwnThePixels, &this->props(), | 131 return SkNewImageFromRasterBitmap(fBitmap, &this->props(), kLocked_SharedPix
elRefMode, |
124 kLocked_SharedPixelRefMode); | 132 fWeOwnThePixels ? kNo_ForceCopyMode : kYes
_ForceCopyMode); |
| 133 } |
| 134 |
| 135 void SkSurface_Raster::onRestoreBackingMutability() { |
| 136 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. |
| 137 if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 138 pr->restoreMutability(); |
| 139 } |
125 } | 140 } |
126 | 141 |
127 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { | 142 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
128 // are we sharing pixelrefs with the image? | 143 // are we sharing pixelrefs with the image? |
129 SkASSERT(this->getCachedImage(kNo_Budgeted)); | 144 SkASSERT(this->getCachedImage(kNo_Budgeted)); |
130 if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.
pixelRef()) { | 145 if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.
pixelRef()) { |
131 SkASSERT(fWeOwnThePixels); | 146 SkASSERT(fWeOwnThePixels); |
132 if (kDiscard_ContentChangeMode == mode) { | 147 if (kDiscard_ContentChangeMode == mode) { |
133 fBitmap.setPixelRef(NULL); | 148 fBitmap.setPixelRef(NULL); |
134 fBitmap.allocPixels(); | 149 fBitmap.allocPixels(); |
(...skipping 16 matching lines...) Expand all Loading... |
151 void* context, const SkSurfaceP
rops* props) { | 166 void* context, const SkSurfaceP
rops* props) { |
152 if (NULL == releaseProc) { | 167 if (NULL == releaseProc) { |
153 context = NULL; | 168 context = NULL; |
154 } | 169 } |
155 if (!SkSurface_Raster::Valid(info, rb)) { | 170 if (!SkSurface_Raster::Valid(info, rb)) { |
156 return NULL; | 171 return NULL; |
157 } | 172 } |
158 if (NULL == pixels) { | 173 if (NULL == pixels) { |
159 return NULL; | 174 return NULL; |
160 } | 175 } |
161 | 176 |
162 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context,
props)); | 177 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rb, releaseProc, context,
props)); |
163 } | 178 } |
164 | 179 |
165 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
e_t rowBytes, | 180 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
e_t rowBytes, |
166 const SkSurfaceProps* props) { | 181 const SkSurfaceProps* props) { |
167 return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props)
; | 182 return NewRasterDirectReleaseProc(info, pixels, rowBytes, NULL, NULL, props)
; |
168 } | 183 } |
169 | 184 |
170 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 185 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
rops) { |
171 if (!SkSurface_Raster::Valid(info)) { | 186 if (!SkSurface_Raster::Valid(info)) { |
172 return NULL; | 187 return NULL; |
173 } | 188 } |
174 | 189 |
175 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); | 190 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, NULL)); |
176 if (NULL == pr.get()) { | 191 if (NULL == pr.get()) { |
177 return NULL; | 192 return NULL; |
178 } | 193 } |
179 return SkNEW_ARGS(SkSurface_Raster, (pr, props)); | 194 return SkNEW_ARGS(SkSurface_Raster, (pr, props)); |
180 } | 195 } |
OLD | NEW |