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