blob: 06ec224a2a9cbc23fa02b83288128a4252329b26 [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"
bsalomon993a4212015-05-29 11:37:25 -070015#include "GrDrawContext.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
reed80c772b2015-07-30 18:58:23 -070026SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, 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)
reed8b26b992015-05-07 15:36:17 -070029 , fTexture(SkRef(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{
35 SkASSERT(tex->width() == w);
36 SkASSERT(tex->height() == h);
37}
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,
89 SkSourceGammaTreatment gammaTreatment) 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());
92 return adjuster.refTextureSafeForParams(params, gammaTreatment, 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 }
senorblanco87751122016-05-20 07:27:38 -0700152 ctx->copySurface(subTx.get(), fTexture, subset, SkIPoint::Make(0, 0));
reed7fb4f8b2016-03-11 04:33:52 -0800153 return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID,
brianosmandddbe382016-07-20 13:55:39 -0700154 fAlphaType, subTx.get(), 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 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700167 SkAutoTUnref<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,
brianosmandddbe382016-07-20 13:55:39 -0700177 at, tex, 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.
robertphillips6738c702016-07-27 12:13:51 -0700254 sk_sp<GrDrawContext> drawContext(ctx->makeDrawContext(SkBackingFit::kExact,
255 width, height,
256 kRGBA_8888_GrPixelConfig,
257 std::move(imageColorSpace),
258 0,
259 origin));
robertphillipsd4c741e2016-04-28 09:55:15 -0700260 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700261 return nullptr;
bsalomon993a4212015-05-29 11:37:25 -0700262 }
263
264 GrPaint paint;
265 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
jbaumanb445a572016-06-09 13:24:48 -0700266 paint.addColorFragmentProcessor(
267 GrYUVEffect::MakeYUVToRGB(yTex.get(), uTex.get(), vTex.get(), yuvSizes, colorSpace, nv12));
bsalomon993a4212015-05-29 11:37:25 -0700268
robertphillipsd4c741e2016-04-28 09:55:15 -0700269 const SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700270
cdalton846c0512016-05-13 10:25:00 -0700271 drawContext->drawRect(GrNoClip(), paint, SkMatrix::I(), rect);
robertphillipsd4c741e2016-04-28 09:55:15 -0700272 ctx->flushSurfaceWrites(drawContext->accessRenderTarget());
273 return sk_make_sp<SkImage_Gpu>(width, height, kNeedNewImageUniqueID,
brianosmandddbe382016-07-20 13:55:39 -0700274 kOpaque_SkAlphaType, drawContext->asTexture().get(),
brianosmandfe4f2e2016-07-21 13:28:36 -0700275 sk_ref_sp(drawContext->getColorSpace()), budgeted);
bsalomon993a4212015-05-29 11:37:25 -0700276}
reed56179002015-07-07 06:11:19 -0700277
jbaumanb445a572016-06-09 13:24:48 -0700278sk_sp<SkImage> SkImage::MakeFromYUVTexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
279 const GrBackendObject yuvTextureHandles[3],
brianosmandddbe382016-07-20 13:55:39 -0700280 const SkISize yuvSizes[3], GrSurfaceOrigin origin,
281 sk_sp<SkColorSpace> imageColorSpace) {
282 return make_from_yuv_textures_copy(ctx, colorSpace, false, yuvTextureHandles, yuvSizes, origin,
283 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700284}
285
286sk_sp<SkImage> SkImage::MakeFromNV12TexturesCopy(GrContext* ctx, SkYUVColorSpace colorSpace,
287 const GrBackendObject yuvTextureHandles[2],
288 const SkISize yuvSizes[2],
brianosmandddbe382016-07-20 13:55:39 -0700289 GrSurfaceOrigin origin,
290 sk_sp<SkColorSpace> imageColorSpace) {
291 return make_from_yuv_textures_copy(ctx, colorSpace, true, yuvTextureHandles, yuvSizes, origin,
292 std::move(imageColorSpace));
jbaumanb445a572016-06-09 13:24:48 -0700293}
294
reed7fb4f8b2016-03-11 04:33:52 -0800295static sk_sp<SkImage> create_image_from_maker(GrTextureMaker* maker, SkAlphaType at, uint32_t id) {
brianosman982eb7f2016-06-06 13:10:58 -0700296 SkAutoTUnref<GrTexture> texture(maker->refTextureForParams(GrTextureParams::ClampNoFilter(),
297 SkSourceGammaTreatment::kRespect));
bsalomon8e74f802016-01-30 10:01:40 -0800298 if (!texture) {
299 return nullptr;
300 }
reed7fb4f8b2016-03-11 04:33:52 -0800301 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), id, at, texture,
brianosmandddbe382016-07-20 13:55:39 -0700302 sk_ref_sp(maker->getColorSpace()), SkBudgeted::kNo);
bsalomon8e74f802016-01-30 10:01:40 -0800303}
304
reed7fb4f8b2016-03-11 04:33:52 -0800305sk_sp<SkImage> SkImage::makeTextureImage(GrContext *context) const {
bsalomon8e74f802016-01-30 10:01:40 -0800306 if (!context) {
307 return nullptr;
308 }
309 if (GrTexture* peek = as_IB(this)->peekTexture()) {
reed7fb4f8b2016-03-11 04:33:52 -0800310 return peek->getContext() == context ? sk_ref_sp(const_cast<SkImage*>(this)) : nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800311 }
bsalomon8e74f802016-01-30 10:01:40 -0800312
313 if (SkImageCacherator* cacher = as_IB(this)->peekCacherator()) {
314 GrImageTextureMaker maker(context, cacher, this, kDisallow_CachingHint);
brianosman69c166d2016-08-17 14:01:05 -0700315 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800316 }
reed9a5a2012016-08-08 09:00:29 -0700317
318 if (const SkBitmap* bmp = as_IB(this)->onPeekBitmap()) {
319 GrBitmapTextureMaker maker(context, *bmp);
brianosman69c166d2016-08-17 14:01:05 -0700320 return create_image_from_maker(&maker, this->alphaType(), this->uniqueID());
bsalomon8e74f802016-01-30 10:01:40 -0800321 }
reed9a5a2012016-08-08 09:00:29 -0700322 return nullptr;
bsalomon8e74f802016-01-30 10:01:40 -0800323}
324
bsalomon634b4302016-07-12 18:11:17 -0700325sk_sp<SkImage> SkImage::makeNonTextureImage() const {
brianosman396fcdb2016-07-22 06:26:11 -0700326 if (!this->isTextureBacked()) {
bsalomon634b4302016-07-12 18:11:17 -0700327 return sk_ref_sp(const_cast<SkImage*>(this));
328 }
brianosman396fcdb2016-07-22 06:26:11 -0700329 SkImageInfo info = as_IB(this)->onImageInfo();
bsalomon634b4302016-07-12 18:11:17 -0700330 size_t rowBytes = info.minRowBytes();
331 size_t size = info.getSafeSize(rowBytes);
332 auto data = SkData::MakeUninitialized(size);
333 if (!data) {
334 return nullptr;
335 }
336 SkPixmap pm(info, data->writable_data(), rowBytes);
337 if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
338 return nullptr;
339 }
340 return MakeRasterData(info, data, rowBytes);
341}
342
reed7fb4f8b2016-03-11 04:33:52 -0800343sk_sp<SkImage> SkImage::MakeTextureFromPixmap(GrContext* ctx, const SkPixmap& pixmap,
344 SkBudgeted budgeted) {
bsalomon0d996862016-03-09 18:44:43 -0800345 if (!ctx) {
346 return nullptr;
347 }
ericrk8bea8902016-03-18 11:52:20 -0700348 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap, budgeted));
bsalomon0d996862016-03-09 18:44:43 -0800349 if (!texture) {
350 return nullptr;
351 }
reed7fb4f8b2016-03-11 04:33:52 -0800352 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
brianosmandddbe382016-07-20 13:55:39 -0700353 pixmap.alphaType(), texture,
354 sk_ref_sp(pixmap.info().colorSpace()), budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800355}
356
reed56179002015-07-07 06:11:19 -0700357///////////////////////////////////////////////////////////////////////////////////////////////////
358
bsalomon4d516a62016-07-28 13:37:31 -0700359namespace {
360struct MipMapLevelData {
361 void* fPixelData;
362 size_t fRowBytes;
bsalomon41b952c2016-03-11 06:46:33 -0800363};
364
bsalomon4d516a62016-07-28 13:37:31 -0700365struct DeferredTextureImage {
cblume33e0cb52016-08-30 12:09:23 -0700366 uint32_t fContextUniqueID;
367 // Right now, the gamma treatment is only considered when generating mipmaps
368 SkSourceGammaTreatment fGammaTreatment;
bsalomon4d516a62016-07-28 13:37:31 -0700369 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpace.
cblume33e0cb52016-08-30 12:09:23 -0700370 int fWidth;
371 int fHeight;
372 SkColorType fColorType;
373 SkAlphaType fAlphaType;
374 void* fColorSpace;
375 size_t fColorSpaceSize;
376 int fColorTableCnt;
377 uint32_t* fColorTableData;
378 int fMipMapLevelCount;
bsalomon4d516a62016-07-28 13:37:31 -0700379 // The fMipMapLevelData array may contain more than 1 element.
380 // It contains fMipMapLevelCount elements.
381 // That means this struct's size is not known at compile-time.
cblume33e0cb52016-08-30 12:09:23 -0700382 MipMapLevelData fMipMapLevelData[1];
bsalomon4d516a62016-07-28 13:37:31 -0700383};
384} // anonymous namespace
385
cblume33e0cb52016-08-30 12:09:23 -0700386static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
387 bool shouldUseMipMaps = false;
388
389 // Use mipmaps if either
390 // 1.) it is a perspective matrix, or
391 // 2.) the quality is med/high and the scale is < 1
392 if (param.fMatrix.hasPerspective()) {
393 shouldUseMipMaps = true;
394 }
395 if (param.fQuality == kMedium_SkFilterQuality ||
396 param.fQuality == kHigh_SkFilterQuality) {
397 SkScalar minAxisScale = param.fMatrix.getMinScale();
398 if (minAxisScale != -1.f && minAxisScale < 1.f) {
399 shouldUseMipMaps = true;
400 }
401 }
402
403
404 return shouldUseMipMaps;
405}
406
407namespace {
408
409class DTIBufferFiller
410{
411public:
412 explicit DTIBufferFiller(intptr_t bufferAsInt)
413 : bufferAsInt_(bufferAsInt) {}
414
415 void fillMember(const void* source, size_t memberOffset, size_t size) {
416 memcpy(reinterpret_cast<void*>(bufferAsInt_ + memberOffset), source, size);
417 }
418
419private:
420
421 intptr_t bufferAsInt_;
422};
423}
424
425#define FILL_MEMBER(bufferFiller, member, source) \
426 bufferFiller.fillMember(source, \
427 offsetof(DeferredTextureImage, member), \
428 sizeof(DeferredTextureImage::member));
429
bsalomon41b952c2016-03-11 06:46:33 -0800430size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& proxy,
ericrkb4da01d2016-06-13 11:18:14 -0700431 const DeferredTextureImageUsageParams params[],
cblume33e0cb52016-08-30 12:09:23 -0700432 int paramCnt, void* buffer,
433 SkSourceGammaTreatment gammaTreatment) const {
ericrkb4da01d2016-06-13 11:18:14 -0700434 // Extract relevant min/max values from the params array.
435 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
436 SkFilterQuality highestFilterQuality = params[0].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700437 bool useMipMaps = should_use_mip_maps(params[0]);
ericrkb4da01d2016-06-13 11:18:14 -0700438 for (int i = 1; i < paramCnt; ++i) {
439 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
440 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
441 if (highestFilterQuality < params[i].fQuality)
442 highestFilterQuality = params[i].fQuality;
cblume33e0cb52016-08-30 12:09:23 -0700443 useMipMaps |= should_use_mip_maps(params[i]);
ericrkb4da01d2016-06-13 11:18:14 -0700444 }
445
bsalomon41b952c2016-03-11 06:46:33 -0800446 const bool fillMode = SkToBool(buffer);
447 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
448 return 0;
449 }
450
ericrkb4da01d2016-06-13 11:18:14 -0700451 // Calculate scaling parameters.
452 bool isScaled = lowestPreScaleMipLevel != 0;
453
454 SkISize scaledSize;
455 if (isScaled) {
456 // SkMipMap::ComputeLevelSize takes an index into an SkMipMap. SkMipMaps don't contain the
457 // base level, so to get an SkMipMap index we must subtract one from the GL MipMap level.
458 scaledSize = SkMipMap::ComputeLevelSize(this->width(), this->height(),
459 lowestPreScaleMipLevel - 1);
460 } else {
461 scaledSize = SkISize::Make(this->width(), this->height());
462 }
463
464 // We never want to scale at higher than SW medium quality, as SW medium matches GPU high.
465 SkFilterQuality scaleFilterQuality = highestFilterQuality;
466 if (scaleFilterQuality > kMedium_SkFilterQuality) {
467 scaleFilterQuality = kMedium_SkFilterQuality;
468 }
469
ericrkc429baf2016-03-24 15:35:45 -0700470 const int maxTextureSize = proxy.fCaps->maxTextureSize();
ericrkb4da01d2016-06-13 11:18:14 -0700471 if (scaledSize.width() > maxTextureSize || scaledSize.height() > maxTextureSize) {
ericrkc429baf2016-03-24 15:35:45 -0700472 return 0;
473 }
474
bsalomon41b952c2016-03-11 06:46:33 -0800475 SkAutoPixmapStorage pixmap;
476 SkImageInfo info;
477 size_t pixelSize = 0;
478 size_t ctSize = 0;
479 int ctCount = 0;
ericrkb4da01d2016-06-13 11:18:14 -0700480 if (!isScaled && this->peekPixels(&pixmap)) {
bsalomon41b952c2016-03-11 06:46:33 -0800481 info = pixmap.info();
482 pixelSize = SkAlign8(pixmap.getSafeSize());
483 if (pixmap.ctable()) {
484 ctCount = pixmap.ctable()->count();
485 ctSize = SkAlign8(pixmap.ctable()->count() * 4);
486 }
487 } else {
488 // Here we're just using presence of data to know whether there is a codec behind the image.
489 // In the future we will access the cacherator and get the exact data that we want to (e.g.
490 // yuv planes) upload.
bungemanffae30d2016-08-03 13:32:32 -0700491 sk_sp<SkData> data(this->refEncoded());
ericrkb4da01d2016-06-13 11:18:14 -0700492 if (!data && !this->peekPixels(nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800493 return 0;
494 }
brianosman69c166d2016-08-17 14:01:05 -0700495 info = SkImageInfo::MakeN32(scaledSize.width(), scaledSize.height(), this->alphaType());
bsalomon41b952c2016-03-11 06:46:33 -0800496 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr));
497 if (fillMode) {
498 pixmap.alloc(info);
ericrkb4da01d2016-06-13 11:18:14 -0700499 if (isScaled) {
500 if (!this->scalePixels(pixmap, scaleFilterQuality,
501 SkImage::kDisallow_CachingHint)) {
502 return 0;
503 }
504 } else {
505 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) {
506 return 0;
507 }
bsalomon41b952c2016-03-11 06:46:33 -0800508 }
509 SkASSERT(!pixmap.ctable());
510 }
511 }
cblume33e0cb52016-08-30 12:09:23 -0700512 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
cblume2c052802016-05-31 09:55:08 -0700513 int mipMapLevelCount = 1;
cblume33e0cb52016-08-30 12:09:23 -0700514 if (useMipMaps) {
515 // SkMipMap only deals with the mipmap levels it generates, which does
516 // not include the base level.
517 // That means it generates and holds levels 1-x instead of 0-x.
518 // So the total mipmap level count is 1 more than what
519 // SkMipMap::ComputeLevelCount returns.
520 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scaledSize.height()) + 1;
521
522 // We already initialized pixelSize to the size of the base level.
523 // SkMipMap will generate the extra mipmap levels. Their sizes need to
524 // be added to the total.
525 // Index 0 here does not refer to the base mipmap level -- it is
526 // SkMipMap's first generated mipmap level (level 1).
527 for (int currentMipMapLevelIndex = mipMapLevelCount - 2; currentMipMapLevelIndex >= 0;
528 currentMipMapLevelIndex--) {
529 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
530 currentMipMapLevelIndex);
531 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.fHeight, at);
532 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullptr));
533 }
534 }
bsalomon41b952c2016-03-11 06:46:33 -0800535 size_t size = 0;
536 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
537 size += dtiSize;
cblume33e0cb52016-08-30 12:09:23 -0700538 size += (mipMapLevelCount - 1) * sizeof(MipMapLevelData);
539 // We subtract 1 because DeferredTextureImage already includes the base
540 // level in its size
bsalomon41b952c2016-03-11 06:46:33 -0800541 size_t pixelOffset = size;
542 size += pixelSize;
543 size_t ctOffset = size;
544 size += ctSize;
bsalomon4d516a62016-07-28 13:37:31 -0700545 size_t colorSpaceOffset = 0;
546 size_t colorSpaceSize = 0;
547 if (info.colorSpace()) {
548 colorSpaceOffset = size;
549 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
550 size += colorSpaceSize;
551 }
bsalomon41b952c2016-03-11 06:46:33 -0800552 if (!fillMode) {
553 return size;
554 }
555 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
cblume33e0cb52016-08-30 12:09:23 -0700556 intptr_t pixelsAsInt = bufferAsInt + pixelOffset;
557 void* pixels = reinterpret_cast<void*>(pixelsAsInt);
558 void* ct = nullptr;
bsalomon41b952c2016-03-11 06:46:33 -0800559 if (ctSize) {
cblume33e0cb52016-08-30 12:09:23 -0700560 ct = reinterpret_cast<void*>(bufferAsInt + ctOffset);
bsalomon41b952c2016-03-11 06:46:33 -0800561 }
562
cblume33e0cb52016-08-30 12:09:23 -0700563 memcpy(reinterpret_cast<void*>(SkAlign8(pixelsAsInt)), pixmap.addr(), pixmap.getSafeSize());
bsalomon41b952c2016-03-11 06:46:33 -0800564 if (ctSize) {
565 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
566 }
567
568 SkASSERT(info == pixmap.info());
569 size_t rowBytes = pixmap.rowBytes();
cblume33e0cb52016-08-30 12:09:23 -0700570 static_assert(std::is_standard_layout<DeferredTextureImage>::value,
571 "offsetof, which we use below, requires the type have standard layout");
572 auto dtiBufferFiller = DTIBufferFiller{bufferAsInt};
573 FILL_MEMBER(dtiBufferFiller, fGammaTreatment, &gammaTreatment);
574 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID);
575 int width = info.width();
576 FILL_MEMBER(dtiBufferFiller, fWidth, &width);
577 int height = info.height();
578 FILL_MEMBER(dtiBufferFiller, fHeight, &height);
579 SkColorType colorType = info.colorType();
580 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType);
581 SkAlphaType alphaType = info.alphaType();
582 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType);
583 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount);
584 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct);
585 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount);
586// FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount[0].fPixelData, &pixels);
587 memcpy(reinterpret_cast<void*>(bufferAsInt +
588 offsetof(DeferredTextureImage, fMipMapLevelData[0].fPixelData)),
589 &pixels, sizeof(pixels));
590 memcpy(reinterpret_cast<void*>(bufferAsInt +
591 offsetof(DeferredTextureImage, fMipMapLevelData[0].fRowBytes)),
592 &rowBytes, sizeof(rowBytes));
bsalomon4d516a62016-07-28 13:37:31 -0700593 if (colorSpaceSize) {
cblume33e0cb52016-08-30 12:09:23 -0700594 void* colorSpace = reinterpret_cast<void*>(bufferAsInt + colorSpaceOffset);
595 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace);
596 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize);
597 info.colorSpace()->writeToMemory(reinterpret_cast<void*>(bufferAsInt + colorSpaceOffset));
bsalomon4d516a62016-07-28 13:37:31 -0700598 } else {
cblume33e0cb52016-08-30 12:09:23 -0700599 memset(reinterpret_cast<void*>(bufferAsInt +
600 offsetof(DeferredTextureImage, fColorSpace)),
601 0, sizeof(DeferredTextureImage::fColorSpace));
602 memset(reinterpret_cast<void*>(bufferAsInt +
603 offsetof(DeferredTextureImage, fColorSpaceSize)),
604 0, sizeof(DeferredTextureImage::fColorSpaceSize));
605 }
606
607 // Fill in the mipmap levels if they exist
608 intptr_t mipLevelPtr = bufferAsInt + pixelOffset + SkAlign8(pixmap.getSafeSize());
609
610 if (useMipMaps) {
611 // offsetof, which we use below, requires the type have standard layout
612 SkASSERT(std::is_standard_layout<MipMapLevelData>::value);
613
614 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr));
615 // 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++) {
621 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), scaledSize.height(),
622 generatedMipLevelIndex);
623
624 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.fHeight, at);
625 SkMipMap::Level mipLevel;
626 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
627
628 // Make sure the mipmap data is after the start of the buffer
629 SkASSERT(mipLevelPtr > bufferAsInt);
630 // Make sure the mipmap data starts before the end of the buffer
631 SkASSERT(static_cast<size_t>(mipLevelPtr) < bufferAsInt + pixelOffset + pixelSize);
632 // Make sure the mipmap data ends before the end of the buffer
633 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <=
634 bufferAsInt + pixelOffset + pixelSize);
635
636 // getSafeSize includes rowbyte padding except for the last row,
637 // right?
638
639 memcpy(reinterpret_cast<void*>(mipLevelPtr), mipLevel.fPixmap.addr(),
640 mipLevel.fPixmap.getSafeSize());
641
642 memcpy(reinterpret_cast<void*>(bufferAsInt +
643 offsetof(DeferredTextureImage, fMipMapLevelData) +
644 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
645 offsetof(MipMapLevelData, fPixelData)),
646 &mipLevelPtr, sizeof(void*));
647 size_t rowBytes = mipLevel.fPixmap.rowBytes();
648 memcpy(reinterpret_cast<void*>(bufferAsInt +
649 offsetof(DeferredTextureImage, fMipMapLevelData) +
650 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) +
651 offsetof(MipMapLevelData, fRowBytes)),
652 &rowBytes, sizeof(rowBytes));
653
654 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
655 }
bsalomon4d516a62016-07-28 13:37:31 -0700656 }
bsalomon41b952c2016-03-11 06:46:33 -0800657 return size;
658}
659
660sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, const void* data,
661 SkBudgeted budgeted) {
662 if (!data) {
663 return nullptr;
664 }
665 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImage*>(data);
666
667 if (!context || context->uniqueID() != dti->fContextUniqueID) {
668 return nullptr;
669 }
670 SkAutoTUnref<SkColorTable> colorTable;
bsalomon4d516a62016-07-28 13:37:31 -0700671 if (dti->fColorTableCnt) {
672 SkASSERT(dti->fColorTableData);
673 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTableCnt));
bsalomon41b952c2016-03-11 06:46:33 -0800674 }
cblume33e0cb52016-08-30 12:09:23 -0700675 int mipLevelCount = dti->fMipMapLevelCount;
676 SkASSERT(mipLevelCount >= 1);
bsalomon4d516a62016-07-28 13:37:31 -0700677 sk_sp<SkColorSpace> colorSpace;
678 if (dti->fColorSpaceSize) {
679 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpaceSize);
680 }
681 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
682 dti->fColorType, dti->fAlphaType, colorSpace);
cblume33e0cb52016-08-30 12:09:23 -0700683 if (mipLevelCount == 1) {
684 SkPixmap pixmap;
685 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
686 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
687 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
688 } else {
689 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
690 for (int i = 0; i < mipLevelCount; i++) {
691 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
692 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
693 }
694
695 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
696 mipLevelCount, SkBudgeted::kYes,
697 dti->fGammaTreatment);
698 }
bsalomon41b952c2016-03-11 06:46:33 -0800699}
700
701///////////////////////////////////////////////////////////////////////////////////////////////////
702
cblume186d2d42016-06-03 11:17:42 -0700703sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
704 const GrMipLevel* texels, int mipLevelCount,
cblume33e0cb52016-08-30 12:09:23 -0700705 SkBudgeted budgeted,
706 SkSourceGammaTreatment gammaTreatment) {
cblume186d2d42016-06-03 11:17:42 -0700707 if (!ctx) {
708 return nullptr;
709 }
710 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, mipLevelCount));
711 if (!texture) {
712 return nullptr;
713 }
cblume33e0cb52016-08-30 12:09:23 -0700714 texture->texturePriv().setGammaTreatment(gammaTreatment);
cblume186d2d42016-06-03 11:17:42 -0700715 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNewImageUniqueID,
brianosmandddbe382016-07-20 13:55:39 -0700716 info.alphaType(), texture, sk_ref_sp(info.colorSpace()),
717 budgeted);
cblume186d2d42016-06-03 11:17:42 -0700718}