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 | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 51 | bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 52 | int srcX, int srcY, CachingHint chint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 53 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 54 | if (!rec.trim(this->width(), this->height())) { |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 57 | return as_IB(this)->onReadPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, chint); |
| 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; |
| 69 | if (as_IB(this)->getROPixels(&bm, chint)) { |
| 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 | d6b3031 | 2015-09-02 07:06:23 -0700 | [diff] [blame] | 81 | void SkImage::preroll(GrContext* ctx) const { |
reed | efd50da | 2015-09-14 11:17:23 -0700 | [diff] [blame] | 82 | // For now, and to maintain parity w/ previous pixelref behavior, we just force the image |
| 83 | // to produce a cached raster-bitmap form, so that drawing to a raster canvas should be fast. |
| 84 | // |
| 85 | SkBitmap bm; |
| 86 | if (as_IB(this)->getROPixels(&bm)) { |
| 87 | bm.lockPixels(); |
| 88 | bm.unlockPixels(); |
| 89 | } |
reed | d6b3031 | 2015-09-02 07:06:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 92 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 | |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 94 | SkAlphaType SkImage::alphaType() const { |
| 95 | return as_IB(this)->onAlphaType(); |
| 96 | } |
| 97 | |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 98 | sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 99 | const SkMatrix* localMatrix) const { |
reed | 6b2d7ac | 2016-08-11 06:42:26 -0700 | [diff] [blame] | 100 | return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix); |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 101 | } |
| 102 | |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 103 | SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
| 104 | SkBitmap bm; |
| 105 | if (as_IB(this)->getROPixels(&bm)) { |
| 106 | return SkImageEncoder::EncodeData(bm, type, quality); |
| 107 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | return nullptr; |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 109 | } |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 110 | |
halcanary | a2e0567 | 2015-12-09 03:56:02 -0800 | [diff] [blame] | 111 | SkData* SkImage::encode(SkPixelSerializer* serializer) const { |
halcanary | f2848b6 | 2015-12-10 12:40:23 -0800 | [diff] [blame] | 112 | SkAutoTUnref<SkPixelSerializer> defaultSerializer; |
| 113 | SkPixelSerializer* effectiveSerializer = serializer; |
| 114 | if (!effectiveSerializer) { |
| 115 | defaultSerializer.reset(SkImageEncoder::CreatePixelSerializer()); |
| 116 | SkASSERT(defaultSerializer.get()); |
| 117 | effectiveSerializer = defaultSerializer.get(); |
| 118 | } |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 119 | sk_sp<SkData> encoded(this->refEncoded()); |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 120 | if (encoded && effectiveSerializer->useEncodedData(encoded->data(), encoded->size())) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 121 | return encoded.release(); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | SkBitmap bm; |
| 125 | SkAutoPixmapUnlock apu; |
| 126 | if (as_IB(this)->getROPixels(&bm) && bm.requestLock(&apu)) { |
halcanary | 6b28017 | 2015-12-07 12:42:24 -0800 | [diff] [blame] | 127 | return effectiveSerializer->encode(apu.pixmap()); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 133 | SkData* SkImage::refEncoded() const { |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 134 | GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx? |
| 135 | return as_IB(this)->onRefEncoded(ctx); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 136 | } |
| 137 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 138 | sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | if (nullptr == encoded || 0 == encoded->size()) { |
| 140 | return nullptr; |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 141 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 142 | SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded.get()); |
| 143 | return SkImage::MakeFromGenerator(generator, 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 { |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 172 | return as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 175 | bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekTexture()); } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 176 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 177 | GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 178 | GrTexture* texture = as_IB(this)->peekTexture(); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 179 | if (texture) { |
| 180 | GrContext* context = texture->getContext(); |
| 181 | if (context) { |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 182 | if (flushPendingGrContextIO) { |
| 183 | context->prepareSurfaceForExternalIO(texture); |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | return texture->getTextureHandle(); |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | #else |
| 192 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | GrTexture* SkImage::getTexture() const { return nullptr; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 194 | |
| 195 | bool SkImage::isTextureBacked() const { return false; } |
| 196 | |
bsalomon | c49e868 | 2015-06-30 11:37:35 -0700 | [diff] [blame] | 197 | GrBackendObject SkImage::getTextureHandle(bool) const { return 0; } |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 198 | |
| 199 | #endif |
| 200 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 201 | /////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | static bool raster_canvas_supports(const SkImageInfo& info) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 204 | switch (info.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 205 | case kN32_SkColorType: |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 206 | return kUnpremul_SkAlphaType != info.alphaType(); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 207 | case kRGB_565_SkColorType: |
| 208 | return true; |
| 209 | case kAlpha_8_SkColorType: |
| 210 | return true; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 217 | SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 218 | : INHERITED(width, height, uniqueID) |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 219 | , fAddedToCache(false) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 220 | {} |
fmalita | 3b0d532 | 2015-09-18 08:07:31 -0700 | [diff] [blame] | 221 | |
| 222 | SkImage_Base::~SkImage_Base() { |
| 223 | if (fAddedToCache.load()) { |
| 224 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 225 | } |
| 226 | } |
| 227 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 228 | bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 229 | int srcX, int srcY, CachingHint) const { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 230 | if (!raster_canvas_supports(dstInfo)) { |
| 231 | return false; |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 232 | } |
| 233 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 234 | SkBitmap bm; |
| 235 | bm.installPixels(dstInfo, dstPixels, dstRowBytes); |
| 236 | SkCanvas canvas(bm); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 237 | |
| 238 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 239 | paint.setBlendMode(SkBlendMode::kSrc); |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 240 | canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 241 | |
reed@google.com | 4f7c615 | 2014-02-06 14:11:56 +0000 | [diff] [blame] | 242 | return true; |
| 243 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 244 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 246 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 247 | bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const { |
| 248 | return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 249 | } |
| 250 | |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 251 | #if SK_SUPPORT_GPU |
| 252 | #include "GrTextureToYUVPlanes.h" |
| 253 | #endif |
| 254 | |
| 255 | #include "SkRGBAToYUV.h" |
| 256 | |
| 257 | bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3], |
Brian Salomon | de3a726 | 2016-02-03 17:09:00 -0500 | [diff] [blame] | 258 | const size_t rowBytes[3], SkYUVColorSpace colorSpace) const { |
bsalomon | f267c1e | 2016-02-01 13:16:14 -0800 | [diff] [blame] | 259 | #if SK_SUPPORT_GPU |
| 260 | if (GrTexture* texture = as_IB(this)->peekTexture()) { |
| 261 | if (GrTextureToYUVPlanes(texture, sizes, planes, rowBytes, colorSpace)) { |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | #endif |
| 266 | return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace); |
| 267 | } |
| 268 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 269 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 270 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 271 | sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 272 | SkPixelRef* pr = bm.pixelRef(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | if (nullptr == pr) { |
| 274 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 275 | } |
| 276 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 277 | return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 278 | } |
| 279 | |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 280 | bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 281 | return as_IB(this)->onAsLegacyBitmap(bitmap, mode); |
| 282 | } |
| 283 | |
| 284 | bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const { |
| 285 | // As the base-class, all we can do is make a copy (regardless of mode). |
| 286 | // Subclasses that want to be more optimal should override. |
msarett | be36d59 | 2016-06-13 11:25:03 -0700 | [diff] [blame] | 287 | SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType) |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 288 | .makeAlphaType(this->alphaType()); |
reed | 3c06511 | 2015-07-08 12:46:22 -0700 | [diff] [blame] | 289 | if (!bitmap->tryAllocPixels(info)) { |
| 290 | return false; |
| 291 | } |
| 292 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 293 | bitmap->reset(); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | if (kRO_LegacyBitmapMode == mode) { |
| 298 | bitmap->setImmutable(); |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 303 | sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
| 304 | const SkMatrix* matrix, const SkPaint* paint) { |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 305 | if (!picture) { |
| 306 | return nullptr; |
| 307 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 308 | return MakeFromGenerator(SkImageGenerator::NewFromPicture(dimensions, picture.get(), |
| 309 | matrix, paint)); |
reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame] | 310 | } |
| 311 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 312 | sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 313 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 314 | SkIPoint* offset) const { |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 315 | if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) { |
| 316 | return nullptr; |
| 317 | } |
| 318 | sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage( |
| 319 | subset, sk_ref_sp(const_cast<SkImage*>(this))); |
| 320 | if (!srcSpecialImage) { |
| 321 | return nullptr; |
| 322 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 323 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 324 | SkAutoTUnref<SkImageFilterCache> cache( |
| 325 | SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize)); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 326 | SkImageFilter::OutputProperties outputProperties(as_IB(this)->onImageInfo().colorSpace()); |
| 327 | SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties); |
| 328 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 329 | sk_sp<SkSpecialImage> result = |
| 330 | filter->filterImage(srcSpecialImage.get(), context, offset); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 331 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 332 | if (!result) { |
| 333 | return nullptr; |
| 334 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 335 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 336 | SkIRect fullSize = SkIRect::MakeWH(result->width(), result->height()); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 337 | #if SK_SUPPORT_GPU |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 338 | if (result->isTextureBacked()) { |
| 339 | GrContext* context = result->getContext(); |
| 340 | sk_sp<GrTexture> texture = result->asTextureRef(context); |
| 341 | fullSize = SkIRect::MakeWH(texture->width(), texture->height()); |
| 342 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 343 | #endif |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 344 | *outSubset = SkIRect::MakeWH(result->width(), result->height()); |
| 345 | if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) { |
| 346 | return nullptr; |
| 347 | } |
| 348 | offset->fX += outSubset->x(); |
| 349 | offset->fY += outSubset->y(); |
| 350 | // This isn't really a "tight" subset, but includes any texture padding. |
| 351 | return result->makeTightSubset(fullSize); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 352 | } |
| 353 | |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 354 | bool SkImage::isLazyGenerated() const { |
| 355 | return as_IB(this)->onIsLazyGenerated(); |
| 356 | } |
| 357 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 358 | bool SkImage::isAlphaOnly() const { |
| 359 | return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType; |
| 360 | } |
| 361 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 362 | ////////////////////////////////////////////////////////////////////////////////////// |
| 363 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 364 | #if !SK_SUPPORT_GPU |
| 365 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 366 | sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted) { |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 367 | return nullptr; |
| 368 | } |
| 369 | |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 370 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 371 | int mipLevelCount, SkBudgeted, SkSourceGammaTreatment) { |
| 372 | return nullptr; |
| 373 | } |
| 374 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 375 | sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 376 | sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 377 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 378 | } |
| 379 | |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 380 | size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&, |
| 381 | const DeferredTextureImageUsageParams[], |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 382 | int paramCnt, void* buffer, |
Brian Osman | 6c15cc7 | 2016-10-17 09:51:37 -0400 | [diff] [blame] | 383 | SkColorSpace* dstColorSpace) const { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*, |
| 388 | SkBudgeted) { |
| 389 | return nullptr; |
| 390 | } |
| 391 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 392 | sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 393 | SkAlphaType, sk_sp<SkColorSpace>) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 394 | return nullptr; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 395 | } |
| 396 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 397 | sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space, |
| 398 | const GrBackendObject yuvTextureHandles[3], |
| 399 | const SkISize yuvSizes[3], |
brianosman | dddbe38 | 2016-07-20 13:55:39 -0700 | [diff] [blame] | 400 | GrSurfaceOrigin origin, |
| 401 | sk_sp<SkColorSpace> imageColorSpace) { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 402 | return nullptr; |
| 403 | } |
| 404 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 405 | sk_sp<SkImage> SkImage::makeTextureImage(GrContext*) const { |
| 406 | return nullptr; |
| 407 | } |
| 408 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 409 | sk_sp<SkImage> SkImage::makeNonTextureImage() const { |
| 410 | return sk_ref_sp(const_cast<SkImage*>(this)); |
| 411 | } |
| 412 | |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 413 | #endif |
| 414 | |
| 415 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 416 | |
cblume | 186d2d4 | 2016-06-03 11:17:42 -0700 | [diff] [blame] | 417 | sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels, |
| 418 | int mipLevelCount, SkBudgeted) { |
| 419 | return nullptr; |
| 420 | } |
reed | a9ca05c | 2016-08-11 03:55:15 -0700 | [diff] [blame] | 421 | |
| 422 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 423 | #include "SkImageDeserializer.h" |
| 424 | |
| 425 | sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) { |
| 426 | return SkImage::MakeFromEncoded(sk_ref_sp(data), subset); |
| 427 | } |
| 428 | sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length, |
| 429 | const SkIRect* subset) { |
| 430 | return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset); |
| 431 | } |
reed | 2d5b714 | 2016-08-17 11:12:33 -0700 | [diff] [blame] | 432 | |
| 433 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 434 | |
| 435 | void SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) { |
| 436 | SkASSERT(image); |
| 437 | SkASSERT(ctx); |
| 438 | as_IB(image)->onPinAsTexture(ctx); |
| 439 | } |
| 440 | |
| 441 | void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) { |
| 442 | SkASSERT(image); |
| 443 | SkASSERT(ctx); |
| 444 | as_IB(image)->onUnpinAsTexture(ctx); |
| 445 | } |