blob: 079992a15e442e9484fba31df68b5d10b6e17a7e [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 Ludwig8ee6cf32019-08-02 09:57:04 -0400286 SkImageFilter_Base::OutputProperties outputProperties(fInfo.colorType(), fInfo.colorSpace());
Michael Ludwigb4580352019-06-21 16:01:42 -0400287
288 // The filters operate in the local space of the src image, where (0,0) corresponds to the
289 // subset's top left corner. But the clip bounds and any crop rects on the filters are in the
290 // original coordinate system, so configure the CTM to correct crop rects and explicitly adjust
291 // the clip bounds (since it is assumed to already be in image space).
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400292 SkImageFilter_Base::Context context(SkMatrix::MakeTrans(-subset.x(), -subset.y()),
293 clipBounds.makeOffset(-subset.x(), -subset.y()),
294 cache.get(), outputProperties);
brianosman2a75e5d2016-09-22 07:15:37 -0700295
Michael Ludwig8ee6cf32019-08-02 09:57:04 -0400296 sk_sp<SkSpecialImage> result = as_IFB(filter)->filterImage(srcSpecialImage.get(), context,
297 offset);
brianosman04a44d02016-09-21 09:46:57 -0700298 if (!result) {
299 return nullptr;
300 }
senorblanco5878dbd2016-05-19 14:50:29 -0700301
Michael Ludwigb4580352019-06-21 16:01:42 -0400302 // The output image and offset are relative to the subset rectangle, so the offset needs to
303 // be shifted to put it in the correct spot with respect to the original coordinate system
304 offset->fX += subset.x();
305 offset->fY += subset.y();
306
307 // Final clip against the exact clipBounds (the clip provided in the context gets adjusted
308 // to account for pixel-moving filters so doesn't always exactly match when finished). The
309 // clipBounds are translated into the clippedDstRect coordinate space, including the
310 // result->subset() ensures that the result's image pixel origin does not affect results.
311 SkIRect dstRect = result->subset();
312 SkIRect clippedDstRect = dstRect;
313 if (!clippedDstRect.intersect(clipBounds.makeOffset(result->subset().x() - offset->x(),
314 result->subset().y() - offset->y()))) {
brianosman04a44d02016-09-21 09:46:57 -0700315 return nullptr;
316 }
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500317
Michael Ludwigb4580352019-06-21 16:01:42 -0400318 // Adjust the geometric offset if the top-left corner moved as well
319 offset->fX += (clippedDstRect.x() - dstRect.x());
320 offset->fY += (clippedDstRect.y() - dstRect.y());
321 *outSubset = clippedDstRect;
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500322 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700323}
324
fmalitaddbbdda2015-08-20 08:47:26 -0700325bool SkImage::isLazyGenerated() const {
326 return as_IB(this)->onIsLazyGenerated();
327}
328
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400329bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); }
reed9e2ed832016-10-31 05:27:28 -0700330
Brian Osmanf6db4952018-07-16 13:06:02 -0400331sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target) const {
Brian Osman15f0f292018-10-01 14:14:46 -0400332 if (!target) {
Matt Sarett6de13102017-03-14 14:10:48 -0400333 return nullptr;
334 }
335
Matt Sarett73879eb2017-03-22 10:07:50 -0400336 // No need to create a new image if:
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400337 // (1) The color spaces are equal.
Matt Sarett73879eb2017-03-22 10:07:50 -0400338 // (2) The color type is kAlpha8.
Brian Osmanb4ae4992018-10-18 11:16:14 -0400339 SkColorSpace* colorSpace = this->colorSpace();
340 if (!colorSpace) {
341 colorSpace = sk_srgb_singleton();
342 }
Brian Osmanf48c9962019-01-14 11:15:50 -0500343 if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) {
Matt Sarettcb874232017-04-05 11:41:27 -0400344 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400345 }
346
Robert Phillips6603a172019-03-05 12:35:44 -0500347 // CONTEXT TODO: propagate the context parameter to the top-level API
348#if SK_SUPPORT_GPU
349 return as_IB(this)->onMakeColorTypeAndColorSpace(as_IB(this)->context(),
350#else
351 return as_IB(this)->onMakeColorTypeAndColorSpace(nullptr,
352#endif
353 this->colorType(), std::move(target));
Brian Osmanf48c9962019-01-14 11:15:50 -0500354}
355
356sk_sp<SkImage> SkImage::makeColorTypeAndColorSpace(SkColorType targetColorType,
357 sk_sp<SkColorSpace> targetColorSpace) const {
358 if (kUnknown_SkColorType == targetColorType || !targetColorSpace) {
359 return nullptr;
360 }
361
362 SkColorType colorType = this->colorType();
363 SkColorSpace* colorSpace = this->colorSpace();
364 if (!colorSpace) {
365 colorSpace = sk_srgb_singleton();
366 }
367 if (colorType == targetColorType &&
368 (SkColorSpace::Equals(colorSpace, targetColorSpace.get()) || this->isAlphaOnly())) {
369 return sk_ref_sp(const_cast<SkImage*>(this));
370 }
371
Robert Phillips6603a172019-03-05 12:35:44 -0500372 // CONTEXT TODO: propagate the context parameter to the top-level API
373#if SK_SUPPORT_GPU
374 return as_IB(this)->onMakeColorTypeAndColorSpace(as_IB(this)->context(),
375#else
376 return as_IB(this)->onMakeColorTypeAndColorSpace(nullptr,
377#endif
378 targetColorType, std::move(targetColorSpace));
Matt Sarett6de13102017-03-14 14:10:48 -0400379}
380
Mike Reedb950b592018-01-02 10:16:12 -0500381sk_sp<SkImage> SkImage::makeNonTextureImage() const {
382 if (!this->isTextureBacked()) {
383 return sk_ref_sp(const_cast<SkImage*>(this));
384 }
385 return this->makeRasterImage();
386}
387
Mike Reedf7ee95c2017-12-04 14:27:01 -0500388sk_sp<SkImage> SkImage::makeRasterImage() const {
389 SkPixmap pm;
390 if (this->peekPixels(&pm)) {
391 return sk_ref_sp(const_cast<SkImage*>(this));
392 }
393
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400394 const size_t rowBytes = fInfo.minRowBytes();
395 size_t size = fInfo.computeByteSize(rowBytes);
Mike Reedf7ee95c2017-12-04 14:27:01 -0500396 if (SkImageInfo::ByteSizeOverflowed(size)) {
397 return nullptr;
398 }
399
400 sk_sp<SkData> data = SkData::MakeUninitialized(size);
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400401 pm = {fInfo.makeColorSpace(nullptr), data->writable_data(), fInfo.minRowBytes()};
Mike Reedf7ee95c2017-12-04 14:27:01 -0500402 if (!this->readPixels(pm, 0, 0)) {
403 return nullptr;
404 }
405
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400406 return SkImage::MakeRasterData(fInfo, std::move(data), rowBytes);
Mike Reedf7ee95c2017-12-04 14:27:01 -0500407}
408
reed56179002015-07-07 06:11:19 -0700409//////////////////////////////////////////////////////////////////////////////////////
410
reed8b26b992015-05-07 15:36:17 -0700411#if !SK_SUPPORT_GPU
412
Greg Daniel94403452017-04-18 15:52:36 -0400413sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
414 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Brian Salomonbfd27492018-03-19 14:08:51 -0400415 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
Greg Daniel94403452017-04-18 15:52:36 -0400416 TextureReleaseProc releaseP, ReleaseContext releaseC) {
417 return nullptr;
418}
419
Eric Karl914a36b2017-10-12 12:44:50 -0700420bool SkImage::MakeBackendTextureFromSkImage(GrContext*,
421 sk_sp<SkImage>,
422 GrBackendTexture*,
423 BackendTextureReleaseProc*) {
424 return false;
425}
426
Greg Daniel94403452017-04-18 15:52:36 -0400427sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
428 const GrBackendTexture& tex, GrSurfaceOrigin origin,
Greg Danielf5d87582017-12-18 14:48:15 -0500429 SkColorType ct, SkAlphaType at,
430 sk_sp<SkColorSpace> cs) {
431 return nullptr;
432}
433
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400434sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopy(GrContext* context,
435 SkYUVColorSpace yuvColorSpace,
436 const GrBackendTexture yuvaTextures[],
437 const SkYUVAIndex yuvaIndices[4],
438 SkISize imageSize,
439 GrSurfaceOrigin imageOrigin,
440 sk_sp<SkColorSpace> imageColorSpace) {
441 return nullptr;
442}
443
444sk_sp<SkImage> SkImage::MakeFromYUVATexturesCopyWithExternalBackend(
445 GrContext* context,
446 SkYUVColorSpace yuvColorSpace,
447 const GrBackendTexture yuvaTextures[],
448 const SkYUVAIndex yuvaIndices[4],
449 SkISize imageSize,
450 GrSurfaceOrigin imageOrigin,
451 const GrBackendTexture& backendTexture,
452 sk_sp<SkColorSpace> imageColorSpace) {
453 return nullptr;
454}
455
reed7fb4f8b2016-03-11 04:33:52 -0800456sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
Brian Salomon6a426c12018-03-15 12:16:02 -0400457 const GrBackendTexture[3],
Brian Salomon6a426c12018-03-15 12:16:02 -0400458 GrSurfaceOrigin origin,
459 sk_sp<SkColorSpace> imageColorSpace) {
460 return nullptr;
461}
462
Brian Salomond2fcfb52018-09-17 21:57:11 -0400463sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
464 GrContext* context, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400465 GrSurfaceOrigin surfaceOrigin, const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400466 sk_sp<SkColorSpace> colorSpace) {
467 return nullptr;
468}
469
Brian Salomon6a426c12018-03-15 12:16:02 -0400470sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace space,
471 const GrBackendTexture[2],
Robert Phillipsc25db632017-12-13 09:22:45 -0500472 GrSurfaceOrigin origin,
473 sk_sp<SkColorSpace> imageColorSpace) {
474 return nullptr;
475}
476
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400477sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace,
478 GrMipMapped mipMapped) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500479 return nullptr;
480}
481
Brian Salomond2fcfb52018-09-17 21:57:11 -0400482sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(GrContext* context,
483 SkYUVColorSpace yuvColorSpace,
484 const GrBackendTexture nv12Textures[2],
485 GrSurfaceOrigin surfaceOrigin,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400486 const GrBackendTexture& backendTexture,
Brian Salomond2fcfb52018-09-17 21:57:11 -0400487 sk_sp<SkColorSpace> colorSpace) {
488 return nullptr;
489}
490
reed7fb4f8b2016-03-11 04:33:52 -0800491#endif
492
493///////////////////////////////////////////////////////////////////////////////////////////////////
494
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500495bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700496 SkASSERT(image);
497 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500498 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700499}
500
501void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
502 SkASSERT(image);
503 SkASSERT(ctx);
504 as_IB(image)->onUnpinAsTexture(ctx);
505}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500506
Mike Reedf2c73642018-05-29 15:41:27 -0400507SkIRect SkImage_getSubset(const SkImage* image) {
508 SkASSERT(image);
509 return as_IB(image)->onGetSubset();
510}
511
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500512///////////////////////////////////////////////////////////////////////////////////////////////////
513
514sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src,
515 SkColorSpace* colorSpace) {
516 // Read the pixels out of the source image, with no conversion
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400517 const SkImageInfo& info = src->imageInfo();
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500518 if (kUnknown_SkColorType == info.colorType()) {
519 SkDEBUGFAIL("Unexpected color type");
520 return nullptr;
521 }
522
523 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400524 size_t size = info.computeByteSize(rowBytes);
Mike Reedc5eb97d2017-10-09 10:42:51 -0400525 if (SkImageInfo::ByteSizeOverflowed(size)) {
Mike Reedf0ffb892017-10-03 14:47:21 -0400526 return nullptr;
527 }
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500528 auto data = SkData::MakeUninitialized(size);
529 if (!data) {
530 return nullptr;
531 }
532
533 SkPixmap pm(info, data->writable_data(), rowBytes);
534 if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) {
535 return nullptr;
536 }
537
538 // Wrap them in a new image with a different color space
539 return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes);
540}