| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapCache.h" | 8 #include "SkBitmapCache.h" |
| 9 #include "SkImage_Gpu.h" | 9 #include "SkImage_Gpu.h" |
| 10 #include "GrCaps.h" |
| 10 #include "GrContext.h" | 11 #include "GrContext.h" |
| 11 #include "GrDrawContext.h" | 12 #include "GrDrawContext.h" |
| 12 #include "effects/GrYUVtoRGBEffect.h" | 13 #include "effects/GrYUVtoRGBEffect.h" |
| 13 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
| 14 #include "SkGpuDevice.h" | 15 #include "SkGpuDevice.h" |
| 15 #include "SkPixelRef.h" | 16 #include "SkPixelRef.h" |
| 16 | 17 |
| 17 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText
ure* tex, | 18 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText
ure* tex, |
| 18 SkSurface::Budgeted budgeted) | 19 SkSurface::Budgeted budgeted) |
| 19 : INHERITED(w, h, uniqueID, nullptr) | 20 : INHERITED(w, h, uniqueID, nullptr) |
| 20 , fTexture(SkRef(tex)) | 21 , fTexture(SkRef(tex)) |
| 21 , fAlphaType(at) | 22 , fAlphaType(at) |
| 22 , fBudgeted(budgeted) | 23 , fBudgeted(budgeted) |
| 23 , fAddedRasterVersionToCache(false) | 24 , fAddedRasterVersionToCache(false) |
| 24 {} | 25 {} |
| 25 | 26 |
| 26 SkImage_Gpu::~SkImage_Gpu() { | 27 SkImage_Gpu::~SkImage_Gpu() { |
| 27 if (fAddedRasterVersionToCache.load()) { | 28 if (fAddedRasterVersionToCache.load()) { |
| 28 SkNotifyBitmapGenIDIsStale(this->uniqueID()); | 29 SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 29 } | 30 } |
| 30 } | 31 } |
| 31 | 32 |
| 32 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { | 33 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
| 33 if (as_IB(image)->getTexture()) { | 34 if (as_IB(image)->getTexture()) { |
| 34 ((SkImage_Gpu*)image)->applyBudgetDecision(); | 35 ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode
tileY, | |
| 39 const SkMatrix* localMatrix) const { | |
| 40 SkBitmap bm; | |
| 41 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaqu
e(), &bm); | |
| 42 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); | |
| 43 } | |
| 44 | |
| 45 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { | 39 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
| 46 if (SkBitmapCache::Find(this->uniqueID(), dst)) { | 40 if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 47 SkASSERT(dst->getGenerationID() == this->uniqueID()); | 41 SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 48 SkASSERT(dst->isImmutable()); | 42 SkASSERT(dst->isImmutable()); |
| 49 SkASSERT(dst->getPixels()); | 43 SkASSERT(dst->getPixels()); |
| 50 return true; | 44 return true; |
| 51 } | 45 } |
| 52 | 46 |
| 53 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; | 47 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; |
| 54 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { | 48 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { |
| 55 return false; | 49 return false; |
| 56 } | 50 } |
| 57 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, | 51 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, |
| 58 dst->getPixels(), dst->rowBytes())) { | 52 dst->getPixels(), dst->rowBytes())) { |
| 59 return false; | 53 return false; |
| 60 } | 54 } |
| 61 | 55 |
| 62 dst->pixelRef()->setImmutableWithID(this->uniqueID()); | 56 dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 63 SkBitmapCache::Add(this->uniqueID(), *dst); | 57 SkBitmapCache::Add(this->uniqueID(), *dst); |
| 64 fAddedRasterVersionToCache.store(true); | 58 fAddedRasterVersionToCache.store(true); |
| 65 return true; | 59 return true; |
| 66 } | 60 } |
| 67 | 61 |
| 68 GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) con
st { | 62 GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) con
st { |
| 63 const bool is_pow2 = SkIsPow2(this->width()) && SkIsPow2(this->height()); |
| 64 const bool npot_tex_supported = ctx->caps()->npotTextureTileSupport(); |
| 65 if (!is_pow2 && kUntiled_SkImageUsageType != usage && !npot_tex_supported) { |
| 66 // load as bitmap, since the GPU can support tiling a non-pow2 texture |
| 67 // related to skbug.com/4365 |
| 68 SkBitmap bitmap; |
| 69 if (this->getROPixels(&bitmap)) { |
| 70 return GrRefCachedBitmapTexture(ctx, bitmap, usage); |
| 71 } else { |
| 72 return nullptr; |
| 73 } |
| 74 } |
| 75 |
| 69 fTexture->ref(); | 76 fTexture->ref(); |
| 70 return fTexture; | 77 return fTexture; |
| 71 } | 78 } |
| 72 | 79 |
| 73 bool SkImage_Gpu::isOpaque() const { | 80 bool SkImage_Gpu::isOpaque() const { |
| 74 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; | 81 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; |
| 75 } | 82 } |
| 76 | 83 |
| 77 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ | 84 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ |
| 78 switch (info.colorType()) { | 85 switch (info.colorType()) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 if (!dst) { | 288 if (!dst) { |
| 282 return nullptr; | 289 return nullptr; |
| 283 } | 290 } |
| 284 | 291 |
| 285 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 292 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 286 const SkIPoint dstP = SkIPoint::Make(0, 0); | 293 const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 287 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); | 294 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 288 return dst; | 295 return dst; |
| 289 } | 296 } |
| 290 | 297 |
| OLD | NEW |