blob: 10a8c0d7efcf35afce67e7ad5c099b6b5785ed25 [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 Osman2c2bc112017-02-28 10:02:49 -050017#include "GrGpu.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050018#include "GrImageTextureMaker.h"
Brian Osman11052242016-10-27 14:47:55 -040019#include "GrRenderTargetContext.h"
Brian Osman32342f02017-03-04 08:12:46 -050020#include "GrResourceProvider.h"
Brian Osmanfe3b5162017-03-02 15:09:20 -050021#include "GrSemaphore.h"
Brian Osmane8e54582016-11-28 10:06:27 -050022#include "GrTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070023#include "GrTexturePriv.h"
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -050024#include "GrTextureProxy.h"
Robert Phillips538f1a32017-03-08 14:32:55 -050025#include "GrTextureToYUVPlanes.h"
Brian Osman63954c92017-03-14 12:07:12 -040026#include "effects/GrNonlinearColorSpaceXformEffect.h"
bsalomonf267c1e2016-02-01 13:16:14 -080027#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070028#include "SkCanvas.h"
Brian Osman2c2bc112017-02-28 10:02:49 -050029#include "SkCrossContextImageData.h"
reed262a71b2015-12-05 13:07:27 -080030#include "SkBitmapCache.h"
Brian Osman3b655982017-03-07 16:58:08 -050031#include "SkGr.h"
reed262a71b2015-12-05 13:07:27 -080032#include "SkImage_Gpu.h"
Brian Osman7992da32016-11-18 11:28:24 -050033#include "SkImageCacherator.h"
Matt Sarettcb6266b2017-01-17 10:48:53 -050034#include "SkImageInfoPriv.h"
ericrkb4da01d2016-06-13 11:18:14 -070035#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070036#include "SkPixelRef.h"
Matt Sarett03dd6d52017-01-23 12:15:09 -050037#include "SkReadPixelsRec.h"
bsalomon993a4212015-05-29 11:37:25 -070038
Robert Phillipsb726d582017-03-09 16:36:32 -050039SkImage_Gpu::SkImage_Gpu(uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070040 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
Robert Phillipsb726d582017-03-09 16:36:32 -050041 : INHERITED(tex->width(), tex->height(), uniqueID)
42 , fContext(tex->getContext())
43 , fProxy(GrSurfaceProxy::MakeWrapped(std::move(tex)))
reed8b26b992015-05-07 15:36:17 -070044 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080045 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070046 , fColorSpace(std::move(colorSpace))
Robert Phillipsb726d582017-03-09 16:36:32 -050047 , fAddedRasterVersionToCache(false) {
48}
49
50SkImage_Gpu::SkImage_Gpu(GrContext* context, uint32_t uniqueID, SkAlphaType at,
51 sk_sp<GrTextureProxy> proxy,
52 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
53 : INHERITED(proxy->width(), proxy->height(), uniqueID)
54 , fContext(context)
55 , fProxy(std::move(proxy))
56 , fAlphaType(at)
57 , fBudgeted(budgeted)
58 , fColorSpace(std::move(colorSpace))
59 , fAddedRasterVersionToCache(false) {
reedc9b5f8b2015-10-22 13:20:20 -070060}
piotaixrcef04f82014-07-14 07:48:04 -070061
reed6f1216a2015-08-04 08:10:13 -070062SkImage_Gpu::~SkImage_Gpu() {
63 if (fAddedRasterVersionToCache.load()) {
64 SkNotifyBitmapGenIDIsStale(this->uniqueID());
65 }
66}
67
Yuqian Li06ee8e82017-03-17 20:50:06 +000068extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
69 if (image->isTextureBacked()) {
70 ((SkImage_Gpu*)image)->applyBudgetDecision();
71 }
72}
73
brianosman396fcdb2016-07-22 06:26:11 -070074SkImageInfo SkImage_Gpu::onImageInfo() const {
75 SkColorType ct;
Robert Phillipsb726d582017-03-09 16:36:32 -050076 if (!GrPixelConfigToColorType(fProxy->config(), &ct)) {
brianosman396fcdb2016-07-22 06:26:11 -070077 ct = kUnknown_SkColorType;
78 }
Robert Phillipsb726d582017-03-09 16:36:32 -050079 return SkImageInfo::Make(fProxy->width(), fProxy->height(), ct, fAlphaType, fColorSpace);
brianosman396fcdb2016-07-22 06:26:11 -070080}
81
brianosman69c166d2016-08-17 14:01:05 -070082static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
83 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070084}
85
Brian Osman61624f02016-12-09 14:51:59 -050086bool SkImage_Gpu::getROPixels(SkBitmap* dst, SkColorSpace* dstColorSpace,
Brian Osman7992da32016-11-18 11:28:24 -050087 CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070088 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
89 SkASSERT(dst->getGenerationID() == this->uniqueID());
90 SkASSERT(dst->isImmutable());
91 SkASSERT(dst->getPixels());
92 return true;
93 }
94
Robert Phillipsb726d582017-03-09 16:36:32 -050095 SkImageInfo ii = make_info(this->width(), this->height(), this->alphaType(),
96 sk_ref_sp(dstColorSpace));
97 if (!dst->tryAllocPixels(ii)) {
reed8b26b992015-05-07 15:36:17 -070098 return false;
99 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500100
101 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
102 fProxy,
103 fColorSpace);
104 if (!sContext) {
105 return false;
106 }
107
108 if (!sContext->readPixels(dst->info(), dst->getPixels(), dst->rowBytes(), 0, 0)) {
reed8b26b992015-05-07 15:36:17 -0700109 return false;
110 }
reed6f1216a2015-08-04 08:10:13 -0700111
112 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -0800113 if (kAllow_CachingHint == chint) {
114 SkBitmapCache::Add(this->uniqueID(), *dst);
115 fAddedRasterVersionToCache.store(true);
116 }
reed8b26b992015-05-07 15:36:17 -0700117 return true;
118}
119
Robert Phillipsb726d582017-03-09 16:36:32 -0500120sk_sp<GrTextureProxy> SkImage_Gpu::asTextureProxyRef(GrContext* context,
121 const GrSamplerParams& params,
122 SkColorSpace* dstColorSpace,
123 sk_sp<SkColorSpace>* texColorSpace,
124 SkScalar scaleAdjust[2]) const {
125 sk_sp<GrTexture> tex(this->asTextureRef(context, params, dstColorSpace,
126 texColorSpace, scaleAdjust));
127
128 return GrSurfaceProxy::MakeWrapped(std::move(tex));
Robert Phillips2c862492017-01-18 10:08:39 -0500129}
130
Brian Salomon514baff2016-11-17 15:17:07 -0500131GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrSamplerParams& params,
Brian Osman61624f02016-12-09 14:51:59 -0500132 SkColorSpace* dstColorSpace,
Robert Phillips67c18d62017-01-20 12:44:06 -0500133 sk_sp<SkColorSpace>* texColorSpace,
134 SkScalar scaleAdjust[2]) const {
Robert Phillipsb726d582017-03-09 16:36:32 -0500135 if (ctx != fContext) {
136 SkASSERT(0);
137 return nullptr;
138 }
139
Brian Osman7992da32016-11-18 11:28:24 -0500140 if (texColorSpace) {
141 *texColorSpace = this->fColorSpace;
142 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500143 GrTexture* texture = fProxy->instantiate(fContext->resourceProvider());
144 if (!texture) {
145 return nullptr;
146 }
147
Robert Phillips0c984a02017-03-16 07:51:56 -0400148 GrTextureAdjuster adjuster(fContext, texture, this->alphaType(), this->bounds(),
Brian Osman7992da32016-11-18 11:28:24 -0500149 this->uniqueID(), this->fColorSpace.get());
Robert Phillips67c18d62017-01-20 12:44:06 -0500150 return adjuster.refTextureSafeForParams(params, nullptr, scaleAdjust);
reed85d91782015-09-10 14:33:38 -0700151}
152
reed8b26b992015-05-07 15:36:17 -0700153static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
154 switch (info.colorType()) {
155 case kRGBA_8888_SkColorType:
156 case kBGRA_8888_SkColorType:
157 break;
158 default:
159 return; // nothing to do
160 }
161
162 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
163 // and in either case, the alpha-byte is always in the same place, so we can safely call
164 // SkPreMultiplyColor()
165 //
166 SkColor* row = (SkColor*)pixels;
167 for (int y = 0; y < info.height(); ++y) {
168 for (int x = 0; x < info.width(); ++x) {
169 row[x] = SkPreMultiplyColor(row[x]);
170 }
171 }
172}
173
Robert Phillips538f1a32017-03-08 14:32:55 -0500174bool SkImage_Gpu::onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
175 const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
Robert Phillipsb726d582017-03-09 16:36:32 -0500176 if (GrTextureToYUVPlanes(fContext, fProxy, sizes, planes, rowBytes, colorSpace)) {
177 return true;
Robert Phillips538f1a32017-03-08 14:32:55 -0500178 }
179
180 return INHERITED::onReadYUV8Planes(sizes, planes, rowBytes, colorSpace);
181}
182
Matt Sarett03dd6d52017-01-23 12:15:09 -0500183bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
reed09553032015-11-23 12:32:16 -0800184 int srcX, int srcY, CachingHint) const {
Matt Sarett03dd6d52017-01-23 12:15:09 -0500185 if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
Matt Sarettcb6266b2017-01-17 10:48:53 -0500186 return false;
187 }
188
Matt Sarett03dd6d52017-01-23 12:15:09 -0500189 SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, srcX, srcY);
190 if (!rec.trim(this->width(), this->height())) {
191 return false;
192 }
193
Robert Phillipsb726d582017-03-09 16:36:32 -0500194 // TODO: this seems to duplicate code in GrTextureContext::onReadPixels and
195 // GrRenderTargetContext::onReadPixels
reed8b26b992015-05-07 15:36:17 -0700196 uint32_t flags = 0;
Matt Sarett03dd6d52017-01-23 12:15:09 -0500197 if (kUnpremul_SkAlphaType == rec.fInfo.alphaType() && kPremul_SkAlphaType == fAlphaType) {
reed8b26b992015-05-07 15:36:17 -0700198 // let the GPU perform this transformation for us
199 flags = GrContext::kUnpremul_PixelOpsFlag;
200 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500201
202 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
203 fProxy,
204 fColorSpace);
205 if (!sContext) {
reed8b26b992015-05-07 15:36:17 -0700206 return false;
207 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500208
209 if (!sContext->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY, flags)) {
210 return false;
211 }
212
reed8b26b992015-05-07 15:36:17 -0700213 // do we have to manually fix-up the alpha channel?
214 // src dst
215 // unpremul premul fix manually
216 // premul unpremul done by kUnpremul_PixelOpsFlag
217 // all other combos need to change.
218 //
219 // Should this be handled by Ganesh? todo:?
220 //
Matt Sarett03dd6d52017-01-23 12:15:09 -0500221 if (kPremul_SkAlphaType == rec.fInfo.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
222 apply_premul(rec.fInfo, rec.fPixels, rec.fRowBytes);
reed8b26b992015-05-07 15:36:17 -0700223 }
224 return true;
225}
226
reed7fb4f8b2016-03-11 04:33:52 -0800227sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
Robert Phillipsb726d582017-03-09 16:36:32 -0500228 GrSurfaceDesc desc = fProxy->desc();
reed7b6945b2015-09-24 00:50:58 -0700229 desc.fWidth = subset.width();
230 desc.fHeight = subset.height();
231
Robert Phillipsb726d582017-03-09 16:36:32 -0500232 sk_sp<GrSurfaceContext> sContext(fContext->contextPriv().makeDeferredSurfaceContext(
Robert Phillipse2f7d182016-12-15 09:23:05 -0500233 desc,
234 SkBackingFit::kExact,
235 fBudgeted));
236 if (!sContext) {
237 return nullptr;
238 }
239
Robert Phillipsb726d582017-03-09 16:36:32 -0500240 if (!sContext->copy(fProxy.get(), subset, SkIPoint::Make(0, 0))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500241 return nullptr;
242 }
243
Robert Phillipsb726d582017-03-09 16:36:32 -0500244 // MDB: this call is okay bc we know 'sContext' was kExact
245 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
246 fAlphaType, sContext->asTextureProxyRef(),
Robert Phillipse2f7d182016-12-15 09:23:05 -0500247 fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700248}
249
reed8b26b992015-05-07 15:36:17 -0700250///////////////////////////////////////////////////////////////////////////////////////////////////
251
reed7fb4f8b2016-03-11 04:33:52 -0800252static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700253 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
254 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800255 SkImage::TextureReleaseProc releaseProc,
256 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700257 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700259 }
Brian Osman32342f02017-03-04 08:12:46 -0500260 sk_sp<GrTexture> tex = ctx->resourceProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700261 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700262 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700263 }
reedde499882015-06-18 13:41:40 -0700264 if (releaseProc) {
265 tex->setRelease(releaseProc, releaseCtx);
266 }
267
Brian Osman766fcbb2017-03-13 09:33:09 -0400268 const SkBudgeted budgeted = (kAdoptAndCache_GrWrapOwnership == ownership)
269 ? SkBudgeted::kYes : SkBudgeted::kNo;
Robert Phillipsb726d582017-03-09 16:36:32 -0500270 return sk_make_sp<SkImage_Gpu>(kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700271 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700272}
273
reed7fb4f8b2016-03-11 04:33:52 -0800274sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700275 SkAlphaType at, sk_sp<SkColorSpace> cs,
276 TextureReleaseProc releaseP, ReleaseContext releaseC) {
277 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
278 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700279}
280
reed7fb4f8b2016-03-11 04:33:52 -0800281sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700282 SkAlphaType at, sk_sp<SkColorSpace> cs) {
283 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
284 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700285}
286
jbaumanb445a572016-06-09 13:24:48 -0700287static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
288 bool nv12,
289 const GrBackendObject yuvTextureHandles[],
290 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700291 GrSurfaceOrigin origin,
292 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800293 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700294
jbaumanb445a572016-06-09 13:24:48 -0700295 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
296 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700297 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700298 }
jbaumanb445a572016-06-09 13:24:48 -0700299 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
300 return nullptr;
301 }
302
303 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
304
bsalomon993a4212015-05-29 11:37:25 -0700305 GrBackendTextureDesc yDesc;
306 yDesc.fConfig = kConfig;
307 yDesc.fOrigin = origin;
308 yDesc.fSampleCnt = 0;
309 yDesc.fTextureHandle = yuvTextureHandles[0];
310 yDesc.fWidth = yuvSizes[0].fWidth;
311 yDesc.fHeight = yuvSizes[0].fHeight;
312
313 GrBackendTextureDesc uDesc;
314 uDesc.fConfig = kConfig;
315 uDesc.fOrigin = origin;
316 uDesc.fSampleCnt = 0;
317 uDesc.fTextureHandle = yuvTextureHandles[1];
318 uDesc.fWidth = yuvSizes[1].fWidth;
319 uDesc.fHeight = yuvSizes[1].fHeight;
320
Robert Phillips26caf892017-01-27 10:58:31 -0500321 sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yDesc);
322 sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uDesc);
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500323 sk_sp<GrSurfaceProxy> vProxy;
324
jbaumanb445a572016-06-09 13:24:48 -0700325 if (nv12) {
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500326 vProxy = uProxy;
jbaumanb445a572016-06-09 13:24:48 -0700327 } else {
328 GrBackendTextureDesc vDesc;
329 vDesc.fConfig = kConfig;
330 vDesc.fOrigin = origin;
331 vDesc.fSampleCnt = 0;
332 vDesc.fTextureHandle = yuvTextureHandles[2];
333 vDesc.fWidth = yuvSizes[2].fWidth;
334 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700335
Robert Phillips26caf892017-01-27 10:58:31 -0500336 vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vDesc);
jbaumanb445a572016-06-09 13:24:48 -0700337 }
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500338 if (!yProxy || !uProxy || !vProxy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700339 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700340 }
341
robertphillipsd4c741e2016-04-28 09:55:15 -0700342 const int width = yuvSizes[0].fWidth;
343 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700344
robertphillipsd4c741e2016-04-28 09:55:15 -0700345 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400346 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
347 SkBackingFit::kExact,
348 width, height,
349 kRGBA_8888_GrPixelConfig,
350 std::move(imageColorSpace),
351 0,
352 origin));
353 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700354 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700355 }
356
357 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400358 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700359 paint.addColorFragmentProcessor(
Robert Phillips296b1cc2017-03-15 10:42:12 -0400360 GrYUVEffect::MakeYUVToRGB(ctx->resourceProvider(),
Robert Phillipsbc7a4fb2017-01-23 15:30:35 -0500361 sk_ref_sp(yProxy->asTextureProxy()),
362 sk_ref_sp(uProxy->asTextureProxy()),
363 sk_ref_sp(vProxy->asTextureProxy()), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700364
Robert Phillipsb726d582017-03-09 16:36:32 -0500365 const SkRect rect = SkRect::MakeIWH(width, height);
robertphillipsc9a37062015-09-01 08:34:28 -0700366
Brian Salomon82f44312017-01-11 13:42:54 -0500367 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
Robert Phillipse60ad622016-11-17 10:22:48 -0500368
369 if (!renderTargetContext->accessRenderTarget()) {
370 return nullptr;
371 }
Brian Osman11052242016-10-27 14:47:55 -0400372 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
Robert Phillipsb726d582017-03-09 16:36:32 -0500373
374 // MDB: this call is okay bc we know 'renderTargetContext' was exact
375 return sk_make_sp<SkImage_Gpu>(ctx, kNeedNewImageUniqueID,
376 kOpaque_SkAlphaType, renderTargetContext->asTextureProxyRef(),
Robert Phillips75a475c2017-01-13 09:18:59 -0500377 renderTargetContext->refColorSpace(), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700378}
reed56179002015-07-07 06:11:19 -0700379
jbaumanb445a572016-06-09 13:24:48 -0700380sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
381 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700382 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
383 sk_sp<SkColorSpace> imageColorSpace) {
384 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
385 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700386}
387
388sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
389 const GrBackendObject yuvTextureHandles[2],
390 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700391 GrSurfaceOrigin origin,
392 sk_sp<SkColorSpace> imageColorSpace) {
393 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
394 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700395}
396
Brian Osman041f7df2017-02-07 11:23:28 -0500397static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id,
398 SkColorSpace* dstColorSpace) {
399 sk_sp<SkColorSpace> texColorSpace;
400 sk_sp<GrTexture> texture(maker->refTextureForParams(GrSamplerParams::ClampNoFilter(),
401 dstColorSpace, &texColorSpace, nullptr));
402 if (!texture) {
403 return nullptr;
404 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500405 return sk_make_sp<SkImage_Gpu>(id, at, std::move(texture),
Brian Osman041f7df2017-02-07 11:23:28 -0500406 std::move(texColorSpace), SkBudgeted::kNo);
407}
408
Brian Osman2c2bc112017-02-28 10:02:49 -0500409sk_sp<SkImage> SkImage::makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const {
Brian Osman041f7df2017-02-07 11:23:28 -0500410 if (!context) {
411 return nullptr;
412 }
413 if (GrTexture* peek = as_IB(this)->peekTexture()) {
414 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
415 }
416
417 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
418 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
419 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
420 }
421
422 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
423 GrBitmapTextureMaker maker(context, *bmp);
424 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID(), dstColorSpace);
425 }
426 return nullptr;
427}
428
Brian Osman2c2bc112017-02-28 10:02:49 -0500429std::unique_ptr<SkCrossContextImageData> SkCrossContextImageData::MakeFromEncoded(
430 GrContext* context, sk_sp<SkData> encoded, SkColorSpace* dstColorSpace) {
431 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(std::move(encoded));
432 if (!codecImage) {
433 return nullptr;
434 }
435
436 // Some backends or drivers don't support (safely) moving resources between contexts
437 if (!context->caps()->crossContextTextureSupport()) {
438 return std::unique_ptr<SkCrossContextImageData>(
439 new SkCrossContextImageData(std::move(codecImage)));
440 }
441
442 sk_sp<SkImage> textureImage = codecImage->makeTextureImage(context, dstColorSpace);
443 if (!textureImage) {
444 // TODO: Force decode to raster here? Do mip-mapping, like getDeferredTextureImageData?
445 return std::unique_ptr<SkCrossContextImageData>(
446 new SkCrossContextImageData(std::move(codecImage)));
447 }
448
449 // Crack open the gpu image, extract the backend data, stick it in the SkCCID
450 GrTexture* texture = as_IB(textureImage)->peekTexture();
451 SkASSERT(texture);
452
453 GrBackendTextureDesc desc;
454 desc.fFlags = kNone_GrBackendTextureFlag;
455 desc.fOrigin = texture->origin();
456 desc.fWidth = texture->width();
457 desc.fHeight = texture->height();
458 desc.fConfig = texture->config();
459 desc.fSampleCnt = 0;
460
461 auto textureData = texture->texturePriv().detachBackendTexture();
462 SkASSERT(textureData);
463
464 SkImageInfo info = as_IB(textureImage)->onImageInfo();
465 return std::unique_ptr<SkCrossContextImageData>(new SkCrossContextImageData(
466 desc, std::move(textureData), info.alphaType(), info.refColorSpace()));
467}
468
469sk_sp<SkImage> SkImage::MakeFromCrossContextImageData(
470 GrContext* context, std::unique_ptr<SkCrossContextImageData> ccid) {
471 if (ccid->fImage) {
472 // No pre-existing GPU resource. We could upload it now (with makeTextureImage),
473 // but we'd need a dstColorSpace.
474 return ccid->fImage;
475 }
476
477 if (ccid->fTextureData) {
Brian Osmanfe3b5162017-03-02 15:09:20 -0500478 ccid->fTextureData->attachToContext(context);
Brian Osman2c2bc112017-02-28 10:02:49 -0500479 }
480
Brian Osman766fcbb2017-03-13 09:33:09 -0400481 // This texture was created by Ganesh on another thread (see MakeFromEncoded, above).
482 // Thus, we can import it back into our cache and treat it as our own (again).
483 GrWrapOwnership ownership = kAdoptAndCache_GrWrapOwnership;
484 return new_wrapped_texture_common(context, ccid->fDesc, ccid->fAlphaType,
485 std::move(ccid->fColorSpace), ownership, nullptr, nullptr);
Brian Osman2c2bc112017-02-28 10:02:49 -0500486}
487
bsalomon634b4302016-07-12 18:11:17 -0700488sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700489 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700490 return sk_ref_sp(const_cast<SkImage*>(this));
491 }
brianosman396fcdb2016-07-22 06:26:11 -0700492 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700493 size_t rowBytes = info.minRowBytes();
494 size_t size = info.getSafeSize(rowBytes);
495 auto data = SkData::MakeUninitialized(size);
496 if (!data) {
497 return nullptr;
498 }
499 SkPixmap pm(info, data->writable_data(), rowBytes);
500 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
501 return nullptr;
502 }
503 return MakeRasterData(info, data, rowBytes);
504}
505
Brian Osmanb92234a2017-01-25 14:13:00 +0000506sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
507 SkBudgeted budgeted) {
508 if (!ctx) {
509 return nullptr;
510 }
511 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
512 if (!texture) {
513 return nullptr;
514 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500515 return sk_make_sp<SkImage_Gpu>(kNeedNewImageUniqueID,
Brian Osmanb92234a2017-01-25 14:13:00 +0000516 pixmap.alphaType(), std::move(texture),
517 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
518}
519
reed56179002015-07-07 06:11:19 -0700520///////////////////////////////////////////////////////////////////////////////////////////////////
521
bsalomon4d516a62016-07-28 13:37:31 -0700522namespace {
523struct MipMapLevelData {
524 void* fPixelData;
525 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800526};
527
bsalomon4d516a62016-07-28 13:37:31 -0700528struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500529 uint32_t fContextUniqueID;
530 // Right now, the destination color mode is only considered when generating mipmaps
531 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700532 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500533 int fWidth;
534 int fHeight;
535 SkColorType fColorType;
536 SkAlphaType fAlphaType;
537 void* fColorSpace;
538 size_t fColorSpaceSize;
Brian Osman7b8400d2016-11-08 17:08:54 -0500539 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700540 // The fMipMapLevelData array may contain more than 1 element.
541 // It contains fMipMapLevelCount elements.
542 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500543 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700544};
545} // anonymous namespace
546
cblume33e0cb52016-08-30 12:09:23 -0700547static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
Eric Karla422d702016-11-30 11:09:29 -0800548 // There is a bug in the mipmap pre-generation logic in use in getDeferredTextureImageData.
549 // This can cause runaway memory leaks, so we are disabling this path until we can
550 // investigate further. crbug.com/669775
551 return false;
cblume33e0cb52016-08-30 12:09:23 -0700552}
553
554namespace {
555
556class DTIBufferFiller
557{
558public:
cblume921bc672016-09-22 05:25:26 -0700559 explicit DTIBufferFiller(char* bufferAsCharPtr)
560 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700561
562 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700563 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700564 }
565
566private:
567
cblume921bc672016-09-22 05:25:26 -0700568 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700569};
570}
571
572#define FILL_MEMBER(bufferFiller, member, source) \
573 bufferFiller.fillMember(source, \
574 offsetof(DeferredTextureImage, member), \
575 sizeof(DeferredTextureImage::member));
576
bsalomon41b952c2016-03-11 06:46:33 -0800577size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700578 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700579 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400580 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700581 // Extract relevant min/max values from the params array.
582 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
583 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700584 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700585 for (int i = 1; i < paramCnt; ++i) {
586 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
587 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
588 if (highestFilterQuality < params[i].fQuality)
589 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700590 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700591 }
592
bsalomon41b952c2016-03-11 06:46:33 -0800593 const bool fillMode = SkToBool(buffer);
594 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
595 return 0;
596 }
597
ericrkb4da01d2016-06-13 11:18:14 -0700598 // Calculate scaling parameters.
599 bool isScaled = lowestPreScaleMipLevel != 0;
600
601 SkISize scaledSize;
602 if (isScaled) {
603 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
604 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
605 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
606 lowestPreScaleMipLevel - 1);
607 } else {
608 scaledSize = SkISize::Make(this->width(), this->height());
609 }
610
611 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
612 SkFilterQuality scaleFilterQuality = highestFilterQuality;
613 if (scaleFilterQuality > kMedium_SkFilterQuality) {
614 scaleFilterQuality = kMedium_SkFilterQuality;
615 }
616
ericrkc429baf2016-03-24 15:35:45 -0700617 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700618 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700619 return 0;
620 }
621
bsalomon41b952c2016-03-11 06:46:33 -0800622 SkAutoPixmapStorage pixmap;
623 SkImageInfo info;
624 size_t pixelSize = 0;
Brian Osmanaaedae72017-01-20 13:21:56 -0500625 if (!isScaled && this->peekPixels(&pixmap) && !pixmap.ctable()) {
bsalomon41b952c2016-03-11 06:46:33 -0800626 info = pixmap.info();
627 pixelSize = SkAlign8(pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800628 } else {
629 // Here we're just using presence of data to know whether there is a codec behind the image.
630 // In the future we will access the cacherator and get the exact data that we want to (e.g.
631 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700632 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700633 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800634 return 0;
635 }
Brian Osman7992da32016-11-18 11:28:24 -0500636 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
637 // Generator backed image. Tweak info to trigger correct kind of decode.
Brian Osman7992da32016-11-18 11:28:24 -0500638 SkImageCacherator::CachedFormat cacheFormat = cacher->chooseCacheFormat(
Brian Osman61624f02016-12-09 14:51:59 -0500639 dstColorSpace, proxy.fCaps.get());
Brian Osman7992da32016-11-18 11:28:24 -0500640 info = cacher->buildCacheInfo(cacheFormat).makeWH(scaledSize.width(),
641 scaledSize.height());
Brian Osman7992da32016-11-18 11:28:24 -0500642 } else {
643 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
644 }
Brian Osmanaaedae72017-01-20 13:21:56 -0500645 if (kIndex_8_SkColorType == info.colorType()) {
646 // Force Index8 to be N32 instead. Index8 is unsupported in Ganesh.
647 info = info.makeColorType(kN32_SkColorType);
648 }
bsalomon41b952c2016-03-11 06:46:33 -0800649 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
650 if (fillMode) {
651 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700652 if (isScaled) {
653 if (!this->scalePixels(pixmap, scaleFilterQuality,
654 SkImage::kDisallow_CachingHint)) {
655 return 0;
656 }
657 } else {
658 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
659 return 0;
660 }
bsalomon41b952c2016-03-11 06:46:33 -0800661 }
662 SkASSERT(!pixmap.ctable());
663 }
664 }
cblume2c052802016-05-31 09:55:08 -0700665 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700666 if (useMipMaps) {
667 // SkMipMap only deals with the mipmap levels it generates, which does
668 // not include the base level.
669 // That means it generates and holds levels 1-x instead of 0-x.
670 // So the total mipmap level count is 1 more than what
671 // SkMipMap::ComputeLevelCount returns.
672 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
673
674 // We already initialized pixelSize to the size of the base level.
675 // SkMipMap will generate the extra mipmap levels. Their sizes need to
676 // be added to the total.
677 // Index 0 here does not refer to the base mipmap level -- it is
678 // SkMipMap's first generated mipmap level (level 1).
679 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
680 currentMipMapLevelIndex--) {
681 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
682 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700683 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700684 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
685 }
686 }
bsalomon41b952c2016-03-11 06:46:33 -0800687 size_t size = 0;
688 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
689 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700690 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
691 // We subtract 1 because DeferredTextureImage already includes the base
692 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800693 size_t pixelOffset = size;
694 size += pixelSize;
bsalomon4d516a62016-07-28 13:37:31 -0700695 size_t colorSpaceOffset = 0;
696 size_t colorSpaceSize = 0;
697 if (info.colorSpace()) {
698 colorSpaceOffset = size;
699 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
700 size += colorSpaceSize;
701 }
bsalomon41b952c2016-03-11 06:46:33 -0800702 if (!fillMode) {
703 return size;
704 }
cblume921bc672016-09-22 05:25:26 -0700705 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
706 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
707 void* pixels = pixelsAsCharPtr;
bsalomon41b952c2016-03-11 06:46:33 -0800708
cblume921bc672016-09-22 05:25:26 -0700709 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
710 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800711
Brian Osman6c15cc72016-10-17 09:51:37 -0400712 // If the context has sRGB support, and we're intending to render to a surface with an attached
713 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
714 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500715 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400716 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
717 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500718 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400719 }
720
bsalomon41b952c2016-03-11 06:46:33 -0800721 SkASSERT(info == pixmap.info());
722 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700723 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
724 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700725 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500726 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700727 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
728 int width = info.width();
729 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
730 int height = info.height();
731 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
732 SkColorType colorType = info.colorType();
733 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
734 SkAlphaType alphaType = info.alphaType();
735 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
cblume33e0cb52016-08-30 12:09:23 -0700736 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700737 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700738 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700739 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700740 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700741 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700742 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700743 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
744 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700745 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700746 } else {
cblume921bc672016-09-22 05:25:26 -0700747 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700748 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700749 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700750 0, sizeof(DeferredTextureImage::fColorSpaceSize));
751 }
752
753 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700754 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700755
756 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700757 static_assert(std::is_standard_layout<MipMapLevelData>::value,
758 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700759
Brian Osman7b8400d2016-11-08 17:08:54 -0500760 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700761 // SkMipMap holds only the mipmap levels it generates.
762 // A programmer can use the data they provided to SkMipMap::Build as level 0.
763 // So the SkMipMap provides levels 1-x but it stores them in its own
764 // range 0-(x-1).
765 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
766 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700767 SkMipMap::Level mipLevel;
768 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
769
770 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700771 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700772 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700773 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700774 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700775 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700776 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700777
778 // getSafeSize includes rowbyte padding except for the last row,
779 // right?
780
cblume921bc672016-09-22 05:25:26 -0700781 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700782
cblume921bc672016-09-22 05:25:26 -0700783 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
784 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
785 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700786 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700787 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
788 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
789 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700790
791 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
792 }
bsalomon4d516a62016-07-28 13:37:31 -0700793 }
bsalomon41b952c2016-03-11 06:46:33 -0800794 return size;
795}
796
797sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
798 SkBudgeted budgeted) {
799 if (!data) {
800 return nullptr;
801 }
802 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
803
804 if (!context || context->uniqueID() != dti->fContextUniqueID) {
805 return nullptr;
806 }
cblume33e0cb52016-08-30 12:09:23 -0700807 int mipLevelCount = dti->fMipMapLevelCount;
808 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700809 sk_sp<SkColorSpace> colorSpace;
810 if (dti->fColorSpaceSize) {
811 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
812 }
813 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
814 dti->fColorType, dti->fAlphaType, colorSpace);
Brian Osmanb92234a2017-01-25 14:13:00 +0000815 if (mipLevelCount == 1) {
816 SkPixmap pixmap;
817 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, dti->fMipMapLevelData[0].fRowBytes);
818 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
819 } else {
820 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
821 for (int i = 0; i < mipLevelCount; i++) {
822 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
823 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
824 }
Brian Osman8ccbbb42017-01-20 14:08:04 -0500825
Brian Osmanb92234a2017-01-25 14:13:00 +0000826 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
827 mipLevelCount, SkBudgeted::kYes,
828 dti->fColorMode);
829 }
bsalomon41b952c2016-03-11 06:46:33 -0800830}
831
832///////////////////////////////////////////////////////////////////////////////////////////////////
833
cblume186d2d42016-06-03 11:17:42 -0700834sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
835 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700836 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500837 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700838 if (!ctx) {
839 return nullptr;
840 }
Robert Phillipsa4c41b32017-03-15 13:02:45 -0400841 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount, colorMode));
cblume186d2d42016-06-03 11:17:42 -0700842 if (!texture) {
843 return nullptr;
844 }
Robert Phillipsb726d582017-03-09 16:36:32 -0500845 return sk_make_sp<SkImage_Gpu>(kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700846 info.alphaType(), std::move(texture),
847 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700848}
Brian Osman63954c92017-03-14 12:07:12 -0400849
850sk_sp<SkImage> SkImage_Gpu::onMakeColorSpace(sk_sp<SkColorSpace> colorSpace) const {
Mike Klein919cc452017-03-18 15:36:52 +0000851 sk_sp<SkColorSpace> srcSpace = fColorSpace ? fColorSpace : SkColorSpace::MakeSRGB();
852 auto xform = GrNonlinearColorSpaceXformEffect::Make(srcSpace.get(), colorSpace.get());
Brian Osman63954c92017-03-14 12:07:12 -0400853 if (!xform) {
854 return sk_ref_sp(const_cast<SkImage_Gpu*>(this));
855 }
856
857 sk_sp<GrRenderTargetContext> renderTargetContext(fContext->makeRenderTargetContext(
858 SkBackingFit::kExact, this->width(), this->height(), kRGBA_8888_GrPixelConfig, nullptr));
859 if (!renderTargetContext) {
860 return nullptr;
861 }
862
863 GrPaint paint;
864 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Robert Phillips296b1cc2017-03-15 10:42:12 -0400865 paint.addColorTextureProcessor(fContext->resourceProvider(), fProxy, nullptr, SkMatrix::I());
Brian Osman63954c92017-03-14 12:07:12 -0400866 paint.addColorFragmentProcessor(std::move(xform));
867
868 const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
869
870 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
871
872 if (!renderTargetContext->accessRenderTarget()) {
873 return nullptr;
874 }
875
876 // MDB: this call is okay bc we know 'renderTargetContext' was exact
877 return sk_make_sp<SkImage_Gpu>(fContext, kNeedNewImageUniqueID,
878 fAlphaType, renderTargetContext->asTextureProxyRef(),
879 std::move(colorSpace), fBudgeted);
880
881}