Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: src/image/SkImage_Raster.cpp

Issue 1778393002: add Make variations to return SkImage by sk_sp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: roll in fmalitas comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkImage_Base.h" 8 #include "SkImage_Base.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 if (0 == size) { 56 if (0 == size) {
57 return false; 57 return false;
58 } 58 }
59 59
60 if (minSize) { 60 if (minSize) {
61 *minSize = size; 61 *minSize = size;
62 } 62 }
63 return true; 63 return true;
64 } 64 }
65 65
66 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, SkColorTable*); 66 SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, SkColorTable*);
67 virtual ~SkImage_Raster(); 67 virtual ~SkImage_Raster();
68 68
69 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, Cac hingHint) const override; 69 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, Cac hingHint) const override;
70 bool onPeekPixels(SkPixmap*) const override; 70 bool onPeekPixels(SkPixmap*) const override;
71 SkData* onRefEncoded(GrContext*) const override; 71 SkData* onRefEncoded(GrContext*) const override;
72 bool getROPixels(SkBitmap*, CachingHint) const override; 72 bool getROPixels(SkBitmap*, CachingHint) const override;
73 GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override; 73 GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override;
74 SkImage* onNewSubset(const SkIRect&) const override; 74 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
75 75
76 // exposed for SkSurface_Raster via SkNewImageFromPixelRef 76 // exposed for SkSurface_Raster via SkNewImageFromPixelRef
77 SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size _t rowBytes); 77 SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size _t rowBytes);
78 78
79 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } 79 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
80 80
81 bool isOpaque() const override; 81 bool isOpaque() const override;
82 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; 82 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override;
83 83
84 SkImage_Raster(const SkBitmap& bm) 84 SkImage_Raster(const SkBitmap& bm)
(...skipping 18 matching lines...) Expand all
103 typedef SkImage_Base INHERITED; 103 typedef SkImage_Base INHERITED;
104 }; 104 };
105 105
106 /////////////////////////////////////////////////////////////////////////////// 106 ///////////////////////////////////////////////////////////////////////////////
107 107
108 static void release_data(void* addr, void* context) { 108 static void release_data(void* addr, void* context) {
109 SkData* data = static_cast<SkData*>(context); 109 SkData* data = static_cast<SkData*>(context);
110 data->unref(); 110 data->unref();
111 } 111 }
112 112
113 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes, 113 SkImage_Raster::SkImage_Raster(const Info& info, sk_sp<SkData> data, size_t rowB ytes,
114 SkColorTable* ctable) 114 SkColorTable* ctable)
115 : INHERITED(info.width(), info.height(), kNeedNewImageUniqueID) 115 : INHERITED(info.width(), info.height(), kNeedNewImageUniqueID)
116 { 116 {
117 data->ref();
118 void* addr = const_cast<void*>(data->data()); 117 void* addr = const_cast<void*>(data->data());
119 118
120 fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data); 119 fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data.relea se());
121 fBitmap.setImmutable(); 120 fBitmap.setImmutable();
122 fBitmap.lockPixels(); 121 fBitmap.lockPixels();
123 } 122 }
124 123
125 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, const SkIPoint& pixelRefOrigin, 124 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, const SkIPoint& pixelRefOrigin,
126 size_t rowBytes) 125 size_t rowBytes)
127 : INHERITED(info.width(), info.height(), pr->getGenerationID()) 126 : INHERITED(info.width(), info.height(), pr->getGenerationID())
128 { 127 {
129 fBitmap.setInfo(info, rowBytes); 128 fBitmap.setInfo(info, rowBytes);
130 fBitmap.setPixelRef(pr, pixelRefOrigin); 129 fBitmap.setPixelRef(pr, pixelRefOrigin);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 if (!ctx) { 165 if (!ctx) {
167 return nullptr; 166 return nullptr;
168 } 167 }
169 168
170 return GrRefCachedBitmapTexture(ctx, fBitmap, params); 169 return GrRefCachedBitmapTexture(ctx, fBitmap, params);
171 #endif 170 #endif
172 171
173 return nullptr; 172 return nullptr;
174 } 173 }
175 174
176 SkImage* SkImage_Raster::onNewSubset(const SkIRect& subset) const { 175 sk_sp<SkImage> SkImage_Raster::onMakeSubset(const SkIRect& subset) const {
177 // TODO : could consider heurist of sharing pixels, if subset is pretty clos e to complete 176 // TODO : could consider heurist of sharing pixels, if subset is pretty clos e to complete
178 177
179 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBi tmap.alphaType()); 178 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBi tmap.alphaType());
180 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); 179 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
181 if (!surface) { 180 if (!surface) {
182 return nullptr; 181 return nullptr;
183 } 182 }
184 surface->getCanvas()->clear(0); 183 surface->getCanvas()->clear(0);
185 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()), 184 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToSca lar(-subset.y()),
186 nullptr); 185 nullptr);
187 return surface->newImageSnapshot(); 186 return sk_sp<SkImage>(surface->newImageSnapshot());
188 } 187 }
189 188
190 /////////////////////////////////////////////////////////////////////////////// 189 ///////////////////////////////////////////////////////////////////////////////
191 190
192 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes, 191 sk_sp<SkImage> SkImage::MakeRasterCopy(const SkPixmap& pmap) {
193 SkColorTable* ctable) {
194 size_t size; 192 size_t size;
195 if (!SkImage_Raster::ValidArgs(info, rowBytes, ctable != nullptr, &size) || !pixels) { 193 if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(),
194 pmap.ctable() != nullptr, &size) || !pmap.add r()) {
196 return nullptr; 195 return nullptr;
197 } 196 }
198 197
199 // Here we actually make a copy of the caller's pixel data 198 // Here we actually make a copy of the caller's pixel data
200 SkAutoDataUnref data(SkData::NewWithCopy(pixels, size)); 199 sk_sp<SkData> data(SkData::NewWithCopy(pmap.addr(), size));
201 return new SkImage_Raster(info, data, rowBytes, ctable); 200 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowByte s(), pmap.ctable());
202 } 201 }
203 202
204 203
205 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t ro wBytes) { 204 sk_sp<SkImage> SkImage::MakeRasterData(const SkImageInfo& info, sk_sp<SkData> da ta,
205 size_t rowBytes) {
206 size_t size; 206 size_t size;
207 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) { 207 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) {
208 return nullptr; 208 return nullptr;
209 } 209 }
210 210
211 // did they give us enough data? 211 // did they give us enough data?
212 if (data->size() < size) { 212 if (data->size() < size) {
213 return nullptr; 213 return nullptr;
214 } 214 }
215 215
216 SkColorTable* ctable = nullptr; 216 SkColorTable* ctable = nullptr;
217 return new SkImage_Raster(info, data, rowBytes, ctable); 217 return sk_make_sp<SkImage_Raster>(info, std::move(data), rowBytes, ctable);
218 } 218 }
219 219
220 SkImage* SkImage::NewFromRaster(const SkImageInfo& info, const void* pixels, siz e_t rowBytes, 220 sk_sp<SkImage> SkImage::MakeFromRaster(const SkPixmap& pmap, RasterReleaseProc p roc,
221 RasterReleaseProc proc, ReleaseContext ctx) { 221 ReleaseContext ctx) {
222 size_t size; 222 size_t size;
223 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !pixels) { 223 if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), false, &size) | | !pmap.addr()) {
224 return nullptr; 224 return nullptr;
225 } 225 }
226 226
227 SkColorTable* ctable = nullptr; 227 sk_sp<SkData> data(SkData::NewWithProc(pmap.addr(), size, proc, ctx));
228 SkAutoDataUnref data(SkData::NewWithProc(pixels, size, proc, ctx)); 228 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowByte s(), pmap.ctable());
229 return new SkImage_Raster(info, data, rowBytes, ctable);
230 } 229 }
231 230
232 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, 231 sk_sp<SkImage> SkMakeImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
233 const SkIPoint& pixelRefOrigin, size_t rowBytes) { 232 const SkIPoint& pixelRefOrigin, size_t ro wBytes) {
234 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) { 233 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) {
235 return nullptr; 234 return nullptr;
236 } 235 }
237 return new SkImage_Raster(info, pr, pixelRefOrigin, rowBytes); 236 return sk_make_sp<SkImage_Raster>(info, pr, pixelRefOrigin, rowBytes);
238 } 237 }
239 238
240 SkImage* SkNewImageFromRasterBitmap(const SkBitmap& bm, ForceCopyMode forceCopy) { 239 sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap& bm, ForceCopyMode for ceCopy) {
241 SkASSERT(nullptr == bm.getTexture()); 240 SkASSERT(nullptr == bm.getTexture());
242 241
243 bool hasColorTable = false; 242 bool hasColorTable = false;
244 if (kIndex_8_SkColorType == bm.colorType()) { 243 if (kIndex_8_SkColorType == bm.colorType()) {
245 SkAutoLockPixels autoLockPixels(bm); 244 SkAutoLockPixels autoLockPixels(bm);
246 hasColorTable = bm.getColorTable() != nullptr; 245 hasColorTable = bm.getColorTable() != nullptr;
247 } 246 }
248 247
249 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null ptr)) { 248 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, null ptr)) {
250 return nullptr; 249 return nullptr;
251 } 250 }
252 251
253 SkImage* image = nullptr; 252 sk_sp<SkImage> image;
254 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { 253 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) {
255 SkBitmap tmp(bm); 254 SkBitmap tmp(bm);
256 tmp.lockPixels(); 255 tmp.lockPixels();
257 if (tmp.getPixels()) { 256 SkPixmap pmap;
258 image = SkImage::NewRasterCopy(tmp.info(), tmp.getPixels(), tmp.rowB ytes(), 257 if (tmp.getPixels() && tmp.peekPixels(&pmap)) {
259 tmp.getColorTable()); 258 image = SkImage::MakeRasterCopy(pmap);
260 } 259 }
261 } else { 260 } else {
262 image = new SkImage_Raster(bm); 261 image = sk_make_sp<SkImage_Raster>(bm);
263 } 262 }
264 return image; 263 return image;
265 } 264 }
266 265
267 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { 266 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) {
268 return ((const SkImage_Raster*)image)->getPixelRef(); 267 return ((const SkImage_Raster*)image)->getPixelRef();
269 } 268 }
270 269
271 bool SkImage_Raster::isOpaque() const { 270 bool SkImage_Raster::isOpaque() const {
272 return fBitmap.isOpaque(); 271 return fBitmap.isOpaque();
273 } 272 }
274 273
275 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c onst { 274 bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) c onst {
276 if (kRO_LegacyBitmapMode == mode) { 275 if (kRO_LegacyBitmapMode == mode) {
277 // When we're a snapshot from a surface, our bitmap may not be marked im mutable 276 // When we're a snapshot from a surface, our bitmap may not be marked im mutable
278 // even though logically always we are, but in that case we can't physic ally share our 277 // even though logically always we are, but in that case we can't physic ally share our
279 // pixelref since the caller might call setImmutable() themselves 278 // pixelref since the caller might call setImmutable() themselves
280 // (thus changing our state). 279 // (thus changing our state).
281 if (fBitmap.isImmutable()) { 280 if (fBitmap.isImmutable()) {
282 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); 281 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
283 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); 282 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin());
284 return true; 283 return true;
285 } 284 }
286 } 285 }
287 return this->INHERITED::onAsLegacyBitmap(bitmap, mode); 286 return this->INHERITED::onAsLegacyBitmap(bitmap, mode);
288 } 287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698