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" |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 13 | #include "GrTextureParamsAdjuster.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 | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame^] | 27 | { |
| 28 | SkASSERT(tex->width() == w); |
| 29 | SkASSERT(tex->height() == h); |
| 30 | } |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 31 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 32 | SkImage_Gpu::~SkImage_Gpu() { |
| 33 | if (fAddedRasterVersionToCache.load()) { |
| 34 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 35 | } |
| 36 | } |
| 37 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 38 | extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
bsalomon | 5581236 | 2015-06-10 08:49:28 -0700 | [diff] [blame] | 39 | if (as_IB(image)->getTexture()) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 40 | ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 41 | } |
| 42 | } |
| 43 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 44 | static SkImageInfo make_info(int w, int h, bool isOpaque) { |
| 45 | return SkImageInfo::MakeN32(w, h, isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
| 46 | } |
| 47 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 48 | bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 49 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 50 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 51 | SkASSERT(dst->isImmutable()); |
| 52 | SkASSERT(dst->getPixels()); |
| 53 | return true; |
| 54 | } |
| 55 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 56 | if (!dst->tryAllocPixels(make_info(this->width(), this->height(), this->isOpaque()))) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 60 | dst->getPixels(), dst->rowBytes())) { |
| 61 | return false; |
| 62 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 63 | |
| 64 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 65 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 66 | fAddedRasterVersionToCache.store(true); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 70 | static void make_raw_texture_stretched_key(uint32_t imageID, |
| 71 | const GrTextureParamsAdjuster::CopyParams& params, |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 72 | GrUniqueKey* stretchedKey) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 73 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 74 | GrUniqueKey::Builder builder(stretchedKey, kDomain, 4); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 75 | builder[0] = imageID; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 76 | builder[1] = params.fFilter; |
| 77 | builder[2] = params.fWidth; |
| 78 | builder[3] = params.fHeight; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 79 | } |
| 80 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 81 | class Texture_GrTextureParamsAdjuster : public GrTextureParamsAdjuster { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 82 | public: |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 83 | Texture_GrTextureParamsAdjuster(const SkImage* image, GrTexture* unstretched) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 84 | : INHERITED(image->width(), image->height()) |
| 85 | , fImage(image) |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 86 | , fOriginal(unstretched) |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 87 | {} |
| 88 | |
| 89 | protected: |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 90 | GrTexture* peekOriginalTexture() override { return fOriginal; } |
| 91 | |
| 92 | GrTexture* refOriginalTexture(GrContext* ctx) override { |
| 93 | return SkRef(fOriginal); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 96 | void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) override { |
| 97 | make_raw_texture_stretched_key(fImage->uniqueID(), copyParams, copyKey); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 98 | } |
| 99 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 100 | void didCacheCopy(const GrUniqueKey& copyKey) override { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 101 | as_IB(fImage)->notifyAddedToCache(); |
| 102 | } |
| 103 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 104 | bool getROBitmap(SkBitmap* bitmap) override { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 105 | return as_IB(fImage)->getROPixels(bitmap); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | const SkImage* fImage; |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 110 | GrTexture* fOriginal; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 111 | |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 112 | typedef GrTextureParamsAdjuster INHERITED; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 115 | GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, const GrTextureParams& params) const { |
bsalomon | 045802d | 2015-10-20 07:58:01 -0700 | [diff] [blame] | 116 | return Texture_GrTextureParamsAdjuster(this, fTexture).refTextureForParams(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 "SkGrPixelRef.h" |
| 190 | #include "SkImageFilter.h" |
| 191 | |
| 192 | class SkGpuImageFilterProxy : public SkImageFilter::Proxy { |
| 193 | GrContext* fCtx; |
| 194 | |
| 195 | public: |
| 196 | SkGpuImageFilterProxy(GrContext* ctx) : fCtx(ctx) {} |
| 197 | |
| 198 | SkBaseDevice* createDevice(int width, int height) override { |
| 199 | GrSurfaceDesc desc; |
| 200 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 201 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 202 | desc.fWidth = width; |
| 203 | desc.fHeight = height; |
| 204 | desc.fSampleCnt = 0; |
| 205 | |
| 206 | SkAutoTUnref<GrTexture> texture(fCtx->textureProvider()->createTexture(desc, true)); |
| 207 | |
| 208 | if (texture) { |
| 209 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 210 | return SkGpuDevice::Create(texture->asRenderTarget(), width, height, &props, |
| 211 | SkGpuDevice::kClear_InitContents); |
| 212 | } else { |
| 213 | return nullptr; |
| 214 | } |
| 215 | } |
| 216 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 217 | bool filterImage(const SkImageFilter* filter, const SkBitmap& src, |
| 218 | const SkImageFilter::Context& ctx, SkBitmap* dst, SkIPoint* offset) override { |
| 219 | return filter->canFilterImageGPU() && |
| 220 | filter->filterImageGPU(this, src, ctx, dst, offset); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 221 | } |
| 222 | }; |
| 223 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 224 | static SkIRect compute_fast_ibounds(SkImageFilter* filter, const SkIRect& srcBounds) { |
| 225 | SkRect fastBounds; |
| 226 | fastBounds.set(srcBounds); |
| 227 | filter->computeFastBounds(fastBounds, &fastBounds); |
| 228 | return fastBounds.roundOut(); |
| 229 | } |
| 230 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 231 | SkImage* SkImage_Gpu::onApplyFilter(SkImageFilter* filter, SkIPoint* offsetResult, |
| 232 | bool forceResultToOriginalSize) const { |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 233 | const SkIRect srcBounds = SkIRect::MakeWH(this->width(), this->height()); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 234 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 235 | if (forceResultToOriginalSize) { |
| 236 | SkBitmap src; |
| 237 | GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &src); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 238 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 239 | const SkIRect clipBounds = srcBounds; |
| 240 | SkGpuImageFilterProxy proxy(fTexture->getContext()); |
reed | 13ccbf8 | 2015-10-20 09:56:52 -0700 | [diff] [blame] | 241 | SkAutoTUnref<SkImageFilter::Cache> cache(SkGpuDevice::NewImageFilterCache()); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame^] | 242 | SkImageFilter::Context ctx(SkMatrix::I(), clipBounds, cache, SkImageFilter::kExact_SizeConstraint); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 243 | |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 244 | SkBitmap dst; |
| 245 | if (!filter->filterImage(&proxy, src, ctx, &dst, offsetResult)) { |
| 246 | return nullptr; |
| 247 | } |
reed | d82bc7b | 2015-10-12 13:17:23 -0700 | [diff] [blame] | 248 | return new SkImage_Gpu(dst.width(), dst.height(), kNeedNewImageUniqueID, dst.alphaType(), |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 249 | dst.getTexture(), SkSurface::kNo_Budgeted); |
| 250 | } |
reed | 59dc0d2 | 2015-10-19 08:24:21 -0700 | [diff] [blame] | 251 | |
| 252 | const SkIRect dstR = compute_fast_ibounds(filter, srcBounds); |
| 253 | |
| 254 | SkImageInfo info = SkImageInfo::MakeN32Premul(dstR.width(), dstR.height()); |
| 255 | SkAutoTUnref<SkSurface> surface(this->onNewSurface(info)); |
| 256 | |
| 257 | SkPaint paint; |
| 258 | paint.setImageFilter(filter); |
| 259 | surface->getCanvas()->drawImage(this, SkIntToScalar(-dstR.x()), SkIntToScalar(-dstR.y()), |
| 260 | &paint); |
| 261 | |
| 262 | offsetResult->set(dstR.x(), dstR.y()); |
| 263 | return surface->newImageSnapshot(); |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 267 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 268 | static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 269 | SkAlphaType at, GrWrapOwnership ownership, |
| 270 | SkImage::TextureReleaseProc releaseProc, |
| 271 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 272 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 274 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 275 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 276 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 277 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 278 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 279 | if (releaseProc) { |
| 280 | tex->setRelease(releaseProc, releaseCtx); |
| 281 | } |
| 282 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 283 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 284 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 285 | } |
| 286 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 287 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 288 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 289 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 293 | SkAlphaType at) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 294 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, nullptr, nullptr); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 295 | } |
| 296 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 297 | SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 298 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 299 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 300 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 301 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 302 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 303 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 304 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 305 | if (!src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 306 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 307 | } |
| 308 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 309 | const bool isBudgeted = true; |
| 310 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 311 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 312 | return nullptr; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 313 | } |
| 314 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 315 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 316 | return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 317 | budgeted); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 318 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 319 | |
| 320 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 321 | const GrBackendObject yuvTextureHandles[3], |
| 322 | const SkISize yuvSizes[3], |
| 323 | GrSurfaceOrigin origin) { |
| 324 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
| 325 | |
| 326 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 327 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 328 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 329 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 330 | } |
| 331 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 332 | GrBackendTextureDesc yDesc; |
| 333 | yDesc.fConfig = kConfig; |
| 334 | yDesc.fOrigin = origin; |
| 335 | yDesc.fSampleCnt = 0; |
| 336 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 337 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 338 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 339 | |
| 340 | GrBackendTextureDesc uDesc; |
| 341 | uDesc.fConfig = kConfig; |
| 342 | uDesc.fOrigin = origin; |
| 343 | uDesc.fSampleCnt = 0; |
| 344 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 345 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 346 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 347 | |
| 348 | GrBackendTextureDesc vDesc; |
| 349 | vDesc.fConfig = kConfig; |
| 350 | vDesc.fOrigin = origin; |
| 351 | vDesc.fSampleCnt = 0; |
| 352 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 353 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 354 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 355 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 356 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 357 | yDesc, kBorrow_GrWrapOwnership)); |
| 358 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 359 | uDesc, kBorrow_GrWrapOwnership)); |
| 360 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 361 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 362 | if (!yTex || !uTex || !vTex) { |
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 | GrSurfaceDesc dstDesc; |
| 367 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 368 | dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 369 | dstDesc.fOrigin = origin; |
| 370 | dstDesc.fWidth = yuvSizes[0].fWidth; |
| 371 | dstDesc.fHeight = yuvSizes[0].fHeight; |
| 372 | dstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 373 | dstDesc.fSampleCnt = 0; |
| 374 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 375 | SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 376 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 377 | return nullptr; |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | GrPaint paint; |
| 381 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 382 | paint.addColorFragmentProcessor(GrYUVtoRGBEffect::Create(yTex, uTex, vTex, yuvSizes, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 383 | colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 384 | |
| 385 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth), |
| 386 | SkIntToScalar(dstDesc.fHeight)); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 387 | SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(dst->asRenderTarget())); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 388 | if (!drawContext) { |
| 389 | return nullptr; |
| 390 | } |
| 391 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 392 | drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 393 | ctx->flushSurfaceWrites(dst); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 394 | return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, |
reed | 7b6945b | 2015-09-24 00:50:58 -0700 | [diff] [blame] | 395 | kOpaque_SkAlphaType, dst, budgeted); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 396 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 397 | |
| 398 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 399 | |
| 400 | GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) { |
| 401 | GrContext* ctx = src->getContext(); |
| 402 | |
| 403 | GrSurfaceDesc desc = src->desc(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 404 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullptr, 0); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 405 | if (!dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 406 | return nullptr; |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 410 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 411 | ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 412 | return dst; |
| 413 | } |
| 414 | |