reed@google.com | 6997ebb | 2012-07-30 19:50:31 +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 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 9 | #include "SkBitmapCache.h" |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 13 | #include "SkImageFilter.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 14 | #include "SkImageFilterCache.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 15 | #include "SkImageGenerator.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 16 | #include "SkImagePriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 17 | #include "SkImageShader.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 18 | #include "SkImage_Base.h" |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 19 | #include "SkNextID.h" |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 20 | #include "SkPicture.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 21 | #include "SkPixelRef.h" |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 22 | #include "SkPixelSerializer.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 23 | #include "SkReadPixelsRec.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 24 | #include "SkSpecialImage.h" |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 25 | #include "SkString.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 26 | #include "SkSurface.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 27 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 28 | #if SK_SUPPORT_GPU |
| 29 | #include "GrTexture.h" |
| 30 | #include "GrContext.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 31 | #include "SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 32 | #endif |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 33 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 34 | SkImage::SkImage(int width, int height, uint32_t uniqueID) |
| 35 | : fWidth(width) |
| 36 | , fHeight(height) |
| 37 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 38 | { |
| 39 | SkASSERT(width > 0); |
| 40 | SkASSERT(height > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 43 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 44 | SkPixmap tmp; |
| 45 | if (!pm) { |
| 46 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 47 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 48 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 49 | } |
| 50 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 51 | #ifdef SK_SUPPORT_LEGACY_PEEKPIXELS_PARMS |
| 52 | const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| 53 | SkPixmap pm; |
| 54 | if (this->peekPixels(&pm)) { |
| 55 | if (info) { |
| 56 | *info = pm.info(); |
| 57 | } |
| 58 | if (rowBytes) { |
| 59 | *rowBytes = pm.rowBytes(); |
| 60 | } |
| 61 | return pm.addr(); |
| 62 | } |
| 63 | return nullptr; |
| 64 | } |
| 65 | #endif |
| 66 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 67 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 68 | int srcX, int srcY, CachingHint chint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 69 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 70 | if (!rec.trim(this->width(), this->height())) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 73 | return as_IB(this)->onReadPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, chint); |
| 74 | } |
| 75 | |
| 76 | bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 77 | if (this->width() == dst.width() && this->height() == dst.height()) { |
| 78 | return this->readPixels(dst, 0, 0, chint); |
| 79 | } |
| 80 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 81 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 82 | // can scale more efficiently) we should take advantage of it here. |
| 83 | // |
| 84 | SkBitmap bm; |
| 85 | if (as_IB(this)->getROPixels(&bm, chint)) { |
| 86 | bm.lockPixels(); |
| 87 | SkPixmap pmap; |
| 88 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 89 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 90 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 91 | // |
| 92 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality); |
| 93 | } |
| 94 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 95 | } |
| 96 | |
reed | d6b3031 | 2015-09-02 07:06:23 -0700 | [diff] [blame] | 97 | void SkImage::preroll(GrContext* ctx) const { |
reed | efd50da | 2015-09-14 11:17:23 -0700 | [diff] [blame] | 98 | // For now, and to maintain parity w/ previous pixelref behavior, we just force the image |
| 99 | // to produce a cached raster-bitmap form, so that drawing to a raster canvas should be fast. |
| 100 | // |
| 101 | SkBitmap bm; |
| 102 | if (as_IB(this)->getROPixels(&bm)) { |
| 103 | bm.lockPixels(); |
| 104 | bm.unlockPixels(); |
| 105 | } |
reed | d6b3031 | 2015-09-02 07:06:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 108 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 109 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 110 | SkAlphaType SkImage::alphaType() const { |
| 111 | return as_IB(this)->onAlphaType(); |
| 112 | } |
| 113 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 114 | sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 115 | const SkMatrix* localMatrix) const { |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 116 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 117 | } |
| 118 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 119 | #ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR |
| 120 | SkShader* SkImage::newShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 121 | const SkMatrix* localMatrix) const { |
| 122 | return this->makeShader(tileX, tileY, localMatrix).release(); |
| 123 | } |
| 124 | #endif |
| 125 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 126 | SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| 127 | SkBitmap bm; |
| 128 | if (as_IB(this)->getROPixels(&bm)) { |
| 129 | return SkImageEncoder::EncodeData(bm, type, quality); |
| 130 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 131 | return nullptr; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 132 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 133 | |
halcanary | a2e0567 | 2015-12-09 03:56:02 -0800 | [diff] [blame] | 134 | SkData* SkImage::encode(SkPixelSerializer* serializer) const { |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 135 | SkAutoTUnref<SkPixelSerializer> defaultSerializer; |
| 136 | SkPixelSerializer* effectiveSerializer = serializer; |
| 137 | if (!effectiveSerializer) { |
| 138 | defaultSerializer.reset(SkImageEncoder::CreatePixelSerializer()); |
| 139 | SkASSERT(defaultSerializer.get()); |
| 140 | effectiveSerializer = defaultSerializer.get(); |
| 141 | } |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 142 | sk_sp<SkData> encoded(this->refEncoded()); |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 143 | if (encoded && effectiveSerializer->useEncodedData(encoded->data(), encoded->size())) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 144 | return encoded.release(); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | SkBitmap bm; |
| 148 | SkAutoPixmapUnlock apu; |
| 149 | if (as_IB(this)->getROPixels(&bm) && bm.requestLock(&apu)) { |
halcanary | 6b28017 | 2015-12-07 12:42:24 -0800 | [diff] [blame] | 150 | return effectiveSerializer->encode(apu.pixmap()); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 156 | SkData* SkImage::refEncoded() const { |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 157 | GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx? |
| 158 | return as_IB(this)->onRefEncoded(ctx); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 159 | } |
| 160 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 161 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | if (nullptr == encoded || 0 == encoded->size()) { |
| 163 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 164 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 165 | SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded.get()); |
| 166 | return SkImage::MakeFromGenerator(generator, subset); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 167 | } |
| 168 | |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 169 | const char* SkImage::toString(SkString* str) const { |
| 170 | str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), |
| 171 | this->isOpaque() ? "opaque" : ""); |
| 172 | return str->c_str(); |
| 173 | } |
| 174 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 175 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 176 | if (subset.isEmpty()) { |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 181 | if (!bounds.contains(subset)) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
| 185 | // optimization : return self if the subset == our bounds |
| 186 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 187 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 188 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 189 | return as_IB(this)->onMakeSubset(subset); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 190 | } |
| 191 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 192 | #if SK_SUPPORT_GPU |
| 193 | |
| 194 | GrTexture* SkImage::getTexture() const { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 195 | return as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 196 | } |
| 197 | |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 198 | bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekTexture()); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 199 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 200 | GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 201 | GrTexture* texture = as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 202 | if (texture) { |
| 203 | GrContext* context = texture->getContext(); |
| 204 | if (context) { |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 205 | if (flushPendingGrContextIO) { |
| 206 | context->prepareSurfaceForExternalIO(texture); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | return texture->getTextureHandle(); |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #else |
| 215 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 216 | GrTexture* SkImage::getTexture() const { return nullptr; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 217 | |
| 218 | bool SkImage::isTextureBacked() const { return false; } |
| 219 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 220 | GrBackendObject SkImage::getTextureHandle(bool) const { return 0; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 221 | |
| 222 | #endif |
| 223 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 224 | /////////////////////////////////////////////////////////////////////////////// |
| 225 | |
| 226 | static bool raster_canvas_supports(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 227 | switch (info.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 228 | case kN32_SkColorType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 229 | return kUnpremul_SkAlphaType != info.alphaType(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 230 | case kRGB_565_SkColorType: |
| 231 | return true; |
| 232 | case kAlpha_8_SkColorType: |
| 233 | return true; |
| 234 | default: |
| 235 | break; |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 240 | SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 241 | : INHERITED(width, height, uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 242 | , fAddedToCache(false) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 243 | {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 244 | |
| 245 | SkImage_Base::~SkImage_Base() { |
| 246 | if (fAddedToCache.load()) { |
| 247 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 248 | } |
| 249 | } |
| 250 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 251 | bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 252 | int srcX, int srcY, CachingHint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 253 | if (!raster_canvas_supports(dstInfo)) { |
| 254 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 255 | } |
| 256 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 257 | SkBitmap bm; |
| 258 | bm.installPixels(dstInfo, dstPixels, dstRowBytes); |
| 259 | SkCanvas canvas(bm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 260 | |
| 261 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 262 | paint.setBlendMode(SkBlendMode::kSrc); |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 263 | canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 264 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 265 | return true; |
| 266 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 267 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 268 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 269 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 270 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 271 | return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 272 | } |
| 273 | |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 274 | #if SK_SUPPORT_GPU |
| 275 | #include "GrTextureToYUVPlanes.h" |
| 276 | #endif |
| 277 | |
| 278 | #include "SkRGBAToYUV.h" |
| 279 | |
| 280 | bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3], |
Brian Salomon | de3a726 | 2016-02-03 17:09:00 -0500 | [diff] [blame] | 281 | const size_t rowBytes[3], SkYUVColorSpace colorSpace) const { |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 282 | #if SK_SUPPORT_GPU |
| 283 | if (GrTexture* texture = as_IB(this)->peekTexture()) { |
| 284 | if (GrTextureToYUVPlanes(texture, sizes, planes, rowBytes, colorSpace)) { |
| 285 | return true; |
| 286 | } |
| 287 | } |
| 288 | #endif |
| 289 | return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace); |
| 290 | } |
| 291 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 292 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 293 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 294 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 295 | SkPixelRef* pr = bm.pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 296 | if (nullptr == pr) { |
| 297 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 298 | } |
| 299 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 300 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 301 | } |
| 302 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 303 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 304 | return as_IB(this)->onAsLegacyBitmap(bitmap, mode); |
| 305 | } |
| 306 | |
| 307 | bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 308 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 309 | // Subclasses that want to be more optimal should override. |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 310 | SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType) |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 311 | .makeAlphaType(this->alphaType()); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 312 | if (!bitmap->tryAllocPixels(info)) { |
| 313 | return false; |
| 314 | } |
| 315 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 316 | bitmap->reset(); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | if (kRO_LegacyBitmapMode == mode) { |
| 321 | bitmap->setImmutable(); |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 326 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
| 327 | const SkMatrix* matrix, const SkPaint* paint) { |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 328 | if (!picture) { |
| 329 | return nullptr; |
| 330 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 331 | return MakeFromGenerator(SkImageGenerator::NewFromPicture(dimensions, picture.get(), |
| 332 | matrix, paint)); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 333 | } |
| 334 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 335 | sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 336 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 337 | SkIPoint* offset) const { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 338 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 339 | return nullptr; |
| 340 | } |
| 341 | sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage( |
| 342 | subset, sk_ref_sp(const_cast<SkImage*>(this))); |
| 343 | if (!srcSpecialImage) { |
| 344 | return nullptr; |
| 345 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 346 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 347 | SkAutoTUnref<SkImageFilterCache> cache( |
| 348 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 349 | SkImageFilter::OutputProperties outputProperties(as_IB(this)->onImageInfo().colorSpace()); |
| 350 | SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties); |
| 351 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 352 | sk_sp<SkSpecialImage> result = |
| 353 | filter->filterImage(srcSpecialImage.get(), context, offset); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 354 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 355 | if (!result) { |
| 356 | return nullptr; |
| 357 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 358 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 359 | SkIRect fullSize = SkIRect::MakeWH(result->width(), result->height()); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 360 | #if SK_SUPPORT_GPU |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 361 | if (result->isTextureBacked()) { |
| 362 | GrContext* context = result->getContext(); |
| 363 | sk_sp<GrTexture> texture = result->asTextureRef(context); |
| 364 | fullSize = SkIRect::MakeWH(texture->width(), texture->height()); |
| 365 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 366 | #endif |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 367 | *outSubset = SkIRect::MakeWH(result->width(), result->height()); |
| 368 | if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) { |
| 369 | return nullptr; |
| 370 | } |
| 371 | offset->fX += outSubset->x(); |
| 372 | offset->fY += outSubset->y(); |
| 373 | // This isn't really a "tight" subset, but includes any texture padding. |
| 374 | return result->makeTightSubset(fullSize); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 375 | } |
| 376 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 377 | bool SkImage::isLazyGenerated() const { |
| 378 | return as_IB(this)->onIsLazyGenerated(); |
| 379 | } |
| 380 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 381 | ////////////////////////////////////////////////////////////////////////////////////// |
| 382 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 383 | #if !SK_SUPPORT_GPU |
| 384 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 385 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 386 | return nullptr; |
| 387 | } |
| 388 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 389 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 390 | int mipLevelCount, SkBudgeted, SkSourceGammaTreatment) { |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 394 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 395 | sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 396 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 397 | } |
| 398 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 399 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&, |
| 400 | const DeferredTextureImageUsageParams[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 401 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame^] | 402 | SkColorSpace* dstColorSpace) const { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*, |
| 407 | SkBudgeted) { |
| 408 | return nullptr; |
| 409 | } |
| 410 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 411 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 412 | SkAlphaType, sk_sp<SkColorSpace>) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 413 | return nullptr; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 414 | } |
| 415 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 416 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 417 | const GrBackendObject yuvTextureHandles[3], |
| 418 | const SkISize yuvSizes[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 419 | GrSurfaceOrigin origin, |
| 420 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 421 | return nullptr; |
| 422 | } |
| 423 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 424 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext*) const { |
| 425 | return nullptr; |
| 426 | } |
| 427 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 428 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 429 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 430 | } |
| 431 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 432 | #endif |
| 433 | |
| 434 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 435 | |
| 436 | #ifdef SK_SUPPORT_LEGACY_IMAGEFACTORY |
| 437 | SkImage* SkImage::NewRasterCopy(const Info& info, const void* pixels, size_t rowBytes, |
| 438 | SkColorTable* ctable) { |
| 439 | return MakeRasterCopy(SkPixmap(info, pixels, rowBytes, ctable)).release(); |
| 440 | } |
| 441 | |
| 442 | SkImage* SkImage::NewRasterData(const Info& info, SkData* pixels, size_t rowBytes) { |
| 443 | return MakeRasterData(info, sk_ref_sp(pixels), rowBytes).release(); |
| 444 | } |
| 445 | |
| 446 | SkImage* SkImage::NewFromRaster(const Info& info, const void* pixels, size_t rowBytes, |
| 447 | RasterReleaseProc proc, ReleaseContext releasectx) { |
| 448 | return MakeFromRaster(SkPixmap(info, pixels, rowBytes), proc, releasectx).release(); |
| 449 | } |
| 450 | |
| 451 | SkImage* SkImage::NewFromBitmap(const SkBitmap& bm) { |
| 452 | return MakeFromBitmap(bm).release(); |
| 453 | } |
| 454 | |
| 455 | SkImage* SkImage::NewFromGenerator(SkImageGenerator* gen, const SkIRect* subset) { |
| 456 | return MakeFromGenerator(gen, subset).release(); |
| 457 | } |
| 458 | |
| 459 | SkImage* SkImage::NewFromEncoded(SkData* encoded, const SkIRect* subset) { |
| 460 | return MakeFromEncoded(sk_ref_sp(encoded), subset).release(); |
| 461 | } |
| 462 | |
| 463 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 464 | TextureReleaseProc proc, ReleaseContext releasectx) { |
| 465 | return MakeFromTexture(ctx, desc, at, proc, releasectx).release(); |
| 466 | } |
| 467 | |
| 468 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 469 | SkAlphaType at) { |
| 470 | return MakeFromAdoptedTexture(ctx, desc, at).release(); |
| 471 | } |
| 472 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 473 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 474 | const GrBackendObject yuvTextureHandles[3], |
| 475 | const SkISize yuvSizes[3], |
| 476 | GrSurfaceOrigin origin) { |
| 477 | return MakeFromYUVTexturesCopy(ctx, space, yuvTextureHandles, yuvSizes, origin).release(); |
| 478 | } |
| 479 | |
| 480 | SkImage* SkImage::NewFromPicture(const SkPicture* picture, const SkISize& dimensions, |
| 481 | const SkMatrix* matrix, const SkPaint* paint) { |
| 482 | return MakeFromPicture(sk_ref_sp(const_cast<SkPicture*>(picture)), dimensions, |
| 483 | matrix, paint).release(); |
| 484 | } |
| 485 | |
| 486 | SkImage* SkImage::NewTextureFromPixmap(GrContext* ctx, const SkPixmap& pmap, SkBudgeted budgeted) { |
| 487 | return MakeTextureFromPixmap(ctx, pmap, budgeted).release(); |
| 488 | } |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 489 | |
| 490 | SkImage* SkImage::NewFromDeferredTextureImageData(GrContext* ctx, const void* data, |
| 491 | SkBudgeted budgeted) { |
| 492 | return MakeFromDeferredTextureImageData(ctx, data, budgeted).release(); |
| 493 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 494 | #endif |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 495 | |
| 496 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 497 | int mipLevelCount, SkBudgeted) { |
| 498 | return nullptr; |
| 499 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 500 | |
| 501 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 502 | #include "SkImageDeserializer.h" |
| 503 | |
| 504 | sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) { |
| 505 | return SkImage::MakeFromEncoded(sk_ref_sp(data), subset); |
| 506 | } |
| 507 | sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length, |
| 508 | const SkIRect* subset) { |
| 509 | return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset); |
| 510 | } |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 511 | |
| 512 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 513 | |
| 514 | void SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) { |
| 515 | SkASSERT(image); |
| 516 | SkASSERT(ctx); |
| 517 | as_IB(image)->onPinAsTexture(ctx); |
| 518 | } |
| 519 | |
| 520 | void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) { |
| 521 | SkASSERT(image); |
| 522 | SkASSERT(ctx); |
| 523 | as_IB(image)->onUnpinAsTexture(ctx); |
| 524 | } |