blob: 938ce98f85c74bed9f92138e3ee9a6893423a08f [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"
mike@reedtribe.org70e35902012-07-29 20:38:16 +000010#include "SkCanvas.h"
reed871872f2015-06-22 12:48:26 -070011#include "SkData.h"
halcanaryf2848b62015-12-10 12:40:23 -080012#include "SkImageEncoder.h"
senorblanco5878dbd2016-05-19 14:50:29 -070013#include "SkImageFilter.h"
brianosman04a44d02016-09-21 09:46:57 -070014#include "SkImageFilterCache.h"
reed5965c8a2015-01-07 18:04:45 -080015#include "SkImageGenerator.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000016#include "SkImagePriv.h"
reed856e9d92015-09-30 12:21:45 -070017#include "SkImageShader.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000018#include "SkImage_Base.h"
reed80c772b2015-07-30 18:58:23 -070019#include "SkNextID.h"
reed7fb4f8b2016-03-11 04:33:52 -080020#include "SkPicture.h"
reed56179002015-07-07 06:11:19 -070021#include "SkPixelRef.h"
reed96472de2014-12-10 09:53:42 -080022#include "SkReadPixelsRec.h"
senorblanco5878dbd2016-05-19 14:50:29 -070023#include "SkSpecialImage.h"
reedf8d18742015-01-02 20:45:37 -080024#include "SkString.h"
reed4af267b2014-11-21 08:46:37 -080025#include "SkSurface.h"
reed56179002015-07-07 06:11:19 -070026
bsalomon55812362015-06-10 08:49:28 -070027#if SK_SUPPORT_GPU
28#include "GrTexture.h"
29#include "GrContext.h"
reed56179002015-07-07 06:11:19 -070030#include "SkImage_Gpu.h"
bsalomon55812362015-06-10 08:49:28 -070031#endif
Robert Phillips8caf85f2018-04-05 09:30:38 -040032#include "GrBackendSurface.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000033
reed80c772b2015-07-30 18:58:23 -070034SkImage::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.comf6627b72012-07-27 18:02:50 +000041}
42
reed6ceeebd2016-03-09 14:26:26 -080043bool SkImage::peekPixels(SkPixmap* pm) const {
44 SkPixmap tmp;
45 if (!pm) {
46 pm = &tmp;
reed@google.com4f7c6152014-02-06 14:11:56 +000047 }
reed6ceeebd2016-03-09 14:26:26 -080048 return as_IB(this)->onPeekPixels(pm);
reed@google.com4f7c6152014-02-06 14:11:56 +000049}
50
reed96472de2014-12-10 09:53:42 -080051bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080052 int srcX, int srcY, CachingHint chint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -050053 return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
reed09553032015-11-23 12:32:16 -080054}
55
56bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const {
reed6868c3f2015-11-24 11:44:47 -080057 if (this->width() == dst.width() && this->height() == dst.height()) {
58 return this->readPixels(dst, 0, 0, chint);
59 }
60
reed09553032015-11-23 12:32:16 -080061 // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself
62 // can scale more efficiently) we should take advantage of it here.
63 //
64 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -050065 if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) {
reed09553032015-11-23 12:32:16 -080066 SkPixmap pmap;
67 // Note: By calling the pixmap scaler, we never cache the final result, so the chint
68 // is (currently) only being applied to the getROPixels. If we get a request to
69 // also attempt to cache the final (scaled) result, we would add that logic here.
70 //
71 return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality);
72 }
73 return false;
reed@google.com4f7c6152014-02-06 14:11:56 +000074}
75
reed88d064d2015-10-12 11:30:02 -070076///////////////////////////////////////////////////////////////////////////////////////////////////
77
Greg Daniel56008aa2018-03-14 15:33:42 -040078SkColorType SkImage::colorType() const {
Brian Osman3a5ff102018-09-27 12:19:23 -040079 return as_IB(this)->onImageInfo().colorType();
Greg Daniel56008aa2018-03-14 15:33:42 -040080}
81
brianosman69c166d2016-08-17 14:01:05 -070082SkAlphaType SkImage::alphaType() const {
Brian Osman3a5ff102018-09-27 12:19:23 -040083 return as_IB(this)->onImageInfo().alphaType();
brianosman69c166d2016-08-17 14:01:05 -070084}
85
Matt Sarett5b4599f2017-03-02 12:42:35 -050086SkColorSpace* SkImage::colorSpace() const {
87 return as_IB(this)->onImageInfo().colorSpace();
88}
89
90sk_sp<SkColorSpace> SkImage::refColorSpace() const {
91 return as_IB(this)->onImageInfo().refColorSpace();
92}
93
reed5671c5b2016-03-09 14:47:34 -080094sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
95 const SkMatrix* localMatrix) const {
reed6b2d7ac2016-08-11 06:42:26 -070096 return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070097}
98
Mike Reed6409f842017-07-11 16:03:13 -040099sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
Leon Scroggins294c1c42016-11-08 14:29:46 +0000100 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500101 SkColorSpace* legacyColorSpace = nullptr;
102 if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) {
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
113 SkBitmap bm;
Mike Reed4edb5d22017-04-17 11:02:51 -0400114 SkPixmap pmap;
Brian Osman61624f02016-12-09 14:51:59 -0500115 SkColorSpace* legacyColorSpace = nullptr;
Mike Reed4edb5d22017-04-17 11:02:51 -0400116 if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) {
Mike Reedef038482017-12-16 08:41:28 -0500117 return SkEncodePixmap(pmap, SkEncodedImageFormat::kPNG, 100);
fmalita2be71252015-09-03 07:17:25 -0700118 }
fmalita2be71252015-09-03 07:17:25 -0700119 return nullptr;
120}
121
Mike Reed6409f842017-07-11 16:03:13 -0400122sk_sp<SkData> SkImage::refEncodedData() const {
123 return sk_sp<SkData>(as_IB(this)->onRefEncoded());
reed871872f2015-06-22 12:48:26 -0700124}
125
reed7fb4f8b2016-03-11 04:33:52 -0800126sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700127 if (nullptr == encoded || 0 == encoded->size()) {
128 return nullptr;
reed5965c8a2015-01-07 18:04:45 -0800129 }
Robert Phillipsa07bf672018-07-13 10:18:38 -0400130 return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded)),
131 subset);
reed5965c8a2015-01-07 18:04:45 -0800132}
133
Mike Reed6409f842017-07-11 16:03:13 -0400134///////////////////////////////////////////////////////////////////////////////////////////////////
135
reed7fb4f8b2016-03-11 04:33:52 -0800136sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700137 if (subset.isEmpty()) {
138 return nullptr;
139 }
140
141 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
142 if (!bounds.contains(subset)) {
143 return nullptr;
144 }
145
146 // optimization : return self if the subset == our bounds
147 if (bounds == subset) {
reed7fb4f8b2016-03-11 04:33:52 -0800148 return sk_ref_sp(const_cast<SkImage*>(this));
reed7b6945b2015-09-24 00:50:58 -0700149 }
reed7fb4f8b2016-03-11 04:33:52 -0800150 return as_IB(this)->onMakeSubset(subset);
reedf803da12015-01-23 05:58:07 -0800151}
152
bsalomon55812362015-06-10 08:49:28 -0700153#if SK_SUPPORT_GPU
154
155GrTexture* SkImage::getTexture() const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400156 return as_IB(this)->onGetTexture();
bsalomon55812362015-06-10 08:49:28 -0700157}
158
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400159bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekProxy()); }
bsalomon55812362015-06-10 08:49:28 -0700160
Robert Phillipsc5509952018-04-04 15:54:55 -0400161GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
162 GrSurfaceOrigin* origin) const {
163 return as_IB(this)->onGetBackendTexture(flushPendingGrContextIO, origin);
164}
Robert Phillipsc5509952018-04-04 15:54:55 -0400165
Brian Osman5bbd0762017-05-08 11:07:42 -0400166bool SkImage::isValid(GrContext* context) const {
Khushalc421ca12018-06-26 14:38:34 -0700167 if (context && context->abandoned()) {
Brian Osman5bbd0762017-05-08 11:07:42 -0400168 return false;
169 }
170 return as_IB(this)->onIsValid(context);
171}
172
bsalomon55812362015-06-10 08:49:28 -0700173#else
174
halcanary96fcdcc2015-08-27 07:41:13 -0700175GrTexture* SkImage::getTexture() const { return nullptr; }
bsalomon55812362015-06-10 08:49:28 -0700176
177bool SkImage::isTextureBacked() const { return false; }
178
Robert Phillipsc5509952018-04-04 15:54:55 -0400179GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
180 GrSurfaceOrigin* origin) const {
181 return GrBackendTexture(); // invalid
182}
183
Brian Osman5bbd0762017-05-08 11:07:42 -0400184bool SkImage::isValid(GrContext* context) const {
185 if (context) {
186 return false;
187 }
188 return as_IB(this)->onIsValid(context);
189}
190
bsalomon55812362015-06-10 08:49:28 -0700191#endif
192
reed@google.com4f7c6152014-02-06 14:11:56 +0000193///////////////////////////////////////////////////////////////////////////////
194
reedaf3fbfc2015-10-04 11:28:36 -0700195SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700196 : INHERITED(width, height, uniqueID)
Mike Reed30301c42018-07-19 09:39:21 -0400197 , fAddedToRasterCache(false)
reedaf3fbfc2015-10-04 11:28:36 -0700198{}
fmalita3b0d5322015-09-18 08:07:31 -0700199
200SkImage_Base::~SkImage_Base() {
Mike Reed30301c42018-07-19 09:39:21 -0400201 if (fAddedToRasterCache.load()) {
fmalita3b0d5322015-09-18 08:07:31 -0700202 SkNotifyBitmapGenIDIsStale(this->uniqueID());
203 }
204}
205
Robert Phillips8caf85f2018-04-05 09:30:38 -0400206GrBackendTexture SkImage_Base::onGetBackendTexture(bool flushPendingGrContextIO,
207 GrSurfaceOrigin* origin) const {
208 return GrBackendTexture(); // invalid
209}
210
reed09553032015-11-23 12:32:16 -0800211bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
212 return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint);
reed871872f2015-06-22 12:48:26 -0700213}
214
215///////////////////////////////////////////////////////////////////////////////////////////////////
reedf803da12015-01-23 05:58:07 -0800216
reed7fb4f8b2016-03-11 04:33:52 -0800217sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) {
reed56179002015-07-07 06:11:19 -0700218 SkPixelRef* pr = bm.pixelRef();
halcanary96fcdcc2015-08-27 07:41:13 -0700219 if (nullptr == pr) {
220 return nullptr;
reed56179002015-07-07 06:11:19 -0700221 }
222
reed1ec04d92016-08-05 12:07:41 -0700223 return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode);
reed56179002015-07-07 06:11:19 -0700224}
225
Cary Clark4f5a79c2018-02-07 15:51:00 -0500226bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const {
227 return as_IB(this)->onAsLegacyBitmap(bitmap);
reed3c065112015-07-08 12:46:22 -0700228}
229
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400230sk_sp<SkCachedData> SkImage_Base::getPlanes(SkYUVSizeInfo*, SkYUVColorSpace*,const void*[3]) {
231 return nullptr;
232}
233
Cary Clark4f5a79c2018-02-07 15:51:00 -0500234bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap) const {
reed3c065112015-07-08 12:46:22 -0700235 // As the base-class, all we can do is make a copy (regardless of mode).
236 // Subclasses that want to be more optimal should override.
Brian Osman7992da32016-11-18 11:28:24 -0500237 SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
reed3c065112015-07-08 12:46:22 -0700238 if (!bitmap->tryAllocPixels(info)) {
239 return false;
240 }
241 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
242 bitmap->reset();
243 return false;
244 }
245
Cary Clark4f5a79c2018-02-07 15:51:00 -0500246 bitmap->setImmutable();
reed3c065112015-07-08 12:46:22 -0700247 return true;
248}
249
Brian Osman138ea972016-12-16 11:55:18 -0500250sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
Matt Sarette94255d2017-01-09 12:38:59 -0500251 const SkMatrix* matrix, const SkPaint* paint,
252 BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
Mike Reed185130c2017-02-15 15:14:16 -0500253 return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture),
254 matrix, paint, bitDepth,
255 std::move(colorSpace)));
Matt Sarette94255d2017-01-09 12:38:59 -0500256}
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500257
senorblanco5878dbd2016-05-19 14:50:29 -0700258sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
259 const SkIRect& clipBounds, SkIRect* outSubset,
260 SkIPoint* offset) const {
brianosman04a44d02016-09-21 09:46:57 -0700261 if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) {
262 return nullptr;
263 }
Brian Osmana50205f2018-07-06 13:57:01 -0400264 SkColorType colorType = as_IB(this)->onImageInfo().colorType();
Brian Osman7992da32016-11-18 11:28:24 -0500265 SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace();
brianosman04a44d02016-09-21 09:46:57 -0700266 sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage(
Brian Osman61624f02016-12-09 14:51:59 -0500267 subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace);
brianosman04a44d02016-09-21 09:46:57 -0700268 if (!srcSpecialImage) {
269 return nullptr;
270 }
senorblanco5878dbd2016-05-19 14:50:29 -0700271
Hal Canary67b39de2016-11-07 11:47:44 -0500272 sk_sp<SkImageFilterCache> cache(
brianosman04a44d02016-09-21 09:46:57 -0700273 SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize));
Brian Osmana50205f2018-07-06 13:57:01 -0400274 SkImageFilter::OutputProperties outputProperties(colorType, colorSpace);
brianosman2a75e5d2016-09-22 07:15:37 -0700275 SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties);
276
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500277 sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset);
brianosman04a44d02016-09-21 09:46:57 -0700278 if (!result) {
279 return nullptr;
280 }
senorblanco5878dbd2016-05-19 14:50:29 -0700281
brianosman04a44d02016-09-21 09:46:57 -0700282 *outSubset = SkIRect::MakeWH(result->width(), result->height());
283 if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) {
284 return nullptr;
285 }
286 offset->fX += outSubset->x();
287 offset->fY += outSubset->y();
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500288
289 // Note that here we're returning the special image's entire backing store, loose padding
290 // and all!
291 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700292}
293
fmalitaddbbdda2015-08-20 08:47:26 -0700294bool SkImage::isLazyGenerated() const {
295 return as_IB(this)->onIsLazyGenerated();
296}
297
reed9e2ed832016-10-31 05:27:28 -0700298bool SkImage::isAlphaOnly() const {
299 return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType;
300}
301
Brian Osmanf6db4952018-07-16 13:06:02 -0400302sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osman15f0f292018-10-01 14:14:46 -0400303 if (!target) {
Matt Sarett6de13102017-03-14 14:10:48 -0400304 return nullptr;
305 }
306
Matt Sarett73879eb2017-03-22 10:07:50 -0400307 // No need to create a new image if:
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400308 // (1) The color spaces are equal.
Matt Sarett73879eb2017-03-22 10:07:50 -0400309 // (2) The color type is kAlpha8.
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400310 if (SkColorSpace::Equals(this->colorSpace(), target.get()) ||
Matt Sarettcb874232017-04-05 11:41:27 -0400311 kAlpha_8_SkColorType == as_IB(this)->onImageInfo().colorType()) {
312 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400313 }
314
Matt Sarettcb874232017-04-05 11:41:27 -0400315 // TODO: We might consider making this a deferred conversion?
Brian Osman15f0f292018-10-01 14:14:46 -0400316 return as_IB(this)->onMakeColorSpace(std::move(target));
Matt Sarett6de13102017-03-14 14:10:48 -0400317}
318
Mike Reedb950b592018-01-02 10:16:12 -0500319sk_sp<SkImage> SkImage::makeNonTextureImage() const {
320 if (!this->isTextureBacked()) {
321 return sk_ref_sp(const_cast<SkImage*>(this));
322 }
323 return this->makeRasterImage();
324}
325
Mike Reedf7ee95c2017-12-04 14:27:01 -0500326sk_sp<SkImage> SkImage::makeRasterImage() const {
327 SkPixmap pm;
328 if (this->peekPixels(&pm)) {
329 return sk_ref_sp(const_cast<SkImage*>(this));
330 }
331
332 const SkImageInfo info = as_IB(this)->onImageInfo();
333 const size_t rowBytes = info.minRowBytes();
334 size_t size = info.computeByteSize(rowBytes);
335 if (SkImageInfo::ByteSizeOverflowed(size)) {
336 return nullptr;
337 }
338
339 sk_sp<SkData> data = SkData::MakeUninitialized(size);
Mike Klein98e38e22018-01-12 15:59:53 +0000340 pm = { info.makeColorSpace(nullptr), data->writable_data(), info.minRowBytes() };
Mike Reedf7ee95c2017-12-04 14:27:01 -0500341 if (!this->readPixels(pm, 0, 0)) {
342 return nullptr;
343 }
344
345 return SkImage::MakeRasterData(info, std::move(data), rowBytes);
346}
347
reed56179002015-07-07 06:11:19 -0700348//////////////////////////////////////////////////////////////////////////////////////
349
reed8b26b992015-05-07 15:36:17 -0700350#if !SK_SUPPORT_GPU
351
Greg Daniel94403452017-04-18 15:52:36 -0400352sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
353 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomonbfd27492018-03-19 14:08:51 -0400354 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
Greg Daniel94403452017-04-18 15:52:36 -0400355 TextureReleaseProc releaseP, ReleaseContext releaseC) {
356 return nullptr;
357}
358
Eric Karl914a36b2017-10-12 12:44:50 -0700359bool SkImage::MakeBackendTextureFromSkImage(GrContext*,
360 sk_sp<SkImage>,
361 GrBackendTexture*,
362 BackendTextureReleaseProc*) {
363 return false;
364}
365
Greg Daniel94403452017-04-18 15:52:36 -0400366sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
367 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Greg Danielf5d87582017-12-18 14:48:15 -0500368 SkColorType ct, SkAlphaType at,
369 sk_sp<SkColorSpace> cs) {
370 return nullptr;
371}
372
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400373sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* context,
374 SkYUVColorSpace yuvColorSpace,
375 const GrBackendTexture yuvaTextures[],
376 const SkYUVAIndex yuvaIndices[4],
377 SkISize imageSize,
378 GrSurfaceOrigin imageOrigin,
379 sk_sp<SkColorSpace> imageColorSpace) {
380 return nullptr;
381}
382
383sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
384 GrContext* context,
385 SkYUVColorSpace yuvColorSpace,
386 const GrBackendTexture yuvaTextures[],
387 const SkYUVAIndex yuvaIndices[4],
388 SkISize imageSize,
389 GrSurfaceOrigin imageOrigin,
390 const GrBackendTexture& backendTexture,
391 sk_sp<SkColorSpace> imageColorSpace) {
392 return nullptr;
393}
394
reed7fb4f8b2016-03-11 04:33:52 -0800395sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
Brian Salomon6a426c12018-03-15 12:16:02 -0400396 const GrBackendTexture[3],
Brian Salomon6a426c12018-03-15 12:16:02 -0400397 GrSurfaceOrigin origin,
398 sk_sp<SkColorSpace> imageColorSpace) {
399 return nullptr;
400}
401
Brian Salomond2fcfb52018-09-17 21:57:11 -0400402sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
403 GrContext* context, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400404 GrSurfaceOrigin surfaceOrigin, const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400405 sk_sp<SkColorSpace> colorSpace) {
406 return nullptr;
407}
408
Brian Salomon6a426c12018-03-15 12:16:02 -0400409sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace space,
410 const GrBackendTexture[2],
Robert Phillipsc25db632017-12-13 09:22:45 -0500411 GrSurfaceOrigin origin,
412 sk_sp<SkColorSpace> imageColorSpace) {
413 return nullptr;
414}
415
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400416sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace,
417 GrMipMapped mipMapped) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500418 return nullptr;
419}
420
Brian Salomond2fcfb52018-09-17 21:57:11 -0400421sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(GrContext* context,
422 SkYUVColorSpace yuvColorSpace,
423 const GrBackendTexture nv12Textures[2],
424 GrSurfaceOrigin surfaceOrigin,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400425 const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400426 sk_sp<SkColorSpace> colorSpace) {
427 return nullptr;
428}
429
reed7fb4f8b2016-03-11 04:33:52 -0800430#endif
431
432///////////////////////////////////////////////////////////////////////////////////////////////////
433
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500434bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700435 SkASSERT(image);
436 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500437 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700438}
439
440void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
441 SkASSERT(image);
442 SkASSERT(ctx);
443 as_IB(image)->onUnpinAsTexture(ctx);
444}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500445
Mike Reedf2c73642018-05-29 15:41:27 -0400446SkIRect SkImage_getSubset(const SkImage* image) {
447 SkASSERT(image);
448 return as_IB(image)->onGetSubset();
449}
450
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500451///////////////////////////////////////////////////////////////////////////////////////////////////
452
453sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src,
454 SkColorSpace* colorSpace) {
455 // Read the pixels out of the source image, with no conversion
456 SkImageInfo info = as_IB(src)->onImageInfo();
457 if (kUnknown_SkColorType == info.colorType()) {
458 SkDEBUGFAIL("Unexpected color type");
459 return nullptr;
460 }
461
462 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400463 size_t size = info.computeByteSize(rowBytes);
Mike Reedc5eb97d2017-10-09 10:42:51 -0400464 if (SkImageInfo::ByteSizeOverflowed(size)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400465 return nullptr;
466 }
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500467 auto data = SkData::MakeUninitialized(size);
468 if (!data) {
469 return nullptr;
470 }
471
472 SkPixmap pm(info, data->writable_data(), rowBytes);
473 if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) {
474 return nullptr;
475 }
476
477 // Wrap them in a new image with a different color space
478 return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes);
479}