blob: 2337a3688217b78d57eaf7b56cbc17c972acf8ce [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
scroggo@google.com7def5e12013-05-31 14:00:10 +000032
reed80c772b2015-07-30 18:58:23 -070033SkImage::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.comf6627b72012-07-27 18:02:50 +000040}
41
reed6ceeebd2016-03-09 14:26:26 -080042bool SkImage::peekPixels(SkPixmap* pm) const {
43 SkPixmap tmp;
44 if (!pm) {
45 pm = &tmp;
reed@google.com4f7c6152014-02-06 14:11:56 +000046 }
reed6ceeebd2016-03-09 14:26:26 -080047 return as_IB(this)->onPeekPixels(pm);
reed@google.com4f7c6152014-02-06 14:11:56 +000048}
49
reed96472de2014-12-10 09:53:42 -080050bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080051 int srcX, int srcY, CachingHint chint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -050052 return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
reed09553032015-11-23 12:32:16 -080053}
54
55bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const {
reed6868c3f2015-11-24 11:44:47 -080056 if (this->width() == dst.width() && this->height() == dst.height()) {
57 return this->readPixels(dst, 0, 0, chint);
58 }
59
reed09553032015-11-23 12:32:16 -080060 // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself
61 // can scale more efficiently) we should take advantage of it here.
62 //
63 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -050064 if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) {
reed09553032015-11-23 12:32:16 -080065 SkPixmap pmap;
66 // Note: By calling the pixmap scaler, we never cache the final result, so the chint
67 // is (currently) only being applied to the getROPixels. If we get a request to
68 // also attempt to cache the final (scaled) result, we would add that logic here.
69 //
70 return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality);
71 }
72 return false;
reed@google.com4f7c6152014-02-06 14:11:56 +000073}
74
reed88d064d2015-10-12 11:30:02 -070075///////////////////////////////////////////////////////////////////////////////////////////////////
76
brianosman69c166d2016-08-17 14:01:05 -070077SkAlphaType SkImage::alphaType() const {
78 return as_IB(this)->onAlphaType();
79}
80
Matt Sarett5b4599f2017-03-02 12:42:35 -050081SkColorSpace* SkImage::colorSpace() const {
82 return as_IB(this)->onImageInfo().colorSpace();
83}
84
85sk_sp<SkColorSpace> SkImage::refColorSpace() const {
86 return as_IB(this)->onImageInfo().refColorSpace();
87}
88
reed5671c5b2016-03-09 14:47:34 -080089sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
90 const SkMatrix* localMatrix) const {
reed6b2d7ac2016-08-11 06:42:26 -070091 return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070092}
93
Mike Reed6409f842017-07-11 16:03:13 -040094sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
Leon Scroggins294c1c42016-11-08 14:29:46 +000095 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -050096 SkColorSpace* legacyColorSpace = nullptr;
97 if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) {
Mike Reed25eef6b2017-12-08 16:20:58 -050098 return SkEncodeBitmap(bm, type, quality);
Leon Scroggins294c1c42016-11-08 14:29:46 +000099 }
100 return nullptr;
101}
102
Mike Reedef038482017-12-16 08:41:28 -0500103sk_sp<SkData> SkImage::encodeToData() const {
104 if (auto encoded = this->refEncodedData()) {
Mike Reed6409f842017-07-11 16:03:13 -0400105 return encoded;
fmalita2be71252015-09-03 07:17:25 -0700106 }
107
108 SkBitmap bm;
Mike Reed4edb5d22017-04-17 11:02:51 -0400109 SkPixmap pmap;
Brian Osman61624f02016-12-09 14:51:59 -0500110 SkColorSpace* legacyColorSpace = nullptr;
Mike Reed4edb5d22017-04-17 11:02:51 -0400111 if (as_IB(this)->getROPixels(&bm, legacyColorSpace) && bm.peekPixels(&pmap)) {
Mike Reedef038482017-12-16 08:41:28 -0500112 return SkEncodePixmap(pmap, SkEncodedImageFormat::kPNG, 100);
fmalita2be71252015-09-03 07:17:25 -0700113 }
fmalita2be71252015-09-03 07:17:25 -0700114 return nullptr;
115}
116
Mike Reed6409f842017-07-11 16:03:13 -0400117sk_sp<SkData> SkImage::refEncodedData() const {
118 return sk_sp<SkData>(as_IB(this)->onRefEncoded());
reed871872f2015-06-22 12:48:26 -0700119}
120
reed7fb4f8b2016-03-11 04:33:52 -0800121sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == encoded || 0 == encoded->size()) {
123 return nullptr;
reed5965c8a2015-01-07 18:04:45 -0800124 }
Mike Reed185130c2017-02-15 15:14:16 -0500125 return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(encoded), subset);
reed5965c8a2015-01-07 18:04:45 -0800126}
127
Mike Reed6409f842017-07-11 16:03:13 -0400128///////////////////////////////////////////////////////////////////////////////////////////////////
129
reedf8d18742015-01-02 20:45:37 -0800130const char* SkImage::toString(SkString* str) const {
131 str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(),
132 this->isOpaque() ? "opaque" : "");
133 return str->c_str();
134}
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 Phillips3390e152017-01-31 17:53:34 -0500161GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO,
162 GrSurfaceOrigin* origin) const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400163 return as_IB(this)->onGetTextureHandle(flushPendingGrContextIO, origin);
bsalomon55812362015-06-10 08:49:28 -0700164}
165
Brian Osman5bbd0762017-05-08 11:07:42 -0400166bool SkImage::isValid(GrContext* context) const {
167 if (context && context->abandoned()) {
168 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 Phillips3390e152017-01-31 17:53:34 -0500179GrBackendObject SkImage::getTextureHandle(bool, GrSurfaceOrigin*) const { return 0; }
bsalomon55812362015-06-10 08:49:28 -0700180
Brian Osman5bbd0762017-05-08 11:07:42 -0400181bool SkImage::isValid(GrContext* context) const {
182 if (context) {
183 return false;
184 }
185 return as_IB(this)->onIsValid(context);
186}
187
bsalomon55812362015-06-10 08:49:28 -0700188#endif
189
reed@google.com4f7c6152014-02-06 14:11:56 +0000190///////////////////////////////////////////////////////////////////////////////
191
reedaf3fbfc2015-10-04 11:28:36 -0700192SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700193 : INHERITED(width, height, uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700194 , fAddedToCache(false)
reedaf3fbfc2015-10-04 11:28:36 -0700195{}
fmalita3b0d5322015-09-18 08:07:31 -0700196
197SkImage_Base::~SkImage_Base() {
198 if (fAddedToCache.load()) {
199 SkNotifyBitmapGenIDIsStale(this->uniqueID());
200 }
201}
202
reed09553032015-11-23 12:32:16 -0800203bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
204 return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint);
reed871872f2015-06-22 12:48:26 -0700205}
206
207///////////////////////////////////////////////////////////////////////////////////////////////////
reedf803da12015-01-23 05:58:07 -0800208
reed7fb4f8b2016-03-11 04:33:52 -0800209sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) {
reed56179002015-07-07 06:11:19 -0700210 SkPixelRef* pr = bm.pixelRef();
halcanary96fcdcc2015-08-27 07:41:13 -0700211 if (nullptr == pr) {
212 return nullptr;
reed56179002015-07-07 06:11:19 -0700213 }
214
reed1ec04d92016-08-05 12:07:41 -0700215 return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode);
reed56179002015-07-07 06:11:19 -0700216}
217
Cary Clark4f5a79c2018-02-07 15:51:00 -0500218bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const {
219 return as_IB(this)->onAsLegacyBitmap(bitmap);
reed3c065112015-07-08 12:46:22 -0700220}
221
Cary Clark4f5a79c2018-02-07 15:51:00 -0500222bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap) const {
reed3c065112015-07-08 12:46:22 -0700223 // As the base-class, all we can do is make a copy (regardless of mode).
224 // Subclasses that want to be more optimal should override.
Brian Osman7992da32016-11-18 11:28:24 -0500225 SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
reed3c065112015-07-08 12:46:22 -0700226 if (!bitmap->tryAllocPixels(info)) {
227 return false;
228 }
229 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
230 bitmap->reset();
231 return false;
232 }
233
Cary Clark4f5a79c2018-02-07 15:51:00 -0500234 bitmap->setImmutable();
reed3c065112015-07-08 12:46:22 -0700235 return true;
236}
237
Brian Osman138ea972016-12-16 11:55:18 -0500238sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
Matt Sarette94255d2017-01-09 12:38:59 -0500239 const SkMatrix* matrix, const SkPaint* paint,
240 BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
Mike Reed185130c2017-02-15 15:14:16 -0500241 return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture),
242 matrix, paint, bitDepth,
243 std::move(colorSpace)));
Matt Sarette94255d2017-01-09 12:38:59 -0500244}
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500245
senorblanco5878dbd2016-05-19 14:50:29 -0700246sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
247 const SkIRect& clipBounds, SkIRect* outSubset,
248 SkIPoint* offset) const {
brianosman04a44d02016-09-21 09:46:57 -0700249 if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) {
250 return nullptr;
251 }
Brian Osman7992da32016-11-18 11:28:24 -0500252 SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace();
brianosman04a44d02016-09-21 09:46:57 -0700253 sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage(
Brian Osman61624f02016-12-09 14:51:59 -0500254 subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace);
brianosman04a44d02016-09-21 09:46:57 -0700255 if (!srcSpecialImage) {
256 return nullptr;
257 }
senorblanco5878dbd2016-05-19 14:50:29 -0700258
Hal Canary67b39de2016-11-07 11:47:44 -0500259 sk_sp<SkImageFilterCache> cache(
brianosman04a44d02016-09-21 09:46:57 -0700260 SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize));
Brian Osman7992da32016-11-18 11:28:24 -0500261 SkImageFilter::OutputProperties outputProperties(colorSpace);
brianosman2a75e5d2016-09-22 07:15:37 -0700262 SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties);
263
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500264 sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset);
brianosman04a44d02016-09-21 09:46:57 -0700265 if (!result) {
266 return nullptr;
267 }
senorblanco5878dbd2016-05-19 14:50:29 -0700268
brianosman04a44d02016-09-21 09:46:57 -0700269 *outSubset = SkIRect::MakeWH(result->width(), result->height());
270 if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) {
271 return nullptr;
272 }
273 offset->fX += outSubset->x();
274 offset->fY += outSubset->y();
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500275
276 // Note that here we're returning the special image's entire backing store, loose padding
277 // and all!
278 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700279}
280
fmalitaddbbdda2015-08-20 08:47:26 -0700281bool SkImage::isLazyGenerated() const {
282 return as_IB(this)->onIsLazyGenerated();
283}
284
reed9e2ed832016-10-31 05:27:28 -0700285bool SkImage::isAlphaOnly() const {
286 return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType;
287}
288
Matt Sarettcb874232017-04-05 11:41:27 -0400289sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target,
290 SkTransferFunctionBehavior premulBehavior) const {
Matt Sarett6de13102017-03-14 14:10:48 -0400291 SkColorSpaceTransferFn fn;
292 if (!target || !target->isNumericalTransferFn(&fn)) {
293 return nullptr;
294 }
295
Matt Sarett73879eb2017-03-22 10:07:50 -0400296 // No need to create a new image if:
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400297 // (1) The color spaces are equal.
Matt Sarett73879eb2017-03-22 10:07:50 -0400298 // (2) The color type is kAlpha8.
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400299 if (SkColorSpace::Equals(this->colorSpace(), target.get()) ||
Matt Sarettcb874232017-04-05 11:41:27 -0400300 kAlpha_8_SkColorType == as_IB(this)->onImageInfo().colorType()) {
301 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400302 }
303
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400304 SkColorType targetColorType = kN32_SkColorType;
305 if (SkTransferFunctionBehavior::kRespect == premulBehavior && target->gammaIsLinear()) {
306 targetColorType = kRGBA_F16_SkColorType;
307 }
308
Matt Sarettcb874232017-04-05 11:41:27 -0400309 // TODO: We might consider making this a deferred conversion?
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400310 return as_IB(this)->onMakeColorSpace(std::move(target), targetColorType, premulBehavior);
Matt Sarett6de13102017-03-14 14:10:48 -0400311}
312
Mike Reedb950b592018-01-02 10:16:12 -0500313sk_sp<SkImage> SkImage::makeNonTextureImage() const {
314 if (!this->isTextureBacked()) {
315 return sk_ref_sp(const_cast<SkImage*>(this));
316 }
317 return this->makeRasterImage();
318}
319
Mike Reedf7ee95c2017-12-04 14:27:01 -0500320sk_sp<SkImage> SkImage::makeRasterImage() const {
321 SkPixmap pm;
322 if (this->peekPixels(&pm)) {
323 return sk_ref_sp(const_cast<SkImage*>(this));
324 }
325
326 const SkImageInfo info = as_IB(this)->onImageInfo();
327 const size_t rowBytes = info.minRowBytes();
328 size_t size = info.computeByteSize(rowBytes);
329 if (SkImageInfo::ByteSizeOverflowed(size)) {
330 return nullptr;
331 }
332
333 sk_sp<SkData> data = SkData::MakeUninitialized(size);
Mike Klein98e38e22018-01-12 15:59:53 +0000334 pm = { info.makeColorSpace(nullptr), data->writable_data(), info.minRowBytes() };
Mike Reedf7ee95c2017-12-04 14:27:01 -0500335 if (!this->readPixels(pm, 0, 0)) {
336 return nullptr;
337 }
338
339 return SkImage::MakeRasterData(info, std::move(data), rowBytes);
340}
341
reed56179002015-07-07 06:11:19 -0700342//////////////////////////////////////////////////////////////////////////////////////
343
reed8b26b992015-05-07 15:36:17 -0700344#if !SK_SUPPORT_GPU
345
Greg Daniel94403452017-04-18 15:52:36 -0400346sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
347 const GrBackendTexture& tex, GrSurfaceOrigin origin,
348 SkAlphaType at, sk_sp<SkColorSpace> cs,
349 TextureReleaseProc releaseP, ReleaseContext releaseC) {
350 return nullptr;
351}
352
Eric Karl914a36b2017-10-12 12:44:50 -0700353bool SkImage::MakeBackendTextureFromSkImage(GrContext*,
354 sk_sp<SkImage>,
355 GrBackendTexture*,
356 BackendTextureReleaseProc*) {
357 return false;
358}
359
Greg Daniel94403452017-04-18 15:52:36 -0400360sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
361 const GrBackendTexture& tex, GrSurfaceOrigin origin,
362 SkAlphaType at, sk_sp<SkColorSpace> cs) {
363 return nullptr;
364}
365
Greg Danielf5d87582017-12-18 14:48:15 -0500366sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
367 const GrBackendTexture& tex, GrSurfaceOrigin origin,
368 SkColorType ct, SkAlphaType at,
369 sk_sp<SkColorSpace> cs) {
370 return nullptr;
371}
372
reed7fb4f8b2016-03-11 04:33:52 -0800373sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
374 const GrBackendObject yuvTextureHandles[3],
375 const SkISize yuvSizes[3],
brianosmandddbe382016-07-20 13:55:39 -0700376 GrSurfaceOrigin origin,
377 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon8e74f802016-01-30 10:01:40 -0800378 return nullptr;
379}
380
Robert Phillipsc25db632017-12-13 09:22:45 -0500381sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
382 const GrBackendTexture yuvTextureHandles[3],
383 const SkISize yuvSizes[3],
384 GrSurfaceOrigin origin,
385 sk_sp<SkColorSpace> imageColorSpace) {
386 return nullptr;
387}
388
Brian Osman041f7df2017-02-07 11:23:28 -0500389sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace) const {
390 return nullptr;
391}
392
reed7fb4f8b2016-03-11 04:33:52 -0800393#endif
394
395///////////////////////////////////////////////////////////////////////////////////////////////////
396
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500397bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700398 SkASSERT(image);
399 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500400 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700401}
402
403void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
404 SkASSERT(image);
405 SkASSERT(ctx);
406 as_IB(image)->onUnpinAsTexture(ctx);
407}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500408
409///////////////////////////////////////////////////////////////////////////////////////////////////
410
411sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src,
412 SkColorSpace* colorSpace) {
413 // Read the pixels out of the source image, with no conversion
414 SkImageInfo info = as_IB(src)->onImageInfo();
415 if (kUnknown_SkColorType == info.colorType()) {
416 SkDEBUGFAIL("Unexpected color type");
417 return nullptr;
418 }
419
420 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400421 size_t size = info.computeByteSize(rowBytes);
Mike Reedc5eb97d2017-10-09 10:42:51 -0400422 if (SkImageInfo::ByteSizeOverflowed(size)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400423 return nullptr;
424 }
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500425 auto data = SkData::MakeUninitialized(size);
426 if (!data) {
427 return nullptr;
428 }
429
430 SkPixmap pm(info, data->writable_data(), rowBytes);
431 if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) {
432 return nullptr;
433 }
434
435 // Wrap them in a new image with a different color space
436 return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes);
437}