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