blob: 0847eb1c572d42ff6afa7749ef079619945cc5d0 [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"
Mike Klein919cc452017-03-18 15:36:52 +000011#include "SkColorSpace_Base.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050012#include "SkCrossContextImageData.h"
reed871872f2015-06-22 12:48:26 -070013#include "SkData.h"
halcanaryf2848b62015-12-10 12:40:23 -080014#include "SkImageEncoder.h"
senorblanco5878dbd2016-05-19 14:50:29 -070015#include "SkImageFilter.h"
brianosman04a44d02016-09-21 09:46:57 -070016#include "SkImageFilterCache.h"
reed5965c8a2015-01-07 18:04:45 -080017#include "SkImageGenerator.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000018#include "SkImagePriv.h"
reed856e9d92015-09-30 12:21:45 -070019#include "SkImageShader.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000020#include "SkImage_Base.h"
reed80c772b2015-07-30 18:58:23 -070021#include "SkNextID.h"
reed7fb4f8b2016-03-11 04:33:52 -080022#include "SkPicture.h"
reed56179002015-07-07 06:11:19 -070023#include "SkPixelRef.h"
fmalita2be71252015-09-03 07:17:25 -070024#include "SkPixelSerializer.h"
Robert Phillips538f1a32017-03-08 14:32:55 -050025#include "SkRGBAToYUV.h"
reed96472de2014-12-10 09:53:42 -080026#include "SkReadPixelsRec.h"
senorblanco5878dbd2016-05-19 14:50:29 -070027#include "SkSpecialImage.h"
Hal Canary1fcc4042016-11-30 17:07:59 -050028#include "SkStream.h"
reedf8d18742015-01-02 20:45:37 -080029#include "SkString.h"
reed4af267b2014-11-21 08:46:37 -080030#include "SkSurface.h"
reed56179002015-07-07 06:11:19 -070031
bsalomon55812362015-06-10 08:49:28 -070032#if SK_SUPPORT_GPU
33#include "GrTexture.h"
34#include "GrContext.h"
reed56179002015-07-07 06:11:19 -070035#include "SkImage_Gpu.h"
bsalomon55812362015-06-10 08:49:28 -070036#endif
scroggo@google.com7def5e12013-05-31 14:00:10 +000037
reed80c772b2015-07-30 18:58:23 -070038SkImage::SkImage(int width, int height, uint32_t uniqueID)
39 : fWidth(width)
40 , fHeight(height)
41 , fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID)
42{
43 SkASSERT(width > 0);
44 SkASSERT(height > 0);
reed@google.comf6627b72012-07-27 18:02:50 +000045}
46
reed6ceeebd2016-03-09 14:26:26 -080047bool SkImage::peekPixels(SkPixmap* pm) const {
48 SkPixmap tmp;
49 if (!pm) {
50 pm = &tmp;
reed@google.com4f7c6152014-02-06 14:11:56 +000051 }
reed6ceeebd2016-03-09 14:26:26 -080052 return as_IB(this)->onPeekPixels(pm);
reed@google.com4f7c6152014-02-06 14:11:56 +000053}
54
reed96472de2014-12-10 09:53:42 -080055bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080056 int srcX, int srcY, CachingHint chint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -050057 return as_IB(this)->onReadPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
reed09553032015-11-23 12:32:16 -080058}
59
60bool SkImage::scalePixels(const SkPixmap& dst, SkFilterQuality quality, CachingHint chint) const {
reed6868c3f2015-11-24 11:44:47 -080061 if (this->width() == dst.width() && this->height() == dst.height()) {
62 return this->readPixels(dst, 0, 0, chint);
63 }
64
reed09553032015-11-23 12:32:16 -080065 // Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself
66 // can scale more efficiently) we should take advantage of it here.
67 //
68 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -050069 if (as_IB(this)->getROPixels(&bm, dst.info().colorSpace(), chint)) {
reed09553032015-11-23 12:32:16 -080070 bm.lockPixels();
71 SkPixmap pmap;
72 // Note: By calling the pixmap scaler, we never cache the final result, so the chint
73 // is (currently) only being applied to the getROPixels. If we get a request to
74 // also attempt to cache the final (scaled) result, we would add that logic here.
75 //
76 return bm.peekPixels(&pmap) && pmap.scalePixels(dst, quality);
77 }
78 return false;
reed@google.com4f7c6152014-02-06 14:11:56 +000079}
80
reed88d064d2015-10-12 11:30:02 -070081///////////////////////////////////////////////////////////////////////////////////////////////////
82
brianosman69c166d2016-08-17 14:01:05 -070083SkAlphaType SkImage::alphaType() const {
84 return as_IB(this)->onAlphaType();
85}
86
Matt Sarett5b4599f2017-03-02 12:42:35 -050087SkColorSpace* SkImage::colorSpace() const {
88 return as_IB(this)->onImageInfo().colorSpace();
89}
90
91sk_sp<SkColorSpace> SkImage::refColorSpace() const {
92 return as_IB(this)->onImageInfo().refColorSpace();
93}
94
reed5671c5b2016-03-09 14:47:34 -080095sk_sp<SkShader> SkImage::makeShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
96 const SkMatrix* localMatrix) const {
reed6b2d7ac2016-08-11 06:42:26 -070097 return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tileX, tileY, localMatrix);
piotaixrcef04f82014-07-14 07:48:04 -070098}
99
Hal Canarydb683012016-11-23 08:55:18 -0700100SkData* SkImage::encode(SkEncodedImageFormat type, int quality) const {
Leon Scroggins294c1c42016-11-08 14:29:46 +0000101 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500102 SkColorSpace* legacyColorSpace = nullptr;
103 if (as_IB(this)->getROPixels(&bm, legacyColorSpace)) {
Hal Canarydb683012016-11-23 08:55:18 -0700104 SkDynamicMemoryWStream buf;
105 return SkEncodeImage(&buf, bm, type, quality) ? buf.detachAsData().release() : nullptr;
Leon Scroggins294c1c42016-11-08 14:29:46 +0000106 }
107 return nullptr;
108}
109
halcanarya2e05672015-12-09 03:56:02 -0800110SkData* SkImage::encode(SkPixelSerializer* serializer) const {
bungemanffae30d2016-08-03 13:32:32 -0700111 sk_sp<SkData> encoded(this->refEncoded());
Hal Canarydb683012016-11-23 08:55:18 -0700112 if (encoded &&
113 (!serializer || serializer->useEncodedData(encoded->data(), encoded->size()))) {
mtklein18300a32016-03-16 13:53:35 -0700114 return encoded.release();
fmalita2be71252015-09-03 07:17:25 -0700115 }
116
117 SkBitmap bm;
118 SkAutoPixmapUnlock apu;
Brian Osman61624f02016-12-09 14:51:59 -0500119 SkColorSpace* legacyColorSpace = nullptr;
120 if (as_IB(this)->getROPixels(&bm, legacyColorSpace) &&
Brian Osman7992da32016-11-18 11:28:24 -0500121 bm.requestLock(&apu)) {
Hal Canarydb683012016-11-23 08:55:18 -0700122 if (serializer) {
123 return serializer->encode(apu.pixmap());
124 } else {
125 SkDynamicMemoryWStream buf;
126 return SkEncodeImage(&buf, apu.pixmap(), SkEncodedImageFormat::kPNG, 100)
127 ? buf.detachAsData().release() : nullptr;
128 }
fmalita2be71252015-09-03 07:17:25 -0700129 }
130
131 return nullptr;
132}
133
reed871872f2015-06-22 12:48:26 -0700134SkData* SkImage::refEncoded() const {
reed05dd2512016-01-05 09:16:19 -0800135 GrContext* ctx = nullptr; // should we allow the caller to pass in a ctx?
136 return as_IB(this)->onRefEncoded(ctx);
reed871872f2015-06-22 12:48:26 -0700137}
138
reed7fb4f8b2016-03-11 04:33:52 -0800139sk_sp<SkImage> SkImage::MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset) {
halcanary96fcdcc2015-08-27 07:41:13 -0700140 if (nullptr == encoded || 0 == encoded->size()) {
141 return nullptr;
reed5965c8a2015-01-07 18:04:45 -0800142 }
Mike Reed185130c2017-02-15 15:14:16 -0500143 return SkImage::MakeFromGenerator(SkImageGenerator::MakeFromEncoded(encoded), subset);
reed5965c8a2015-01-07 18:04:45 -0800144}
145
reedf8d18742015-01-02 20:45:37 -0800146const char* SkImage::toString(SkString* str) const {
147 str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(),
148 this->isOpaque() ? "opaque" : "");
149 return str->c_str();
150}
151
reed7fb4f8b2016-03-11 04:33:52 -0800152sk_sp<SkImage> SkImage::makeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700153 if (subset.isEmpty()) {
154 return nullptr;
155 }
156
157 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
158 if (!bounds.contains(subset)) {
159 return nullptr;
160 }
161
162 // optimization : return self if the subset == our bounds
163 if (bounds == subset) {
reed7fb4f8b2016-03-11 04:33:52 -0800164 return sk_ref_sp(const_cast<SkImage*>(this));
reed7b6945b2015-09-24 00:50:58 -0700165 }
reed7fb4f8b2016-03-11 04:33:52 -0800166 return as_IB(this)->onMakeSubset(subset);
reedf803da12015-01-23 05:58:07 -0800167}
168
bsalomon55812362015-06-10 08:49:28 -0700169#if SK_SUPPORT_GPU
170
171GrTexture* SkImage::getTexture() const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400172 return as_IB(this)->onGetTexture();
bsalomon55812362015-06-10 08:49:28 -0700173}
174
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400175bool SkImage::isTextureBacked() const { return SkToBool(as_IB(this)->peekProxy()); }
bsalomon55812362015-06-10 08:49:28 -0700176
Robert Phillips3390e152017-01-31 17:53:34 -0500177GrBackendObject SkImage::getTextureHandle(bool flushPendingGrContextIO,
178 GrSurfaceOrigin* origin) const {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400179 return as_IB(this)->onGetTextureHandle(flushPendingGrContextIO, origin);
bsalomon55812362015-06-10 08:49:28 -0700180}
181
182#else
183
halcanary96fcdcc2015-08-27 07:41:13 -0700184GrTexture* SkImage::getTexture() const { return nullptr; }
bsalomon55812362015-06-10 08:49:28 -0700185
186bool SkImage::isTextureBacked() const { return false; }
187
Robert Phillips3390e152017-01-31 17:53:34 -0500188GrBackendObject SkImage::getTextureHandle(bool, GrSurfaceOrigin*) const { return 0; }
bsalomon55812362015-06-10 08:49:28 -0700189
190#endif
191
reed@google.com4f7c6152014-02-06 14:11:56 +0000192///////////////////////////////////////////////////////////////////////////////
193
reedaf3fbfc2015-10-04 11:28:36 -0700194SkImage_Base::SkImage_Base(int width, int height, uint32_t uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700195 : INHERITED(width, height, uniqueID)
fmalita3b0d5322015-09-18 08:07:31 -0700196 , fAddedToCache(false)
reedaf3fbfc2015-10-04 11:28:36 -0700197{}
fmalita3b0d5322015-09-18 08:07:31 -0700198
199SkImage_Base::~SkImage_Base() {
200 if (fAddedToCache.load()) {
201 SkNotifyBitmapGenIDIsStale(this->uniqueID());
202 }
203}
204
Robert Phillips538f1a32017-03-08 14:32:55 -0500205bool SkImage_Base::onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
206 const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
207 return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace);
208}
209
reed871872f2015-06-22 12:48:26 -0700210///////////////////////////////////////////////////////////////////////////////////////////////////
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
bsalomonf267c1e2016-02-01 13:16:14 -0800216bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3],
Brian Salomonde3a7262016-02-03 17:09:00 -0500217 const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
Robert Phillips538f1a32017-03-08 14:32:55 -0500218 return as_IB(this)->onReadYUV8Planes(sizes, planes, rowBytes, colorSpace);
bsalomonf267c1e2016-02-01 13:16:14 -0800219}
220
reed871872f2015-06-22 12:48:26 -0700221///////////////////////////////////////////////////////////////////////////////////////////////////
reedf803da12015-01-23 05:58:07 -0800222
reed7fb4f8b2016-03-11 04:33:52 -0800223sk_sp<SkImage> SkImage::MakeFromBitmap(const SkBitmap& bm) {
reed56179002015-07-07 06:11:19 -0700224 SkPixelRef* pr = bm.pixelRef();
halcanary96fcdcc2015-08-27 07:41:13 -0700225 if (nullptr == pr) {
226 return nullptr;
reed56179002015-07-07 06:11:19 -0700227 }
228
reed1ec04d92016-08-05 12:07:41 -0700229 return SkMakeImageFromRasterBitmap(bm, kIfMutable_SkCopyPixelsMode);
reed56179002015-07-07 06:11:19 -0700230}
231
reed3c065112015-07-08 12:46:22 -0700232bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const {
233 return as_IB(this)->onAsLegacyBitmap(bitmap, mode);
234}
235
236bool SkImage_Base::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const {
237 // As the base-class, all we can do is make a copy (regardless of mode).
238 // Subclasses that want to be more optimal should override.
Brian Osman7992da32016-11-18 11:28:24 -0500239 SkImageInfo info = this->onImageInfo().makeColorType(kN32_SkColorType).makeColorSpace(nullptr);
reed3c065112015-07-08 12:46:22 -0700240 if (!bitmap->tryAllocPixels(info)) {
241 return false;
242 }
243 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) {
244 bitmap->reset();
245 return false;
246 }
247
248 if (kRO_LegacyBitmapMode == mode) {
249 bitmap->setImmutable();
250 }
251 return true;
252}
253
Brian Osman138ea972016-12-16 11:55:18 -0500254sk_sp<SkImage> SkImage::MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
Matt Sarette94255d2017-01-09 12:38:59 -0500255 const SkMatrix* matrix, const SkPaint* paint,
256 BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace) {
Mike Reed185130c2017-02-15 15:14:16 -0500257 return MakeFromGenerator(SkImageGenerator::MakeFromPicture(dimensions, std::move(picture),
258 matrix, paint, bitDepth,
259 std::move(colorSpace)));
Matt Sarette94255d2017-01-09 12:38:59 -0500260}
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500261
senorblanco5878dbd2016-05-19 14:50:29 -0700262sk_sp<SkImage> SkImage::makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
263 const SkIRect& clipBounds, SkIRect* outSubset,
264 SkIPoint* offset) const {
brianosman04a44d02016-09-21 09:46:57 -0700265 if (!filter || !outSubset || !offset || !this->bounds().contains(subset)) {
266 return nullptr;
267 }
Brian Osman7992da32016-11-18 11:28:24 -0500268 SkColorSpace* colorSpace = as_IB(this)->onImageInfo().colorSpace();
brianosman04a44d02016-09-21 09:46:57 -0700269 sk_sp<SkSpecialImage> srcSpecialImage = SkSpecialImage::MakeFromImage(
Brian Osman61624f02016-12-09 14:51:59 -0500270 subset, sk_ref_sp(const_cast<SkImage*>(this)), colorSpace);
brianosman04a44d02016-09-21 09:46:57 -0700271 if (!srcSpecialImage) {
272 return nullptr;
273 }
senorblanco5878dbd2016-05-19 14:50:29 -0700274
Hal Canary67b39de2016-11-07 11:47:44 -0500275 sk_sp<SkImageFilterCache> cache(
brianosman04a44d02016-09-21 09:46:57 -0700276 SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize));
Brian Osman7992da32016-11-18 11:28:24 -0500277 SkImageFilter::OutputProperties outputProperties(colorSpace);
brianosman2a75e5d2016-09-22 07:15:37 -0700278 SkImageFilter::Context context(SkMatrix::I(), clipBounds, cache.get(), outputProperties);
279
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500280 sk_sp<SkSpecialImage> result = filter->filterImage(srcSpecialImage.get(), context, offset);
brianosman04a44d02016-09-21 09:46:57 -0700281 if (!result) {
282 return nullptr;
283 }
senorblanco5878dbd2016-05-19 14:50:29 -0700284
brianosman04a44d02016-09-21 09:46:57 -0700285 *outSubset = SkIRect::MakeWH(result->width(), result->height());
286 if (!outSubset->intersect(clipBounds.makeOffset(-offset->x(), -offset->y()))) {
287 return nullptr;
288 }
289 offset->fX += outSubset->x();
290 offset->fY += outSubset->y();
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500291
292 // Note that here we're returning the special image's entire backing store, loose padding
293 // and all!
294 return result->asImage();
senorblanco5878dbd2016-05-19 14:50:29 -0700295}
296
fmalitaddbbdda2015-08-20 08:47:26 -0700297bool SkImage::isLazyGenerated() const {
298 return as_IB(this)->onIsLazyGenerated();
299}
300
reed9e2ed832016-10-31 05:27:28 -0700301bool SkImage::isAlphaOnly() const {
302 return as_IB(this)->onImageInfo().colorType() == kAlpha_8_SkColorType;
303}
304
Matt Sarettcb874232017-04-05 11:41:27 -0400305sk_sp<SkImage> SkImage::makeColorSpace(sk_sp<SkColorSpace> target,
306 SkTransferFunctionBehavior premulBehavior) const {
307 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
308 // TODO (msarett, brianosman): Implement this.
309 return nullptr;
310 }
311
Matt Sarett6de13102017-03-14 14:10:48 -0400312 SkColorSpaceTransferFn fn;
313 if (!target || !target->isNumericalTransferFn(&fn)) {
314 return nullptr;
315 }
316
Matt Sarett73879eb2017-03-22 10:07:50 -0400317 // No need to create a new image if:
318 // (1) The color spaces are equal (nullptr is considered to be sRGB).
319 // (2) The color type is kAlpha8.
Mike Klein919cc452017-03-18 15:36:52 +0000320 if ((!this->colorSpace() && target->isSRGB()) ||
Matt Sarettf3880932017-03-24 10:06:03 -0400321 SkColorSpace::Equals(this->colorSpace(), target.get()) ||
Matt Sarettcb874232017-04-05 11:41:27 -0400322 kAlpha_8_SkColorType == as_IB(this)->onImageInfo().colorType()) {
323 return sk_ref_sp(const_cast<SkImage*>(this));
Matt Sarett6de13102017-03-14 14:10:48 -0400324 }
325
Matt Sarettcb874232017-04-05 11:41:27 -0400326 // TODO: We might consider making this a deferred conversion?
327 return as_IB(this)->onMakeColorSpace(std::move(target));
Matt Sarett6de13102017-03-14 14:10:48 -0400328}
329
reed56179002015-07-07 06:11:19 -0700330//////////////////////////////////////////////////////////////////////////////////////
331
reed8b26b992015-05-07 15:36:17 -0700332#if !SK_SUPPORT_GPU
333
cblume33e0cb52016-08-30 12:09:23 -0700334sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
Brian Osman7b8400d2016-11-08 17:08:54 -0500335 int mipLevelCount, SkBudgeted, SkDestinationSurfaceColorMode) {
cblume33e0cb52016-08-30 12:09:23 -0700336 return nullptr;
337}
338
reed7fb4f8b2016-03-11 04:33:52 -0800339sk_sp<SkImage> SkImage::MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType,
brianosmandddbe382016-07-20 13:55:39 -0700340 sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700341 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700342}
343
bsalomon41b952c2016-03-11 06:46:33 -0800344size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy&,
345 const DeferredTextureImageUsageParams[],
cblume33e0cb52016-08-30 12:09:23 -0700346 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400347 SkColorSpace* dstColorSpace) const {
bsalomon41b952c2016-03-11 06:46:33 -0800348 return 0;
349}
350
351sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void*,
352 SkBudgeted) {
353 return nullptr;
354}
355
reed7fb4f8b2016-03-11 04:33:52 -0800356sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&,
brianosmandddbe382016-07-20 13:55:39 -0700357 SkAlphaType, sk_sp<SkColorSpace>) {
halcanary96fcdcc2015-08-27 07:41:13 -0700358 return nullptr;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700359}
360
reed7fb4f8b2016-03-11 04:33:52 -0800361sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace space,
362 const GrBackendObject yuvTextureHandles[3],
363 const SkISize yuvSizes[3],
brianosmandddbe382016-07-20 13:55:39 -0700364 GrSurfaceOrigin origin,
365 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon8e74f802016-01-30 10:01:40 -0800366 return nullptr;
367}
368
Brian Osman041f7df2017-02-07 11:23:28 -0500369sk_sp<SkImage> SkImage::makeTextureImage(GrContext*, SkColorSpace* dstColorSpace) const {
370 return nullptr;
371}
372
Brian Osman2c2bc112017-02-28 10:02:49 -0500373std::unique_ptr<SkCrossContextImageData> SkCrossContextImageData::MakeFromEncoded(
374 GrContext*, sk_sp<SkData> encoded, SkColorSpace* dstColorSpace) {
375 sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(encoded));
376 if (!image) {
377 return nullptr;
378 }
379 // TODO: Force decode to raster here?
380 return std::unique_ptr<SkCrossContextImageData>(new SkCrossContextImageData(std::move(image)));
381}
382
383sk_sp<SkImage> SkImage::MakeFromCrossContextImageData(
384 GrContext*, std::unique_ptr<SkCrossContextImageData> ccid) {
385 return ccid->fImage;
386}
387
bsalomon634b4302016-07-12 18:11:17 -0700388sk_sp<SkImage> SkImage::makeNonTextureImage() const {
389 return sk_ref_sp(const_cast<SkImage*>(this));
390}
391
reed7fb4f8b2016-03-11 04:33:52 -0800392#endif
393
394///////////////////////////////////////////////////////////////////////////////////////////////////
395
cblume186d2d42016-06-03 11:17:42 -0700396sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
397 int mipLevelCount, SkBudgeted) {
398 return nullptr;
399}
reeda9ca05c2016-08-11 03:55:15 -0700400
401///////////////////////////////////////////////////////////////////////////////////////////////////
402#include "SkImageDeserializer.h"
403
404sk_sp<SkImage> SkImageDeserializer::makeFromData(SkData* data, const SkIRect* subset) {
405 return SkImage::MakeFromEncoded(sk_ref_sp(data), subset);
406}
407sk_sp<SkImage> SkImageDeserializer::makeFromMemory(const void* data, size_t length,
408 const SkIRect* subset) {
409 return SkImage::MakeFromEncoded(SkData::MakeWithCopy(data, length), subset);
410}
reed2d5b7142016-08-17 11:12:33 -0700411
412///////////////////////////////////////////////////////////////////////////////////////////////////
413
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500414bool SkImage_pinAsTexture(const SkImage* image, GrContext* ctx) {
reed2d5b7142016-08-17 11:12:33 -0700415 SkASSERT(image);
416 SkASSERT(ctx);
Derek Sollenbergerd3ea9b72016-11-09 11:25:13 -0500417 return as_IB(image)->onPinAsTexture(ctx);
reed2d5b7142016-08-17 11:12:33 -0700418}
419
420void SkImage_unpinAsTexture(const SkImage* image, GrContext* ctx) {
421 SkASSERT(image);
422 SkASSERT(ctx);
423 as_IB(image)->onUnpinAsTexture(ctx);
424}
Brian Osman0d4ff6c2017-01-17 16:10:07 -0500425
426///////////////////////////////////////////////////////////////////////////////////////////////////
427
428sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage* src,
429 SkColorSpace* colorSpace) {
430 // Read the pixels out of the source image, with no conversion
431 SkImageInfo info = as_IB(src)->onImageInfo();
432 if (kUnknown_SkColorType == info.colorType()) {
433 SkDEBUGFAIL("Unexpected color type");
434 return nullptr;
435 }
436
437 size_t rowBytes = info.minRowBytes();
438 size_t size = info.getSafeSize(rowBytes);
439 auto data = SkData::MakeUninitialized(size);
440 if (!data) {
441 return nullptr;
442 }
443
444 SkPixmap pm(info, data->writable_data(), rowBytes);
445 if (!src->readPixels(pm, 0, 0, SkImage::kDisallow_CachingHint)) {
446 return nullptr;
447 }
448
449 // Wrap them in a new image with a different color space
450 return SkImage::MakeRasterData(info.makeColorSpace(sk_ref_sp(colorSpace)), data, rowBytes);
451}