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