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" |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 11 | #include "SkColorSpace_Base.h" |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 12 | #include "SkCrossContextImageData.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 13 | #include "SkData.h" |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 15 | #include "SkImageFilter.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 16 | #include "SkImageFilterCache.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 17 | #include "SkImageGenerator.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 18 | #include "SkImagePriv.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 19 | #include "SkImageShader.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 20 | #include "SkImage_Base.h" |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 21 | #include "SkNextID.h" |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 22 | #include "SkPicture.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 23 | #include "SkPixelRef.h" |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 24 | #include "SkPixelSerializer.h" |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 25 | #include "SkRGBAToYUV.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 26 | #include "SkReadPixelsRec.h" |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 27 | #include "SkSpecialImage.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 28 | #include "SkStream.h" |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 29 | #include "SkString.h" |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 30 | #include "SkSurface.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 31 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 32 | #if SK_SUPPORT_GPU |
| 33 | #include "GrTexture.h" |
| 34 | #include "GrContext.h" |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 35 | #include "SkImage_Gpu.h" |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 36 | #endif |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 37 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 38 | SkImage::SkImage(int width, int height, uint32_t uniqueID) |
| 39 | : fWidth(width) |
| 40 | , fHeight(height) |
| 41 | , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 42 | { |
| 43 | SkASSERT(width > 0); |
| 44 | SkASSERT(height > 0); |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 47 | bool SkImage::peekPixels(SkPixmap* pm) const { |
| 48 | SkPixmap tmp; |
| 49 | if (!pm) { |
| 50 | pm = &tmp; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 51 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 52 | return as_IB(this)->onPeekPixels(pm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 55 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 56 | int srcX, int srcY, CachingHint chint) const { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 57 | return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const { |
reed | 6868c3f | 2015-11-24 11:44:47 -0800 | [diff] [blame] | 61 | if (this->width() == dst.width() && this->height() == dst.height()) { |
| 62 | return this->readPixels(dst, 0, 0, chint); |
| 63 | } |
| 64 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 65 | // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself |
| 66 | // can scale more efficiently) we should take advantage of it here. |
| 67 | // |
| 68 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 69 | if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 70 | bm.lockPixels(); |
| 71 | SkPixmap pmap; |
| 72 | // Note: By calling the pixmap scaler, we never cache the final result, so the chint |
| 73 | // is (currently) only being applied to the getROPixels. If we get a request to |
| 74 | // also attempt to cache the final (scaled) result, we would add that logic here. |
| 75 | // |
| 76 | return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality); |
| 77 | } |
| 78 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 81 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 82 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 83 | SkAlphaType SkImage::alphaType() const { |
| 84 | return as_IB(this)->onAlphaType(); |
| 85 | } |
| 86 | |
Matt Sarett | 5b4599f | 2017-03-02 12:42:35 -0500 | [diff] [blame] | 87 | SkColorSpace* SkImage::colorSpace() const { |
| 88 | return as_IB(this)->onImageInfo().colorSpace(); |
| 89 | } |
| 90 | |
| 91 | sk_sp<SkColorSpace> SkImage::refColorSpace() const { |
| 92 | return as_IB(this)->onImageInfo().refColorSpace(); |
| 93 | } |
| 94 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 95 | sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 96 | const SkMatrix* localMatrix) const { |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 97 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 100 | SkData* SkImage::encode(SkEncodedImageFormat type, int quality) const { |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 101 | SkBitmap bm; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 102 | SkColorSpace* legacyColorSpace = nullptr; |
| 103 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 104 | SkDynamicMemoryWStream buf; |
| 105 | return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData().release() : nullptr; |
Leon Scroggins | 294c1c4 | 2016-11-08 14:29:46 +0000 | [diff] [blame] | 106 | } |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
halcanary | a2e0567 | 2015-12-09 03:56:02 -0800 | [diff] [blame] | 110 | SkData* SkImage::encode(SkPixelSerializer* serializer) const { |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 111 | sk_sp<SkData> encoded(this->refEncoded()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 112 | if (encoded && |
| 113 | (!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 114 | return encoded.release(); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | SkBitmap bm; |
| 118 | SkAutoPixmapUnlock apu; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 119 | SkColorSpace* legacyColorSpace = nullptr; |
| 120 | if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 121 | bm.requestLock(&apu)) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 122 | if (serializer) { |
| 123 | return serializer->encode(apu.pixmap()); |
| 124 | } else { |
| 125 | SkDynamicMemoryWStream buf; |
| 126 | return SkEncodeImage(&buf, apu.pixmap(), SkEncodedImageFormat::kPNG, 100) |
| 127 | ? buf.detachAsData().release() : nullptr; |
| 128 | } |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return nullptr; |
| 132 | } |
| 133 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 134 | SkData* SkImage::refEncoded() const { |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 135 | GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx? |
| 136 | return as_IB(this)->onRefEncoded(ctx); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 137 | } |
| 138 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 139 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | if (nullptr == encoded || 0 == encoded->size()) { |
| 141 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 142 | } |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 143 | return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(encoded), subset); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 144 | } |
| 145 | |
reed | f8d1874 | 2015-01-02 20:45:37 -0800 | [diff] [blame] | 146 | const char* SkImage::toString(SkString* str) const { |
| 147 | str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), |
| 148 | this->isOpaque() ? "opaque" : ""); |
| 149 | return str->c_str(); |
| 150 | } |
| 151 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 152 | sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const { |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 153 | if (subset.isEmpty()) { |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 158 | if (!bounds.contains(subset)) { |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | // optimization : return self if the subset == our bounds |
| 163 | if (bounds == subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 164 | return sk_ref_sp(const_cast<SkImage*>(this)); |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 165 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 166 | return as_IB(this)->onMakeSubset(subset); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 167 | } |
| 168 | |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 169 | #if SK_SUPPORT_GPU |
| 170 | |
| 171 | GrTexture* SkImage::getTexture() const { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 172 | return as_IB(this)->onGetTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 175 | bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekProxy()); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 176 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 177 | GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO, |
| 178 | GrSurfaceOrigin* origin) const { |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 179 | return as_IB(this)->onGetTextureHandle(flushPendingGrContextIO, origin); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #else |
| 183 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 184 | GrTexture* SkImage::getTexture() const { return nullptr; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 185 | |
| 186 | bool SkImage::isTextureBacked() const { return false; } |
| 187 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 188 | GrBackendObject SkImage::getTextureHandle(bool, GrSurfaceOrigin*) const { return 0; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 189 | |
| 190 | #endif |
| 191 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 194 | SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 195 | : INHERITED(width, height, uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 196 | , fAddedToCache(false) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 197 | {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 198 | |
| 199 | SkImage_Base::~SkImage_Base() { |
| 200 | if (fAddedToCache.load()) { |
| 201 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 202 | } |
| 203 | } |
| 204 | |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 205 | bool SkImage_Base::onReadYUV8Planes(const SkISize sizes[3], void* const planes[3], |
| 206 | const size_t rowBytes[3], SkYUVColorSpace colorSpace) const { |
| 207 | return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace); |
| 208 | } |
| 209 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 210 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 211 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 212 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 213 | return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 214 | } |
| 215 | |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 216 | bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3], |
Brian Salomon | de3a726 | 2016-02-03 17:09:00 -0500 | [diff] [blame] | 217 | const size_t rowBytes[3], SkYUVColorSpace colorSpace) const { |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 218 | return as_IB(this)->onReadYUV8Planes(sizes, planes, rowBytes, colorSpace); |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 219 | } |
| 220 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 221 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 222 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 223 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 224 | SkPixelRef* pr = bm.pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 225 | if (nullptr == pr) { |
| 226 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 227 | } |
| 228 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 229 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 230 | } |
| 231 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 232 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 233 | return as_IB(this)->onAsLegacyBitmap(bitmap, mode); |
| 234 | } |
| 235 | |
| 236 | bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 237 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 238 | // Subclasses that want to be more optimal should override. |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 239 | SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 240 | if (!bitmap->tryAllocPixels(info)) { |
| 241 | return false; |
| 242 | } |
| 243 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 244 | bitmap->reset(); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | if (kRO_LegacyBitmapMode == mode) { |
| 249 | bitmap->setImmutable(); |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 254 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 255 | const SkMatrix* matrix, const SkPaint* paint, |
| 256 | BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) { |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 257 | return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture), |
| 258 | matrix, paint, bitDepth, |
| 259 | std::move(colorSpace))); |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 260 | } |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 261 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 262 | sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 263 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 264 | SkIPoint* offset) const { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 265 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 266 | return nullptr; |
| 267 | } |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 268 | SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace(); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 269 | sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 270 | subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 271 | if (!srcSpecialImage) { |
| 272 | return nullptr; |
| 273 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 274 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 275 | sk_sp<SkImageFilterCache> cache( |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 276 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 277 | SkImageFilter::OutputProperties outputProperties(colorSpace); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 278 | SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties); |
| 279 | |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 280 | sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 281 | if (!result) { |
| 282 | return nullptr; |
| 283 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 284 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 285 | *outSubset = SkIRect::MakeWH(result->width(), result->height()); |
| 286 | if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) { |
| 287 | return nullptr; |
| 288 | } |
| 289 | offset->fX += outSubset->x(); |
| 290 | offset->fY += outSubset->y(); |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 291 | |
| 292 | // Note that here we're returning the special image's entire backing store, loose padding |
| 293 | // and all! |
| 294 | return result->asImage(); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 295 | } |
| 296 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 297 | bool SkImage::isLazyGenerated() const { |
| 298 | return as_IB(this)->onIsLazyGenerated(); |
| 299 | } |
| 300 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 301 | bool SkImage::isAlphaOnly() const { |
| 302 | return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType; |
| 303 | } |
| 304 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 305 | sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target, |
| 306 | SkTransferFunctionBehavior premulBehavior) const { |
| 307 | if (SkTransferFunctionBehavior::kRespect == premulBehavior) { |
| 308 | // TODO (msarett, brianosman): Implement this. |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 312 | SkColorSpaceTransferFn fn; |
| 313 | if (!target || !target->isNumericalTransferFn(&fn)) { |
| 314 | return nullptr; |
| 315 | } |
| 316 | |
Matt Sarett | 73879eb | 2017-03-22 10:07:50 -0400 | [diff] [blame] | 317 | // No need to create a new image if: |
| 318 | // (1) The color spaces are equal (nullptr is considered to be sRGB). |
| 319 | // (2) The color type is kAlpha8. |
Mike Klein | 919cc45 | 2017-03-18 15:36:52 +0000 | [diff] [blame] | 320 | if ((!this->colorSpace() && target->isSRGB()) || |
Matt Sarett | f388093 | 2017-03-24 10:06:03 -0400 | [diff] [blame] | 321 | SkColorSpace::Equals(this->colorSpace(), target.get()) || |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 322 | kAlpha_8_SkColorType == as_IB(this)->onImageInfo().colorType()) { |
| 323 | return sk_ref_sp(const_cast<SkImage*>(this)); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 326 | // TODO: We might consider making this a deferred conversion? |
| 327 | return as_IB(this)->onMakeColorSpace(std::move(target)); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 328 | } |
| 329 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 330 | ////////////////////////////////////////////////////////////////////////////////////// |
| 331 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 332 | #if !SK_SUPPORT_GPU |
| 333 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 334 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 335 | int mipLevelCount, SkBudgeted, SkDestinationSurfaceColorMode) { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 336 | return nullptr; |
| 337 | } |
| 338 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 339 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 340 | sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 341 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 342 | } |
| 343 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 344 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&, |
| 345 | const DeferredTextureImageUsageParams[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 346 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 347 | SkColorSpace* dstColorSpace) const { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*, |
| 352 | SkBudgeted) { |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 356 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 357 | SkAlphaType, sk_sp<SkColorSpace>) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 358 | return nullptr; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 359 | } |
| 360 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 361 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 362 | const GrBackendObject yuvTextureHandles[3], |
| 363 | const SkISize yuvSizes[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 364 | GrSurfaceOrigin origin, |
| 365 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 366 | return nullptr; |
| 367 | } |
| 368 | |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 369 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace) const { |
| 370 | return nullptr; |
| 371 | } |
| 372 | |
Brian Osman | 2c2bc11 | 2017-02-28 10:02:49 -0500 | [diff] [blame] | 373 | std::unique_ptr<SkCrossContextImageData> SkCrossContextImageData::MakeFromEncoded( |
| 374 | GrContext*, sk_sp<SkData> encoded, SkColorSpace* dstColorSpace) { |
| 375 | sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(encoded)); |
| 376 | if (!image) { |
| 377 | return nullptr; |
| 378 | } |
| 379 | // TODO: Force decode to raster here? |
| 380 | return std::unique_ptr<SkCrossContextImageData>(new SkCrossContextImageData(std::move(image))); |
| 381 | } |
| 382 | |
| 383 | sk_sp<SkImage> SkImage::MakeFromCrossContextImageData( |
| 384 | GrContext*, std::unique_ptr<SkCrossContextImageData> ccid) { |
| 385 | return ccid->fImage; |
| 386 | } |
| 387 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 388 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 389 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 390 | } |
| 391 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 392 | #endif |
| 393 | |
| 394 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 395 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 396 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 397 | int mipLevelCount, SkBudgeted) { |
| 398 | return nullptr; |
| 399 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 400 | |
| 401 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 402 | #include "SkImageDeserializer.h" |
| 403 | |
| 404 | sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) { |
| 405 | return SkImage::MakeFromEncoded(sk_ref_sp(data), subset); |
| 406 | } |
| 407 | sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length, |
| 408 | const SkIRect* subset) { |
| 409 | return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset); |
| 410 | } |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 411 | |
| 412 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 413 | |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 414 | bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) { |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 415 | SkASSERT(image); |
| 416 | SkASSERT(ctx); |
Derek Sollenberger | d3ea9b7 | 2016-11-09 11:25:13 -0500 | [diff] [blame] | 417 | return as_IB(image)->onPinAsTexture(ctx); |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) { |
| 421 | SkASSERT(image); |
| 422 | SkASSERT(ctx); |
| 423 | as_IB(image)->onUnpinAsTexture(ctx); |
| 424 | } |
Brian Osman | 0d4ff6c | 2017-01-17 16:10:07 -0500 | [diff] [blame] | 425 | |
| 426 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 427 | |
| 428 | sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src, |
| 429 | SkColorSpace* colorSpace) { |
| 430 | // Read the pixels out of the source image, with no conversion |
| 431 | SkImageInfo info = as_IB(src)->onImageInfo(); |
| 432 | if (kUnknown_SkColorType == info.colorType()) { |
| 433 | SkDEBUGFAIL("Unexpected color type"); |
| 434 | return nullptr; |
| 435 | } |
| 436 | |
| 437 | size_t rowBytes = info.minRowBytes(); |
| 438 | size_t size = info.getSafeSize(rowBytes); |
| 439 | auto data = SkData::MakeUninitialized(size); |
| 440 | if (!data) { |
| 441 | return nullptr; |
| 442 | } |
| 443 | |
| 444 | SkPixmap pm(info, data->writable_data(), rowBytes); |
| 445 | if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) { |
| 446 | return nullptr; |
| 447 | } |
| 448 | |
| 449 | // Wrap them in a new image with a different color space |
| 450 | return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes); |
| 451 | } |