| 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" | |
| 11 #include "GrContext.h" | 10 #include "GrContext.h" |
| 12 #include "GrDrawContext.h" | 11 #include "GrDrawContext.h" |
| 13 #include "effects/GrYUVtoRGBEffect.h" | 12 #include "effects/GrYUVtoRGBEffect.h" |
| 14 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
| 15 #include "SkGpuDevice.h" | 14 #include "SkGpuDevice.h" |
| 16 #include "SkPixelRef.h" | 15 #include "SkPixelRef.h" |
| 17 | 16 |
| 18 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText
ure* tex, | 17 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText
ure* tex, |
| 19 SkSurface::Budgeted budgeted) | 18 SkSurface::Budgeted budgeted) |
| 20 : INHERITED(w, h, uniqueID, nullptr) | 19 : INHERITED(w, h, uniqueID, nullptr) |
| 21 , fTexture(SkRef(tex)) | 20 , fTexture(SkRef(tex)) |
| 22 , fAlphaType(at) | 21 , fAlphaType(at) |
| 23 , fBudgeted(budgeted) | 22 , fBudgeted(budgeted) |
| 24 , fAddedRasterVersionToCache(false) | 23 , fAddedRasterVersionToCache(false) |
| 25 {} | 24 {} |
| 26 | 25 |
| 27 SkImage_Gpu::~SkImage_Gpu() { | 26 SkImage_Gpu::~SkImage_Gpu() { |
| 28 if (fAddedRasterVersionToCache.load()) { | 27 if (fAddedRasterVersionToCache.load()) { |
| 29 SkNotifyBitmapGenIDIsStale(this->uniqueID()); | 28 SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 30 } | 29 } |
| 31 } | 30 } |
| 32 | 31 |
| 33 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { | 32 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
| 34 if (as_IB(image)->getTexture()) { | 33 if (as_IB(image)->getTexture()) { |
| 35 ((SkImage_Gpu*)image)->applyBudgetDecision(); | 34 ((SkImage_Gpu*)image)->applyBudgetDecision(); |
| 36 } | 35 } |
| 37 } | 36 } |
| 38 | 37 |
| 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 |
| 39 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { | 45 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
| 40 if (SkBitmapCache::Find(this->uniqueID(), dst)) { | 46 if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 41 SkASSERT(dst->getGenerationID() == this->uniqueID()); | 47 SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 42 SkASSERT(dst->isImmutable()); | 48 SkASSERT(dst->isImmutable()); |
| 43 SkASSERT(dst->getPixels()); | 49 SkASSERT(dst->getPixels()); |
| 44 return true; | 50 return true; |
| 45 } | 51 } |
| 46 | 52 |
| 47 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; | 53 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; |
| 48 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { | 54 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { |
| 49 return false; | 55 return false; |
| 50 } | 56 } |
| 51 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, | 57 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, |
| 52 dst->getPixels(), dst->rowBytes())) { | 58 dst->getPixels(), dst->rowBytes())) { |
| 53 return false; | 59 return false; |
| 54 } | 60 } |
| 55 | 61 |
| 56 dst->pixelRef()->setImmutableWithID(this->uniqueID()); | 62 dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 57 SkBitmapCache::Add(this->uniqueID(), *dst); | 63 SkBitmapCache::Add(this->uniqueID(), *dst); |
| 58 fAddedRasterVersionToCache.store(true); | 64 fAddedRasterVersionToCache.store(true); |
| 59 return true; | 65 return true; |
| 60 } | 66 } |
| 61 | 67 |
| 62 GrTexture* SkImage_Gpu::asTextureRef(GrContext* ctx, SkImageUsageType usage) con
st { | 68 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 | |
| 76 fTexture->ref(); | 69 fTexture->ref(); |
| 77 return fTexture; | 70 return fTexture; |
| 78 } | 71 } |
| 79 | 72 |
| 80 bool SkImage_Gpu::isOpaque() const { | 73 bool SkImage_Gpu::isOpaque() const { |
| 81 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; | 74 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; |
| 82 } | 75 } |
| 83 | 76 |
| 84 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ | 77 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ |
| 85 switch (info.colorType()) { | 78 switch (info.colorType()) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 if (!dst) { | 281 if (!dst) { |
| 289 return nullptr; | 282 return nullptr; |
| 290 } | 283 } |
| 291 | 284 |
| 292 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 285 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
| 293 const SkIPoint dstP = SkIPoint::Make(0, 0); | 286 const SkIPoint dstP = SkIPoint::Make(0, 0); |
| 294 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); | 287 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
| 295 return dst; | 288 return dst; |
| 296 } | 289 } |
| 297 | 290 |
| OLD | NEW |