blob: a1ecf5bdad0dc85982bd525fb79df75c4f53782b [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"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "GrBitmapTextureMaker.h"
reed856e9d92015-09-30 12:21:45 -070014#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#include "GrContext.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050016#include "GrContextPriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050017#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
Brian Osmane8e54582016-11-28 10:06:27 -050019#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070020#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050021#include "GrTextureProxy.h"
bsalomonf267c1e2016-02-01 13:16:14 -080022#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070023#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080024#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070025#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080026#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050027#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050028#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070029#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070030#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050031#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070032
bungeman6bd52842016-10-27 09:30:08 -070033SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070034 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070035 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070036 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070037 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080038 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070039 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070040 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070041{
bungeman6bd52842016-10-27 09:30:08 -070042 SkASSERT(fTexture->width() == w);
43 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070044}
piotaixrcef04f82014-07-14 07:48:04 -070045
reed6f1216a2015-08-04 08:10:13 -070046SkImage_Gpu::~SkImage_Gpu() {
47 if (fAddedRasterVersionToCache.load()) {
48 SkNotifyBitmapGenIDIsStale(this->uniqueID());
49 }
50}
51
bsalomoneaaaf0b2015-01-23 08:08:04 -080052extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070053 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070054 ((SkImage_Gpu*)image)->applyBudgetDecision();
55 }
56}
57
brianosman396fcdb2016-07-22 06:26:11 -070058SkImageInfo SkImage_Gpu::onImageInfo() const {
59 SkColorType ct;
60 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
61 ct = kUnknown_SkColorType;
62 }
63 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
64}
65
brianosman69c166d2016-08-17 14:01:05 -070066static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
67 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070068}
69
Brian Osman61624f02016-12-09 14:51:59 -050070bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050071 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070072 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
73 SkASSERT(dst->getGenerationID() == this->uniqueID());
74 SkASSERT(dst->isImmutable());
75 SkASSERT(dst->getPixels());
76 return true;
77 }
78
brianosman69c166d2016-08-17 14:01:05 -070079 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070080 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070081 return false;
82 }
83 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
84 dst->getPixels(), dst->rowBytes())) {
85 return false;
86 }
reed6f1216a2015-08-04 08:10:13 -070087
88 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080089 if (kAllow_CachingHint == chint) {
90 SkBitmapCache::Add(this->uniqueID(), *dst);
91 fAddedRasterVersionToCache.store(true);
92 }
reed8b26b992015-05-07 15:36:17 -070093 return true;
94}
95
Robert Phillips2c862492017-01-18 10:08:39 -050096sk_sp<GrSurfaceProxy> SkImage_Gpu::refProxy() const {
97 return GrSurfaceProxy::MakeWrapped(fTexture);
98}
99
Brian Salomon514baff2016-11-17 15:17:07 -0500100GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500101 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500102 sk_sp<SkColorSpace>* texColorSpace,
103 SkScalar scaleAdjust[2]) const {
Brian Osman7992da32016-11-18 11:28:24 -0500104 if (texColorSpace) {
105 *texColorSpace = this->fColorSpace;
106 }
107 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(),
108 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500109 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700110}
111
reed8b26b992015-05-07 15:36:17 -0700112static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
113 switch (info.colorType()) {
114 case kRGBA_8888_SkColorType:
115 case kBGRA_8888_SkColorType:
116 break;
117 default:
118 return; // nothing to do
119 }
120
121 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
122 // and in either case, the alpha-byte is always in the same place, so we can safely call
123 // SkPreMultiplyColor()
124 //
125 SkColor* row = (SkColor*)pixels;
126 for (int y = 0; y < info.height(); ++y) {
127 for (int x = 0; x < info.width(); ++x) {
128 row[x] = SkPreMultiplyColor(row[x]);
129 }
130 }
131}
132
Matt Sarett03dd6d52017-01-23 12:15:09 -0500133bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800134 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500135 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500136 return false;
137 }
138
Matt Sarett03dd6d52017-01-23 12:15:09 -0500139 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
140 if (!rec.trim(this->width(), this->height())) {
141 return false;
142 }
143
144 GrPixelConfig config = SkImageInfo2GrPixelConfig(rec.fInfo, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700145 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500146 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700147 // let the GPU perform this transformation for us
148 flags = GrContext::kUnpremul_PixelOpsFlag;
149 }
Matt Sarett03dd6d52017-01-23 12:15:09 -0500150 if (!fTexture->readPixels(fColorSpace.get(), rec.fX, rec.fY, rec.fInfo.width(),
151 rec.fInfo.height(), config, rec.fInfo.colorSpace(), rec.fPixels,
152 rec.fRowBytes, flags)) {
reed8b26b992015-05-07 15:36:17 -0700153 return false;
154 }
155 // do we have to manually fix-up the alpha channel?
156 // src dst
157 // unpremul premul fix manually
158 // premul unpremul done by kUnpremul_PixelOpsFlag
159 // all other combos need to change.
160 //
161 // Should this be handled by Ganesh? todo:?
162 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500163 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
164 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700165 }
166 return true;
167}
168
reed7fb4f8b2016-03-11 04:33:52 -0800169sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700170 GrContext* ctx = fTexture->getContext();
171 GrSurfaceDesc desc = fTexture->desc();
172 desc.fWidth = subset.width();
173 desc.fHeight = subset.height();
174
Robert Phillipse2f7d182016-12-15 09:23:05 -0500175 sk_sp<GrSurfaceContext> sContext(ctx->contextPriv().makeDeferredSurfaceContext(
176 desc,
177 SkBackingFit::kExact,
178 fBudgeted));
179 if (!sContext) {
180 return nullptr;
181 }
182
183 // TODO: make gpu images be proxy-backed so we don't need to do this
184 sk_sp<GrSurfaceProxy> tmpSrc(GrSurfaceProxy::MakeWrapped(fTexture));
185 if (!tmpSrc) {
186 return nullptr;
187 }
188
189 if (!sContext->copy(tmpSrc.get(), subset, SkIPoint::Make(0, 0))) {
190 return nullptr;
191 }
192
193 // TODO: make gpu images be proxy-backed so we don't need to do this
194 GrSurface* subTx = sContext->asDeferredSurface()->instantiate(ctx->textureProvider());
reed7b6945b2015-09-24 00:50:58 -0700195 if (!subTx) {
196 return nullptr;
197 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500198
reed7fb4f8b2016-03-11 04:33:52 -0800199 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500200 fAlphaType, sk_ref_sp(subTx->asTexture()),
201 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700202}
203
reed8b26b992015-05-07 15:36:17 -0700204///////////////////////////////////////////////////////////////////////////////////////////////////
205
reed7fb4f8b2016-03-11 04:33:52 -0800206static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700207 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
208 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800209 SkImage::TextureReleaseProc releaseProc,
210 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700211 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700213 }
bungeman6bd52842016-10-27 09:30:08 -0700214 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700215 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700216 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700217 }
reedde499882015-06-18 13:41:40 -0700218 if (releaseProc) {
219 tex->setRelease(releaseProc, releaseCtx);
220 }
221
bsalomon5ec26ae2016-02-25 08:33:02 -0800222 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800223 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700224 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225}
226
reed7fb4f8b2016-03-11 04:33:52 -0800227sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700228 SkAlphaType at, sk_sp<SkColorSpace> cs,
229 TextureReleaseProc releaseP, ReleaseContext releaseC) {
230 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
231 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700232}
233
reed7fb4f8b2016-03-11 04:33:52 -0800234sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700235 SkAlphaType at, sk_sp<SkColorSpace> cs) {
236 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
237 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700238}
239
jbaumanb445a572016-06-09 13:24:48 -0700240static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
241 bool nv12,
242 const GrBackendObject yuvTextureHandles[],
243 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700244 GrSurfaceOrigin origin,
245 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800246 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700247
jbaumanb445a572016-06-09 13:24:48 -0700248 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
249 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700251 }
jbaumanb445a572016-06-09 13:24:48 -0700252 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
253 return nullptr;
254 }
255
256 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
257
bsalomon993a4212015-05-29 11:37:25 -0700258 GrBackendTextureDesc yDesc;
259 yDesc.fConfig = kConfig;
260 yDesc.fOrigin = origin;
261 yDesc.fSampleCnt = 0;
262 yDesc.fTextureHandle = yuvTextureHandles[0];
263 yDesc.fWidth = yuvSizes[0].fWidth;
264 yDesc.fHeight = yuvSizes[0].fHeight;
265
266 GrBackendTextureDesc uDesc;
267 uDesc.fConfig = kConfig;
268 uDesc.fOrigin = origin;
269 uDesc.fSampleCnt = 0;
270 uDesc.fTextureHandle = yuvTextureHandles[1];
271 uDesc.fWidth = yuvSizes[1].fWidth;
272 uDesc.fHeight = yuvSizes[1].fHeight;
273
jbaumanb445a572016-06-09 13:24:48 -0700274 sk_sp<GrTexture> yTex(
275 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500276 sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrapped(std::move(yTex));
277
jbaumanb445a572016-06-09 13:24:48 -0700278 sk_sp<GrTexture> uTex(
279 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500280 sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrapped(std::move(uTex));
281
282 sk_sp<GrSurfaceProxy> vProxy;
283
jbaumanb445a572016-06-09 13:24:48 -0700284 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500285 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700286 } else {
287 GrBackendTextureDesc vDesc;
288 vDesc.fConfig = kConfig;
289 vDesc.fOrigin = origin;
290 vDesc.fSampleCnt = 0;
291 vDesc.fTextureHandle = yuvTextureHandles[2];
292 vDesc.fWidth = yuvSizes[2].fWidth;
293 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700294
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500295 sk_sp<GrTexture> vTex = sk_sp<GrTexture>(
jbaumanb445a572016-06-09 13:24:48 -0700296 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500297 vProxy = GrSurfaceProxy::MakeWrapped(std::move(vTex));
jbaumanb445a572016-06-09 13:24:48 -0700298 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500299 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700301 }
302
robertphillipsd4c741e2016-04-28 09:55:15 -0700303 const int width = yuvSizes[0].fWidth;
304 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700305
robertphillipsd4c741e2016-04-28 09:55:15 -0700306 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400307 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
308 SkBackingFit::kExact,
309 width, height,
310 kRGBA_8888_GrPixelConfig,
311 std::move(imageColorSpace),
312 0,
313 origin));
314 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700315 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700316 }
317
318 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400319 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700320 paint.addColorFragmentProcessor(
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500321 GrYUVEffect::MakeYUVToRGB(ctx,
322 sk_ref_sp(yProxy->asTextureProxy()),
323 sk_ref_sp(uProxy->asTextureProxy()),
324 sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700325
robertphillipsd4c741e2016-04-28 09:55:15 -0700326 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700327
Brian Salomon82f44312017-01-11 13:42:54 -0500328 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500329
330 if (!renderTargetContext->accessRenderTarget()) {
331 return nullptr;
332 }
Brian Osman11052242016-10-27 14:47:55 -0400333 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700334 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400335 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500336 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700337}
reed56179002015-07-07 06:11:19 -0700338
jbaumanb445a572016-06-09 13:24:48 -0700339sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
340 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700341 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
342 sk_sp<SkColorSpace> imageColorSpace) {
343 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
344 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700345}
346
347sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
348 const GrBackendObject yuvTextureHandles[2],
349 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700350 GrSurfaceOrigin origin,
351 sk_sp<SkColorSpace> imageColorSpace) {
352 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
353 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700354}
355
bsalomon634b4302016-07-12 18:11:17 -0700356sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700357 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700358 return sk_ref_sp(const_cast<SkImage*>(this));
359 }
brianosman396fcdb2016-07-22 06:26:11 -0700360 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700361 size_t rowBytes = info.minRowBytes();
362 size_t size = info.getSafeSize(rowBytes);
363 auto data = SkData::MakeUninitialized(size);
364 if (!data) {
365 return nullptr;
366 }
367 SkPixmap pm(info, data->writable_data(), rowBytes);
368 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
369 return nullptr;
370 }
371 return MakeRasterData(info, data, rowBytes);
372}
373
reed56179002015-07-07 06:11:19 -0700374///////////////////////////////////////////////////////////////////////////////////////////////////
375
bsalomon4d516a62016-07-28 13:37:31 -0700376namespace {
377struct MipMapLevelData {
378 void* fPixelData;
379 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800380};
381
bsalomon4d516a62016-07-28 13:37:31 -0700382struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500383 uint32_t fContextUniqueID;
384 // Right now, the destination color mode is only considered when generating mipmaps
385 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700386 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500387 int fWidth;
388 int fHeight;
389 SkColorType fColorType;
390 SkAlphaType fAlphaType;
391 void* fColorSpace;
392 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500393 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700394 // The fMipMapLevelData array may contain more than 1 element.
395 // It contains fMipMapLevelCount elements.
396 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500397 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700398};
399} // anonymous namespace
400
cblume33e0cb52016-08-30 12:09:23 -0700401static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800402 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
403 // This can cause runaway memory leaks, so we are disabling this path until we can
404 // investigate further. crbug.com/669775
405 return false;
cblume33e0cb52016-08-30 12:09:23 -0700406}
407
408namespace {
409
410class DTIBufferFiller
411{
412public:
cblume921bc672016-09-22 05:25:26 -0700413 explicit DTIBufferFiller(char* bufferAsCharPtr)
414 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700415
416 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700417 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700418 }
419
420private:
421
cblume921bc672016-09-22 05:25:26 -0700422 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700423};
424}
425
426#define FILL_MEMBER(bufferFiller, member, source) \
427 bufferFiller.fillMember(source, \
428 offsetof(DeferredTextureImage, member), \
429 sizeof(DeferredTextureImage::member));
430
bsalomon41b952c2016-03-11 06:46:33 -0800431size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700432 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700433 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400434 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700435 // Extract relevant min/max values from the params array.
436 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
437 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700438 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700439 for (int i = 1; i < paramCnt; ++i) {
440 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
441 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
442 if (highestFilterQuality < params[i].fQuality)
443 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700444 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700445 }
446
bsalomon41b952c2016-03-11 06:46:33 -0800447 const bool fillMode = SkToBool(buffer);
448 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
449 return 0;
450 }
451
ericrkb4da01d2016-06-13 11:18:14 -0700452 // Calculate scaling parameters.
453 bool isScaled = lowestPreScaleMipLevel != 0;
454
455 SkISize scaledSize;
456 if (isScaled) {
457 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
458 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
459 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
460 lowestPreScaleMipLevel - 1);
461 } else {
462 scaledSize = SkISize::Make(this->width(), this->height());
463 }
464
465 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
466 SkFilterQuality scaleFilterQuality = highestFilterQuality;
467 if (scaleFilterQuality > kMedium_SkFilterQuality) {
468 scaleFilterQuality = kMedium_SkFilterQuality;
469 }
470
ericrkc429baf2016-03-24 15:35:45 -0700471 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700472 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700473 return 0;
474 }
475
bsalomon41b952c2016-03-11 06:46:33 -0800476 SkAutoPixmapStorage pixmap;
477 SkImageInfo info;
478 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500479 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800480 info = pixmap.info();
481 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800482 } else {
483 // Here we're just using presence of data to know whether there is a codec behind the image.
484 // In the future we will access the cacherator and get the exact data that we want to (e.g.
485 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700486 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700487 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800488 return 0;
489 }
Brian Osman7992da32016-11-18 11:28:24 -0500490 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
491 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500492 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500493 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500494 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
495 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500496 } else {
497 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
498 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500499 if (kIndex_8_SkColorType == info.colorType()) {
500 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
501 info = info.makeColorType(kN32_SkColorType);
502 }
bsalomon41b952c2016-03-11 06:46:33 -0800503 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
504 if (fillMode) {
505 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700506 if (isScaled) {
507 if (!this->scalePixels(pixmap, scaleFilterQuality,
508 SkImage::kDisallow_CachingHint)) {
509 return 0;
510 }
511 } else {
512 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
513 return 0;
514 }
bsalomon41b952c2016-03-11 06:46:33 -0800515 }
516 SkASSERT(!pixmap.ctable());
517 }
518 }
cblume2c052802016-05-31 09:55:08 -0700519 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700520 if (useMipMaps) {
521 // SkMipMap only deals with the mipmap levels it generates, which does
522 // not include the base level.
523 // That means it generates and holds levels 1-x instead of 0-x.
524 // So the total mipmap level count is 1 more than what
525 // SkMipMap::ComputeLevelCount returns.
526 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
527
528 // We already initialized pixelSize to the size of the base level.
529 // SkMipMap will generate the extra mipmap levels. Their sizes need to
530 // be added to the total.
531 // Index 0 here does not refer to the base mipmap level -- it is
532 // SkMipMap's first generated mipmap level (level 1).
533 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
534 currentMipMapLevelIndex--) {
535 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
536 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700537 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700538 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
539 }
540 }
bsalomon41b952c2016-03-11 06:46:33 -0800541 size_t size = 0;
542 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
543 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700544 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
545 // We subtract 1 because DeferredTextureImage already includes the base
546 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800547 size_t pixelOffset = size;
548 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700549 size_t colorSpaceOffset = 0;
550 size_t colorSpaceSize = 0;
551 if (info.colorSpace()) {
552 colorSpaceOffset = size;
553 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
554 size += colorSpaceSize;
555 }
bsalomon41b952c2016-03-11 06:46:33 -0800556 if (!fillMode) {
557 return size;
558 }
cblume921bc672016-09-22 05:25:26 -0700559 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
560 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
561 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800562
cblume921bc672016-09-22 05:25:26 -0700563 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
564 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800565
Brian Osman6c15cc72016-10-17 09:51:37 -0400566 // If the context has sRGB support, and we're intending to render to a surface with an attached
567 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
568 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500569 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400570 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
571 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500572 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400573 }
574
bsalomon41b952c2016-03-11 06:46:33 -0800575 SkASSERT(info == pixmap.info());
576 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700577 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
578 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700579 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500580 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700581 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
582 int width = info.width();
583 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
584 int height = info.height();
585 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
586 SkColorType colorType = info.colorType();
587 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
588 SkAlphaType alphaType = info.alphaType();
589 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700590 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700591 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700592 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700593 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700594 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700595 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700596 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700597 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
598 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700599 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700600 } else {
cblume921bc672016-09-22 05:25:26 -0700601 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700602 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700603 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700604 0, sizeof(DeferredTextureImage::fColorSpaceSize));
605 }
606
607 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700608 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700609
610 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700611 static_assert(std::is_standard_layout<MipMapLevelData>::value,
612 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700613
Brian Osman7b8400d2016-11-08 17:08:54 -0500614 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700615 // SkMipMap holds only the mipmap levels it generates.
616 // A programmer can use the data they provided to SkMipMap::Build as level 0.
617 // So the SkMipMap provides levels 1-x but it stores them in its own
618 // range 0-(x-1).
619 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
620 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700621 SkMipMap::Level mipLevel;
622 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
623
624 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700625 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700626 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700627 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700628 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700629 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700630 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700631
632 // getSafeSize includes rowbyte padding except for the last row,
633 // right?
634
cblume921bc672016-09-22 05:25:26 -0700635 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700636
cblume921bc672016-09-22 05:25:26 -0700637 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
638 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
639 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700640 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700641 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
642 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
643 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700644
645 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
646 }
bsalomon4d516a62016-07-28 13:37:31 -0700647 }
bsalomon41b952c2016-03-11 06:46:33 -0800648 return size;
649}
650
651sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
652 SkBudgeted budgeted) {
653 if (!data) {
654 return nullptr;
655 }
656 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
657
658 if (!context || context->uniqueID() != dti->fContextUniqueID) {
659 return nullptr;
660 }
cblume33e0cb52016-08-30 12:09:23 -0700661 int mipLevelCount = dti->fMipMapLevelCount;
662 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700663 sk_sp<SkColorSpace> colorSpace;
664 if (dti->fColorSpaceSize) {
665 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
666 }
667 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
668 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osman8ccbbb42017-01-20 14:08:04 -0500669 SkAutoSTArray<16, GrMipLevel> texels(mipLevelCount);
670 for (int i = 0; i < mipLevelCount; i++) {
671 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
672 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
cblume33e0cb52016-08-30 12:09:23 -0700673 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500674
675 return SkImage::MakeTextureFromMipMap(context, info, texels.get(), mipLevelCount,
676 SkBudgeted::kYes, dti->fColorMode);
bsalomon41b952c2016-03-11 06:46:33 -0800677}
678
679///////////////////////////////////////////////////////////////////////////////////////////////////
680
cblume186d2d42016-06-03 11:17:42 -0700681sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
682 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700683 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500684 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700685 if (!ctx) {
686 return nullptr;
687 }
bungeman6bd52842016-10-27 09:30:08 -0700688 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700689 if (!texture) {
690 return nullptr;
691 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500692 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700693 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700694 info.alphaType(), std::move(texture),
695 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700696}