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(SkPicture* picture, TileMode tmx, TileMode tmy) | 21 SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy) |
22 : fPicture(picture) | 22 : fPicture(SkRef(picture)) |
23 , fTmx(tmx) | 23 , fTmx(tmx) |
24 , fTmy(tmy) { | 24 , fTmy(tmy) { } |
25 SkSafeRef(fPicture); | |
26 } | |
27 | 25 |
28 SkPictureShader::SkPictureShader(SkReadBuffer& buffer) | 26 SkPictureShader::SkPictureShader(SkReadBuffer& buffer) |
29 : INHERITED(buffer) { | 27 : INHERITED(buffer) { |
30 fTmx = static_cast<SkShader::TileMode>(buffer.read32()); | 28 fTmx = static_cast<SkShader::TileMode>(buffer.read32()); |
31 fTmy = static_cast<SkShader::TileMode>(buffer.read32()); | 29 fTmy = static_cast<SkShader::TileMode>(buffer.read32()); |
32 if (buffer.readBool()) { | 30 if (buffer.readBool()) { |
33 fPicture = SkPicture::CreateFromBuffer(buffer); | 31 fPicture = SkPicture::CreateFromBuffer(buffer); |
34 } else { | 32 } else { |
35 fPicture = NULL; | 33 // For backwards compatibility, create a dummy picture. Older versions a llowed |
scroggo
2014/04/15 19:15:49
This code is only for handling already existing SK
f(malita)
2014/04/15 19:40:40
Yes, should be safe to remove.
scroggo
2014/04/15 20:33:20
Done.
| |
34 // creating an SkPictureShader with a NULL SkPicture. | |
35 SkPictureRecorder factory; | |
36 factory.beginRecording(0, 0); | |
37 fPicture = factory.endRecording(); | |
36 } | 38 } |
37 } | 39 } |
38 | 40 |
39 SkPictureShader::~SkPictureShader() { | 41 SkPictureShader::~SkPictureShader() { |
40 SkSafeUnref(fPicture); | 42 fPicture->unref(); |
41 } | 43 } |
42 | 44 |
43 SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileM ode tmy) { | 45 SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileM ode tmy) { |
46 if (!picture || 0 == picture->width() || 0 == picture->height()) { | |
47 return NULL; | |
48 } | |
44 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy)); | 49 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy)); |
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); |
52 buffer.writeBool(NULL != fPicture); | 57 buffer.writeBool(NULL != fPicture); |
53 if (fPicture) { | 58 if (fPicture) { |
54 fPicture->flatten(buffer); | 59 fPicture->flatten(buffer); |
55 } | 60 } |
56 } | 61 } |
57 | 62 |
58 SkShader* SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { | 63 SkShader* SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { |
59 if (!fPicture || (0 == fPicture->width() && 0 == fPicture->height())) { | 64 // FIXME: Older versions of this class allowed a picture with 0 width or |
60 return NULL; | 65 // height, but would bail out here. Allow a 0 width/height to support |
61 } | 66 // reading an SkPictureShader from an SKP. Note that we will still return |
67 // NULL in that case due to an empty tileSize. If/when we can disregard | |
68 // old SKPs, it is safe to assert the picture's width and height are | |
69 // greater than zero. | |
f(malita)
2014/04/15 19:40:40
It's safe to disregard old SKPs - the shader is no
scroggo
2014/04/15 20:33:20
Done.
| |
70 SkASSERT(fPicture); // && fPicture->width() > 0 && fPicture->height() > 0); | |
62 | 71 |
63 SkMatrix m; | 72 SkMatrix m; |
64 if (this->hasLocalMatrix()) { | 73 if (this->hasLocalMatrix()) { |
65 m.setConcat(matrix, this->getLocalMatrix()); | 74 m.setConcat(matrix, this->getLocalMatrix()); |
66 } else { | 75 } else { |
67 m = matrix; | 76 m = matrix; |
68 } | 77 } |
69 | 78 |
70 // Use a rotation-invariant scale | 79 // Use a rotation-invariant scale |
71 SkPoint scale; | 80 SkPoint scale; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 | 218 |
210 #if SK_SUPPORT_GPU | 219 #if SK_SUPPORT_GPU |
211 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai nt) const { | 220 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai nt) const { |
212 SkAutoTUnref<SkShader> bitmapShader(this->buildBitmapShader(context->getMatr ix())); | 221 SkAutoTUnref<SkShader> bitmapShader(this->buildBitmapShader(context->getMatr ix())); |
213 if (!bitmapShader) { | 222 if (!bitmapShader) { |
214 return NULL; | 223 return NULL; |
215 } | 224 } |
216 return bitmapShader->asNewEffect(context, paint); | 225 return bitmapShader->asNewEffect(context, paint); |
217 } | 226 } |
218 #endif | 227 #endif |
OLD | NEW |