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 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 8 | #include "SkImage_Gpu.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" |
| 11 | #include "effects/GrYUVtoRGBEffect.h" |
| 12 | #include "SkCanvas.h" |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 13 | #include "SkGpuDevice.h" |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 14 | |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 15 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 16 | SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 17 | int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted) |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 18 | : INHERITED(w, h, uniqueID, NULL) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 19 | , fTexture(SkRef(tex)) |
reed | 3f10b9d | 2014-11-21 10:27:53 -0800 | [diff] [blame] | 20 | , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 21 | , fAlphaType(at) |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 22 | , fBudgeted(budgeted) |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 23 | {} |
piotaixr | cef04f8 | 2014-07-14 07:48:04 -0700 | [diff] [blame] | 24 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 25 | SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 26 | GrTexture* tex = this->getTexture(); |
| 27 | SkASSERT(tex); |
| 28 | GrContext* ctx = tex->getContext(); |
| 29 | if (!ctx) { |
| 30 | // the texture may have been abandoned, so we have to check |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 31 | return NULL; |
| 32 | } |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 33 | // TODO: Change signature of onNewSurface to take a budgeted param. |
| 34 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
| 35 | return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props); |
robertphillips@google.com | 97b6b07 | 2012-10-31 14:48:39 +0000 | [diff] [blame] | 36 | } |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 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 | |
| 44 | SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY, |
| 45 | const SkMatrix* localMatrix) const { |
| 46 | SkBitmap bm; |
| 47 | GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaque(), &bm); |
| 48 | return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); |
| 49 | } |
| 50 | |
| 51 | bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
| 52 | SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 53 | if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(), at))) { |
| 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 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool SkImage_Gpu::isOpaque() const { |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 64 | return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 68 | switch (info.colorType()) { |
| 69 | case kRGBA_8888_SkColorType: |
| 70 | case kBGRA_8888_SkColorType: |
| 71 | break; |
| 72 | default: |
| 73 | return; // nothing to do |
| 74 | } |
| 75 | |
| 76 | // SkColor is not necesarily RGBA or BGRA, but it is one of them on little-endian, |
| 77 | // and in either case, the alpha-byte is always in the same place, so we can safely call |
| 78 | // SkPreMultiplyColor() |
| 79 | // |
| 80 | SkColor* row = (SkColor*)pixels; |
| 81 | for (int y = 0; y < info.height(); ++y) { |
| 82 | for (int x = 0; x < info.width(); ++x) { |
| 83 | row[x] = SkPreMultiplyColor(row[x]); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 89 | int srcX, int srcY) const { |
| 90 | GrPixelConfig config = SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), |
| 91 | info.profileType()); |
| 92 | uint32_t flags = 0; |
| 93 | if (kUnpremul_SkAlphaType == info.alphaType() && kPremul_SkAlphaType == fAlphaType) { |
| 94 | // let the GPU perform this transformation for us |
| 95 | flags = GrContext::kUnpremul_PixelOpsFlag; |
| 96 | } |
| 97 | if (!fTexture->readPixels(srcX, srcY, info.width(), info.height(), config, |
| 98 | pixels, rowBytes, flags)) { |
| 99 | return false; |
| 100 | } |
| 101 | // do we have to manually fix-up the alpha channel? |
| 102 | // src dst |
| 103 | // unpremul premul fix manually |
| 104 | // premul unpremul done by kUnpremul_PixelOpsFlag |
| 105 | // all other combos need to change. |
| 106 | // |
| 107 | // Should this be handled by Ganesh? todo:? |
| 108 | // |
| 109 | if (kPremul_SkAlphaType == info.alphaType() && kUnpremul_SkAlphaType == fAlphaType) { |
| 110 | apply_premul(info, pixels, rowBytes); |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 116 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 117 | static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 118 | SkAlphaType at, GrWrapOwnership ownership, |
| 119 | SkImage::TextureReleaseProc releaseProc, |
| 120 | SkImage::ReleaseContext releaseCtx) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 121 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
| 122 | return NULL; |
| 123 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 124 | SkAutoTUnref<GrTexture> tex(ctx->textureProvider()->wrapBackendTexture(desc, ownership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 125 | if (!tex) { |
| 126 | return NULL; |
| 127 | } |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 128 | if (releaseProc) { |
| 129 | tex->setRelease(releaseProc, releaseCtx); |
| 130 | } |
| 131 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 132 | const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 133 | return SkNEW_ARGS(SkImage_Gpu, |
| 134 | (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted)); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 135 | |
| 136 | } |
| 137 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 138 | SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, |
| 139 | TextureReleaseProc releaseP, ReleaseContext releaseC) { |
| 140 | return new_wrapped_texture_common(ctx, desc, at, kBorrow_GrWrapOwnership, releaseP, releaseC); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | SkImage* SkImage::NewFromAdoptedTexture(GrContext* ctx, const GrBackendTextureDesc& desc, |
| 144 | SkAlphaType at) { |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 145 | return new_wrapped_texture_common(ctx, desc, at, kAdopt_GrWrapOwnership, NULL, NULL); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 146 | } |
| 147 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 148 | SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& desc, |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 149 | SkAlphaType at) { |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 150 | if (desc.fWidth <= 0 || desc.fHeight <= 0) { |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 151 | return NULL; |
| 152 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 153 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 154 | SkAutoTUnref<GrTexture> src(ctx->textureProvider()->wrapBackendTexture( |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 155 | desc, kBorrow_GrWrapOwnership)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 156 | if (!src) { |
| 157 | return NULL; |
| 158 | } |
| 159 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 160 | const bool isBudgeted = true; |
| 161 | SkAutoTUnref<GrTexture> dst(GrDeepCopyTexture(src, isBudgeted)); |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 162 | if (!dst) { |
| 163 | return NULL; |
| 164 | } |
| 165 | |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 166 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 167 | const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()? |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 168 | return SkNEW_ARGS(SkImage_Gpu, (desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
| 169 | at, dst, sampleCount, budgeted)); |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 170 | } |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 171 | |
| 172 | SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorSpace, |
| 173 | const GrBackendObject yuvTextureHandles[3], |
| 174 | const SkISize yuvSizes[3], |
| 175 | GrSurfaceOrigin origin) { |
| 176 | const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; |
| 177 | |
| 178 | if (yuvSizes[0].fWidth <= 0 || yuvSizes[0].fHeight <= 0 || |
| 179 | yuvSizes[1].fWidth <= 0 || yuvSizes[1].fHeight <= 0 || |
| 180 | yuvSizes[2].fWidth <= 0 || yuvSizes[2].fHeight <= 0) { |
| 181 | return NULL; |
| 182 | } |
| 183 | static const GrPixelConfig kConfig = kAlpha_8_GrPixelConfig; |
| 184 | GrBackendTextureDesc yDesc; |
| 185 | yDesc.fConfig = kConfig; |
| 186 | yDesc.fOrigin = origin; |
| 187 | yDesc.fSampleCnt = 0; |
| 188 | yDesc.fTextureHandle = yuvTextureHandles[0]; |
| 189 | yDesc.fWidth = yuvSizes[0].fWidth; |
| 190 | yDesc.fHeight = yuvSizes[0].fHeight; |
| 191 | |
| 192 | GrBackendTextureDesc uDesc; |
| 193 | uDesc.fConfig = kConfig; |
| 194 | uDesc.fOrigin = origin; |
| 195 | uDesc.fSampleCnt = 0; |
| 196 | uDesc.fTextureHandle = yuvTextureHandles[1]; |
| 197 | uDesc.fWidth = yuvSizes[1].fWidth; |
| 198 | uDesc.fHeight = yuvSizes[1].fHeight; |
| 199 | |
| 200 | GrBackendTextureDesc vDesc; |
| 201 | vDesc.fConfig = kConfig; |
| 202 | vDesc.fOrigin = origin; |
| 203 | vDesc.fSampleCnt = 0; |
| 204 | vDesc.fTextureHandle = yuvTextureHandles[2]; |
| 205 | vDesc.fWidth = yuvSizes[2].fWidth; |
| 206 | vDesc.fHeight = yuvSizes[2].fHeight; |
| 207 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 208 | SkAutoTUnref<GrTexture> yTex(ctx->textureProvider()->wrapBackendTexture( |
| 209 | yDesc, kBorrow_GrWrapOwnership)); |
| 210 | SkAutoTUnref<GrTexture> uTex(ctx->textureProvider()->wrapBackendTexture( |
| 211 | uDesc, kBorrow_GrWrapOwnership)); |
| 212 | SkAutoTUnref<GrTexture> vTex(ctx->textureProvider()->wrapBackendTexture( |
| 213 | vDesc, kBorrow_GrWrapOwnership)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 214 | if (!yTex || !uTex || !vTex) { |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | GrSurfaceDesc dstDesc; |
| 219 | // Needs to be a render target in order to draw to it for the yuv->rgb conversion. |
| 220 | dstDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 221 | dstDesc.fOrigin = origin; |
| 222 | dstDesc.fWidth = yuvSizes[0].fWidth; |
| 223 | dstDesc.fHeight = yuvSizes[0].fHeight; |
| 224 | dstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 225 | dstDesc.fSampleCnt = 0; |
| 226 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 227 | SkAutoTUnref<GrTexture> dst(ctx->textureProvider()->createTexture(dstDesc, true)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 228 | if (!dst) { |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | GrPaint paint; |
| 233 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
joshualitt | 2cdec31 | 2015-07-09 07:31:31 -0700 | [diff] [blame] | 234 | paint.addColorProcessor(GrYUVtoRGBEffect::Create(paint.getProcessorDataManager(), yTex, uTex, |
| 235 | vTex, yuvSizes, colorSpace))->unref(); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 236 | |
| 237 | const SkRect rect = SkRect::MakeWH(SkIntToScalar(dstDesc.fWidth), |
| 238 | SkIntToScalar(dstDesc.fHeight)); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 239 | GrDrawContext* drawContext = ctx->drawContext(); |
| 240 | drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 241 | ctx->flushSurfaceWrites(dst); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 242 | return SkNEW_ARGS(SkImage_Gpu, (dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, |
| 243 | kOpaque_SkAlphaType, dst, 0, budgeted)); |
bsalomon | 993a421 | 2015-05-29 11:37:25 -0700 | [diff] [blame] | 244 | } |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 245 | |
| 246 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 247 | |
| 248 | GrTexture* GrDeepCopyTexture(GrTexture* src, bool budgeted) { |
| 249 | GrContext* ctx = src->getContext(); |
| 250 | |
| 251 | GrSurfaceDesc desc = src->desc(); |
reed | 5617900 | 2015-07-07 06:11:19 -0700 | [diff] [blame] | 252 | GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, NULL, 0); |
| 253 | if (!dst) { |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 258 | const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 259 | ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 260 | return dst; |
| 261 | } |
| 262 | |