blob: a006e14ecf12ed5923416cedc75d9ba67eda782a [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"
reed856e9d92015-09-30 12:21:45 -070013#include "GrCaps.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000014#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
bsalomonf1ecd212015-12-09 17:06:02 -080016#include "GrImageIDTextureAdjuster.h"
cblume33e0cb52016-08-30 12:09:23 -070017#include "GrTexturePriv.h"
bsalomonf267c1e2016-02-01 13:16:14 -080018#include "effects/GrYUVEffect.h"
bsalomon993a4212015-05-29 11:37:25 -070019#include "SkCanvas.h"
reed262a71b2015-12-05 13:07:27 -080020#include "SkBitmapCache.h"
bsalomon89fe56b2015-10-29 10:49:28 -070021#include "SkGrPriv.h"
reed262a71b2015-12-05 13:07:27 -080022#include "SkImage_Gpu.h"
ericrkb4da01d2016-06-13 11:18:14 -070023#include "SkMipMap.h"
reed6f1216a2015-08-04 08:10:13 -070024#include "SkPixelRef.h"
bsalomon993a4212015-05-29 11:37:25 -070025
bungeman6bd52842016-10-27 09:30:08 -070026SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, sk_sp<GrTexture> tex,
brianosmandddbe382016-07-20 13:55:39 -070027 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
reedaf3fbfc2015-10-04 11:28:36 -070028 : INHERITED(w, h, uniqueID)
bungeman6bd52842016-10-27 09:30:08 -070029 , fTexture(std::move(tex))
reed8b26b992015-05-07 15:36:17 -070030 , fAlphaType(at)
bsalomoneaaaf0b2015-01-23 08:08:04 -080031 , fBudgeted(budgeted)
brianosmandddbe382016-07-20 13:55:39 -070032 , fColorSpace(std::move(colorSpace))
bsalomon1cd63112015-08-05 06:58:39 -070033 , fAddedRasterVersionToCache(false)
reedc9b5f8b2015-10-22 13:20:20 -070034{
bungeman6bd52842016-10-27 09:30:08 -070035 SkASSERT(fTexture->width() == w);
36 SkASSERT(fTexture->height() == h);
reedc9b5f8b2015-10-22 13:20:20 -070037}
piotaixrcef04f82014-07-14 07:48:04 -070038
reed6f1216a2015-08-04 08:10:13 -070039SkImage_Gpu::~SkImage_Gpu() {
40 if (fAddedRasterVersionToCache.load()) {
41 SkNotifyBitmapGenIDIsStale(this->uniqueID());
42 }
43}
44
bsalomoneaaaf0b2015-01-23 08:08:04 -080045extern void SkTextureImageApplyBudgetedDecision(SkImage* image) {
robertphillipsea70c4b2016-07-20 08:54:31 -070046 if (image->isTextureBacked()) {
reed8b26b992015-05-07 15:36:17 -070047 ((SkImage_Gpu*)image)->applyBudgetDecision();
48 }
49}
50
brianosman396fcdb2016-07-22 06:26:11 -070051SkImageInfo SkImage_Gpu::onImageInfo() const {
52 SkColorType ct;
53 if (!GrPixelConfigToColorType(fTexture->config(), &ct)) {
54 ct = kUnknown_SkColorType;
55 }
56 return SkImageInfo::Make(fTexture->width(), fTexture->height(), ct, fAlphaType, fColorSpace);
57}
58
brianosman69c166d2016-08-17 14:01:05 -070059static SkImageInfo make_info(int w, int h, SkAlphaType at, sk_sp<SkColorSpace> colorSpace) {
60 return SkImageInfo::MakeN32(w, h, at, std::move(colorSpace));
reed88d064d2015-10-12 11:30:02 -070061}
62
reed09553032015-11-23 12:32:16 -080063bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const {
reed6f1216a2015-08-04 08:10:13 -070064 if (SkBitmapCache::Find(this->uniqueID(), dst)) {
65 SkASSERT(dst->getGenerationID() == this->uniqueID());
66 SkASSERT(dst->isImmutable());
67 SkASSERT(dst->getPixels());
68 return true;
69 }
70
brianosman69c166d2016-08-17 14:01:05 -070071 if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->alphaType(),
brianosmandddbe382016-07-20 13:55:39 -070072 this->fColorSpace))) {
reed8b26b992015-05-07 15:36:17 -070073 return false;
74 }
75 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
76 dst->getPixels(), dst->rowBytes())) {
77 return false;
78 }
reed6f1216a2015-08-04 08:10:13 -070079
80 dst->pixelRef()->setImmutableWithID(this->uniqueID());
reed09553032015-11-23 12:32:16 -080081 if (kAllow_CachingHint == chint) {
82 SkBitmapCache::Add(this->uniqueID(), *dst);
83 fAddedRasterVersionToCache.store(true);
84 }
reed8b26b992015-05-07 15:36:17 -070085 return true;
86}
87
brianosman982eb7f2016-06-06 13:10:58 -070088GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params,
Brian Osman7b8400d2016-11-08 17:08:54 -050089 SkDestinationSurfaceColorMode colorMode) const {
brianosman5814b8a2016-08-18 06:43:03 -070090 GrTextureAdjuster adjuster(this->peekTexture(), this->alphaType(), this->bounds(), this->uniqueID(),
reed2d5b7142016-08-17 11:12:33 -070091 this->onImageInfo().colorSpace());
Brian Osman7b8400d2016-11-08 17:08:54 -050092 return adjuster.refTextureSafeForParams(params, colorMode, nullptr);
reed85d91782015-09-10 14:33:38 -070093}
94
reed8b26b992015-05-07 15:36:17 -070095static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) {
96 switch (info.colorType()) {
97 case kRGBA_8888_SkColorType:
98 case kBGRA_8888_SkColorType:
99 break;
100 default:
101 return; // nothing to do
102 }
103
104 // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian,
105 // and in either case, the alpha-byte is always in the same place, so we can safely call
106 // SkPreMultiplyColor()
107 //
108 SkColor* row = (SkColor*)pixels;
109 for (int y = 0; y < info.height(); ++y) {
110 for (int x = 0; x < info.width(); ++x) {
111 row[x] = SkPreMultiplyColor(row[x]);
112 }
113 }
114}
115
116bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
reed09553032015-11-23 12:32:16 -0800117 int srcX, int srcY, CachingHint) const {
brianosmanb109b8c2016-06-16 13:03:24 -0700118 GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps());
reed8b26b992015-05-07 15:36:17 -0700119 uint32_t flags = 0;
120 if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) {
121 // let the GPU perform this transformation for us
122 flags = GrContext::kUnpremul_PixelOpsFlag;
123 }
124 if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config,
125 pixels, rowBytes, flags)) {
126 return false;
127 }
128 // do we have to manually fix-up the alpha channel?
129 // src dst
130 // unpremul premul fix manually
131 // premul unpremul done by kUnpremul_PixelOpsFlag
132 // all other combos need to change.
133 //
134 // Should this be handled by Ganesh? todo:?
135 //
136 if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) {
137 apply_premul(info, pixels, rowBytes);
138 }
139 return true;
140}
141
reed7fb4f8b2016-03-11 04:33:52 -0800142sk_sp<SkImage> SkImage_Gpu::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700143 GrContext* ctx = fTexture->getContext();
144 GrSurfaceDesc desc = fTexture->desc();
145 desc.fWidth = subset.width();
146 desc.fHeight = subset.height();
147
senorblanco87751122016-05-20 07:27:38 -0700148 sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, fBudgeted));
reed7b6945b2015-09-24 00:50:58 -0700149 if (!subTx) {
150 return nullptr;
151 }
bungeman6bd52842016-10-27 09:30:08 -0700152 ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800153 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700154 fAlphaType, std::move(subTx), fColorSpace, fBudgeted);
reed7b6945b2015-09-24 00:50:58 -0700155}
156
reed8b26b992015-05-07 15:36:17 -0700157///////////////////////////////////////////////////////////////////////////////////////////////////
158
reed7fb4f8b2016-03-11 04:33:52 -0800159static sk_sp<SkImage> new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700160 SkAlphaType at, sk_sp<SkColorSpace> colorSpace,
161 GrWrapOwnership ownership,
reed7fb4f8b2016-03-11 04:33:52 -0800162 SkImage::TextureReleaseProc releaseProc,
163 SkImage::ReleaseContext releaseCtx) {
reed8b26b992015-05-07 15:36:17 -0700164 if (desc.fWidth <= 0 || desc.fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700166 }
bungeman6bd52842016-10-27 09:30:08 -0700167 sk_sp<GrTexture> tex = ctx->textureProvider()->wrapBackendTexture(desc, ownership);
reed8b26b992015-05-07 15:36:17 -0700168 if (!tex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
reed8b26b992015-05-07 15:36:17 -0700170 }
reedde499882015-06-18 13:41:40 -0700171 if (releaseProc) {
172 tex->setRelease(releaseProc, releaseCtx);
173 }
174
bsalomon5ec26ae2016-02-25 08:33:02 -0800175 const SkBudgeted budgeted = SkBudgeted::kNo;
reed7fb4f8b2016-03-11 04:33:52 -0800176 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700177 at, std::move(tex), std::move(colorSpace), budgeted);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700178}
179
reed7fb4f8b2016-03-11 04:33:52 -0800180sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700181 SkAlphaType at, sk_sp<SkColorSpace> cs,
182 TextureReleaseProc releaseP, ReleaseContext releaseC) {
183 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kBorrow_GrWrapOwnership,
184 releaseP, releaseC);
bsalomon6dc6f5f2015-06-18 09:12:16 -0700185}
186
reed7fb4f8b2016-03-11 04:33:52 -0800187sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc,
brianosmandddbe382016-07-20 13:55:39 -0700188 SkAlphaType at, sk_sp<SkColorSpace> cs) {
189 return new_wrapped_texture_common(ctx, desc, at, std::move(cs), kAdopt_GrWrapOwnership,
190 nullptr, nullptr);
reed8b26b992015-05-07 15:36:17 -0700191}
192
jbaumanb445a572016-06-09 13:24:48 -0700193static sk_sp<SkImage> make_from_yuv_textures_copy(GrContext* ctx, SkYUVColorSpace colorSpace,
194 bool nv12,
195 const GrBackendObject yuvTextureHandles[],
196 const SkISize yuvSizes[],
brianosmandddbe382016-07-20 13:55:39 -0700197 GrSurfaceOrigin origin,
198 sk_sp<SkColorSpace> imageColorSpace) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800199 const SkBudgeted budgeted = SkBudgeted::kYes;
bsalomon993a4212015-05-29 11:37:25 -0700200
jbaumanb445a572016-06-09 13:24:48 -0700201 if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || yuvSizes[1].fWidth <= 0 ||
202 yuvSizes[1].fHeight <= 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700203 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700204 }
jbaumanb445a572016-06-09 13:24:48 -0700205 if (!nv12 && (yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0)) {
206 return nullptr;
207 }
208
209 const GrPixelConfig kConfig = nv12 ? kRGBA_8888_GrPixelConfig : kAlpha_8_GrPixelConfig;
210
bsalomon993a4212015-05-29 11:37:25 -0700211 GrBackendTextureDesc yDesc;
212 yDesc.fConfig = kConfig;
213 yDesc.fOrigin = origin;
214 yDesc.fSampleCnt = 0;
215 yDesc.fTextureHandle = yuvTextureHandles[0];
216 yDesc.fWidth = yuvSizes[0].fWidth;
217 yDesc.fHeight = yuvSizes[0].fHeight;
218
219 GrBackendTextureDesc uDesc;
220 uDesc.fConfig = kConfig;
221 uDesc.fOrigin = origin;
222 uDesc.fSampleCnt = 0;
223 uDesc.fTextureHandle = yuvTextureHandles[1];
224 uDesc.fWidth = yuvSizes[1].fWidth;
225 uDesc.fHeight = yuvSizes[1].fHeight;
226
jbaumanb445a572016-06-09 13:24:48 -0700227 sk_sp<GrTexture> yTex(
228 ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
229 sk_sp<GrTexture> uTex(
230 ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
231 sk_sp<GrTexture> vTex;
232 if (nv12) {
233 vTex = uTex;
234 } else {
235 GrBackendTextureDesc vDesc;
236 vDesc.fConfig = kConfig;
237 vDesc.fOrigin = origin;
238 vDesc.fSampleCnt = 0;
239 vDesc.fTextureHandle = yuvTextureHandles[2];
240 vDesc.fWidth = yuvSizes[2].fWidth;
241 vDesc.fHeight = yuvSizes[2].fHeight;
bsalomon993a4212015-05-29 11:37:25 -0700242
jbaumanb445a572016-06-09 13:24:48 -0700243 vTex = sk_sp<GrTexture>(
244 ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
245 }
bsalomon993a4212015-05-29 11:37:25 -0700246 if (!yTex || !uTex || !vTex) {
halcanary96fcdcc2015-08-27 07:41:13 -0700247 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700248 }
249
robertphillipsd4c741e2016-04-28 09:55:15 -0700250 const int width = yuvSizes[0].fWidth;
251 const int height = yuvSizes[0].fHeight;
robertphillipsaa19a5f2016-04-28 06:21:55 -0700252
robertphillipsd4c741e2016-04-28 09:55:15 -0700253 // Needs to be a render target in order to draw to it for the yuv->rgb conversion.
Brian Osman11052242016-10-27 14:47:55 -0400254 sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeRenderTargetContext(
255 SkBackingFit::kExact,
256 width, height,
257 kRGBA_8888_GrPixelConfig,
258 std::move(imageColorSpace),
259 0,
260 origin));
261 if (!renderTargetContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700262 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700263 }
264
265 GrPaint paint;
Mike Reed7d954ad2016-10-28 15:42:34 -0400266 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
jbaumanb445a572016-06-09 13:24:48 -0700267 paint.addColorFragmentProcessor(
268 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700269
robertphillipsd4c741e2016-04-28 09:55:15 -0700270 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700271
Brian Osman11052242016-10-27 14:47:55 -0400272 renderTargetContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
273 ctx->flushSurfaceWrites(renderTargetContext->accessRenderTarget());
robertphillipsd4c741e2016-04-28 09:55:15 -0700274 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
Brian Osman11052242016-10-27 14:47:55 -0400275 kOpaque_SkAlphaType, renderTargetContext->asTexture(),
276 sk_ref_sp(renderTargetContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700277}
reed56179002015-07-07 06:11:19 -0700278
jbaumanb445a572016-06-09 13:24:48 -0700279sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
280 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700281 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
282 sk_sp<SkColorSpace> imageColorSpace) {
283 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
284 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700285}
286
287sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
288 const GrBackendObject yuvTextureHandles[2],
289 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700290 GrSurfaceOrigin origin,
291 sk_sp<SkColorSpace> imageColorSpace) {
292 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
293 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700294}
295
reed7fb4f8b2016-03-11 04:33:52 -0800296static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500297 sk_sp<GrTexture> texture(
298 maker->refTextureForParams(GrTextureParams::ClampNoFilter(),
299 SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware));
bsalomon8e74f802016-01-30 10:01:40 -0800300 if (!texture) {
301 return nullptr;
302 }
bungeman6bd52842016-10-27 09:30:08 -0700303 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700304 sk_ref_sp(maker->getColorSpace()), SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800305}
306
reed7fb4f8b2016-03-11 04:33:52 -0800307sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800308 if (!context) {
309 return nullptr;
310 }
311 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800312 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800313 }
bsalomon8e74f802016-01-30 10:01:40 -0800314
315 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
316 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
brianosman69c166d2016-08-17 14:01:05 -0700317 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800318 }
reed9a5a2012016-08-08 09:00:29 -0700319
320 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
321 GrBitmapTextureMaker maker(context, *bmp);
brianosman69c166d2016-08-17 14:01:05 -0700322 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800323 }
reed9a5a2012016-08-08 09:00:29 -0700324 return nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800325}
326
bsalomon634b4302016-07-12 18:11:17 -0700327sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700328 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700329 return sk_ref_sp(const_cast<SkImage*>(this));
330 }
brianosman396fcdb2016-07-22 06:26:11 -0700331 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700332 size_t rowBytes = info.minRowBytes();
333 size_t size = info.getSafeSize(rowBytes);
334 auto data = SkData::MakeUninitialized(size);
335 if (!data) {
336 return nullptr;
337 }
338 SkPixmap pm(info, data->writable_data(), rowBytes);
339 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
340 return nullptr;
341 }
342 return MakeRasterData(info, data, rowBytes);
343}
344
reed7fb4f8b2016-03-11 04:33:52 -0800345sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
346 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800347 if (!ctx) {
348 return nullptr;
349 }
bungeman6bd52842016-10-27 09:30:08 -0700350 sk_sp<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800351 if (!texture) {
352 return nullptr;
353 }
reed7fb4f8b2016-03-11 04:33:52 -0800354 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700355 pixmap.alphaType(), std::move(texture),
brianosmandddbe382016-07-20 13:55:39 -0700356 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800357}
358
reed56179002015-07-07 06:11:19 -0700359///////////////////////////////////////////////////////////////////////////////////////////////////
360
bsalomon4d516a62016-07-28 13:37:31 -0700361namespace {
362struct MipMapLevelData {
363 void* fPixelData;
364 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800365};
366
bsalomon4d516a62016-07-28 13:37:31 -0700367struct DeferredTextureImage {
Brian Osman7b8400d2016-11-08 17:08:54 -0500368 uint32_t fContextUniqueID;
369 // Right now, the destination color mode is only considered when generating mipmaps
370 SkDestinationSurfaceColorMode fColorMode;
bsalomon4d516a62016-07-28 13:37:31 -0700371 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
Brian Osman7b8400d2016-11-08 17:08:54 -0500372 int fWidth;
373 int fHeight;
374 SkColorType fColorType;
375 SkAlphaType fAlphaType;
376 void* fColorSpace;
377 size_t fColorSpaceSize;
378 int fColorTableCnt;
379 uint32_t* fColorTableData;
380 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700381 // The fMipMapLevelData array may contain more than 1 element.
382 // It contains fMipMapLevelCount elements.
383 // That means this struct's size is not known at compile-time.
Brian Osman7b8400d2016-11-08 17:08:54 -0500384 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700385};
386} // anonymous namespace
387
cblume33e0cb52016-08-30 12:09:23 -0700388static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
389 bool shouldUseMipMaps = false;
390
391 // Use mipmaps if either
392 // 1.) it is a perspective matrix, or
393 // 2.) the quality is med/high and the scale is < 1
394 if (param.fMatrix.hasPerspective()) {
395 shouldUseMipMaps = true;
396 }
397 if (param.fQuality == kMedium_SkFilterQuality ||
398 param.fQuality == kHigh_SkFilterQuality) {
399 SkScalar minAxisScale = param.fMatrix.getMinScale();
400 if (minAxisScale != -1.f && minAxisScale < 1.f) {
401 shouldUseMipMaps = true;
402 }
403 }
404
405
406 return shouldUseMipMaps;
407}
408
409namespace {
410
411class DTIBufferFiller
412{
413public:
cblume921bc672016-09-22 05:25:26 -0700414 explicit DTIBufferFiller(char* bufferAsCharPtr)
415 : bufferAsCharPtr_(bufferAsCharPtr) {}
cblume33e0cb52016-08-30 12:09:23 -0700416
417 void fillMember(const void* source, size_t memberOffset, size_t size) {
cblume921bc672016-09-22 05:25:26 -0700418 memcpy(bufferAsCharPtr_ + memberOffset, source, size);
cblume33e0cb52016-08-30 12:09:23 -0700419 }
420
421private:
422
cblume921bc672016-09-22 05:25:26 -0700423 char* bufferAsCharPtr_;
cblume33e0cb52016-08-30 12:09:23 -0700424};
425}
426
427#define FILL_MEMBER(bufferFiller, member, source) \
428 bufferFiller.fillMember(source, \
429 offsetof(DeferredTextureImage, member), \
430 sizeof(DeferredTextureImage::member));
431
bsalomon41b952c2016-03-11 06:46:33 -0800432size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700433 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700434 int paramCnt, void* buffer,
Brian Osman6c15cc72016-10-17 09:51:37 -0400435 SkColorSpace* dstColorSpace) const {
ericrkb4da01d2016-06-13 11:18:14 -0700436 // Extract relevant min/max values from the params array.
437 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
438 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700439 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700440 for (int i = 1; i < paramCnt; ++i) {
441 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
442 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
443 if (highestFilterQuality < params[i].fQuality)
444 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700445 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700446 }
447
bsalomon41b952c2016-03-11 06:46:33 -0800448 const bool fillMode = SkToBool(buffer);
449 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
450 return 0;
451 }
452
ericrkb4da01d2016-06-13 11:18:14 -0700453 // Calculate scaling parameters.
454 bool isScaled = lowestPreScaleMipLevel != 0;
455
456 SkISize scaledSize;
457 if (isScaled) {
458 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
459 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
460 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
461 lowestPreScaleMipLevel - 1);
462 } else {
463 scaledSize = SkISize::Make(this->width(), this->height());
464 }
465
466 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
467 SkFilterQuality scaleFilterQuality = highestFilterQuality;
468 if (scaleFilterQuality > kMedium_SkFilterQuality) {
469 scaleFilterQuality = kMedium_SkFilterQuality;
470 }
471
ericrkc429baf2016-03-24 15:35:45 -0700472 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700473 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700474 return 0;
475 }
476
bsalomon41b952c2016-03-11 06:46:33 -0800477 SkAutoPixmapStorage pixmap;
478 SkImageInfo info;
479 size_t pixelSize = 0;
480 size_t ctSize = 0;
481 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700482 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800483 info = pixmap.info();
484 pixelSize = SkAlign8(pixmap.getSafeSize());
485 if (pixmap.ctable()) {
486 ctCount = pixmap.ctable()->count();
487 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
488 }
489 } else {
490 // Here we're just using presence of data to know whether there is a codec behind the image.
491 // In the future we will access the cacherator and get the exact data that we want to (e.g.
492 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700493 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700494 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800495 return 0;
496 }
brianosman32b5e702016-10-13 06:44:23 -0700497 info = as_IB(this)->onImageInfo().makeWH(scaledSize.width(), scaledSize.height());
bsalomon41b952c2016-03-11 06:46:33 -0800498 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
499 if (fillMode) {
500 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700501 if (isScaled) {
502 if (!this->scalePixels(pixmap, scaleFilterQuality,
503 SkImage::kDisallow_CachingHint)) {
504 return 0;
505 }
506 } else {
507 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
508 return 0;
509 }
bsalomon41b952c2016-03-11 06:46:33 -0800510 }
511 SkASSERT(!pixmap.ctable());
512 }
513 }
cblume2c052802016-05-31 09:55:08 -0700514 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700515 if (useMipMaps) {
516 // SkMipMap only deals with the mipmap levels it generates, which does
517 // not include the base level.
518 // That means it generates and holds levels 1-x instead of 0-x.
519 // So the total mipmap level count is 1 more than what
520 // SkMipMap::ComputeLevelCount returns.
521 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
522
523 // We already initialized pixelSize to the size of the base level.
524 // SkMipMap will generate the extra mipmap levels. Their sizes need to
525 // be added to the total.
526 // Index 0 here does not refer to the base mipmap level -- it is
527 // SkMipMap's first generated mipmap level (level 1).
528 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
529 currentMipMapLevelIndex--) {
530 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
531 currentMipMapLevelIndex);
brianosman32b5e702016-10-13 06:44:23 -0700532 SkImageInfo mipInfo = info.makeWH(mipSize.fWidth, mipSize.fHeight);
cblume33e0cb52016-08-30 12:09:23 -0700533 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
534 }
535 }
bsalomon41b952c2016-03-11 06:46:33 -0800536 size_t size = 0;
537 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
538 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700539 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
540 // We subtract 1 because DeferredTextureImage already includes the base
541 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800542 size_t pixelOffset = size;
543 size += pixelSize;
544 size_t ctOffset = size;
545 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700546 size_t colorSpaceOffset = 0;
547 size_t colorSpaceSize = 0;
548 if (info.colorSpace()) {
549 colorSpaceOffset = size;
550 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
551 size += colorSpaceSize;
552 }
bsalomon41b952c2016-03-11 06:46:33 -0800553 if (!fillMode) {
554 return size;
555 }
cblume921bc672016-09-22 05:25:26 -0700556 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer);
557 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset;
558 void* pixels = pixelsAsCharPtr;
cblume33e0cb52016-08-30 12:09:23 -0700559 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800560 if (ctSize) {
cblume921bc672016-09-22 05:25:26 -0700561 ct = bufferAsCharPtr + ctOffset;
bsalomon41b952c2016-03-11 06:46:33 -0800562 }
563
cblume921bc672016-09-22 05:25:26 -0700564 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAsCharPtr))),
565 pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800566 if (ctSize) {
567 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
568 }
569
Brian Osman6c15cc72016-10-17 09:51:37 -0400570 // If the context has sRGB support, and we're intending to render to a surface with an attached
571 // color space, and the image has an sRGB-like color space attached, then use our gamma (sRGB)
572 // aware mip-mapping.
Brian Osman7b8400d2016-11-08 17:08:54 -0500573 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
Brian Osman6c15cc72016-10-17 09:51:37 -0400574 if (proxy.fCaps->srgbSupport() && SkToBool(dstColorSpace) &&
575 info.colorSpace() && info.colorSpace()->gammaCloseToSRGB()) {
Brian Osman7b8400d2016-11-08 17:08:54 -0500576 colorMode = SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware;
Brian Osman6c15cc72016-10-17 09:51:37 -0400577 }
578
bsalomon41b952c2016-03-11 06:46:33 -0800579 SkASSERT(info == pixmap.info());
580 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700581 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
582 "offsetof, which we use below, requires the type have standard layout");
cblume921bc672016-09-22 05:25:26 -0700583 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr};
Brian Osman7b8400d2016-11-08 17:08:54 -0500584 FILL_MEMBER(dtiBufferFiller, fColorMode, &colorMode);
cblume33e0cb52016-08-30 12:09:23 -0700585 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
586 int width = info.width();
587 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
588 int height = info.height();
589 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
590 SkColorType colorType = info.colorType();
591 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
592 SkAlphaType alphaType = info.alphaType();
593 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
594 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
595 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
596 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
cblume921bc672016-09-22 05:25:26 -0700597 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData),
cblume33e0cb52016-08-30 12:09:23 -0700598 &pixels, sizeof(pixels));
cblume921bc672016-09-22 05:25:26 -0700599 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes),
cblume33e0cb52016-08-30 12:09:23 -0700600 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700601 if (colorSpaceSize) {
cblume921bc672016-09-22 05:25:26 -0700602 void* colorSpace = bufferAsCharPtr + colorSpaceOffset;
cblume33e0cb52016-08-30 12:09:23 -0700603 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
604 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
cblume921bc672016-09-22 05:25:26 -0700605 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset);
bsalomon4d516a62016-07-28 13:37:31 -0700606 } else {
cblume921bc672016-09-22 05:25:26 -0700607 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace),
cblume33e0cb52016-08-30 12:09:23 -0700608 0, sizeof(DeferredTextureImage::fColorSpace));
cblume921bc672016-09-22 05:25:26 -0700609 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize),
cblume33e0cb52016-08-30 12:09:23 -0700610 0, sizeof(DeferredTextureImage::fColorSpaceSize));
611 }
612
613 // Fill in the mipmap levels if they exist
cblume921bc672016-09-22 05:25:26 -0700614 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700615
616 if (useMipMaps) {
cblume94ddf542016-09-16 10:07:32 -0700617 static_assert(std::is_standard_layout<MipMapLevelData>::value,
618 "offsetof, which we use below, requires the type have a standard layout");
cblume33e0cb52016-08-30 12:09:23 -0700619
Brian Osman7b8400d2016-11-08 17:08:54 -0500620 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(pixmap, colorMode, nullptr));
cblume33e0cb52016-08-30 12:09:23 -0700621 // SkMipMap holds only the mipmap levels it generates.
622 // A programmer can use the data they provided to SkMipMap::Build as level 0.
623 // So the SkMipMap provides levels 1-x but it stores them in its own
624 // range 0-(x-1).
625 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLevelCount - 1;
626 generatedMipLevelIndex++) {
cblume33e0cb52016-08-30 12:09:23 -0700627 SkMipMap::Level mipLevel;
628 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
629
630 // Make sure the mipmap data is after the start of the buffer
cblume921bc672016-09-22 05:25:26 -0700631 SkASSERT(mipLevelPtr > bufferAsCharPtr);
cblume33e0cb52016-08-30 12:09:23 -0700632 // Make sure the mipmap data starts before the end of the buffer
cblume921bc672016-09-22 05:25:26 -0700633 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700634 // Make sure the mipmap data ends before the end of the buffer
cblume12f75272016-09-19 06:18:03 -0700635 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
cblume921bc672016-09-22 05:25:26 -0700636 bufferAsCharPtr + pixelOffset + pixelSize);
cblume33e0cb52016-08-30 12:09:23 -0700637
638 // getSafeSize includes rowbyte padding except for the last row,
639 // right?
640
cblume921bc672016-09-22 05:25:26 -0700641 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSafeSize());
cblume33e0cb52016-08-30 12:09:23 -0700642
cblume921bc672016-09-22 05:25:26 -0700643 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
644 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
645 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(void*));
cblume33e0cb52016-08-30 12:09:23 -0700646 size_t rowBytes = mipLevel.fPixmap.rowBytes();
cblume921bc672016-09-22 05:25:26 -0700647 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData) +
648 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
649 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBytes));
cblume33e0cb52016-08-30 12:09:23 -0700650
651 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
652 }
bsalomon4d516a62016-07-28 13:37:31 -0700653 }
bsalomon41b952c2016-03-11 06:46:33 -0800654 return size;
655}
656
657sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
658 SkBudgeted budgeted) {
659 if (!data) {
660 return nullptr;
661 }
662 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
663
664 if (!context || context->uniqueID() != dti->fContextUniqueID) {
665 return nullptr;
666 }
Hal Canary67b39de2016-11-07 11:47:44 -0500667 sk_sp<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700668 if (dti->fColorTableCnt) {
669 SkASSERT(dti->fColorTableData);
670 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800671 }
cblume33e0cb52016-08-30 12:09:23 -0700672 int mipLevelCount = dti->fMipMapLevelCount;
673 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700674 sk_sp<SkColorSpace> colorSpace;
675 if (dti->fColorSpaceSize) {
676 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
677 }
678 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
679 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700680 if (mipLevelCount == 1) {
681 SkPixmap pixmap;
682 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
683 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
684 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
685 } else {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400686 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
cblume33e0cb52016-08-30 12:09:23 -0700687 for (int i = 0; i < mipLevelCount; i++) {
688 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
689 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
690 }
691
692 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
693 mipLevelCount, SkBudgeted::kYes,
Brian Osman7b8400d2016-11-08 17:08:54 -0500694 dti->fColorMode);
cblume33e0cb52016-08-30 12:09:23 -0700695 }
bsalomon41b952c2016-03-11 06:46:33 -0800696}
697
698///////////////////////////////////////////////////////////////////////////////////////////////////
699
cblume186d2d42016-06-03 11:17:42 -0700700sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
701 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700702 SkBudgeted budgeted,
Brian Osman7b8400d2016-11-08 17:08:54 -0500703 SkDestinationSurfaceColorMode colorMode) {
cblume186d2d42016-06-03 11:17:42 -0700704 if (!ctx) {
705 return nullptr;
706 }
bungeman6bd52842016-10-27 09:30:08 -0700707 sk_sp<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
cblume186d2d42016-06-03 11:17:42 -0700708 if (!texture) {
709 return nullptr;
710 }
Brian Osman7b8400d2016-11-08 17:08:54 -0500711 texture->texturePriv().setMipColorMode(colorMode);
cblume186d2d42016-06-03 11:17:42 -0700712 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
bungeman6bd52842016-10-27 09:30:08 -0700713 info.alphaType(), std::move(texture),
714 sk_ref_sp(info.colorSpace()), budgeted);
cblume186d2d42016-06-03 11:17:42 -0700715}