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 sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override; |
27 SkImage* onNewImageSnapshot(SkBudgeted, ForceCopyMode) override; | 27 sk_sp<SkImage> onNewImageSnapshot(SkBudgeted, 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 const SkImageInfo& info = pr->info(); | 102 const SkImageInfo& info = pr->info(); |
103 | 103 |
104 fBitmap.setInfo(info, pr->rowBytes()); | 104 fBitmap.setInfo(info, pr->rowBytes()); |
105 fBitmap.setPixelRef(pr); | 105 fBitmap.setPixelRef(pr); |
106 fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs w
ill match | 106 fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs w
ill match |
107 fWeOwnThePixels = true; | 107 fWeOwnThePixels = true; |
108 } | 108 } |
109 | 109 |
110 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->p
rops()); } | 110 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->p
rops()); } |
111 | 111 |
112 SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) { | 112 sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
113 return SkSurface::NewRaster(info, &this->props()); | 113 return SkSurface::MakeRaster(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(SkBudgeted, ForceCopyMode forceCop
yMode) { | 121 sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(SkBudgeted, ForceCopyMode fo
rceCopyMode) { |
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 { | 128 } else { |
129 forceCopyMode = kYes_ForceCopyMode; | 129 forceCopyMode = kYes_ForceCopyMode; |
130 } | 130 } |
131 | 131 |
132 // 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. |
133 // Lock the shared pixel ref to ensure peekPixels() is usable. | 133 // Lock the shared pixel ref to ensure peekPixels() is usable. |
134 return SkMakeImageFromRasterBitmap(fBitmap, forceCopyMode).release(); | 134 return SkMakeImageFromRasterBitmap(fBitmap, forceCopyMode); |
135 } | 135 } |
136 | 136 |
137 void SkSurface_Raster::onRestoreBackingMutability() { | 137 void SkSurface_Raster::onRestoreBackingMutability() { |
138 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. | 138 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. |
139 if (SkPixelRef* pr = fBitmap.pixelRef()) { | 139 if (SkPixelRef* pr = fBitmap.pixelRef()) { |
140 pr->restoreMutability(); | 140 pr->restoreMutability(); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { | 144 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
(...skipping 17 matching lines...) Expand all Loading... |
162 // Now fBitmap is a deep copy of itself (and therefore different from | 162 // Now fBitmap is a deep copy of itself (and therefore different from |
163 // what is being used by the image. Next we update the canvas to use | 163 // what is being used by the image. Next we update the canvas to use |
164 // this as its backend, so we can't modify the image's pixels anymore. | 164 // this as its backend, so we can't modify the image's pixels anymore. |
165 SkASSERT(this->getCachedCanvas()); | 165 SkASSERT(this->getCachedCanvas()); |
166 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
ce(fBitmap); | 166 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurfa
ce(fBitmap); |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 /////////////////////////////////////////////////////////////////////////////// | 170 /////////////////////////////////////////////////////////////////////////////// |
171 | 171 |
172 SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void*
pixels, size_t rb, | 172 sk_sp<SkSurface> SkSurface::MakeRasterDirectReleaseProc(const SkImageInfo& info,
void* pixels, |
173 void (*releaseProc)(void* pixel
s, void* context), | 173 size_t rb, void (*releaseProc)(void* pixels, void* context), void* conte
xt, |
174 void* context, const SkSurfaceP
rops* props) { | 174 const SkSurfaceProps* props) { |
175 if (nullptr == releaseProc) { | 175 if (nullptr == releaseProc) { |
176 context = nullptr; | 176 context = nullptr; |
177 } | 177 } |
178 if (!SkSurface_Raster::Valid(info, rb)) { | 178 if (!SkSurface_Raster::Valid(info, rb)) { |
179 return nullptr; | 179 return nullptr; |
180 } | 180 } |
181 if (nullptr == pixels) { | 181 if (nullptr == pixels) { |
182 return nullptr; | 182 return nullptr; |
183 } | 183 } |
184 | 184 |
185 return new SkSurface_Raster(info, pixels, rb, releaseProc, context, props); | 185 return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context,
props); |
186 } | 186 } |
187 | 187 |
188 SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, siz
e_t rowBytes, | 188 sk_sp<SkSurface> SkSurface::MakeRasterDirect(const SkImageInfo& info, void* pixe
ls, size_t rowBytes, |
189 const SkSurfaceProps* props) { | 189 const SkSurfaceProps* props) { |
190 return NewRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr,
props); | 190 return MakeRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr,
props); |
191 } | 191 } |
192 | 192 |
193 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, size_t rowBytes, | 193 sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes, |
194 const SkSurfaceProps* props) { | 194 const SkSurfaceProps* props) { |
195 if (!SkSurface_Raster::Valid(info)) { | 195 if (!SkSurface_Raster::Valid(info)) { |
196 return nullptr; | 196 return nullptr; |
197 } | 197 } |
198 | 198 |
199 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, null
ptr)); | 199 SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, null
ptr)); |
200 if (nullptr == pr.get()) { | 200 if (nullptr == pr.get()) { |
201 return nullptr; | 201 return nullptr; |
202 } | 202 } |
203 if (rowBytes) { | 203 if (rowBytes) { |
204 SkASSERT(pr->rowBytes() == rowBytes); | 204 SkASSERT(pr->rowBytes() == rowBytes); |
205 } | 205 } |
206 return new SkSurface_Raster(pr, props); | 206 return sk_make_sp<SkSurface_Raster>(pr, props); |
207 } | 207 } |
208 | 208 |
209 SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* p
rops) { | 209 sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, const SkSurfaceP
rops* props) { |
210 return NewRaster(info, 0, props); | 210 return MakeRaster(info, 0, props); |
211 } | 211 } |
OLD | NEW |