reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 8 | #include "GrCaps.h" |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 11 | #include "GrTextureParamsAdjuster.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 12 | #include "effects/GrYUVtoRGBEffect.h" |
| 13 | #include "SkCanvas.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame^] | 14 | #include "SkBitmapCache.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 15 | #include "SkGpuDevice.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame^] | 16 | #include "SkGrPixelRef.h" |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 17 | #include "SkGrPriv.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame^] | 18 | #include "SkImageFilter.h" |
| 19 | #include "SkImage_Gpu.h" |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 20 | #include "SkPixelRef.h" |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 21 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 22 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 23 | SkSurface::Budgeted budgeted) |
reed | af3fbfc | 2015-10-04 11:28:36 -0700 | [diff] [blame] | 24 | : INHERITED(w, h, uniqueID) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 25 | , fTexture(SkRef(tex)) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 26 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 27 | , fBudgeted(budgeted) |
bsalomon | 1cd6311 | 2015-08-05 06:58:39 -0700 | [diff] [blame] | 28 | , fAddedRasterVersionToCache(false) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 29 | { |
| 30 | SkASSERT(tex->width() == w); |
| 31 | SkASSERT(tex->height() == h); |
| 32 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 33 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 34 | SkImage_Gpu::~SkImage_Gpu() { |
| 35 | if (fAddedRasterVersionToCache.load()) { |
| 36 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 37 | } |
| 38 | } |
| 39 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 40 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 41 | if (as_IB(image)->getTexture()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 42 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 43 | } |
| 44 | } |
| 45 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 46 | static SkImageInfo make_info(int w, int h, bool isOpaque) { |
| 47 | return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 48 | } |
| 49 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 50 | bool SkImage_Gpu::getROPixels(SkBitmap* dst, CachingHint chint) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 51 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 52 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 53 | SkASSERT(dst->isImmutable()); |
| 54 | SkASSERT(dst->getPixels()); |
| 55 | return true; |
| 56 | } |
| 57 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 58 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 62 | dst->getPixels(), dst->rowBytes())) { |
| 63 | return false; |
| 64 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 65 | |
| 66 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 67 | if (kAllow_CachingHint == chint) { |
| 68 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 69 | fAddedRasterVersionToCache.store(true); |
| 70 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame^] | 74 | bool SkImage_Gpu::asBitmapForImageFilters(SkBitmap* bitmap) const { |
| 75 | bitmap->setInfo(make_info(this->width(), this->height(), this->isOpaque())); |
| 76 | bitmap->setPixelRef(new SkGrPixelRef(bitmap->info(), fTexture))->unref(); |
| 77 | bitmap->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 78 | return true; |
| 79 | } |
| 80 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 81 | class GpuImage_GrTextureAdjuster : public GrTextureAdjuster { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 82 | public: |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 83 | GpuImage_GrTextureAdjuster(const SkImage_Gpu* image) |
| 84 | : INHERITED(image->peekTexture()) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 85 | , fImage(image) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 86 | {} |
| 87 | |
| 88 | protected: |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 89 | void makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) override { |
| 90 | GrUniqueKey baseKey; |
| 91 | GrMakeKeyFromImageID(&baseKey, fImage->uniqueID(), |
| 92 | SkIRect::MakeWH(fImage->width(), fImage->height())); |
| 93 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 96 | void didCacheCopy(const GrUniqueKey& copyKey) override { as_IB(fImage)->notifyAddedToCache(); } |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 97 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 98 | private: |
| 99 | const SkImage* fImage; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 100 | |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 101 | typedef GrTextureAdjuster INHERITED; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 104 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
bsalomon | 89fe56b | 2015-10-29 10:49:28 -0700 | [diff] [blame] | 105 | return GpuImage_GrTextureAdjuster(this).refTextureSafeForParams(params, nullptr); |
reed | 85d9178 | 2015-09-10 14:33:38 -0700 | [diff] [blame] | 106 | } |
| 107 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 108 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 109 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 113 | switch (info.colorType()) { |
| 114 | case kRGBA_8888_SkColorType: |
| 115 | case kBGRA_8888_SkColorType: |
| 116 | break; |
| 117 | default: |
| 118 | return; // nothing to do |
| 119 | } |
| 120 | |
| 121 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 122 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 123 | // SkPreMultiplyColor() |
| 124 | // |
| 125 | SkColor* row = (SkColor*)pixels; |
| 126 | for (int y = 0; y < info.height(); ++y) { |
| 127 | for (int x = 0; x < info.width(); ++x) { |
| 128 | row[x] = SkPreMultiplyColor(row[x]); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 134 | int srcX, int srcY, CachingHint) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 135 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
| 136 | info.profileType()); |
| 137 | uint32_t flags = 0; |
| 138 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 139 | // let the GPU perform this transformation for us |
| 140 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 141 | } |
| 142 | if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, |
| 143 | pixels, rowBytes, flags)) { |
| 144 | return false; |
| 145 | } |
| 146 | // do we have to manually fix-up the alpha channel? |
| 147 | // src dst |
| 148 | // unpremul premul fix manually |
| 149 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 150 | // all other combos need to change. |
| 151 | // |
| 152 | // Should this be handled by Ganesh? todo:? |
| 153 | // |
| 154 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 155 | apply_premul(info, pixels, rowBytes); |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 160 | SkImage* SkImage_Gpu::onNewSubset(const SkIRect& subset) const { |
| 161 | GrContext* ctx = fTexture->getContext(); |
| 162 | GrSurfaceDesc desc = fTexture->desc(); |
| 163 | desc.fWidth = subset.width(); |
| 164 | desc.fHeight = subset.height(); |
| 165 | |
| 166 | GrTexture* subTx = ctx->textureProvider()->createTexture(desc, |
| 167 | SkSurface::kYes_Budgeted == fBudgeted); |
| 168 | if (!subTx) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0)); |
| 172 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx, |
| 173 | fBudgeted); |
| 174 | } |
| 175 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 176 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 177 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 178 | class SkGpuImageFilterProxy : public SkImageFilter::Proxy { |
| 179 | GrContext* fCtx; |
| 180 | |
| 181 | public: |
| 182 | SkGpuImageFilterProxy(GrContext* ctx) : fCtx(ctx) {} |
| 183 | |
| 184 | SkBaseDevice* createDevice(int width, int height) override { |
| 185 | GrSurfaceDesc desc; |
| 186 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 187 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 188 | desc.fWidth = width; |
| 189 | desc.fHeight = height; |
| 190 | desc.fSampleCnt = 0; |
| 191 | |
| 192 | SkAutoTUnref<GrTexture> texture(fCtx->textureProvider()->createTexture(desc, true)); |
| 193 | |
| 194 | if (texture) { |
| 195 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 196 | return SkGpuDevice::Create(texture->asRenderTarget(), width, height, &props, |
| 197 | SkGpuDevice::kClear_InitContents); |
| 198 | } else { |
| 199 | return nullptr; |
| 200 | } |
| 201 | } |
| 202 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 203 | bool filterImage(const SkImageFilter* filter, const SkBitmap& src, |
| 204 | const SkImageFilter::Context& ctx, SkBitmap* dst, SkIPoint* offset) override { |
| 205 | return filter->canFilterImageGPU() && |
| 206 | filter->filterImageGPU(this, src, ctx, dst, offset); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 207 | } |
| 208 | }; |
| 209 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 210 | static SkIRect compute_fast_ibounds(SkImageFilter* filter, const SkIRect& srcBounds) { |
| 211 | SkRect fastBounds; |
| 212 | fastBounds.set(srcBounds); |
| 213 | filter->computeFastBounds(fastBounds, &fastBounds); |
| 214 | return fastBounds.roundOut(); |
| 215 | } |
| 216 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 217 | SkImage* SkImage_Gpu::onApplyFilter(SkImageFilter* filter, SkIPoint* offsetResult, |
| 218 | bool forceResultToOriginalSize) const { |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 219 | const SkIRect srcBounds = SkIRect::MakeWH(this->width(), this->height()); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 220 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 221 | if (forceResultToOriginalSize) { |
| 222 | SkBitmap src; |
| 223 | GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &src); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 224 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 225 | const SkIRect clipBounds = srcBounds; |
| 226 | SkGpuImageFilterProxy proxy(fTexture->getContext()); |
reed | 13ccbf8 | 2015-10-20 09:56:52 -0700 | [diff] [blame] | 227 | SkAutoTUnref<SkImageFilter::Cache> cache(SkGpuDevice::NewImageFilterCache()); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 228 | SkImageFilter::Context ctx(SkMatrix::I(), clipBounds, cache, SkImageFilter::kExact_SizeConstraint); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 229 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 230 | SkBitmap dst; |
| 231 | if (!filter->filterImage(&proxy, src, ctx, &dst, offsetResult)) { |
| 232 | return nullptr; |
| 233 | } |
reed | d82bc7b | 2015-10-12 13:17:23 -0700 | [diff] [blame] | 234 | return new SkImage_Gpu(dst.width(), dst.height(), kNeedNewImageUniqueID, dst.alphaType(), |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 235 | dst.getTexture(), SkSurface::kNo_Budgeted); |
| 236 | } |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 237 | |
| 238 | const SkIRect dstR = compute_fast_ibounds(filter, srcBounds); |
| 239 | |
| 240 | SkImageInfo info = SkImageInfo::MakeN32Premul(dstR.width(), dstR.height()); |
| 241 | SkAutoTUnref<SkSurface> surface(this->onNewSurface(info)); |
| 242 | |
| 243 | SkPaint paint; |
| 244 | paint.setImageFilter(filter); |
| 245 | surface->getCanvas()->drawImage(this, SkIntToScalar(-dstR.x()), SkIntToScalar(-dstR.y()), |
| 246 | &paint); |
| 247 | |
| 248 | offsetResult->set(dstR.x(), dstR.y()); |
| 249 | return surface->newImageSnapshot(); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 253 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 254 | static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 255 | SkAlphaType at, GrWrapOwnership ownership, |
| 256 | SkImage::TextureReleaseProc releaseProc, |
| 257 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 258 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 260 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 261 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 262 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 263 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 264 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 265 | if (releaseProc) { |
| 266 | tex->setRelease(releaseProc, releaseCtx); |
| 267 | } |
| 268 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 269 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 270 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 271 | } |
| 272 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 273 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 274 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 275 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 279 | SkAlphaType at) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 280 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 281 | } |
| 282 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 283 | SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 284 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 285 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 286 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 287 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 288 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 289 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 290 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 291 | if (!src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 292 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 293 | } |
| 294 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 295 | const bool isBudgeted = true; |
| 296 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 297 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 299 | } |
| 300 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 301 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 302 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 303 | budgeted); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 304 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 305 | |
| 306 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 307 | const GrBackendObject yuvTextureHandles[3], |
| 308 | const SkISize yuvSizes[3], |
| 309 | GrSurfaceOrigin origin) { |
| 310 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
| 311 | |
| 312 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 313 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 314 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 315 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 316 | } |
| 317 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 318 | GrBackendTextureDesc yDesc; |
| 319 | yDesc.fConfig = kConfig; |
| 320 | yDesc.fOrigin = origin; |
| 321 | yDesc.fSampleCnt = 0; |
| 322 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 323 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 324 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 325 | |
| 326 | GrBackendTextureDesc uDesc; |
| 327 | uDesc.fConfig = kConfig; |
| 328 | uDesc.fOrigin = origin; |
| 329 | uDesc.fSampleCnt = 0; |
| 330 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 331 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 332 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 333 | |
| 334 | GrBackendTextureDesc vDesc; |
| 335 | vDesc.fConfig = kConfig; |
| 336 | vDesc.fOrigin = origin; |
| 337 | vDesc.fSampleCnt = 0; |
| 338 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 339 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 340 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 341 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 342 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 343 | yDesc, kBorrow_GrWrapOwnership)); |
| 344 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 345 | uDesc, kBorrow_GrWrapOwnership)); |
| 346 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 347 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 348 | if (!yTex || !uTex || !vTex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 349 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | GrSurfaceDesc dstDesc; |
| 353 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 354 | dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 355 | dstDesc.fOrigin = origin; |
| 356 | dstDesc.fWidth = yuvSizes[0].fWidth; |
| 357 | dstDesc.fHeight = yuvSizes[0].fHeight; |
| 358 | dstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 359 | dstDesc.fSampleCnt = 0; |
| 360 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 361 | SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 362 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 363 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | GrPaint paint; |
| 367 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 368 | paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(yTex, uTex, vTex, yuvSizes, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 369 | colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 370 | |
| 371 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth), |
| 372 | SkIntToScalar(dstDesc.fHeight)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 373 | SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(dst->asRenderTarget())); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 374 | if (!drawContext) { |
| 375 | return nullptr; |
| 376 | } |
| 377 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 378 | drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 379 | ctx->flushSurfaceWrites(dst); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 380 | return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 381 | kOpaque_SkAlphaType, dst, budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 382 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 383 | |
| 384 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 385 | |
| 386 | GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) { |
| 387 | GrContext* ctx = src->getContext(); |
| 388 | |
| 389 | GrSurfaceDesc desc = src->desc(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 390 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 391 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 392 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 396 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 397 | ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 398 | return dst; |
| 399 | } |
| 400 | |