OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "SkPictureShader.h" | 8 #include "SkPictureShader.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkBitmapProcShader.h" | 11 #include "SkBitmapProcShader.h" |
12 #include "SkCanvas.h" | 12 #include "SkCanvas.h" |
13 #include "SkMatrixUtils.h" | 13 #include "SkMatrixUtils.h" |
14 #include "SkPicture.h" | 14 #include "SkPicture.h" |
15 #include "SkReadBuffer.h" | 15 #include "SkReadBuffer.h" |
16 | 16 |
17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
18 #include "GrContext.h" | 18 #include "GrContext.h" |
19 #endif | 19 #endif |
20 | 20 |
21 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod
e tmy, | 21 SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMod
e tmy, |
22 const SkMatrix* localMatrix) | 22 const SkMatrix* localMatrix, const SkRect* tile
) |
23 : INHERITED(localMatrix) | 23 : INHERITED(localMatrix) |
24 , fPicture(SkRef(picture)) | 24 , fPicture(SkRef(picture)) |
25 , fTmx(tmx) | 25 , fTmx(tmx) |
26 , fTmy(tmy) { } | 26 , fTmy(tmy) { |
| 27 fTile = tile ? *tile : SkRect::MakeWH(SkIntToScalar(picture->width()), |
| 28 SkIntToScalar(picture->height())); |
| 29 } |
27 | 30 |
28 SkPictureShader::SkPictureShader(SkReadBuffer& buffer) | 31 SkPictureShader::SkPictureShader(SkReadBuffer& buffer) |
29 : INHERITED(buffer) { | 32 : INHERITED(buffer) { |
30 fTmx = static_cast<SkShader::TileMode>(buffer.read32()); | 33 fTmx = static_cast<SkShader::TileMode>(buffer.read32()); |
31 fTmy = static_cast<SkShader::TileMode>(buffer.read32()); | 34 fTmy = static_cast<SkShader::TileMode>(buffer.read32()); |
| 35 buffer.readRect(&fTile); |
32 fPicture = SkPicture::CreateFromBuffer(buffer); | 36 fPicture = SkPicture::CreateFromBuffer(buffer); |
33 } | 37 } |
34 | 38 |
35 SkPictureShader::~SkPictureShader() { | 39 SkPictureShader::~SkPictureShader() { |
36 fPicture->unref(); | 40 fPicture->unref(); |
37 } | 41 } |
38 | 42 |
39 SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx,
TileMode tmy, | 43 SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx,
TileMode tmy, |
40 const SkMatrix* localMatrix) { | 44 const SkMatrix* localMatrix, const SkRe
ct* tile) { |
41 if (!picture || 0 == picture->width() || 0 == picture->height()) { | 45 if (!picture || 0 == picture->width() || 0 == picture->height() |
| 46 || (NULL != tile && tile->isEmpty())) { |
42 return NULL; | 47 return NULL; |
43 } | 48 } |
44 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix)); | 49 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile)); |
45 } | 50 } |
46 | 51 |
47 void SkPictureShader::flatten(SkWriteBuffer& buffer) const { | 52 void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
48 this->INHERITED::flatten(buffer); | 53 this->INHERITED::flatten(buffer); |
49 | 54 |
50 buffer.write32(fTmx); | 55 buffer.write32(fTmx); |
51 buffer.write32(fTmy); | 56 buffer.write32(fTmy); |
| 57 buffer.writeRect(fTile); |
52 fPicture->flatten(buffer); | 58 fPicture->flatten(buffer); |
53 } | 59 } |
54 | 60 |
55 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatri
x* localM) const { | 61 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatri
x* localM) const { |
56 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); | 62 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); |
57 | 63 |
58 SkMatrix m; | 64 SkMatrix m; |
59 m.setConcat(matrix, this->getLocalMatrix()); | 65 m.setConcat(matrix, this->getLocalMatrix()); |
60 if (localM) { | 66 if (localM) { |
61 m.preConcat(*localM); | 67 m.preConcat(*localM); |
62 } | 68 } |
63 | 69 |
64 // Use a rotation-invariant scale | 70 // Use a rotation-invariant scale |
65 SkPoint scale; | 71 SkPoint scale; |
66 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { | 72 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { |
67 // Decomposition failed, use an approximation. | 73 // Decomposition failed, use an approximation. |
68 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), | 74 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), |
69 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); | 75 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); |
70 } | 76 } |
71 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() *
fPicture->height()); | 77 SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTil
e.height()); |
72 | 78 |
73 SkISize tileSize = scaledSize.toRound(); | 79 SkISize tileSize = scaledSize.toRound(); |
74 if (tileSize.isEmpty()) { | 80 if (tileSize.isEmpty()) { |
75 return NULL; | 81 return NULL; |
76 } | 82 } |
77 | 83 |
78 // The actual scale, compensating for rounding. | 84 // The actual scale, compensating for rounding. |
79 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->
width(), | 85 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.widt
h(), |
80 SkIntToScalar(tileSize.height()) / fPicture-
>height()); | 86 SkIntToScalar(tileSize.height()) / fTile.hei
ght()); |
81 | 87 |
82 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); | 88 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); |
83 | 89 |
84 if (!fCachedBitmapShader || tileScale != fCachedTileScale) { | 90 if (!fCachedBitmapShader || tileScale != fCachedTileScale) { |
85 SkBitmap bm; | 91 SkBitmap bm; |
86 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { | 92 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
87 return NULL; | 93 return NULL; |
88 } | 94 } |
89 bm.eraseColor(SK_ColorTRANSPARENT); | 95 bm.eraseColor(SK_ColorTRANSPARENT); |
90 | 96 |
91 SkCanvas canvas(bm); | 97 SkCanvas canvas(bm); |
92 canvas.scale(tileScale.width(), tileScale.height()); | 98 canvas.scale(tileScale.width(), tileScale.height()); |
| 99 canvas.translate(fTile.x(), fTile.y()); |
93 canvas.drawPicture(fPicture); | 100 canvas.drawPicture(fPicture); |
94 | 101 |
95 fCachedTileScale = tileScale; | 102 fCachedTileScale = tileScale; |
96 | 103 |
97 SkMatrix shaderMatrix = this->getLocalMatrix(); | 104 SkMatrix shaderMatrix = this->getLocalMatrix(); |
98 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); | 105 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
99 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatr
ix)); | 106 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatr
ix)); |
100 } | 107 } |
101 | 108 |
102 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. | 109 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect); | 202 return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect); |
196 } | 203 } |
197 #else | 204 #else |
198 bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint, | 205 bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint, |
199 const SkMatrix* localMatrix, GrColor* paintCol
or, | 206 const SkMatrix* localMatrix, GrColor* paintCol
or, |
200 GrEffect** effect) const { | 207 GrEffect** effect) const { |
201 SkDEBUGFAIL("Should not call in GPU-less build"); | 208 SkDEBUGFAIL("Should not call in GPU-less build"); |
202 return false; | 209 return false; |
203 } | 210 } |
204 #endif | 211 #endif |
OLD | NEW |