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" |
reed | 4c1abdc | 2016-08-05 14:50:19 -0700 | [diff] [blame] | 10 | #include "SkBitmapProcShader.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 12 | #include "SkColorTable.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 14 | #include "SkImagePriv.h" |
| 15 | #include "SkPixelRef.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 16 | #include "SkSurface.h" |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 17 | |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
reed | 0e7bddf | 2016-08-17 13:24:05 -0700 | [diff] [blame] | 20 | #include "GrImageIDTextureAdjuster.h" |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 21 | #include "SkGr.h" |
| 22 | #include "SkGrPriv.h" |
| 23 | #endif |
| 24 | |
halcanary | 8d3ad5a | 2016-03-24 09:18:14 -0700 | [diff] [blame] | 25 | // fixes https://bug.skia.org/5096 |
| 26 | static bool is_not_subset(const SkBitmap& bm) { |
| 27 | SkASSERT(bm.pixelRef()); |
| 28 | SkISize dim = bm.pixelRef()->info().dimensions(); |
| 29 | SkASSERT(dim != bm.dimensions() || bm.pixelRefOrigin().isZero()); |
| 30 | return dim == bm.dimensions(); |
| 31 | } |
| 32 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 33 | class SkImage_Raster : public SkImage_Base { |
| 34 | public: |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 35 | static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable, |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 36 | size_t* minSize) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 37 | const int maxDimension = SK_MaxS32 >> 2; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 38 | |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 39 | if (info.width() <= 0 || info.height() <= 0) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 40 | return false; |
| 41 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 42 | if (info.width() > maxDimension || info.height() > maxDimension) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 43 | return false; |
| 44 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 45 | if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 46 | return false; |
| 47 | } |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 48 | if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 52 | if (kUnknown_SkColorType == info.colorType()) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 55 | |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 56 | const bool needsCT = kIndex_8_SkColorType == info.colorType(); |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 57 | if (needsCT != hasColorTable) { |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | |
scroggo | db7dc09 | 2015-12-18 06:56:01 -0800 | [diff] [blame] | 61 | if (rowBytes < info.minRowBytes()) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 64 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 65 | size_t size = info.getSafeSize(rowBytes); |
| 66 | if (0 == size) { |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 69 | |
| 70 | if (minSize) { |
| 71 | *minSize = size; |
| 72 | } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 76 | SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, SkColorTable*); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 77 | virtual ~SkImage_Raster(); |
| 78 | |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 79 | SkImageInfo onImageInfo() const override { |
| 80 | return fBitmap.info(); |
| 81 | } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 82 | SkAlphaType onAlphaType() const override { |
| 83 | return fBitmap.alphaType(); |
| 84 | } |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 85 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 86 | 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] | 87 | bool onPeekPixels(SkPixmap*) const override; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 88 | const SkBitmap* onPeekBitmap() const override { return &fBitmap; } |
| 89 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 90 | SkData* onRefEncoded(GrContext*) const override; |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 91 | bool getROPixels(SkBitmap*, CachingHint) const override; |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 92 | GrTexture* asTextureRef(GrContext*, const GrTextureParams&, |
| 93 | SkSourceGammaTreatment) const override; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 94 | sk_sp<SkImage> onMakeSubset(const SkIRect&) const override; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 95 | |
| 96 | // exposed for SkSurface_Raster via SkNewImageFromPixelRef |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 97 | SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size_t rowBytes); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 98 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 99 | SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
| 100 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 101 | bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override; |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 102 | |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 103 | SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable = false) |
halcanary | 8d3ad5a | 2016-03-24 09:18:14 -0700 | [diff] [blame] | 104 | : INHERITED(bm.width(), bm.height(), |
| 105 | is_not_subset(bm) ? bm.getGenerationID() |
| 106 | : (uint32_t)kNeedNewImageUniqueID) |
reed | 02d91d1 | 2015-07-30 20:13:43 -0700 | [diff] [blame] | 107 | , fBitmap(bm) |
| 108 | { |
| 109 | if (bm.pixelRef()->isPreLocked()) { |
| 110 | // we only preemptively lock if there is no chance of triggering something expensive |
| 111 | // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. |
fmalita | 9a5d1ab | 2015-07-27 10:27:28 -0700 | [diff] [blame] | 112 | fBitmap.lockPixels(); |
| 113 | } |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 114 | SkASSERT(bitmapMayBeMutable || fBitmap.isImmutable()); |
fmalita | 9a5d1ab | 2015-07-27 10:27:28 -0700 | [diff] [blame] | 115 | } |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 116 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 117 | bool onIsLazyGenerated() const override { |
| 118 | return fBitmap.pixelRef() && fBitmap.pixelRef()->isLazyGenerated(); |
| 119 | } |
| 120 | |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 121 | #if SK_SUPPORT_GPU |
| 122 | sk_sp<GrTexture> refPinnedTexture(uint32_t* uniqueID) const override; |
| 123 | void onPinAsTexture(GrContext*) const override; |
| 124 | void onUnpinAsTexture(GrContext*) const override; |
| 125 | #endif |
| 126 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 127 | private: |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 128 | SkBitmap fBitmap; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 129 | |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 130 | #if SK_SUPPORT_GPU |
| 131 | mutable sk_sp<GrTexture> fPinnedTexture; |
| 132 | mutable int32_t fPinnedCount = 0; |
| 133 | mutable uint32_t fPinnedUniqueID = 0; |
| 134 | #endif |
| 135 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 136 | typedef SkImage_Base INHERITED; |
| 137 | }; |
| 138 | |
| 139 | /////////////////////////////////////////////////////////////////////////////// |
| 140 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 141 | static void release_data(void* addr, void* context) { |
| 142 | SkData* data = static_cast<SkData*>(context); |
| 143 | data->unref(); |
| 144 | } |
| 145 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 146 | 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] | 147 | SkColorTable* ctable) |
| 148 | : INHERITED(info.width(), info.height(), kNeedNewImageUniqueID) |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 149 | { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 150 | void* addr = const_cast<void*>(data->data()); |
| 151 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 152 | fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data.release()); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 153 | fBitmap.setImmutable(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 154 | fBitmap.lockPixels(); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 155 | } |
| 156 | |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 157 | SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, const SkIPoint& pixelRefOrigin, |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 158 | size_t rowBytes) |
| 159 | : INHERITED(info.width(), info.height(), pr->getGenerationID()) |
commit-bot@chromium.org | f190178 | 2014-01-08 22:24:55 +0000 | [diff] [blame] | 160 | { |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 161 | fBitmap.setInfo(info, rowBytes); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 162 | fBitmap.setPixelRef(pr, pixelRefOrigin); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 163 | fBitmap.lockPixels(); |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 164 | SkASSERT(fBitmap.isImmutable()); |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 165 | } |
| 166 | |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 167 | SkImage_Raster::~SkImage_Raster() { |
| 168 | #if SK_SUPPORT_GPU |
| 169 | SkASSERT(nullptr == fPinnedTexture.get()); // want the caller to have manually unpinned |
| 170 | #endif |
| 171 | } |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 172 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 173 | bool SkImage_Raster::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 174 | int srcX, int srcY, CachingHint) const { |
reed | b4f629c | 2014-12-25 13:55:08 -0800 | [diff] [blame] | 175 | SkBitmap shallowCopy(fBitmap); |
| 176 | return shallowCopy.readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 177 | } |
| 178 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 179 | bool SkImage_Raster::onPeekPixels(SkPixmap* pm) const { |
| 180 | return fBitmap.peekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 181 | } |
| 182 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 183 | SkData* SkImage_Raster::onRefEncoded(GrContext*) const { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 184 | SkPixelRef* pr = fBitmap.pixelRef(); |
| 185 | const SkImageInfo prInfo = pr->info(); |
| 186 | const SkImageInfo bmInfo = fBitmap.info(); |
| 187 | |
| 188 | // we only try if we (the image) cover the entire area of the pixelRef |
| 189 | if (prInfo.width() == bmInfo.width() && prInfo.height() == bmInfo.height()) { |
| 190 | return pr->refEncodedData(); |
| 191 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | return nullptr; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 193 | } |
| 194 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 195 | bool SkImage_Raster::getROPixels(SkBitmap* dst, CachingHint) const { |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 196 | *dst = fBitmap; |
| 197 | return true; |
| 198 | } |
| 199 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 200 | GrTexture* SkImage_Raster::asTextureRef(GrContext* ctx, const GrTextureParams& params, |
| 201 | SkSourceGammaTreatment gammaTreatment) const { |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 202 | #if SK_SUPPORT_GPU |
| 203 | if (!ctx) { |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 207 | uint32_t uniqueID; |
| 208 | sk_sp<GrTexture> tex = this->refPinnedTexture(&uniqueID); |
| 209 | if (tex) { |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame] | 210 | GrTextureAdjuster adjuster(fPinnedTexture.get(), fBitmap.alphaType(), fBitmap.bounds(), |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 211 | fPinnedUniqueID, fBitmap.colorSpace()); |
| 212 | return adjuster.refTextureSafeForParams(params, gammaTreatment, nullptr); |
| 213 | } |
| 214 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 215 | return GrRefCachedBitmapTexture(ctx, fBitmap, params, gammaTreatment); |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 216 | #endif |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 217 | |
reed | 746f31f | 2015-09-16 12:53:29 -0700 | [diff] [blame] | 218 | return nullptr; |
| 219 | } |
| 220 | |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 221 | #if SK_SUPPORT_GPU |
| 222 | |
| 223 | sk_sp<GrTexture> SkImage_Raster::refPinnedTexture(uint32_t* uniqueID) const { |
| 224 | if (fPinnedTexture) { |
| 225 | SkASSERT(fPinnedCount > 0); |
| 226 | SkASSERT(fPinnedUniqueID != 0); |
| 227 | *uniqueID = fPinnedUniqueID; |
| 228 | return fPinnedTexture; |
| 229 | } |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
| 233 | void SkImage_Raster::onPinAsTexture(GrContext* ctx) const { |
| 234 | if (fPinnedTexture) { |
| 235 | SkASSERT(fPinnedCount > 0); |
| 236 | SkASSERT(fPinnedUniqueID != 0); |
| 237 | SkASSERT(fPinnedTexture->getContext() == ctx); |
| 238 | } else { |
| 239 | SkASSERT(fPinnedCount == 0); |
| 240 | SkASSERT(fPinnedUniqueID == 0); |
| 241 | fPinnedTexture.reset(GrRefCachedBitmapTexture(ctx, fBitmap, |
| 242 | GrTextureParams::ClampNoFilter(), |
| 243 | SkSourceGammaTreatment::kRespect)); |
| 244 | fPinnedUniqueID = fBitmap.getGenerationID(); |
| 245 | } |
| 246 | // Note: we always increment, even if we failed to create the pinned texture |
| 247 | ++fPinnedCount; |
| 248 | } |
| 249 | |
| 250 | void SkImage_Raster::onUnpinAsTexture(GrContext* ctx) const { |
| 251 | // Note: we always decrement, even if fPinnedTexture is null |
| 252 | SkASSERT(fPinnedCount > 0); |
| 253 | SkASSERT(fPinnedUniqueID != 0); |
| 254 | if (fPinnedTexture) { |
| 255 | SkASSERT(fPinnedTexture->getContext() == ctx); |
| 256 | } |
| 257 | |
| 258 | if (0 == --fPinnedCount) { |
| 259 | fPinnedTexture.reset(nullptr); |
| 260 | fPinnedUniqueID = 0; |
| 261 | } |
| 262 | } |
| 263 | #endif |
| 264 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 265 | sk_sp<SkImage> SkImage_Raster::onMakeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 266 | // TODO : could consider heurist of sharing pixels, if subset is pretty close to complete |
| 267 | |
| 268 | SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBitmap.alphaType()); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 269 | auto surface(SkSurface::MakeRaster(info)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 270 | if (!surface) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | surface->getCanvas()->clear(0); |
| 274 | surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), |
| 275 | nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 276 | return surface->makeImageSnapshot(); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 277 | } |
| 278 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 281 | sk_sp<SkImage> SkImage::MakeRasterCopy(const SkPixmap& pmap) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 282 | size_t size; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 283 | if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), |
| 284 | pmap.ctable() != nullptr, &size) || !pmap.addr()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 285 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 286 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 287 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 288 | // Here we actually make a copy of the caller's pixel data |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 289 | sk_sp<SkData> data(SkData::MakeWithCopy(pmap.addr(), size)); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 290 | 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] | 291 | } |
| 292 | |
| 293 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 294 | sk_sp<SkImage> SkImage::MakeRasterData(const SkImageInfo& info, sk_sp<SkData> data, |
| 295 | size_t rowBytes) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 296 | size_t size; |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 297 | if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 299 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 300 | |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 301 | // did they give us enough data? |
reed@google.com | 999da9c | 2014-02-06 13:43:07 +0000 | [diff] [blame] | 302 | if (data->size() < size) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 303 | return nullptr; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 304 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 305 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 306 | SkColorTable* ctable = nullptr; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 307 | 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] | 308 | } |
| 309 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 310 | sk_sp<SkImage> SkImage::MakeFromRaster(const SkPixmap& pmap, RasterReleaseProc proc, |
| 311 | ReleaseContext ctx) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 312 | size_t size; |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 313 | if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), false, &size) || !pmap.addr()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | return nullptr; |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 315 | } |
| 316 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 317 | sk_sp<SkData> data(SkData::MakeWithProc(pmap.addr(), size, proc, ctx)); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 318 | 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] | 319 | } |
| 320 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 321 | sk_sp<SkImage> SkMakeImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
| 322 | const SkIPoint& pixelRefOrigin, size_t rowBytes) { |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 323 | if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 324 | return nullptr; |
reed | 7983bf9 | 2015-01-23 04:24:16 -0800 | [diff] [blame] | 325 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 326 | return sk_make_sp<SkImage_Raster>(info, pr, pixelRefOrigin, rowBytes); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 327 | } |
| 328 | |
reed | 4c1abdc | 2016-08-05 14:50:19 -0700 | [diff] [blame] | 329 | sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap& bm, SkCopyPixelsMode cpm, |
| 330 | SkTBlitterAllocator* allocator) { |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 331 | bool hasColorTable = false; |
| 332 | if (kIndex_8_SkColorType == bm.colorType()) { |
| 333 | SkAutoLockPixels autoLockPixels(bm); |
| 334 | hasColorTable = bm.getColorTable() != nullptr; |
| 335 | } |
| 336 | |
| 337 | if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, nullptr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 338 | return nullptr; |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 339 | } |
| 340 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 341 | sk_sp<SkImage> image; |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 342 | if (kAlways_SkCopyPixelsMode == cpm || (!bm.isImmutable() && kNever_SkCopyPixelsMode != cpm)) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 343 | SkBitmap tmp(bm); |
| 344 | tmp.lockPixels(); |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 345 | SkPixmap pmap; |
| 346 | if (tmp.getPixels() && tmp.peekPixels(&pmap)) { |
| 347 | image = SkImage::MakeRasterCopy(pmap); |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 348 | } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 349 | } else { |
reed | 4c1abdc | 2016-08-05 14:50:19 -0700 | [diff] [blame] | 350 | if (allocator) { |
| 351 | image.reset(allocator->createT<SkImage_Raster>(bm, kNever_SkCopyPixelsMode == cpm)); |
| 352 | image.get()->ref(); // account for the allocator being an owner |
| 353 | } else { |
| 354 | image = sk_make_sp<SkImage_Raster>(bm, kNever_SkCopyPixelsMode == cpm); |
| 355 | } |
kkinnunen | 73953e7 | 2015-02-23 22:12:12 -0800 | [diff] [blame] | 356 | } |
| 357 | return image; |
reed@google.com | 58b21ec | 2012-07-30 18:20:12 +0000 | [diff] [blame] | 358 | } |
| 359 | |
piotaixr | 6515175 | 2014-10-16 11:58:39 -0700 | [diff] [blame] | 360 | const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) { |
| 361 | return ((const SkImage_Raster*)image)->getPixelRef(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 362 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 363 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 364 | bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 365 | if (kRO_LegacyBitmapMode == mode) { |
| 366 | // When we're a snapshot from a surface, our bitmap may not be marked immutable |
| 367 | // even though logically always we are, but in that case we can't physically share our |
| 368 | // pixelref since the caller might call setImmutable() themselves |
| 369 | // (thus changing our state). |
| 370 | if (fBitmap.isImmutable()) { |
fmalita | ea561bf | 2015-07-09 17:10:31 -0700 | [diff] [blame] | 371 | bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes()); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 372 | bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin()); |
| 373 | return true; |
| 374 | } |
| 375 | } |
| 376 | return this->INHERITED::onAsLegacyBitmap(bitmap, mode); |
| 377 | } |