blob: de6729b97e9cd96886a08c218b994837fb0cbdde [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkData.h"
11#include "include/core/SkImageEncoder.h"
12#include "include/core/SkImageFilter.h"
13#include "include/core/SkImageGenerator.h"
14#include "include/core/SkPicture.h"
15#include "include/core/SkString.h"
16#include "include/core/SkSurface.h"
17#include "src/core/SkBitmapCache.h"
18#include "src/core/SkCachedData.h"
19#include "src/core/SkColorSpacePriv.h"
20#include "src/core/SkImageFilterCache.h"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -040021#include "src/core/SkImageFilter_Base.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkImagePriv.h"
23#include "src/core/SkNextID.h"
24#include "src/core/SkSpecialImage.h"
25#include "src/image/SkImage_Base.h"
26#include "src/image/SkReadPixelsRec.h"
27#include "src/shaders/SkImageShader.h"
reed56179002015-07-07 06:11:19 -070028
bsalomon55812362015-06-10 08:49:28 -070029#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/gpu/GrContext.h"
31#include "include/gpu/GrTexture.h"
32#include "src/image/SkImage_Gpu.h"
bsalomon55812362015-06-10 08:49:28 -070033#endif
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "include/gpu/GrBackendSurface.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000035
Brian Salomon5ad6fd32019-03-21 15:30:08 -040036SkImage::SkImage(const SkImageInfo& info, uint32_t uniqueID)
37 : fInfo(info)
38 , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) {
39 SkASSERT(info.width() > 0);
40 SkASSERT(info.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
Brian Salomon5ad6fd32019-03-21 15:30:08 -040051bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX,
52 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 Osmane50cdf02018-10-19 13:02:14 -040065 if (as_IB(this)->getROPixels(&bm, 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
Brian Salomon5ad6fd32019-03-21 15:30:08 -040078SkColorType SkImage::colorType() const { return fInfo.colorType(); }
Greg Daniel56008aa2018-03-14 15:33:42 -040079
Brian Salomon5ad6fd32019-03-21 15:30:08 -040080SkAlphaType SkImage::alphaType() const { return fInfo.alphaType(); }
brianosman69c166d2016-08-17 14:01:05 -070081
Brian Salomon5ad6fd32019-03-21 15:30:08 -040082SkColorSpace* SkImage::colorSpace() const { return fInfo.colorSpace(); }
Matt Sarett5b4599f2017-03-02 12:42:35 -050083
Brian Salomon5ad6fd32019-03-21 15:30:08 -040084sk_sp<SkColorSpace> SkImage::refColorSpace() const { return fInfo.refColorSpace(); }
Matt Sarett5b4599f2017-03-02 12:42:35 -050085
Mike Reede25b4472019-04-02 17:49:12 -040086sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy,
reed5671c5b2016-03-09 14:47:34 -080087 const SkMatrix* localMatrix) const {
Mike Reede25b4472019-04-02 17:49:12 -040088 return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070089}
90
Mike Reed6409f842017-07-11 16:03:13 -040091sk_sp<SkData> SkImage::encodeToData(SkEncodedImageFormat type, int quality) const {
Leon Scroggins294c1c42016-11-08 14:29:46 +000092 SkBitmap bm;
Brian Osmane50cdf02018-10-19 13:02:14 -040093 if (as_IB(this)->getROPixels(&bm)) {
Mike Reed25eef6b2017-12-08 16:20:58 -050094 return SkEncodeBitmap(bm, type, quality);
Leon Scroggins294c1c42016-11-08 14:29:46 +000095 }
96 return nullptr;
97}
98
Mike Reedef038482017-12-16 08:41:28 -050099sk_sp<SkData> SkImage::encodeToData() const {
100 if (auto encoded = this->refEncodedData()) {
Mike Reed6409f842017-07-11 16:03:13 -0400101 return encoded;
fmalita2be71252015-09-03 07:17:25 -0700102 }
103
Brian Osmane50cdf02018-10-19 13:02:14 -0400104 return this->encodeToData(SkEncodedImageFormat::kPNG, 100);
fmalita2be71252015-09-03 07:17:25 -0700105}
106
Mike Reed6409f842017-07-11 16:03:13 -0400107sk_sp<SkData> SkImage::refEncodedData() const {
108 return sk_sp<SkData>(as_IB(this)->onRefEncoded());
reed871872f2015-06-22 12:48:26 -0700109}
110
reed7fb4f8b2016-03-11 04:33:52 -0800111sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700112 if (nullptr == encoded || 0 == encoded->size()) {
113 return nullptr;
reed5965c8a2015-01-07 18:04:45 -0800114 }
Robert Phillipsa07bf672018-07-13 10:18:38 -0400115 return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(std::move(encoded)),
116 subset);
reed5965c8a2015-01-07 18:04:45 -0800117}
118
Mike Reed6409f842017-07-11 16:03:13 -0400119///////////////////////////////////////////////////////////////////////////////////////////////////
120
reed7fb4f8b2016-03-11 04:33:52 -0800121sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700122 if (subset.isEmpty()) {
123 return nullptr;
124 }
125
126 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
127 if (!bounds.contains(subset)) {
128 return nullptr;
129 }
130
131 // optimization : return self if the subset == our bounds
132 if (bounds == subset) {
reed7fb4f8b2016-03-11 04:33:52 -0800133 return sk_ref_sp(const_cast<SkImage*>(this));
reed7b6945b2015-09-24 00:50:58 -0700134 }
Robert Phillips6603a172019-03-05 12:35:44 -0500135
136 // CONTEXT TODO: propagate the context parameter to the top-level API
137#if SK_SUPPORT_GPU
138 return as_IB(this)->onMakeSubset(as_IB(this)->context(), subset);
139#else
140 return as_IB(this)->onMakeSubset(nullptr, subset);
141#endif
reedf803da12015-01-23 05:58:07 -0800142}
143
bsalomon55812362015-06-10 08:49:28 -0700144#if SK_SUPPORT_GPU
145
146GrTexture* SkImage::getTexture() const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400147 return as_IB(this)->onGetTexture();
bsalomon55812362015-06-10 08:49:28 -0700148}
149
Jim Van Verth21bd60d2018-10-12 15:00:20 -0400150bool SkImage::isTextureBacked() const { return as_IB(this)->onIsTextureBacked(); }
bsalomon55812362015-06-10 08:49:28 -0700151
Robert Phillipsc5509952018-04-04 15:54:55 -0400152GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
153 GrSurfaceOrigin* origin) const {
154 return as_IB(this)->onGetBackendTexture(flushPendingGrContextIO, origin);
155}
Robert Phillipsc5509952018-04-04 15:54:55 -0400156
Brian Osman5bbd0762017-05-08 11:07:42 -0400157bool SkImage::isValid(GrContext* context) const {
Khushalc421ca12018-06-26 14:38:34 -0700158 if (context && context->abandoned()) {
Brian Osman5bbd0762017-05-08 11:07:42 -0400159 return false;
160 }
161 return as_IB(this)->onIsValid(context);
162}
163
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400164GrSemaphoresSubmitted SkImage::flush(GrContext* context, const GrFlushInfo& flushInfo) {
165 return as_IB(this)->onFlush(context, flushInfo);
166}
167
168void SkImage::flush(GrContext* context) { as_IB(this)->onFlush(context, {}); }
169
bsalomon55812362015-06-10 08:49:28 -0700170#else
171
halcanary96fcdcc2015-08-27 07:41:13 -0700172GrTexture* SkImage::getTexture() const { return nullptr; }
bsalomon55812362015-06-10 08:49:28 -0700173
174bool SkImage::isTextureBacked() const { return false; }
175
Robert Phillipsc5509952018-04-04 15:54:55 -0400176GrBackendTexture SkImage::getBackendTexture(bool flushPendingGrContextIO,
177 GrSurfaceOrigin* origin) const {
178 return GrBackendTexture(); // invalid
179}
180
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
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400188GrSemaphoresSubmitted SkImage::flush(GrContext*, const GrFlushInfo&) {
189 return GrSemaphoresSubmitted::kNo;
190}
191
192void SkImage::flush(GrContext*) {}
193
bsalomon55812362015-06-10 08:49:28 -0700194#endif
195
reed@google.com4f7c6152014-02-06 14:11:56 +0000196///////////////////////////////////////////////////////////////////////////////
197
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400198SkImage_Base::SkImage_Base(const SkImageInfo& info, uint32_t uniqueID)
199 : INHERITED(info, uniqueID), fAddedToRasterCache(false) {}
fmalita3b0d5322015-09-18 08:07:31 -0700200
201SkImage_Base::~SkImage_Base() {
Mike Reed30301c42018-07-19 09:39:21 -0400202 if (fAddedToRasterCache.load()) {
fmalita3b0d5322015-09-18 08:07:31 -0700203 SkNotifyBitmapGenIDIsStale(this->uniqueID());
204 }
205}
206
Robert Phillips8caf85f2018-04-05 09:30:38 -0400207GrBackendTexture SkImage_Base::onGetBackendTexture(bool flushPendingGrContextIO,
208 GrSurfaceOrigin* origin) const {
209 return GrBackendTexture(); // invalid
210}
211
reed09553032015-11-23 12:32:16 -0800212bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
213 return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint);
reed871872f2015-06-22 12:48:26 -0700214}
215
216///////////////////////////////////////////////////////////////////////////////////////////////////
reedf803da12015-01-23 05:58:07 -0800217
reed7fb4f8b2016-03-11 04:33:52 -0800218sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) {
Brian Osman41ba8262018-10-19 14:56:26 -0400219 if (!bm.pixelRef()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700220 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
Jim Van Verthe24b5872018-10-29 16:26:02 -0400230sk_sp<SkCachedData> SkImage_Base::getPlanes(SkYUVASizeInfo*, SkYUVAIndex[4],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400231 SkYUVColorSpace*, const void*[4]) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400232 return nullptr;
233}
234
Cary Clark4f5a79c2018-02-07 15:51:00 -0500235bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap) const {
reed3c065112015-07-08 12:46:22 -0700236 // As the base-class, all we can do is make a copy (regardless of mode).
237 // Subclasses that want to be more optimal should override.
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400238 SkImageInfo info = fInfo.makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
reed3c065112015-07-08 12:46:22 -0700239 if (!bitmap->tryAllocPixels(info)) {
240 return false;
241 }
242 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
243 bitmap->reset();
244 return false;
245 }
246
Cary Clark4f5a79c2018-02-07 15:51:00 -0500247 bitmap->setImmutable();
reed3c065112015-07-08 12:46:22 -0700248 return true;
249}
250
Brian Osman138ea972016-12-16 11:55:18 -0500251sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
Matt Sarette94255d2017-01-09 12:38:59 -0500252 const SkMatrix* matrix, const SkPaint* paint,
253 BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
Mike Reed185130c2017-02-15 15:14:16 -0500254 return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture),
255 matrix, paint, bitDepth,
256 std::move(colorSpace)));
Matt Sarette94255d2017-01-09 12:38:59 -0500257}
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500258
senorblanco5878dbd2016-05-19 14:50:29 -0700259sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
260 const SkIRect& clipBounds, SkIRect* outSubset,
261 SkIPoint* offset) const {
Robert Phillips27467652019-01-10 16:34:22 -0500262 GrContext* context = as_IB(this)->context();
263
264 return this->makeWithFilter(context, filter, subset, clipBounds, outSubset, offset);
265}
266
267sk_sp<SkImage> SkImage::makeWithFilter(GrContext* grContext,
268 const SkImageFilter* filter, const SkIRect& subset,
269 const SkIRect& clipBounds, SkIRect* outSubset,
270 SkIPoint* offset) const {
brianosman04a44d02016-09-21 09:46:57 -0700271 if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) {
272 return nullptr;
273 }
Robert Phillipsbc429442019-02-20 08:26:03 -0500274 sk_sp<SkSpecialImage> srcSpecialImage =
275#if SK_SUPPORT_GPU
276 SkSpecialImage::MakeFromImage(grContext, subset, sk_ref_sp(const_cast<SkImage*>(this)));
277#else
278 SkSpecialImage::MakeFromImage(nullptr, subset, sk_ref_sp(const_cast<SkImage*>(this)));
279#endif
brianosman04a44d02016-09-21 09:46:57 -0700280 if (!srcSpecialImage) {
281 return nullptr;
282 }
senorblanco5878dbd2016-05-19 14:50:29 -0700283
Hal Canary67b39de2016-11-07 11:47:44 -0500284 sk_sp<SkImageFilterCache> cache(
brianosman04a44d02016-09-21 09:46:57 -0700285 SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize));
Michael Ludwigb4580352019-06-21 16:01:42 -0400286
287 // The filters operate in the local space of the src image, where (0,0) corresponds to the
288 // subset's top left corner. But the clip bounds and any crop rects on the filters are in the
289 // original coordinate system, so configure the CTM to correct crop rects and explicitly adjust
290 // the clip bounds (since it is assumed to already be in image space).
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400291 SkImageFilter_Base::Context context(SkMatrix::MakeTrans(-subset.x(), -subset.y()),
292 clipBounds.makeOffset(-subset.x(), -subset.y()),
Michael Ludwige30a4852019-08-14 14:35:42 -0400293 cache.get(), fInfo.colorType(), fInfo.colorSpace(),
294 srcSpecialImage.get());
brianosman2a75e5d2016-09-22 07:15:37 -0700295
Michael Ludwigea071232019-08-26 10:52:15 -0400296 sk_sp<SkSpecialImage> result = as_IFB(filter)->filterImage(context).imageAndOffset(offset);
brianosman04a44d02016-09-21 09:46:57 -0700297 if (!result) {
298 return nullptr;
299 }
senorblanco5878dbd2016-05-19 14:50:29 -0700300
Michael Ludwigb4580352019-06-21 16:01:42 -0400301 // The output image and offset are relative to the subset rectangle, so the offset needs to
302 // be shifted to put it in the correct spot with respect to the original coordinate system
303 offset->fX += subset.x();
304 offset->fY += subset.y();
305
306 // Final clip against the exact clipBounds (the clip provided in the context gets adjusted
307 // to account for pixel-moving filters so doesn't always exactly match when finished). The
308 // clipBounds are translated into the clippedDstRect coordinate space, including the
309 // result->subset() ensures that the result's image pixel origin does not affect results.
310 SkIRect dstRect = result->subset();
311 SkIRect clippedDstRect = dstRect;
312 if (!clippedDstRect.intersect(clipBounds.makeOffset(result->subset().x() - offset->x(),
313 result->subset().y() - offset->y()))) {
brianosman04a44d02016-09-21 09:46:57 -0700314 return nullptr;
315 }
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500316
Michael Ludwigb4580352019-06-21 16:01:42 -0400317 // Adjust the geometric offset if the top-left corner moved as well
318 offset->fX += (clippedDstRect.x() - dstRect.x());
319 offset->fY += (clippedDstRect.y() - dstRect.y());
320 *outSubset = clippedDstRect;
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500321 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700322}
323
fmalitaddbbdda2015-08-20 08:47:26 -0700324bool SkImage::isLazyGenerated() const {
325 return as_IB(this)->onIsLazyGenerated();
326}
327
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400328bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); }
reed9e2ed832016-10-31 05:27:28 -0700329
Brian Osmanf6db4952018-07-16 13:06:02 -0400330sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osman15f0f292018-10-01 14:14:46 -0400331 if (!target) {
Matt Sarett6de13102017-03-14 14:10:48 -0400332 return nullptr;
333 }
334
Matt Sarett73879eb2017-03-22 10:07:50 -0400335 // No need to create a new image if:
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400336 // (1) The color spaces are equal.
Matt Sarett73879eb2017-03-22 10:07:50 -0400337 // (2) The color type is kAlpha8.
Brian Osmanb4ae4992018-10-18 11:16:14 -0400338 SkColorSpace* colorSpace = this->colorSpace();
339 if (!colorSpace) {
340 colorSpace = sk_srgb_singleton();
341 }
Brian Osmanf48c9962019-01-14 11:15:50 -0500342 if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) {
Matt Sarettcb874232017-04-05 11:41:27 -0400343 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400344 }
345
Robert Phillips6603a172019-03-05 12:35:44 -0500346 // CONTEXT TODO: propagate the context parameter to the top-level API
347#if SK_SUPPORT_GPU
348 return as_IB(this)->onMakeColorTypeAndColorSpace(as_IB(this)->context(),
349#else
350 return as_IB(this)->onMakeColorTypeAndColorSpace(nullptr,
351#endif
352 this->colorType(), std::move(target));
Brian Osmanf48c9962019-01-14 11:15:50 -0500353}
354
355sk_sp<SkImage> SkImage::makeColorTypeAndColorSpace(SkColorType targetColorType,
356 sk_sp<SkColorSpace> targetColorSpace) const {
357 if (kUnknown_SkColorType == targetColorType || !targetColorSpace) {
358 return nullptr;
359 }
360
361 SkColorType colorType = this->colorType();
362 SkColorSpace* colorSpace = this->colorSpace();
363 if (!colorSpace) {
364 colorSpace = sk_srgb_singleton();
365 }
366 if (colorType == targetColorType &&
367 (SkColorSpace::Equals(colorSpace, targetColorSpace.get()) || this->isAlphaOnly())) {
368 return sk_ref_sp(const_cast<SkImage*>(this));
369 }
370
Robert Phillips6603a172019-03-05 12:35:44 -0500371 // CONTEXT TODO: propagate the context parameter to the top-level API
372#if SK_SUPPORT_GPU
373 return as_IB(this)->onMakeColorTypeAndColorSpace(as_IB(this)->context(),
374#else
375 return as_IB(this)->onMakeColorTypeAndColorSpace(nullptr,
376#endif
377 targetColorType, std::move(targetColorSpace));
Matt Sarett6de13102017-03-14 14:10:48 -0400378}
379
Brian Osmand5148372019-08-14 16:14:51 -0400380sk_sp<SkImage> SkImage::reinterpretColorSpace(sk_sp<SkColorSpace> target) const {
381 if (!target) {
382 return nullptr;
383 }
384
385 // No need to create a new image if:
386 // (1) The color spaces are equal.
387 // (2) The color type is kAlpha8.
388 SkColorSpace* colorSpace = this->colorSpace();
389 if (!colorSpace) {
390 colorSpace = sk_srgb_singleton();
391 }
392 if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) {
393 return sk_ref_sp(const_cast<SkImage*>(this));
394 }
395
396 return as_IB(this)->onReinterpretColorSpace(std::move(target));
397}
398
Mike Reedb950b592018-01-02 10:16:12 -0500399sk_sp<SkImage> SkImage::makeNonTextureImage() const {
400 if (!this->isTextureBacked()) {
401 return sk_ref_sp(const_cast<SkImage*>(this));
402 }
403 return this->makeRasterImage();
404}
405
Mike Reedf7ee95c2017-12-04 14:27:01 -0500406sk_sp<SkImage> SkImage::makeRasterImage() const {
407 SkPixmap pm;
408 if (this->peekPixels(&pm)) {
409 return sk_ref_sp(const_cast<SkImage*>(this));
410 }
411
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400412 const size_t rowBytes = fInfo.minRowBytes();
413 size_t size = fInfo.computeByteSize(rowBytes);
Mike Reedf7ee95c2017-12-04 14:27:01 -0500414 if (SkImageInfo::ByteSizeOverflowed(size)) {
415 return nullptr;
416 }
417
418 sk_sp<SkData> data = SkData::MakeUninitialized(size);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400419 pm = {fInfo.makeColorSpace(nullptr), data->writable_data(), fInfo.minRowBytes()};
Mike Reedf7ee95c2017-12-04 14:27:01 -0500420 if (!this->readPixels(pm, 0, 0)) {
421 return nullptr;
422 }
423
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400424 return SkImage::MakeRasterData(fInfo, std::move(data), rowBytes);
Mike Reedf7ee95c2017-12-04 14:27:01 -0500425}
426
reed56179002015-07-07 06:11:19 -0700427//////////////////////////////////////////////////////////////////////////////////////
428
reed8b26b992015-05-07 15:36:17 -0700429#if !SK_SUPPORT_GPU
430
Mike Reeda5acbf92019-08-14 11:08:41 -0400431sk_sp<SkImage> SkImage::DecodeToTexture(GrContext*, const void*, size_t, const SkIRect*) {
432 return nullptr;
433}
434
Greg Daniel94403452017-04-18 15:52:36 -0400435sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
436 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomonbfd27492018-03-19 14:08:51 -0400437 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
Greg Daniel94403452017-04-18 15:52:36 -0400438 TextureReleaseProc releaseP, ReleaseContext releaseC) {
439 return nullptr;
440}
441
Eric Karl914a36b2017-10-12 12:44:50 -0700442bool SkImage::MakeBackendTextureFromSkImage(GrContext*,
443 sk_sp<SkImage>,
444 GrBackendTexture*,
445 BackendTextureReleaseProc*) {
446 return false;
447}
448
Greg Daniel94403452017-04-18 15:52:36 -0400449sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
450 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Greg Danielf5d87582017-12-18 14:48:15 -0500451 SkColorType ct, SkAlphaType at,
452 sk_sp<SkColorSpace> cs) {
453 return nullptr;
454}
455
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400456sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* context,
457 SkYUVColorSpace yuvColorSpace,
458 const GrBackendTexture yuvaTextures[],
459 const SkYUVAIndex yuvaIndices[4],
460 SkISize imageSize,
461 GrSurfaceOrigin imageOrigin,
462 sk_sp<SkColorSpace> imageColorSpace) {
463 return nullptr;
464}
465
466sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
467 GrContext* context,
468 SkYUVColorSpace yuvColorSpace,
469 const GrBackendTexture yuvaTextures[],
470 const SkYUVAIndex yuvaIndices[4],
471 SkISize imageSize,
472 GrSurfaceOrigin imageOrigin,
473 const GrBackendTexture& backendTexture,
Robert Phillipseb3eb982019-08-19 13:35:48 -0400474 sk_sp<SkColorSpace> imageColorSpace,
475 TextureReleaseProc textureReleaseProc,
476 ReleaseContext releaseContext) {
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400477 return nullptr;
478}
479
reed7fb4f8b2016-03-11 04:33:52 -0800480sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
Brian Salomon6a426c12018-03-15 12:16:02 -0400481 const GrBackendTexture[3],
Brian Salomon6a426c12018-03-15 12:16:02 -0400482 GrSurfaceOrigin origin,
483 sk_sp<SkColorSpace> imageColorSpace) {
484 return nullptr;
485}
486
Brian Salomond2fcfb52018-09-17 21:57:11 -0400487sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
488 GrContext* context, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400489 GrSurfaceOrigin surfaceOrigin, const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400490 sk_sp<SkColorSpace> colorSpace) {
491 return nullptr;
492}
493
Brian Salomon6a426c12018-03-15 12:16:02 -0400494sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace space,
495 const GrBackendTexture[2],
Robert Phillipsc25db632017-12-13 09:22:45 -0500496 GrSurfaceOrigin origin,
497 sk_sp<SkColorSpace> imageColorSpace) {
498 return nullptr;
499}
500
Brian Osmand566e2e2019-08-14 13:19:04 -0400501sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, GrMipMapped mipMapped) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500502 return nullptr;
503}
504
Robert Phillipseb3eb982019-08-19 13:35:48 -0400505sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopyWithExternalBackend(
506 GrContext* context,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400507 SkYUVColorSpace yuvColorSpace,
508 const GrBackendTexture nv12Textures[2],
Robert Phillipseb3eb982019-08-19 13:35:48 -0400509 GrSurfaceOrigin imageOrigin,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400510 const GrBackendTexture& backendTexture,
Robert Phillipseb3eb982019-08-19 13:35:48 -0400511 sk_sp<SkColorSpace> imageColorSpace,
512 TextureReleaseProc textureReleaseProc,
513 ReleaseContext releaseContext) {
Brian Salomond2fcfb52018-09-17 21:57:11 -0400514 return nullptr;
515}
516
reed7fb4f8b2016-03-11 04:33:52 -0800517#endif
518
519///////////////////////////////////////////////////////////////////////////////////////////////////
520
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500521bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700522 SkASSERT(image);
523 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500524 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700525}
526
527void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
528 SkASSERT(image);
529 SkASSERT(ctx);
530 as_IB(image)->onUnpinAsTexture(ctx);
531}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500532
Mike Reedf2c73642018-05-29 15:41:27 -0400533SkIRect SkImage_getSubset(const SkImage* image) {
534 SkASSERT(image);
535 return as_IB(image)->onGetSubset();
536}