blob: d14f001cafc7128657a4db920eef4d0aca56d03c [file] [log] [blame]
reed@google.com5d4ba882012-07-31 15:45:27 +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
cblume33e0cb52016-08-30 12:09:23 -07008#include <cstddef>
9#include <cstring>
10#include <type_traits>
11
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkAutoPixmapStorage.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014#include "GrBackendTextureImageGenerator.h"
Stan Iliev7e910df2017-06-02 10:29:21 -040015#include "GrAHardwareBufferImageGenerator.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050016#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070017#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000018#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050019#include "GrContextPriv.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050020#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050021#include "GrImageTextureMaker.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050022#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040023#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050024#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050025#include "GrSemaphore.h"
Eric Karl914a36b2017-10-12 12:44:50 -070026#include "GrSurfacePriv.h"
Brian Osmane8e54582016-11-28 10:06:27 -050027#include "GrTextureAdjuster.h"
Robert Phillips646e4292017-06-13 12:44:56 -040028#include "GrTexture.h"
Eric Karl914a36b2017-10-12 12:44:50 -070029#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050030#include "GrTextureProxy.h"
Brian Osman63954c92017-03-14 12:07:12 -040031#include "effects/GrNonlinearColorSpaceXformEffect.h"
Ethan Nicholas7461a4a2017-12-21 14:18:01 -050032#include "effects/GrYUVtoRGBEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070033#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080034#include "SkBitmapCache.h"
Brian Osman3b655982017-03-07 16:58:08 -050035#include "SkGr.h"
reed262a71b2015-12-05 13:07:27 -080036#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050037#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050038#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070039#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070040#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050041#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070042
Robert Phillipsb726d582017-03-09 16:36:32 -050043SkImage_Gpu::SkImage_Gpu(GrContext* context, uint32_t uniqueID, SkAlphaType at,
44 sk_sp<GrTextureProxy> proxy,
45 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
Robert Phillipsa108c922017-10-10 10:42:19 -040046 : INHERITED(proxy->worstCaseWidth(), proxy->worstCaseHeight(), uniqueID)
Robert Phillipsb726d582017-03-09 16:36:32 -050047 , fContext(context)
48 , fProxy(std::move(proxy))
49 , fAlphaType(at)
50 , fBudgeted(budgeted)
51 , fColorSpace(std::move(colorSpace))
52 , fAddedRasterVersionToCache(false) {
reedc9b5f8b2015-10-22 13:20:20 -070053}
piotaixrcef04f82014-07-14 07:48:04 -070054
reed6f1216a2015-08-04 08:10:13 -070055SkImage_Gpu::~SkImage_Gpu() {
56 if (fAddedRasterVersionToCache.load()) {
57 SkNotifyBitmapGenIDIsStale(this->uniqueID());
58 }
59}
60
brianosman396fcdb2016-07-22 06:26:11 -070061SkImageInfo SkImage_Gpu::onImageInfo() const {
62 SkColorType ct;
Robert Phillipsb726d582017-03-09 16:36:32 -050063 if (!GrPixelConfigToColorType(fProxy->config(), &ct)) {
brianosman396fcdb2016-07-22 06:26:11 -070064 ct = kUnknown_SkColorType;
65 }
Robert Phillipsb726d582017-03-09 16:36:32 -050066 return SkImageInfo::Make(fProxy->width(), fProxy->height(), ct, fAlphaType, fColorSpace);
brianosman396fcdb2016-07-22 06:26:11 -070067}
68
Brian Osman62517712017-04-26 16:26:39 -040069bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace*, CachingHint chint) const {
70 // The SkColorSpace parameter "dstColorSpace" is really just a hint about how/where the bitmap
71 // will be used. The client doesn't expect that we convert to that color space, it's intended
72 // for codec-backed images, to drive our decoding heuristic. In theory we *could* read directly
73 // into that color space (to save the client some effort in whatever they're about to do), but
74 // that would make our use of the bitmap cache incorrect (or much less efficient, assuming we
75 // rolled the dstColorSpace into the key).
Mike Reed5fa3d6d2017-03-25 09:51:00 -040076 const auto desc = SkBitmapCacheDesc::Make(this);
77 if (SkBitmapCache::Find(desc, dst)) {
reed6f1216a2015-08-04 08:10:13 -070078 SkASSERT(dst->getGenerationID() == this->uniqueID());
79 SkASSERT(dst->isImmutable());
80 SkASSERT(dst->getPixels());
81 return true;
82 }
83
Mike Reed7a542c52017-04-11 12:03:44 -040084 SkBitmapCache::RecPtr rec = nullptr;
85 SkPixmap pmap;
86 if (kAllow_CachingHint == chint) {
Brian Osman62517712017-04-26 16:26:39 -040087 rec = SkBitmapCache::Alloc(desc, this->onImageInfo(), &pmap);
Mike Reed7a542c52017-04-11 12:03:44 -040088 if (!rec) {
89 return false;
90 }
91 } else {
Brian Osman62517712017-04-26 16:26:39 -040092 if (!dst->tryAllocPixels(this->onImageInfo()) || !dst->peekPixels(&pmap)) {
Mike Reed7a542c52017-04-11 12:03:44 -040093 return false;
94 }
reed8b26b992015-05-07 15:36:17 -070095 }
Robert Phillipsb726d582017-03-09 16:36:32 -050096
Brian Salomon2084ffa2017-03-27 17:32:18 -040097 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
98 fProxy,
99 fColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500100 if (!sContext) {
101 return false;
102 }
103
Mike Reed7a542c52017-04-11 12:03:44 -0400104 if (!sContext->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
reed8b26b992015-05-07 15:36:17 -0700105 return false;
106 }
reed6f1216a2015-08-04 08:10:13 -0700107
Mike Reed7a542c52017-04-11 12:03:44 -0400108 if (rec) {
109 SkBitmapCache::Add(std::move(rec), dst);
reed09553032015-11-23 12:32:16 -0800110 fAddedRasterVersionToCache.store(true);
111 }
reed8b26b992015-05-07 15:36:17 -0700112 return true;
113}
114
Robert Phillipsb726d582017-03-09 16:36:32 -0500115sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400116 const GrSamplerState& params,
Robert Phillipsb726d582017-03-09 16:36:32 -0500117 SkColorSpace* dstColorSpace,
118 sk_sp<SkColorSpace>* texColorSpace,
119 SkScalar scaleAdjust[2]) const {
Robert Phillips30a38ff2017-03-22 10:31:11 -0400120 if (context != fContext) {
Robert Phillipsb726d582017-03-09 16:36:32 -0500121 SkASSERT(0);
122 return nullptr;
123 }
124
Brian Osman7992da32016-11-18 11:28:24 -0500125 if (texColorSpace) {
126 *texColorSpace = this->fColorSpace;
127 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500128
Greg Danielc77085d2017-11-01 16:38:48 -0400129 GrTextureAdjuster adjuster(fContext, fProxy, this->alphaType(), this->uniqueID(),
130 this->fColorSpace.get());
Greg Daniel4faa0402017-09-29 15:21:28 -0400131 return adjuster.refTextureProxySafeForParams(params, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700132}
133
reed8b26b992015-05-07 15:36:17 -0700134static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
135 switch (info.colorType()) {
136 case kRGBA_8888_SkColorType:
137 case kBGRA_8888_SkColorType:
138 break;
139 default:
140 return; // nothing to do
141 }
142
143 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
144 // and in either case, the alpha-byte is always in the same place, so we can safely call
145 // SkPreMultiplyColor()
146 //
147 SkColor* row = (SkColor*)pixels;
148 for (int y = 0; y < info.height(); ++y) {
149 for (int x = 0; x < info.width(); ++x) {
150 row[x] = SkPreMultiplyColor(row[x]);
151 }
Brian Salomon9708af82018-02-05 12:57:10 -0500152 row = (SkColor*)((char*)(row) + rowBytes);
reed8b26b992015-05-07 15:36:17 -0700153 }
154}
155
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400156GrBackendObject SkImage_Gpu::onGetTextureHandle(bool flushPendingGrContextIO,
157 GrSurfaceOrigin* origin) const {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400158 SkASSERT(fProxy);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400159
Greg Danielf2336e42018-01-23 16:38:14 -0500160 if (!fContext->contextPriv().resourceProvider() && !fProxy->priv().isInstantiated()) {
161 // This image was created with a DDL context and cannot be instantiated. Thus we return 0
162 // here which is considered invalid for all backends.
163 return 0;
164 }
165
166 if (GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState()) {
167 SkASSERT(fContext->contextPriv().resourceProvider());
168 fProxy->priv().doLazyInstantiation(fContext->contextPriv().resourceProvider());
169 if (!fProxy->priv().isInstantiated()) {
170 // We failed to instantiate the lazy proxy. Thus we return 0 here which is considered
171 // invalid for all backends.
172 return 0;
173 }
174 }
175
Robert Phillips6be756b2018-01-16 15:07:54 -0500176 if (!fProxy->instantiate(fContext->contextPriv().resourceProvider())) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400177 return 0;
178 }
179
180 GrTexture* texture = fProxy->priv().peekTexture();
181
182 if (texture) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400183 if (flushPendingGrContextIO) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400184 fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400185 }
186 if (origin) {
Robert Phillips7ee385e2017-03-30 08:02:11 -0400187 *origin = fProxy->origin();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400188 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400189 return texture->getTextureHandle();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400190 }
191 return 0;
192}
193
194GrTexture* SkImage_Gpu::onGetTexture() const {
195 GrTextureProxy* proxy = this->peekProxy();
196 if (!proxy) {
197 return nullptr;
198 }
199
Robert Phillips6be756b2018-01-16 15:07:54 -0500200 if (!proxy->instantiate(fContext->contextPriv().resourceProvider())) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400201 return nullptr;
202 }
203
204 return proxy->priv().peekTexture();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400205}
206
Matt Sarett03dd6d52017-01-23 12:15:09 -0500207bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800208 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500209 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500210 return false;
211 }
212
Matt Sarett03dd6d52017-01-23 12:15:09 -0500213 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
214 if (!rec.trim(this->width(), this->height())) {
215 return false;
216 }
217
Robert Phillipsb726d582017-03-09 16:36:32 -0500218 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
219 // GrRenderTargetContext::onReadPixels
reed8b26b992015-05-07 15:36:17 -0700220 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500221 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700222 // let the GPU perform this transformation for us
Robert Phillipse78b7252017-04-06 07:59:41 -0400223 flags = GrContextPriv::kUnpremul_PixelOpsFlag;
reed8b26b992015-05-07 15:36:17 -0700224 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500225
Brian Osmana8ac9242017-09-07 10:19:08 -0400226 // This hack allows us to call makeNonTextureImage on images with arbitrary color spaces.
227 // Otherwise, we'll be unable to create a render target context.
228 // TODO: This shouldn't be necessary - we need more robust support for images (and surfaces)
229 // with arbitrary color spaces. Unfortunately, this is one spot where we go from image to
230 // surface (rather than the opposite), and our lenient image rules break our (currently) more
231 // strict surface rules.
Mike Klein98e38e22018-01-12 15:59:53 +0000232 // We treat null-dst color space as always equal to fColorSpace for this kind of read-back.
Brian Osmana8ac9242017-09-07 10:19:08 -0400233 sk_sp<SkColorSpace> surfaceColorSpace = fColorSpace;
Mike Klein98e38e22018-01-12 15:59:53 +0000234 if (!flags) {
235 if (!dstInfo.colorSpace() ||
236 SkColorSpace::Equals(fColorSpace.get(), dstInfo.colorSpace())) {
237 surfaceColorSpace = nullptr;
238 }
Brian Osmana8ac9242017-09-07 10:19:08 -0400239 }
240
Brian Salomon2084ffa2017-03-27 17:32:18 -0400241 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
Brian Osmana8ac9242017-09-07 10:19:08 -0400242 fProxy, surfaceColorSpace);
Robert Phillipsb726d582017-03-09 16:36:32 -0500243 if (!sContext) {
reed8b26b992015-05-07 15:36:17 -0700244 return false;
245 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500246
247 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
248 return false;
249 }
250
reed8b26b992015-05-07 15:36:17 -0700251 // do we have to manually fix-up the alpha channel?
252 // src dst
253 // unpremul premul fix manually
254 // premul unpremul done by kUnpremul_PixelOpsFlag
255 // all other combos need to change.
256 //
257 // Should this be handled by Ganesh? todo:?
258 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500259 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
260 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700261 }
262 return true;
263}
264
reed7fb4f8b2016-03-11 04:33:52 -0800265sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
Brian Salomon63e79732017-05-15 21:23:13 -0400266 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400267 desc.fOrigin = fProxy->origin();
reed7b6945b2015-09-24 00:50:58 -0700268 desc.fWidth = subset.width();
269 desc.fHeight = subset.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400270 desc.fConfig = fProxy->config();
reed7b6945b2015-09-24 00:50:58 -0700271
Robert Phillipsb726d582017-03-09 16:36:32 -0500272 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Robert Phillipse2f7d182016-12-15 09:23:05 -0500273 desc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400274 GrMipMapped::kNo,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500275 SkBackingFit::kExact,
276 fBudgeted));
277 if (!sContext) {
278 return nullptr;
279 }
280
Robert Phillipsb726d582017-03-09 16:36:32 -0500281 if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500282 return nullptr;
283 }
284
Robert Phillipsb726d582017-03-09 16:36:32 -0500285 // MDB: this call is okay bc we know 'sContext' was kExact
286 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
287 fAlphaType, sContext->asTextureProxyRef(),
Robert Phillipse2f7d182016-12-15 09:23:05 -0500288 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700289}
290
reed8b26b992015-05-07 15:36:17 -0700291///////////////////////////////////////////////////////////////////////////////////////////////////
292
Greg Daniel7ef28f32017-04-20 16:41:55 +0000293static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx,
294 const GrBackendTexture& backendTex,
295 GrSurfaceOrigin origin,
brianosmandddbe382016-07-20 13:55:39 -0700296 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
297 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800298 SkImage::TextureReleaseProc releaseProc,
299 SkImage::ReleaseContext releaseCtx) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000300 if (backendTex.width() <= 0 || backendTex.height() <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700301 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700302 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400303
Robert Phillipsadbe1322018-01-17 13:35:46 -0500304 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
305 sk_sp<GrTextureProxy> proxy = proxyProvider->createWrappedTextureProxy(
306 backendTex, origin, ownership, releaseProc, releaseCtx);
307 if (!proxy) {
Robert Phillipse201ebc2018-01-17 18:12:50 +0000308 return nullptr;
309 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500310
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400311 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500312 at, std::move(proxy), std::move(colorSpace), SkBudgeted::kNo);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700313}
314
Greg Daniel94403452017-04-18 15:52:36 -0400315sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
316 const GrBackendTexture& tex, GrSurfaceOrigin origin,
317 SkAlphaType at, sk_sp<SkColorSpace> cs,
318 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500319 if (!ctx) {
320 return nullptr;
321 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000322 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kBorrow_GrWrapOwnership,
323 releaseP, releaseC);
Greg Daniel94403452017-04-18 15:52:36 -0400324}
325
Greg Danielfaa095e2017-12-19 13:15:02 -0500326bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config,
Greg Danielf5d87582017-12-18 14:48:15 -0500327 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) {
328 // TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
329 // create a fake image info here.
330 SkImageInfo info = SkImageInfo::Make(1, 1, ct, at, cs);
331 if (!SkImageInfoIsValidAllowNumericalCS(info)) {
332 return false;
333 }
334
Greg Danielfaa095e2017-12-19 13:15:02 -0500335 return ctx->caps()->validateBackendTexture(tex, ct, config);
Greg Danielf5d87582017-12-18 14:48:15 -0500336}
337
338sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
339 const GrBackendTexture& tex, GrSurfaceOrigin origin,
340 SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
341 TextureReleaseProc releaseP, ReleaseContext releaseC) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500342 if (!ctx) {
343 return nullptr;
344 }
Greg Danielf5d87582017-12-18 14:48:15 -0500345 GrBackendTexture texCopy = tex;
Greg Danielfaa095e2017-12-19 13:15:02 -0500346 if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500347 return nullptr;
348 }
349 return MakeFromTexture(ctx, texCopy, origin, at, cs, releaseP, releaseC);
350}
351
Greg Daniel94403452017-04-18 15:52:36 -0400352sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
353 const GrBackendTexture& tex, GrSurfaceOrigin origin,
354 SkAlphaType at, sk_sp<SkColorSpace> cs) {
Greg Danielf2336e42018-01-23 16:38:14 -0500355 if (!ctx->contextPriv().resourceProvider()) {
356 // We have a DDL context and we don't support adopted textures for them.
357 return nullptr;
358 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000359 return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kAdopt_GrWrapOwnership,
360 nullptr, nullptr);
361}
362
Greg Danielf5d87582017-12-18 14:48:15 -0500363sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
364 const GrBackendTexture& tex, GrSurfaceOrigin origin,
365 SkColorType ct, SkAlphaType at,
366 sk_sp<SkColorSpace> cs) {
367 GrBackendTexture texCopy = tex;
Greg Danielfaa095e2017-12-19 13:15:02 -0500368 if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
Greg Danielf5d87582017-12-18 14:48:15 -0500369 return nullptr;
370 }
371 return MakeFromAdoptedTexture(ctx, texCopy, origin, at, cs);
372}
373
Greg Daniel7ef28f32017-04-20 16:41:55 +0000374static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
375 int width, int height,
376 GrPixelConfig config,
377 GrBackendObject handle) {
Brian Salomon8fe24272017-07-07 12:56:11 -0400378 switch (backend) {
379 case kOpenGL_GrBackend: {
380 const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
381 return GrBackendTexture(width, height, config, *glInfo);
382 }
Mike Reedd20b5c42017-06-14 06:03:10 -0400383#ifdef SK_VULKAN
Brian Salomon8fe24272017-07-07 12:56:11 -0400384 case kVulkan_GrBackend: {
385 const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
386 return GrBackendTexture(width, height, *vkInfo);
387 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +0000388#endif
Brian Salomon8fe24272017-07-07 12:56:11 -0400389 case kMock_GrBackend: {
390 const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
391 return GrBackendTexture(width, height, config, *mockInfo);
392 }
393 default:
394 return GrBackendTexture();
395 }
Greg Daniel94403452017-04-18 15:52:36 -0400396}
397
Robert Phillipsc25db632017-12-13 09:22:45 -0500398static bool are_yuv_sizes_valid(const SkISize yuvSizes[], bool nv12) {
399 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 ||
400 yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0) {
401 return false;
402 }
403 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
404 return false;
405 }
406
407 return true;
408}
409
jbaumanb445a572016-06-09 13:24:48 -0700410static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
411 bool nv12,
Robert Phillipsc25db632017-12-13 09:22:45 -0500412 const GrBackendTexture yuvBackendTextures[],
jbaumanb445a572016-06-09 13:24:48 -0700413 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700414 GrSurfaceOrigin origin,
415 sk_sp<SkColorSpace> imageColorSpace) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500416 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
417
Robert Phillipsc25db632017-12-13 09:22:45 -0500418 if (!are_yuv_sizes_valid(yuvSizes, nv12)) {
jbaumanb445a572016-06-09 13:24:48 -0700419 return nullptr;
420 }
421
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500422 sk_sp<GrTextureProxy> yProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[0],
423 origin);
424 sk_sp<GrTextureProxy> uProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[1],
425 origin);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000426 sk_sp<GrTextureProxy> vProxy;
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500427
jbaumanb445a572016-06-09 13:24:48 -0700428 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500429 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700430 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500431 vProxy = proxyProvider->createWrappedTextureProxy(yuvBackendTextures[2], origin);
jbaumanb445a572016-06-09 13:24:48 -0700432 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500433 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700434 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700435 }
436
robertphillipsd4c741e2016-04-28 09:55:15 -0700437 const int width = yuvSizes[0].fWidth;
438 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700439
robertphillipsd4c741e2016-04-28 09:55:15 -0700440 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400441 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext(
Brian Salomonbdecacf2018-02-02 20:32:49 -0500442 SkBackingFit::kExact, width, height, kRGBA_8888_GrPixelConfig,
443 std::move(imageColorSpace), 1, GrMipMapped::kNo, origin));
Brian Osman11052242016-10-27 14:47:55 -0400444 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700445 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700446 }
447
448 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400449 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Ethan Nicholas7461a4a2017-12-21 14:18:01 -0500450 paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Make(yProxy, uProxy, vProxy,
451 yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700452
Robert Phillipsb726d582017-03-09 16:36:32 -0500453 const SkRect rect = SkRect::MakeIWH(width, height);
robertphillipsc9a37062015-09-01 08:34:28 -0700454
Brian Salomon82f44312017-01-11 13:42:54 -0500455 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500456
Robert Phillips7ee385e2017-03-30 08:02:11 -0400457 if (!renderTargetContext->asSurfaceProxy()) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500458 return nullptr;
459 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400460 ctx->contextPriv().flushSurfaceWrites(renderTargetContext->asSurfaceProxy());
Robert Phillipsb726d582017-03-09 16:36:32 -0500461
462 // MDB: this call is okay bc we know 'renderTargetContext' was exact
Brian Salomonf3569f02017-10-24 12:52:33 -0400463 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID, kOpaque_SkAlphaType,
464 renderTargetContext->asTextureProxyRef(),
Robert Phillipsc25db632017-12-13 09:22:45 -0500465 renderTargetContext->colorSpaceInfo().refColorSpace(),
466 SkBudgeted::kYes);
467}
468
469static sk_sp<SkImage> make_from_yuv_objects_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
470 bool nv12,
471 const GrBackendObject yuvTextureHandles[],
472 const SkISize yuvSizes[],
473 GrSurfaceOrigin origin,
474 sk_sp<SkColorSpace> imageColorSpace) {
475 if (!are_yuv_sizes_valid(yuvSizes, nv12)) {
476 return nullptr;
477 }
478
479 GrBackendTexture backendTextures[3];
480
481 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
482
483 GrBackend backend = ctx->contextPriv().getBackend();
484 backendTextures[0] = make_backend_texture_from_handle(backend,
485 yuvSizes[0].fWidth,
486 yuvSizes[0].fHeight,
487 kConfig,
488 yuvTextureHandles[0]);
489 backendTextures[1] = make_backend_texture_from_handle(backend,
490 yuvSizes[1].fWidth,
491 yuvSizes[1].fHeight,
492 kConfig,
493 yuvTextureHandles[1]);
494
495 if (!nv12) {
496 backendTextures[2] = make_backend_texture_from_handle(backend,
497 yuvSizes[2].fWidth,
498 yuvSizes[2].fHeight,
499 kConfig,
500 yuvTextureHandles[2]);
501 }
502
503 return make_from_yuv_textures_copy(ctx, colorSpace, nv12,
504 backendTextures, yuvSizes, origin,
505 std::move(imageColorSpace));
bsalomon993a4212015-05-29 11:37:25 -0700506}
reed56179002015-07-07 06:11:19 -0700507
jbaumanb445a572016-06-09 13:24:48 -0700508sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
509 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700510 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
511 sk_sp<SkColorSpace> imageColorSpace) {
Robert Phillipsc25db632017-12-13 09:22:45 -0500512 return make_from_yuv_objects_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
513 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700514}
515
516sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
517 const GrBackendObject yuvTextureHandles[2],
518 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700519 GrSurfaceOrigin origin,
520 sk_sp<SkColorSpace> imageColorSpace) {
Robert Phillipsc25db632017-12-13 09:22:45 -0500521 return make_from_yuv_objects_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
522 std::move(imageColorSpace));
523}
524
525sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
526 const GrBackendTexture yuvBackendTextures[3],
527 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
528 sk_sp<SkColorSpace> imageColorSpace) {
529 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvBackendTextures, yuvSizes, origin,
530 std::move(imageColorSpace));
531}
532
533sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
534 const GrBackendTexture yuvBackendTextures[2],
535 const SkISize yuvSizes[2],
536 GrSurfaceOrigin origin,
537 sk_sp<SkColorSpace> imageColorSpace) {
538 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvBackendTextures, yuvSizes, origin,
brianosmandddbe382016-07-20 13:55:39 -0700539 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700540}
541
Robert Phillips3798c862017-03-27 11:08:16 -0400542static sk_sp<SkImage> create_image_from_maker(GrContext* context, GrTextureMaker* maker,
543 SkAlphaType at, uint32_t id,
Brian Osman041f7df2017-02-07 11:23:28 -0500544 SkColorSpace* dstColorSpace) {
545 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400546 sk_sp<GrTextureProxy> proxy(maker->refTextureProxyForParams(
547 GrSamplerState::ClampNearest(), dstColorSpace, &texColorSpace, nullptr));
Robert Phillips3798c862017-03-27 11:08:16 -0400548 if (!proxy) {
Brian Osman041f7df2017-02-07 11:23:28 -0500549 return nullptr;
550 }
Robert Phillips3798c862017-03-27 11:08:16 -0400551 return sk_make_sp<SkImage_Gpu>(context, id, at,
552 std::move(proxy), std::move(texColorSpace), SkBudgeted::kNo);
Brian Osman041f7df2017-02-07 11:23:28 -0500553}
554
Brian Osman2c2bc112017-02-28 10:02:49 -0500555sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500556 if (!context) {
557 return nullptr;
558 }
Robert Phillips87444052017-06-23 14:09:30 -0400559 if (GrContext* incumbent = as_IB(this)->context()) {
560 return incumbent == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
Brian Osman041f7df2017-02-07 11:23:28 -0500561 }
562
Brian Osmandf7e0752017-04-26 16:20:28 -0400563 if (this->isLazyGenerated()) {
564 GrImageTextureMaker maker(context, this, kDisallow_CachingHint);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400565 return create_image_from_maker(context, &maker, this->alphaType(),
566 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500567 }
568
569 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
570 GrBitmapTextureMaker maker(context, *bmp);
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400571 return create_image_from_maker(context, &maker, this->alphaType(),
572 this->uniqueID(), dstColorSpace);
Brian Osman041f7df2017-02-07 11:23:28 -0500573 }
574 return nullptr;
575}
576
Brian Osman13dddce2017-05-09 13:19:50 -0400577sk_sp<SkImage> SkImage::MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> encoded,
578 bool buildMips, SkColorSpace* dstColorSpace) {
579 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
580 if (!codecImage) {
581 return nullptr;
582 }
583
584 // Some backends or drivers don't support (safely) moving resources between contexts
585 if (!context || !context->caps()->crossContextTextureSupport()) {
586 return codecImage;
587 }
588
589 // Turn the codec image into a GrTextureProxy
590 GrImageTextureMaker maker(context, codecImage.get(), kDisallow_CachingHint);
591 sk_sp<SkColorSpace> texColorSpace;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400592 GrSamplerState samplerState(
593 GrSamplerState::WrapMode::kClamp,
594 buildMips ? GrSamplerState::Filter::kMipMap : GrSamplerState::Filter::kBilerp);
595 sk_sp<GrTextureProxy> proxy(
596 maker.refTextureProxyForParams(samplerState, dstColorSpace, &texColorSpace, nullptr));
Brian Osman13dddce2017-05-09 13:19:50 -0400597 if (!proxy) {
598 return codecImage;
599 }
600
Robert Phillips6be756b2018-01-16 15:07:54 -0500601 if (!proxy->instantiate(context->contextPriv().resourceProvider())) {
Brian Osman13dddce2017-05-09 13:19:50 -0400602 return codecImage;
603 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400604 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
Brian Osman13dddce2017-05-09 13:19:50 -0400605
606 // Flush any writes or uploads
607 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
608
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500609 GrGpu* gpu = context->contextPriv().getGpu();
610 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -0400611
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400612 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400613 std::move(sema), codecImage->alphaType(),
Brian Osman13dddce2017-05-09 13:19:50 -0400614 std::move(texColorSpace));
615 return SkImage::MakeFromGenerator(std::move(gen));
616}
617
Brian Osman63bc48d2017-11-07 10:37:00 -0500618sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
619 bool buildMips, SkColorSpace* dstColorSpace) {
620 // Some backends or drivers don't support (safely) moving resources between contexts
621 if (!context || !context->caps()->crossContextTextureSupport()) {
622 return SkImage::MakeRasterCopy(pixmap);
623 }
624
Greg Daniel9e788112018-02-08 14:29:37 -0500625 // If we don't have access to the resource provider and gpu (i.e. in a DDL context) we will not
626 // be able to make everything needed for a GPU CrossContext image. Thus return a raster copy
627 // instead.
628 if (!context->contextPriv().resourceProvider()) {
629 return SkImage::MakeRasterCopy(pixmap);
630 }
631
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500632 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Osman63bc48d2017-11-07 10:37:00 -0500633 // Turn the pixmap into a GrTextureProxy
634 sk_sp<GrTextureProxy> proxy;
635 if (buildMips) {
636 SkBitmap bmp;
637 bmp.installPixels(pixmap);
Greg Daniela4ead652018-02-07 10:21:48 -0500638 proxy = proxyProvider->createMipMapProxyFromBitmap(bmp, dstColorSpace);
Brian Osman63bc48d2017-11-07 10:37:00 -0500639 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500640 proxy = GrUploadPixmapToTextureProxy(proxyProvider, pixmap, SkBudgeted::kYes,
641 dstColorSpace);
Brian Osman63bc48d2017-11-07 10:37:00 -0500642 }
643
644 if (!proxy) {
645 return SkImage::MakeRasterCopy(pixmap);
646 }
647
648 sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
649
650 // Flush any writes or uploads
651 context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500652 GrGpu* gpu = context->contextPriv().getGpu();
Brian Osman63bc48d2017-11-07 10:37:00 -0500653
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500654 sk_sp<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
Brian Osman63bc48d2017-11-07 10:37:00 -0500655
656 auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), proxy->origin(),
657 std::move(sema), pixmap.alphaType(),
658 pixmap.info().refColorSpace());
659 return SkImage::MakeFromGenerator(std::move(gen));
660}
661
Derek Sollenberger7a869872017-06-27 15:37:25 -0400662#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
Stan Iliev7e910df2017-06-02 10:29:21 -0400663sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
664 sk_sp<SkColorSpace> cs) {
665 auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs);
666 return SkImage::MakeFromGenerator(std::move(gen));
667}
668#endif
669
reed56179002015-07-07 06:11:19 -0700670///////////////////////////////////////////////////////////////////////////////////////////////////
671
Eric Karl914a36b2017-10-12 12:44:50 -0700672bool SkImage::MakeBackendTextureFromSkImage(GrContext* ctx,
673 sk_sp<SkImage> image,
674 GrBackendTexture* backendTexture,
675 BackendTextureReleaseProc* releaseProc) {
676 if (!image || !ctx || !backendTexture || !releaseProc) {
677 return false;
678 }
679
680 // Ensure we have a texture backed image.
681 if (!image->isTextureBacked()) {
682 image = image->makeTextureImage(ctx, nullptr);
683 if (!image) {
684 return false;
685 }
686 }
687 GrTexture* texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800688 if (!texture) {
689 // In context-loss cases, we may not have a texture.
690 return false;
691 }
Eric Karl914a36b2017-10-12 12:44:50 -0700692
693 // If the image's context doesn't match the provided context, fail.
694 if (texture->getContext() != ctx) {
695 return false;
696 }
697
698 // Flush any pending IO on the texture.
699 ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
700 SkASSERT(!texture->surfacePriv().hasPendingIO());
701
702 // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
703 // image is not unique, or if the texture wraps an external object.
704 if (!image->unique() || !texture->surfacePriv().hasUniqueRef() ||
705 texture->resourcePriv().refsWrappedObjects()) {
706 // onMakeSubset will always copy the image.
707 image = as_IB(image)->onMakeSubset(image->bounds());
708 if (!image) {
709 return false;
710 }
711
712 texture = image->getTexture();
Eric Karl36591e52018-01-19 13:45:02 -0800713 if (!texture) {
714 return false;
715 }
Eric Karl914a36b2017-10-12 12:44:50 -0700716
717 // Flush to ensure that the copy is completed before we return the texture.
718 ctx->contextPriv().prepareSurfaceForExternalIO(as_IB(image)->peekProxy());
719 SkASSERT(!texture->surfacePriv().hasPendingIO());
720 }
721
722 SkASSERT(!texture->resourcePriv().refsWrappedObjects());
723 SkASSERT(texture->surfacePriv().hasUniqueRef());
724 SkASSERT(image->unique());
725
726 // Take a reference to the GrTexture and release the image.
727 sk_sp<GrTexture> textureRef(SkSafeRef(texture));
728 image = nullptr;
729
730 // Steal the backend texture from the GrTexture, releasing the GrTexture in the process.
731 return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc);
732}
733
734///////////////////////////////////////////////////////////////////////////////////////////////////
735
cblume186d2d42016-06-03 11:17:42 -0700736sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
Robert Phillips590533f2017-07-11 14:22:35 -0400737 const GrMipLevel texels[], int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700738 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500739 SkDestinationSurfaceColorMode colorMode) {
Robert Phillips1119dc32017-04-11 12:54:57 -0400740 SkASSERT(mipLevelCount >= 1);
cblume186d2d42016-06-03 11:17:42 -0700741 if (!ctx) {
742 return nullptr;
743 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500744 GrProxyProvider* proxyProvider = ctx->contextPriv().proxyProvider();
745
Greg Daniel55afd6d2017-09-29 09:32:44 -0400746 // For images where the client is passing the mip data we require that all the mip levels have
747 // valid data.
748 for (int i = 0; i < mipLevelCount; ++i) {
749 if (!texels[i].fPixels) {
750 return nullptr;
751 }
752 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500753 sk_sp<GrTextureProxy> proxy(GrUploadMipMapToTextureProxy(proxyProvider, info,
754 texels, mipLevelCount, colorMode));
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400755 if (!proxy) {
cblume186d2d42016-06-03 11:17:42 -0700756 return nullptr;
757 }
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400758
759 SkASSERT(proxy->priv().isExact());
760 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
761 info.alphaType(), std::move(proxy),
762 info.refColorSpace(), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700763}
Brian Osman63954c92017-03-14 12:07:12 -0400764
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400765sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> target, SkColorType,
Matt Sarett9f3dcb32017-05-04 08:53:32 -0400766 SkTransferFunctionBehavior premulBehavior) const {
767 if (SkTransferFunctionBehavior::kRespect == premulBehavior) {
768 // TODO: Implement this.
769 return nullptr;
770 }
771
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400772 sk_sp<SkColorSpace> srcSpace = fColorSpace;
773 if (!fColorSpace) {
774 if (target->isSRGB()) {
775 return sk_ref_sp(const_cast<SkImage*>((SkImage*)this));
776 }
777
778 srcSpace = SkColorSpace::MakeSRGB();
779 }
780
781 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), target.get());
Brian Osman63954c92017-03-14 12:07:12 -0400782 if (!xform) {
783 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
784 }
785
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400786 sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeDeferredRenderTargetContext(
Brian Osman63954c92017-03-14 12:07:12 -0400787 SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr));
788 if (!renderTargetContext) {
789 return nullptr;
790 }
791
792 GrPaint paint;
793 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman2240be92017-10-18 13:15:13 -0400794 paint.addColorTextureProcessor(fProxy, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400795 paint.addColorFragmentProcessor(std::move(xform));
796
797 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
798
799 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
800
Robert Phillips7ee385e2017-03-30 08:02:11 -0400801 if (!renderTargetContext->asTextureProxy()) {
Brian Osman63954c92017-03-14 12:07:12 -0400802 return nullptr;
803 }
804
805 // MDB: this call is okay bc we know 'renderTargetContext' was exact
806 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
807 fAlphaType, renderTargetContext->asTextureProxyRef(),
Matt Sarettd3df9ec2017-06-05 10:45:30 -0400808 std::move(target), fBudgeted);
Brian Osman63954c92017-03-14 12:07:12 -0400809
810}
Brian Osman5bbd0762017-05-08 11:07:42 -0400811
812bool SkImage_Gpu::onIsValid(GrContext* context) const {
813 // The base class has already checked that context isn't abandoned (if it's not nullptr)
814 if (fContext->abandoned()) {
815 return false;
816 }
817
818 if (context && context != fContext) {
819 return false;
820 }
821
822 return true;
823}