OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkPDFShader.h" | 10 #include "SkPDFShader.h" |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 SkShader::GradientInfo fInfo; | 427 SkShader::GradientInfo fInfo; |
428 SkAutoFree fColorData; // This provides storage for arrays in fInfo. | 428 SkAutoFree fColorData; // This provides storage for arrays in fInfo. |
429 SkMatrix fCanvasTransform; | 429 SkMatrix fCanvasTransform; |
430 SkMatrix fShaderTransform; | 430 SkMatrix fShaderTransform; |
431 SkIRect fBBox; | 431 SkIRect fBBox; |
432 | 432 |
433 SkBitmap fImage; | 433 SkBitmap fImage; |
434 uint32_t fPixelGeneration; | 434 uint32_t fPixelGeneration; |
435 SkShader::TileMode fImageTileModes[2]; | 435 SkShader::TileMode fImageTileModes[2]; |
436 | 436 |
437 State(const SkShader& shader, const SkMatrix& canvasTransform, | 437 State(SkShader* shader, const SkMatrix& canvasTransform, |
438 const SkIRect& bbox, SkScalar rasterScale); | 438 const SkIRect& bbox, SkScalar rasterScale); |
439 | 439 |
440 bool operator==(const State& b) const; | 440 bool operator==(const State& b) const; |
441 | 441 |
442 SkPDFShader::State* CreateAlphaToLuminosityState() const; | 442 SkPDFShader::State* CreateAlphaToLuminosityState() const; |
443 SkPDFShader::State* CreateOpaqueState() const; | 443 SkPDFShader::State* CreateOpaqueState() const; |
444 | 444 |
445 bool GradientHasAlpha() const; | 445 bool GradientHasAlpha() const; |
446 | 446 |
447 private: | 447 private: |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } else { | 508 } else { |
509 SkPDFObject* shader = canon->findFunctionShader(state); | 509 SkPDFObject* shader = canon->findFunctionShader(state); |
510 return shader ? SkRef(shader) | 510 return shader ? SkRef(shader) |
511 : SkPDFFunctionShader::Create(canon, autoState); | 511 : SkPDFFunctionShader::Create(canon, autoState); |
512 } | 512 } |
513 } | 513 } |
514 | 514 |
515 // static | 515 // static |
516 SkPDFObject* SkPDFShader::GetPDFShader(SkPDFDocument* doc, | 516 SkPDFObject* SkPDFShader::GetPDFShader(SkPDFDocument* doc, |
517 SkScalar dpi, | 517 SkScalar dpi, |
518 const SkShader& shader, | 518 SkShader* shader, |
519 const SkMatrix& matrix, | 519 const SkMatrix& matrix, |
520 const SkIRect& surfaceBBox, | 520 const SkIRect& surfaceBBox, |
521 SkScalar rasterScale) { | 521 SkScalar rasterScale) { |
522 SkAutoTDelete<SkPDFShader::State> state(new State(shader, matrix, surfaceBBo
x, rasterScale)); | 522 SkAutoTDelete<SkPDFShader::State> state(new State(shader, matrix, surfaceBBo
x, rasterScale)); |
523 return get_pdf_shader_by_state(doc, dpi, &state); | 523 return get_pdf_shader_by_state(doc, dpi, &state); |
524 } | 524 } |
525 | 525 |
526 static sk_sp<SkPDFDict> get_gradient_resource_dict( | 526 static sk_sp<SkPDFDict> get_gradient_resource_dict( |
527 SkPDFObject* functionShader, | 527 SkPDFObject* functionShader, |
528 SkPDFObject* gState) { | 528 SkPDFObject* gState) { |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1084 break; | 1084 break; |
1085 case SkShader::kSweep_GradientType: | 1085 case SkShader::kSweep_GradientType: |
1086 case SkShader::kNone_GradientType: | 1086 case SkShader::kNone_GradientType: |
1087 case SkShader::kColor_GradientType: | 1087 case SkShader::kColor_GradientType: |
1088 break; | 1088 break; |
1089 } | 1089 } |
1090 } | 1090 } |
1091 return true; | 1091 return true; |
1092 } | 1092 } |
1093 | 1093 |
1094 SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransfor
m, | 1094 SkPDFShader::State::State(SkShader* shader, const SkMatrix& canvasTransform, |
1095 const SkIRect& bbox, SkScalar rasterScale) | 1095 const SkIRect& bbox, SkScalar rasterScale) |
1096 : fCanvasTransform(canvasTransform), | 1096 : fCanvasTransform(canvasTransform), |
1097 fBBox(bbox), | 1097 fBBox(bbox), |
1098 fPixelGeneration(0) { | 1098 fPixelGeneration(0) { |
1099 fInfo.fColorCount = 0; | 1099 fInfo.fColorCount = 0; |
1100 fInfo.fColors = nullptr; | 1100 fInfo.fColors = nullptr; |
1101 fInfo.fColorOffsets = nullptr; | 1101 fInfo.fColorOffsets = nullptr; |
1102 fShaderTransform = shader.getLocalMatrix(); | 1102 fShaderTransform = shader->getLocalMatrix(); |
1103 fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; | 1103 fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; |
1104 | 1104 |
1105 fType = shader.asAGradient(&fInfo); | 1105 fType = shader->asAGradient(&fInfo); |
1106 | 1106 |
1107 if (fType == SkShader::kNone_GradientType) { | 1107 if (fType == SkShader::kNone_GradientType) { |
1108 SkMatrix matrix; | 1108 SkMatrix matrix; |
1109 if (shader.isABitmap(&fImage, &matrix, fImageTileModes)) { | 1109 if (shader->isABitmap(&fImage, &matrix, fImageTileModes)) { |
1110 SkASSERT(matrix.isIdentity()); | 1110 SkASSERT(matrix.isIdentity()); |
1111 } else { | 1111 } else { |
1112 // Generic fallback for unsupported shaders: | 1112 // Generic fallback for unsupported shaders: |
1113 // * allocate a bbox-sized bitmap | 1113 // * allocate a bbox-sized bitmap |
1114 // * shade the whole area | 1114 // * shade the whole area |
1115 // * use the result as a bitmap shader | 1115 // * use the result as a bitmap shader |
1116 | 1116 |
1117 // bbox is in device space. While that's exactly what we want for si
zing our bitmap, | 1117 // bbox is in device space. While that's exactly what we want for si
zing our bitmap, |
1118 // we need to map it into shader space for adjustments (to match | 1118 // we need to map it into shader space for adjustments (to match |
1119 // SkPDFImageShader::Create's behavior). | 1119 // SkPDFImageShader::Create's behavior). |
(...skipping 12 matching lines...) Expand all Loading... |
1132 | 1132 |
1133 SkISize size = SkISize::Make(SkScalarRoundToInt(rasterScale * bbox.w
idth()), | 1133 SkISize size = SkISize::Make(SkScalarRoundToInt(rasterScale * bbox.w
idth()), |
1134 SkScalarRoundToInt(rasterScale * bbox.h
eight())); | 1134 SkScalarRoundToInt(rasterScale * bbox.h
eight())); |
1135 SkSize scale = SkSize::Make(SkIntToScalar(size.width()) / shaderRect
.width(), | 1135 SkSize scale = SkSize::Make(SkIntToScalar(size.width()) / shaderRect
.width(), |
1136 SkIntToScalar(size.height()) / shaderRec
t.height()); | 1136 SkIntToScalar(size.height()) / shaderRec
t.height()); |
1137 | 1137 |
1138 fImage.allocN32Pixels(size.width(), size.height()); | 1138 fImage.allocN32Pixels(size.width(), size.height()); |
1139 fImage.eraseColor(SK_ColorTRANSPARENT); | 1139 fImage.eraseColor(SK_ColorTRANSPARENT); |
1140 | 1140 |
1141 SkPaint p; | 1141 SkPaint p; |
1142 p.setShader(const_cast<SkShader*>(&shader)); | 1142 p.setShader(sk_ref_sp(shader)); |
1143 | 1143 |
1144 SkCanvas canvas(fImage); | 1144 SkCanvas canvas(fImage); |
1145 canvas.scale(scale.width(), scale.height()); | 1145 canvas.scale(scale.width(), scale.height()); |
1146 canvas.translate(-shaderRect.x(), -shaderRect.y()); | 1146 canvas.translate(-shaderRect.x(), -shaderRect.y()); |
1147 canvas.drawPaint(p); | 1147 canvas.drawPaint(p); |
1148 | 1148 |
1149 fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y()); | 1149 fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y()); |
1150 fShaderTransform.preScale(1 / scale.width(), 1 / scale.height()); | 1150 fShaderTransform.preScale(1 / scale.width(), 1 / scale.height()); |
1151 } | 1151 } |
1152 fPixelGeneration = fImage.getGenerationID(); | 1152 fPixelGeneration = fImage.getGenerationID(); |
1153 } else { | 1153 } else { |
1154 AllocateGradientInfoStorage(); | 1154 AllocateGradientInfoStorage(); |
1155 shader.asAGradient(&fInfo); | 1155 shader->asAGradient(&fInfo); |
1156 } | 1156 } |
1157 } | 1157 } |
1158 | 1158 |
1159 SkPDFShader::State::State(const SkPDFShader::State& other) | 1159 SkPDFShader::State::State(const SkPDFShader::State& other) |
1160 : fType(other.fType), | 1160 : fType(other.fType), |
1161 fCanvasTransform(other.fCanvasTransform), | 1161 fCanvasTransform(other.fCanvasTransform), |
1162 fShaderTransform(other.fShaderTransform), | 1162 fShaderTransform(other.fShaderTransform), |
1163 fBBox(other.fBBox) | 1163 fBBox(other.fBBox) |
1164 { | 1164 { |
1165 // Only gradients supported for now, since that is all that is used. | 1165 // Only gradients supported for now, since that is all that is used. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1227 return false; | 1227 return false; |
1228 } | 1228 } |
1229 | 1229 |
1230 void SkPDFShader::State::AllocateGradientInfoStorage() { | 1230 void SkPDFShader::State::AllocateGradientInfoStorage() { |
1231 fColorData.set(sk_malloc_throw( | 1231 fColorData.set(sk_malloc_throw( |
1232 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); | 1232 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
1233 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); | 1233 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
1234 fInfo.fColorOffsets = | 1234 fInfo.fColorOffsets = |
1235 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); | 1235 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
1236 } | 1236 } |
OLD | NEW |