blob: c9064831fc8f0c5d619d37a57284a2e356bfaaae [file] [log] [blame]
reed@google.com6997ebb2012-07-30 19:50:31 +00001/*
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.comf6627b72012-07-27 18:02:50 +00008#include "SkBitmap.h"
fmalita3b0d5322015-09-18 08:07:31 -07009#include "SkBitmapCache.h"
Brian Osmanb70fd912018-10-22 16:10:44 -040010#include "SkCachedData.h"
mike@reedtribe.org70e35902012-07-29 20:38:16 +000011#include "SkCanvas.h"
Brian Osmanb4ae4992018-10-18 11:16:14 -040012#include "SkColorSpacePriv.h"
reed871872f2015-06-22 12:48:26 -070013#include "SkData.h"
halcanaryf2848b62015-12-10 12:40:23 -080014#include "SkImageEncoder.h"
senorblanco5878dbd2016-05-19 14:50:29 -070015#include "SkImageFilter.h"
brianosman04a44d02016-09-21 09:46:57 -070016#include "SkImageFilterCache.h"
reed5965c8a2015-01-07 18:04:45 -080017#include "SkImageGenerator.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000018#include "SkImagePriv.h"
reed856e9d92015-09-30 12:21:45 -070019#include "SkImageShader.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000020#include "SkImage_Base.h"
reed80c772b2015-07-30 18:58:23 -070021#include "SkNextID.h"
reed7fb4f8b2016-03-11 04:33:52 -080022#include "SkPicture.h"
reed96472de2014-12-10 09:53:42 -080023#include "SkReadPixelsRec.h"
senorblanco5878dbd2016-05-19 14:50:29 -070024#include "SkSpecialImage.h"
reedf8d18742015-01-02 20:45:37 -080025#include "SkString.h"
reed4af267b2014-11-21 08:46:37 -080026#include "SkSurface.h"
reed56179002015-07-07 06:11:19 -070027
bsalomon55812362015-06-10 08:49:28 -070028#if SK_SUPPORT_GPU
bsalomon55812362015-06-10 08:49:28 -070029#include "GrContext.h"
Robert Phillipsbc429442019-02-20 08:26:03 -050030#include "GrTexture.h"
reed56179002015-07-07 06:11:19 -070031#include "SkImage_Gpu.h"
bsalomon55812362015-06-10 08:49:28 -070032#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -040033#include "GrBackendSurface.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000034
reed80c772b2015-07-30 18:58:23 -070035SkImage::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.comf6627b72012-07-27 18:02:50 +000042}
43
reed6ceeebd2016-03-09 14:26:26 -080044bool SkImage::peekPixels(SkPixmap* pm) const {
45 SkPixmap tmp;
46 if (!pm) {
47 pm = &tmp;
reed@google.com4f7c6152014-02-06 14:11:56 +000048 }
reed6ceeebd2016-03-09 14:26:26 -080049 return as_IB(this)->onPeekPixels(pm);
reed@google.com4f7c6152014-02-06 14:11:56 +000050}
51
reed96472de2014-12-10 09:53:42 -080052bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080053 int srcX, int srcY, CachingHint chint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -050054 return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
reed09553032015-11-23 12:32:16 -080055}
56
57bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const {
reed6868c3f2015-11-24 11:44:47 -080058 if (this->width() == dst.width() && this->height() == dst.height()) {
59 return this->readPixels(dst, 0, 0, chint);
60 }
61
reed09553032015-11-23 12:32:16 -080062 // 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 Osmane50cdf02018-10-19 13:02:14 -040066 if (as_IB(this)->getROPixels(&bm, chint)) {
reed09553032015-11-23 12:32:16 -080067 SkPixmap pmap;
68 // Note: By calling the pixmap scaler, we never cache the final result, so the chint
69 // is (currently) only being applied to the getROPixels. If we get a request to
70 // also attempt to cache the final (scaled) result, we would add that logic here.
71 //
72 return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality);
73 }
74 return false;
reed@google.com4f7c6152014-02-06 14:11:56 +000075}
76
reed88d064d2015-10-12 11:30:02 -070077///////////////////////////////////////////////////////////////////////////////////////////////////
78
Greg Daniel56008aa2018-03-14 15:33:42 -040079SkColorType SkImage::colorType() const {
Brian Osman3a5ff102018-09-27 12:19:23 -040080 return as_IB(this)->onImageInfo().colorType();
Greg Daniel56008aa2018-03-14 15:33:42 -040081}
82
brianosman69c166d2016-08-17 14:01:05 -070083SkAlphaType SkImage::alphaType() const {
Brian Osman3a5ff102018-09-27 12:19:23 -040084 return as_IB(this)->onImageInfo().alphaType();
brianosman69c166d2016-08-17 14:01:05 -070085}
86
Matt Sarett5b4599f2017-03-02 12:42:35 -050087SkColorSpace* SkImage::colorSpace() const {
88 return as_IB(this)->onImageInfo().colorSpace();
89}
90
91sk_sp<SkColorSpace> SkImage::refColorSpace() const {
92 return as_IB(this)->onImageInfo().refColorSpace();
93}
94
reed5671c5b2016-03-09 14:47:34 -080095sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
96 const SkMatrix* localMatrix) const {
reed6b2d7ac2016-08-11 06:42:26 -070097 return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070098}
99
Mike Reed6409f842017-07-11 16:03:13 -0400100sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
Leon Scroggins294c1c42016-11-08 14:29:46 +0000101 SkBitmap bm;
Brian Osmane50cdf02018-10-19 13:02:14 -0400102 if (as_IB(this)->getROPixels(&bm)) {
Mike Reed25eef6b2017-12-08 16:20:58 -0500103 return SkEncodeBitmap(bm, type, quality);
Leon Scroggins294c1c42016-11-08 14:29:46 +0000104 }
105 return nullptr;
106}
107
Mike Reedef038482017-12-16 08:41:28 -0500108sk_sp<SkData> SkImage::encodeToData() const {
109 if (auto encoded = this->refEncodedData()) {
Mike Reed6409f842017-07-11 16:03:13 -0400110 return encoded;
fmalita2be71252015-09-03 07:17:25 -0700111 }
112
Brian Osmane50cdf02018-10-19 13:02:14 -0400113 return this->encodeToData(SkEncodedImageFormat::kPNG, 100);
fmalita2be71252015-09-03 07:17:25 -0700114}
115
Mike Reed6409f842017-07-11 16:03:13 -0400116sk_sp<SkData> SkImage::refEncodedData() const {
117 return sk_sp<SkData>(as_IB(this)->onRefEncoded());
reed871872f2015-06-22 12:48:26 -0700118}
119
reed7fb4f8b2016-03-11 04:33:52 -0800120sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700121 if (nullptr == encoded || 0 == encoded->size()) {
122 return nullptr;
reed5965c8a2015-01-07 18:04:45 -0800123 }
Robert Phillipsa07bf672018-07-13 10:18:38 -0400124 return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded)),
125 subset);
reed5965c8a2015-01-07 18:04:45 -0800126}
127
Mike Reed6409f842017-07-11 16:03:13 -0400128///////////////////////////////////////////////////////////////////////////////////////////////////
129
reed7fb4f8b2016-03-11 04:33:52 -0800130sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700131 if (subset.isEmpty()) {
132 return nullptr;
133 }
134
135 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
136 if (!bounds.contains(subset)) {
137 return nullptr;
138 }
139
140 // optimization : return self if the subset == our bounds
141 if (bounds == subset) {
reed7fb4f8b2016-03-11 04:33:52 -0800142 return sk_ref_sp(const_cast<SkImage*>(this));
reed7b6945b2015-09-24 00:50:58 -0700143 }
reed7fb4f8b2016-03-11 04:33:52 -0800144 return as_IB(this)->onMakeSubset(subset);
reedf803da12015-01-23 05:58:07 -0800145}
146
bsalomon55812362015-06-10 08:49:28 -0700147#if SK_SUPPORT_GPU
148
149GrTexture* SkImage::getTexture() const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400150 return as_IB(this)->onGetTexture();
bsalomon55812362015-06-10 08:49:28 -0700151}
152
Jim Van Verth21bd60d2018-10-12 15:00:20 -0400153bool SkImage::isTextureBacked() const { return as_IB(this)->onIsTextureBacked(); }
bsalomon55812362015-06-10 08:49:28 -0700154
Robert Phillipsc5509952018-04-04 15:54:55 -0400155GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
156 GrSurfaceOrigin* origin) const {
157 return as_IB(this)->onGetBackendTexture(flushPendingGrContextIO, origin);
158}
Robert Phillipsc5509952018-04-04 15:54:55 -0400159
Brian Osman5bbd0762017-05-08 11:07:42 -0400160bool SkImage::isValid(GrContext* context) const {
Khushalc421ca12018-06-26 14:38:34 -0700161 if (context && context->abandoned()) {
Brian Osman5bbd0762017-05-08 11:07:42 -0400162 return false;
163 }
164 return as_IB(this)->onIsValid(context);
165}
166
bsalomon55812362015-06-10 08:49:28 -0700167#else
168
halcanary96fcdcc2015-08-27 07:41:13 -0700169GrTexture* SkImage::getTexture() const { return nullptr; }
bsalomon55812362015-06-10 08:49:28 -0700170
171bool SkImage::isTextureBacked() const { return false; }
172
Robert Phillipsc5509952018-04-04 15:54:55 -0400173GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
174 GrSurfaceOrigin* origin) const {
175 return GrBackendTexture(); // invalid
176}
177
Brian Osman5bbd0762017-05-08 11:07:42 -0400178bool SkImage::isValid(GrContext* context) const {
179 if (context) {
180 return false;
181 }
182 return as_IB(this)->onIsValid(context);
183}
184
bsalomon55812362015-06-10 08:49:28 -0700185#endif
186
reed@google.com4f7c6152014-02-06 14:11:56 +0000187///////////////////////////////////////////////////////////////////////////////
188
reedaf3fbfc2015-10-04 11:28:36 -0700189SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700190 : INHERITED(width, height, uniqueID)
Mike Reed30301c42018-07-19 09:39:21 -0400191 , fAddedToRasterCache(false)
reedaf3fbfc2015-10-04 11:28:36 -0700192{}
fmalita3b0d5322015-09-18 08:07:31 -0700193
194SkImage_Base::~SkImage_Base() {
Mike Reed30301c42018-07-19 09:39:21 -0400195 if (fAddedToRasterCache.load()) {
fmalita3b0d5322015-09-18 08:07:31 -0700196 SkNotifyBitmapGenIDIsStale(this->uniqueID());
197 }
198}
199
Robert Phillips8caf85f2018-04-05 09:30:38 -0400200GrBackendTexture SkImage_Base::onGetBackendTexture(bool flushPendingGrContextIO,
201 GrSurfaceOrigin* origin) const {
202 return GrBackendTexture(); // invalid
203}
204
reed09553032015-11-23 12:32:16 -0800205bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
206 return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint);
reed871872f2015-06-22 12:48:26 -0700207}
208
209///////////////////////////////////////////////////////////////////////////////////////////////////
reedf803da12015-01-23 05:58:07 -0800210
reed7fb4f8b2016-03-11 04:33:52 -0800211sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) {
Brian Osman41ba8262018-10-19 14:56:26 -0400212 if (!bm.pixelRef()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700213 return nullptr;
reed56179002015-07-07 06:11:19 -0700214 }
215
reed1ec04d92016-08-05 12:07:41 -0700216 return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode);
reed56179002015-07-07 06:11:19 -0700217}
218
Cary Clark4f5a79c2018-02-07 15:51:00 -0500219bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const {
220 return as_IB(this)->onAsLegacyBitmap(bitmap);
reed3c065112015-07-08 12:46:22 -0700221}
222
Jim Van Verthe24b5872018-10-29 16:26:02 -0400223sk_sp<SkCachedData> SkImage_Base::getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400224 SkYUVColorSpace*, const void*[4]) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400225 return nullptr;
226}
227
Cary Clark4f5a79c2018-02-07 15:51:00 -0500228bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap) const {
reed3c065112015-07-08 12:46:22 -0700229 // As the base-class, all we can do is make a copy (regardless of mode).
230 // Subclasses that want to be more optimal should override.
Brian Osman7992da32016-11-18 11:28:24 -0500231 SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
reed3c065112015-07-08 12:46:22 -0700232 if (!bitmap->tryAllocPixels(info)) {
233 return false;
234 }
235 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
236 bitmap->reset();
237 return false;
238 }
239
Cary Clark4f5a79c2018-02-07 15:51:00 -0500240 bitmap->setImmutable();
reed3c065112015-07-08 12:46:22 -0700241 return true;
242}
243
Brian Osman138ea972016-12-16 11:55:18 -0500244sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
Matt Sarette94255d2017-01-09 12:38:59 -0500245 const SkMatrix* matrix, const SkPaint* paint,
246 BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
Mike Reed185130c2017-02-15 15:14:16 -0500247 return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture),
248 matrix, paint, bitDepth,
249 std::move(colorSpace)));
Matt Sarette94255d2017-01-09 12:38:59 -0500250}
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500251
senorblanco5878dbd2016-05-19 14:50:29 -0700252sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
253 const SkIRect& clipBounds, SkIRect* outSubset,
254 SkIPoint* offset) const {
Robert Phillips27467652019-01-10 16:34:22 -0500255 GrContext* context = as_IB(this)->context();
256
257 return this->makeWithFilter(context, filter, subset, clipBounds, outSubset, offset);
258}
259
260sk_sp<SkImage> SkImage::makeWithFilter(GrContext* grContext,
261 const SkImageFilter* filter, const SkIRect& subset,
262 const SkIRect& clipBounds, SkIRect* outSubset,
263 SkIPoint* offset) const {
brianosman04a44d02016-09-21 09:46:57 -0700264 if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) {
265 return nullptr;
266 }
Robert Phillipsbc429442019-02-20 08:26:03 -0500267 sk_sp<SkSpecialImage> srcSpecialImage =
268#if SK_SUPPORT_GPU
269 SkSpecialImage::MakeFromImage(grContext, subset, sk_ref_sp(const_cast<SkImage*>(this)));
270#else
271 SkSpecialImage::MakeFromImage(nullptr, subset, sk_ref_sp(const_cast<SkImage*>(this)));
272#endif
brianosman04a44d02016-09-21 09:46:57 -0700273 if (!srcSpecialImage) {
274 return nullptr;
275 }
senorblanco5878dbd2016-05-19 14:50:29 -0700276
Hal Canary67b39de2016-11-07 11:47:44 -0500277 sk_sp<SkImageFilterCache> cache(
brianosman04a44d02016-09-21 09:46:57 -0700278 SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize));
Brian Osmanb9c49782018-10-12 12:01:22 -0400279 SkImageFilter::OutputProperties outputProperties(as_IB(this)->onImageInfo().colorType(),
280 as_IB(this)->onImageInfo().colorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -0700281 SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties);
282
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500283 sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset);
brianosman04a44d02016-09-21 09:46:57 -0700284 if (!result) {
285 return nullptr;
286 }
senorblanco5878dbd2016-05-19 14:50:29 -0700287
brianosman04a44d02016-09-21 09:46:57 -0700288 *outSubset = SkIRect::MakeWH(result->width(), result->height());
289 if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) {
290 return nullptr;
291 }
292 offset->fX += outSubset->x();
293 offset->fY += outSubset->y();
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500294
295 // Note that here we're returning the special image's entire backing store, loose padding
296 // and all!
297 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700298}
299
fmalitaddbbdda2015-08-20 08:47:26 -0700300bool SkImage::isLazyGenerated() const {
301 return as_IB(this)->onIsLazyGenerated();
302}
303
reed9e2ed832016-10-31 05:27:28 -0700304bool SkImage::isAlphaOnly() const {
305 return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType;
306}
307
Brian Osmanf6db4952018-07-16 13:06:02 -0400308sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osman15f0f292018-10-01 14:14:46 -0400309 if (!target) {
Matt Sarett6de13102017-03-14 14:10:48 -0400310 return nullptr;
311 }
312
Matt Sarett73879eb2017-03-22 10:07:50 -0400313 // No need to create a new image if:
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400314 // (1) The color spaces are equal.
Matt Sarett73879eb2017-03-22 10:07:50 -0400315 // (2) The color type is kAlpha8.
Brian Osmanb4ae4992018-10-18 11:16:14 -0400316 SkColorSpace* colorSpace = this->colorSpace();
317 if (!colorSpace) {
318 colorSpace = sk_srgb_singleton();
319 }
Brian Osmanf48c9962019-01-14 11:15:50 -0500320 if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) {
Matt Sarettcb874232017-04-05 11:41:27 -0400321 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400322 }
323
Brian Osmanf48c9962019-01-14 11:15:50 -0500324 return as_IB(this)->onMakeColorTypeAndColorSpace(this->colorType(), std::move(target));
325}
326
327sk_sp<SkImage> SkImage::makeColorTypeAndColorSpace(SkColorType targetColorType,
328 sk_sp<SkColorSpace> targetColorSpace) const {
329 if (kUnknown_SkColorType == targetColorType || !targetColorSpace) {
330 return nullptr;
331 }
332
333 SkColorType colorType = this->colorType();
334 SkColorSpace* colorSpace = this->colorSpace();
335 if (!colorSpace) {
336 colorSpace = sk_srgb_singleton();
337 }
338 if (colorType == targetColorType &&
339 (SkColorSpace::Equals(colorSpace, targetColorSpace.get()) || this->isAlphaOnly())) {
340 return sk_ref_sp(const_cast<SkImage*>(this));
341 }
342
343 return as_IB(this)->onMakeColorTypeAndColorSpace(targetColorType, std::move(targetColorSpace));
Matt Sarett6de13102017-03-14 14:10:48 -0400344}
345
Mike Reedb950b592018-01-02 10:16:12 -0500346sk_sp<SkImage> SkImage::makeNonTextureImage() const {
347 if (!this->isTextureBacked()) {
348 return sk_ref_sp(const_cast<SkImage*>(this));
349 }
350 return this->makeRasterImage();
351}
352
Mike Reedf7ee95c2017-12-04 14:27:01 -0500353sk_sp<SkImage> SkImage::makeRasterImage() const {
354 SkPixmap pm;
355 if (this->peekPixels(&pm)) {
356 return sk_ref_sp(const_cast<SkImage*>(this));
357 }
358
359 const SkImageInfo info = as_IB(this)->onImageInfo();
360 const size_t rowBytes = info.minRowBytes();
361 size_t size = info.computeByteSize(rowBytes);
362 if (SkImageInfo::ByteSizeOverflowed(size)) {
363 return nullptr;
364 }
365
366 sk_sp<SkData> data = SkData::MakeUninitialized(size);
Mike Klein98e38e22018-01-12 15:59:53 +0000367 pm = { info.makeColorSpace(nullptr), data->writable_data(), info.minRowBytes() };
Mike Reedf7ee95c2017-12-04 14:27:01 -0500368 if (!this->readPixels(pm, 0, 0)) {
369 return nullptr;
370 }
371
372 return SkImage::MakeRasterData(info, std::move(data), rowBytes);
373}
374
reed56179002015-07-07 06:11:19 -0700375//////////////////////////////////////////////////////////////////////////////////////
376
reed8b26b992015-05-07 15:36:17 -0700377#if !SK_SUPPORT_GPU
378
Greg Daniel94403452017-04-18 15:52:36 -0400379sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
380 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomonbfd27492018-03-19 14:08:51 -0400381 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
Greg Daniel94403452017-04-18 15:52:36 -0400382 TextureReleaseProc releaseP, ReleaseContext releaseC) {
383 return nullptr;
384}
385
Eric Karl914a36b2017-10-12 12:44:50 -0700386bool SkImage::MakeBackendTextureFromSkImage(GrContext*,
387 sk_sp<SkImage>,
388 GrBackendTexture*,
389 BackendTextureReleaseProc*) {
390 return false;
391}
392
Greg Daniel94403452017-04-18 15:52:36 -0400393sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
394 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Greg Danielf5d87582017-12-18 14:48:15 -0500395 SkColorType ct, SkAlphaType at,
396 sk_sp<SkColorSpace> cs) {
397 return nullptr;
398}
399
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400400sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* context,
401 SkYUVColorSpace yuvColorSpace,
402 const GrBackendTexture yuvaTextures[],
403 const SkYUVAIndex yuvaIndices[4],
404 SkISize imageSize,
405 GrSurfaceOrigin imageOrigin,
406 sk_sp<SkColorSpace> imageColorSpace) {
407 return nullptr;
408}
409
410sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
411 GrContext* context,
412 SkYUVColorSpace yuvColorSpace,
413 const GrBackendTexture yuvaTextures[],
414 const SkYUVAIndex yuvaIndices[4],
415 SkISize imageSize,
416 GrSurfaceOrigin imageOrigin,
417 const GrBackendTexture& backendTexture,
418 sk_sp<SkColorSpace> imageColorSpace) {
419 return nullptr;
420}
421
reed7fb4f8b2016-03-11 04:33:52 -0800422sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
Brian Salomon6a426c12018-03-15 12:16:02 -0400423 const GrBackendTexture[3],
Brian Salomon6a426c12018-03-15 12:16:02 -0400424 GrSurfaceOrigin origin,
425 sk_sp<SkColorSpace> imageColorSpace) {
426 return nullptr;
427}
428
Brian Salomond2fcfb52018-09-17 21:57:11 -0400429sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
430 GrContext* context, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400431 GrSurfaceOrigin surfaceOrigin, const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400432 sk_sp<SkColorSpace> colorSpace) {
433 return nullptr;
434}
435
Brian Salomon6a426c12018-03-15 12:16:02 -0400436sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace space,
437 const GrBackendTexture[2],
Robert Phillipsc25db632017-12-13 09:22:45 -0500438 GrSurfaceOrigin origin,
439 sk_sp<SkColorSpace> imageColorSpace) {
440 return nullptr;
441}
442
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400443sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace,
444 GrMipMapped mipMapped) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500445 return nullptr;
446}
447
Brian Salomond2fcfb52018-09-17 21:57:11 -0400448sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(GrContext* context,
449 SkYUVColorSpace yuvColorSpace,
450 const GrBackendTexture nv12Textures[2],
451 GrSurfaceOrigin surfaceOrigin,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400452 const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400453 sk_sp<SkColorSpace> colorSpace) {
454 return nullptr;
455}
456
reed7fb4f8b2016-03-11 04:33:52 -0800457#endif
458
459///////////////////////////////////////////////////////////////////////////////////////////////////
460
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500461bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700462 SkASSERT(image);
463 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500464 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700465}
466
467void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
468 SkASSERT(image);
469 SkASSERT(ctx);
470 as_IB(image)->onUnpinAsTexture(ctx);
471}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500472
Mike Reedf2c73642018-05-29 15:41:27 -0400473SkIRect SkImage_getSubset(const SkImage* image) {
474 SkASSERT(image);
475 return as_IB(image)->onGetSubset();
476}
477
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500478///////////////////////////////////////////////////////////////////////////////////////////////////
479
480sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src,
481 SkColorSpace* colorSpace) {
482 // Read the pixels out of the source image, with no conversion
483 SkImageInfo info = as_IB(src)->onImageInfo();
484 if (kUnknown_SkColorType == info.colorType()) {
485 SkDEBUGFAIL("Unexpected color type");
486 return nullptr;
487 }
488
489 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400490 size_t size = info.computeByteSize(rowBytes);
Mike Reedc5eb97d2017-10-09 10:42:51 -0400491 if (SkImageInfo::ByteSizeOverflowed(size)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400492 return nullptr;
493 }
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500494 auto data = SkData::MakeUninitialized(size);
495 if (!data) {
496 return nullptr;
497 }
498
499 SkPixmap pm(info, data->writable_data(), rowBytes);
500 if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) {
501 return nullptr;
502 }
503
504 // Wrap them in a new image with a different color space
505 return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes);
506}