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 "SkImage_Gpu.h" | 9 #include "SkImage_Gpu.h" |
9 #include "GrContext.h" | 10 #include "GrContext.h" |
10 #include "GrDrawContext.h" | 11 #include "GrDrawContext.h" |
11 #include "effects/GrYUVtoRGBEffect.h" | 12 #include "effects/GrYUVtoRGBEffect.h" |
12 #include "SkCanvas.h" | 13 #include "SkCanvas.h" |
13 #include "SkGpuDevice.h" | 14 #include "SkGpuDevice.h" |
14 | 15 #include "SkPixelRef.h" |
15 | 16 |
16 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, |
17 int sampleCountForNewSurfaces, SkSurface::Budgeted budg
eted) | 18 int sampleCountForNewSurfaces, SkSurface::Budgeted budg
eted) |
18 : INHERITED(w, h, uniqueID, NULL) | 19 : INHERITED(w, h, uniqueID, NULL) |
19 , fTexture(SkRef(tex)) | 20 , fTexture(SkRef(tex)) |
20 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) | 21 , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) |
21 , fAlphaType(at) | 22 , fAlphaType(at) |
22 , fBudgeted(budgeted) | 23 , fBudgeted(budgeted) |
23 {} | 24 {} |
24 | 25 |
| 26 SkImage_Gpu::~SkImage_Gpu() { |
| 27 if (fAddedRasterVersionToCache.load()) { |
| 28 SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 29 } |
| 30 } |
| 31 |
25 SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfacePro
ps& props) const { | 32 SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfacePro
ps& props) const { |
26 GrTexture* tex = this->getTexture(); | 33 GrTexture* tex = this->getTexture(); |
27 SkASSERT(tex); | 34 SkASSERT(tex); |
28 GrContext* ctx = tex->getContext(); | 35 GrContext* ctx = tex->getContext(); |
29 if (!ctx) { | 36 if (!ctx) { |
30 // the texture may have been abandoned, so we have to check | 37 // the texture may have been abandoned, so we have to check |
31 return NULL; | 38 return NULL; |
32 } | 39 } |
33 // TODO: Change signature of onNewSurface to take a budgeted param. | 40 // TODO: Change signature of onNewSurface to take a budgeted param. |
34 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; | 41 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; |
35 return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSur
faces, &props); | 42 return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSur
faces, &props); |
36 } | 43 } |
37 | 44 |
38 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { | 45 extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { |
39 if (as_IB(image)->getTexture()) { | 46 if (as_IB(image)->getTexture()) { |
40 ((SkImage_Gpu*)image)->applyBudgetDecision(); | 47 ((SkImage_Gpu*)image)->applyBudgetDecision(); |
41 } | 48 } |
42 } | 49 } |
43 | 50 |
44 SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode
tileY, | 51 SkShader* SkImage_Gpu::onNewShader(SkShader::TileMode tileX, SkShader::TileMode
tileY, |
45 const SkMatrix* localMatrix) const { | 52 const SkMatrix* localMatrix) const { |
46 SkBitmap bm; | 53 SkBitmap bm; |
47 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaqu
e(), &bm); | 54 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), this->isOpaqu
e(), &bm); |
48 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); | 55 return SkShader::CreateBitmapShader(bm, tileX, tileY, localMatrix); |
49 } | 56 } |
50 | 57 |
51 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { | 58 bool SkImage_Gpu::getROPixels(SkBitmap* dst) const { |
| 59 if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 60 SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 61 SkASSERT(dst->isImmutable()); |
| 62 SkASSERT(dst->getPixels()); |
| 63 return true; |
| 64 } |
| 65 |
52 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; | 66 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; |
53 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { | 67 if (!dst->tryAllocPixels(SkImageInfo::MakeN32(this->width(), this->height(),
at))) { |
54 return false; | 68 return false; |
55 } | 69 } |
56 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, | 70 if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPix
elConfig, |
57 dst->getPixels(), dst->rowBytes())) { | 71 dst->getPixels(), dst->rowBytes())) { |
58 return false; | 72 return false; |
59 } | 73 } |
| 74 |
| 75 dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 76 SkBitmapCache::Add(this->uniqueID(), *dst); |
| 77 fAddedRasterVersionToCache.store(true); |
60 return true; | 78 return true; |
61 } | 79 } |
62 | 80 |
63 bool SkImage_Gpu::isOpaque() const { | 81 bool SkImage_Gpu::isOpaque() const { |
64 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; | 82 return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_Sk
AlphaType; |
65 } | 83 } |
66 | 84 |
67 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ | 85 static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
{ |
68 switch (info.colorType()) { | 86 switch (info.colorType()) { |
69 case kRGBA_8888_SkColorType: | 87 case kRGBA_8888_SkColorType: |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 if (!dst) { | 271 if (!dst) { |
254 return NULL; | 272 return NULL; |
255 } | 273 } |
256 | 274 |
257 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 275 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
258 const SkIPoint dstP = SkIPoint::Make(0, 0); | 276 const SkIPoint dstP = SkIPoint::Make(0, 0); |
259 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); | 277 ctx->copySurface(dst, src, srcR, dstP, GrContext::kFlushWrites_PixelOp); |
260 return dst; | 278 return dst; |
261 } | 279 } |
262 | 280 |
OLD | NEW |