reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImage_Base.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 11 | #include "SkColorTable.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 13 | #include "SkImagePriv.h" |
| 14 | #include "SkPixelRef.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 15 | #include "SkSurface.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 16 | |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
| 19 | #include "SkGr.h" |
| 20 | #include "SkGrPriv.h" |
| 21 | #endif |
| 22 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 23 | class SkImage_Raster : public SkImage_Base { |
| 24 | public: |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 25 | static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable, |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 26 | size_t* minSize) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 27 | const int maxDimension = SK_MaxS32 >> 2; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 28 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 29 | if (info.width() <= 0 || info.height() <= 0) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 30 | return false; |
| 31 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 32 | if (info.width() > maxDimension || info.height() > maxDimension) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 33 | return false; |
| 34 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 35 | if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 36 | return false; |
| 37 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 38 | if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 42 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 45 | |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 46 | const bool needsCT = kIndex_8_SkColorType == info.colorType(); |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 47 | if (needsCT != hasColorTable) { |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 48 | return false; |
| 49 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 50 | |
scroggo | db7dc09 | 2015-12-18 06:56:01 -0800 | [diff] [blame] | 51 | if (rowBytes < info.minRowBytes()) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 54 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 55 | size_t size = info.getSafeSize(rowBytes); |
| 56 | if (0 == size) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 59 | |
| 60 | if (minSize) { |
| 61 | *minSize = size; |
| 62 | } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 66 | SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, SkColorTable*); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 67 | virtual ~SkImage_Raster(); |
| 68 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 69 | bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override; |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 70 | bool onPeekPixels(SkPixmap*) const override; |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 71 | SkData* onRefEncoded(GrContext*) const override; |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 72 | bool getROPixels(SkBitmap*, CachingHint) const override; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 73 | GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 74 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 75 | |
| 76 | // exposed for SkSurface_Raster via SkNewImageFromPixelRef |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 77 | SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size_t rowBytes); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 78 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 79 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 80 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 81 | bool isOpaque() const override; |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 82 | bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 83 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 84 | SkImage_Raster(const SkBitmap& bm) |
| 85 | : INHERITED(bm.width(), bm.height(), bm.getGenerationID()) |
reed | 02d91d1 | 2015-07-30 20:13:43 -0700 | [diff] [blame] | 86 | , fBitmap(bm) |
| 87 | { |
| 88 | if (bm.pixelRef()->isPreLocked()) { |
| 89 | // we only preemptively lock if there is no chance of triggering something expensive |
| 90 | // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
fmalita | 9a5d1ab | 2015-07-27 10:27:28 -0700 | [diff] [blame] | 91 | fBitmap.lockPixels(); |
| 92 | } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 93 | SkASSERT(fBitmap.isImmutable()); |
fmalita | 9a5d1ab | 2015-07-27 10:27:28 -0700 | [diff] [blame] | 94 | } |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 95 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 96 | bool onIsLazyGenerated() const override { |
| 97 | return fBitmap.pixelRef() && fBitmap.pixelRef()->isLazyGenerated(); |
| 98 | } |
| 99 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 100 | private: |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 101 | SkBitmap fBitmap; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 102 | |
| 103 | typedef SkImage_Base INHERITED; |
| 104 | }; |
| 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 108 | static void release_data(void* addr, void* context) { |
| 109 | SkData* data = static_cast<SkData*>(context); |
| 110 | data->unref(); |
| 111 | } |
| 112 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 113 | SkImage_Raster::SkImage_Raster(const Info& info, sk_sp<SkData> data, size_t rowBytes, |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 114 | SkColorTable* ctable) |
| 115 | : INHERITED(info.width(), info.height(), kNeedNewImageUniqueID) |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 116 | { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 117 | void* addr = const_cast<void*>(data->data()); |
| 118 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 119 | fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data.release()); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 120 | fBitmap.setImmutable(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 121 | fBitmap.lockPixels(); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 122 | } |
| 123 | |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 124 | SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, const SkIPoint& pixelRefOrigin, |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 125 | size_t rowBytes) |
| 126 | : INHERITED(info.width(), info.height(), pr->getGenerationID()) |
commit-bot@chromium.org | f190178 | 2014-01-08 22:24:55 +0000 | [diff] [blame] | 127 | { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 128 | fBitmap.setInfo(info, rowBytes); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 129 | fBitmap.setPixelRef(pr, pixelRefOrigin); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 130 | fBitmap.lockPixels(); |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 131 | SkASSERT(fBitmap.isImmutable()); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | SkImage_Raster::~SkImage_Raster() {} |
| 135 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 136 | bool SkImage_Raster::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 137 | int srcX, int srcY, CachingHint) const { |
reed | b4f629c | 2014-12-25 13:55:08 -0800 | [diff] [blame] | 138 | SkBitmap shallowCopy(fBitmap); |
| 139 | return shallowCopy.readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 142 | bool SkImage_Raster::onPeekPixels(SkPixmap* pm) const { |
| 143 | return fBitmap.peekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 144 | } |
| 145 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 146 | SkData* SkImage_Raster::onRefEncoded(GrContext*) const { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 147 | SkPixelRef* pr = fBitmap.pixelRef(); |
| 148 | const SkImageInfo prInfo = pr->info(); |
| 149 | const SkImageInfo bmInfo = fBitmap.info(); |
| 150 | |
| 151 | // we only try if we (the image) cover the entire area of the pixelRef |
| 152 | if (prInfo.width() == bmInfo.width() && prInfo.height() == bmInfo.height()) { |
| 153 | return pr->refEncodedData(); |
| 154 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 155 | return nullptr; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 156 | } |
| 157 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 158 | bool SkImage_Raster::getROPixels(SkBitmap* dst, CachingHint) const { |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 159 | *dst = fBitmap; |
| 160 | return true; |
| 161 | } |
| 162 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 163 | GrTexture* SkImage_Raster::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 164 | #if SK_SUPPORT_GPU |
| 165 | if (!ctx) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 169 | return GrRefCachedBitmapTexture(ctx, fBitmap, params); |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 170 | #endif |
| 171 | |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 175 | sk_sp<SkImage> SkImage_Raster::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 176 | // TODO : could consider heurist of sharing pixels, if subset is pretty close to complete |
| 177 | |
| 178 | SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBitmap.alphaType()); |
| 179 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 180 | if (!surface) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | surface->getCanvas()->clear(0); |
| 184 | surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), |
| 185 | nullptr); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 186 | return sk_sp<SkImage>(surface->newImageSnapshot()); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 191 | sk_sp<SkImage> SkImage::MakeRasterCopy(const SkPixmap& pmap) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 192 | size_t size; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 193 | if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), |
| 194 | pmap.ctable() != nullptr, &size) || !pmap.addr()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 196 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 197 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 198 | // Here we actually make a copy of the caller's pixel data |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 199 | sk_sp<SkData> data(SkData::NewWithCopy(pmap.addr(), size)); |
| 200 | return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes(), pmap.ctable()); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 204 | sk_sp<SkImage> SkImage::MakeRasterData(const SkImageInfo& info, sk_sp<SkData> data, |
| 205 | size_t rowBytes) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 206 | size_t size; |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 207 | if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 208 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 209 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 210 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 211 | // did they give us enough data? |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 212 | if (data->size() < size) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 213 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 214 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 215 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 216 | SkColorTable* ctable = nullptr; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 217 | return sk_make_sp<SkImage_Raster>(info, std::move(data), rowBytes, ctable); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 218 | } |
| 219 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 220 | sk_sp<SkImage> SkImage::MakeFromRaster(const SkPixmap& pmap, RasterReleaseProc proc, |
| 221 | ReleaseContext ctx) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 222 | size_t size; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 223 | if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), false, &size) || !pmap.addr()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 224 | return nullptr; |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 225 | } |
| 226 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 227 | sk_sp<SkData> data(SkData::NewWithProc(pmap.addr(), size, proc, ctx)); |
| 228 | return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes(), pmap.ctable()); |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 229 | } |
| 230 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 231 | sk_sp<SkImage> SkMakeImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
| 232 | const SkIPoint& pixelRefOrigin, size_t rowBytes) { |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 233 | if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 234 | return nullptr; |
reed | 7983bf9 | 2015-01-23 04:24:16 -0800 | [diff] [blame] | 235 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 236 | return sk_make_sp<SkImage_Raster>(info, pr, pixelRefOrigin, rowBytes); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 237 | } |
| 238 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 239 | sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap& bm, ForceCopyMode forceCopy) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 240 | SkASSERT(nullptr == bm.getTexture()); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 241 | |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 242 | bool hasColorTable = false; |
| 243 | if (kIndex_8_SkColorType == bm.colorType()) { |
| 244 | SkAutoLockPixels autoLockPixels(bm); |
| 245 | hasColorTable = bm.getColorTable() != nullptr; |
| 246 | } |
| 247 | |
| 248 | if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, nullptr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 249 | return nullptr; |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 250 | } |
| 251 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 252 | sk_sp<SkImage> image; |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 253 | if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 254 | SkBitmap tmp(bm); |
| 255 | tmp.lockPixels(); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 256 | SkPixmap pmap; |
| 257 | if (tmp.getPixels() && tmp.peekPixels(&pmap)) { |
| 258 | image = SkImage::MakeRasterCopy(pmap); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 259 | } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 260 | } else { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 261 | image = sk_make_sp<SkImage_Raster>(bm); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 262 | } |
| 263 | return image; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 264 | } |
| 265 | |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 266 | const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { |
| 267 | return ((const SkImage_Raster*)image)->getPixelRef(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 268 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 269 | |
| 270 | bool SkImage_Raster::isOpaque() const { |
| 271 | return fBitmap.isOpaque(); |
| 272 | } |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 273 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 274 | bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 275 | if (kRO_LegacyBitmapMode == mode) { |
| 276 | // When we're a snapshot from a surface, our bitmap may not be marked immutable |
| 277 | // even though logically always we are, but in that case we can't physically share our |
| 278 | // pixelref since the caller might call setImmutable() themselves |
| 279 | // (thus changing our state). |
| 280 | if (fBitmap.isImmutable()) { |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 281 | bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 282 | bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); |
| 283 | return true; |
| 284 | } |
| 285 | } |
| 286 | return this->INHERITED::onAsLegacyBitmap(bitmap, mode); |
| 287 | } |