Chromium Code Reviews| Index: src/pdf/SkPDFShader.cpp |
| diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp |
| index a0dffb7791fc4724ecc5a16403fc4b4b2efa6b58..5ea916b7a50f9c8c0bf549a09c4ac99969ccdad7 100644 |
| --- a/src/pdf/SkPDFShader.cpp |
| +++ b/src/pdf/SkPDFShader.cpp |
| @@ -13,12 +13,15 @@ |
| #include "SkData.h" |
| #include "SkPDFCatalog.h" |
| #include "SkPDFDevice.h" |
| +#include "SkPDFFormXObject.h" |
| +#include "SkPDFGraphicState.h" |
| #include "SkPDFTypes.h" |
| #include "SkPDFUtils.h" |
| #include "SkScalar.h" |
| #include "SkStream.h" |
| #include "SkTemplates.h" |
| #include "SkThread.h" |
| +#include "SkTSet.h" |
| #include "SkTypes.h" |
| static bool transformBBox(const SkMatrix& matrix, SkRect* bbox) { |
| @@ -398,7 +401,7 @@ class SkPDFShader::State { |
| public: |
| SkShader::GradientType fType; |
| SkShader::GradientInfo fInfo; |
| - SkAutoFree fColorData; |
| + SkAutoFree fColorData; // This provides storage for arrays in fInfo. |
| SkMatrix fCanvasTransform; |
| SkMatrix fShaderTransform; |
| SkIRect fBBox; |
| @@ -407,9 +410,20 @@ public: |
| uint32_t fPixelGeneration; |
| SkShader::TileMode fImageTileModes[2]; |
| - explicit State(const SkShader& shader, const SkMatrix& canvasTransform, |
| + State(const SkShader& shader, const SkMatrix& canvasTransform, |
| const SkIRect& bbox); |
| + |
| bool operator==(const State& b) const; |
| + |
| + SkPDFShader::State* CreateAlphaToLuminosityState() const; |
| + SkPDFShader::State* CreateOpaqueState() const; |
| + |
| + bool GradientHasAlpha() const; |
| + |
| +private: |
| + State(const State& other); |
| + State operator=(const State& rhs) { return State(rhs); } |
|
vandebo (ex-Chrome)
2013/07/09 17:47:44
I don't think you need an implementation.
ducky
2013/07/10 21:42:26
Wait, wha?
So I need an assignment operator, but i
vandebo (ex-Chrome)
2013/07/11 22:22:23
See http://google-styleguide.googlecode.com/svn/tr
ducky
2013/07/12 03:40:00
Done.
|
| + void AllocateGradientInfoStorage(); |
| }; |
| class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader { |
| @@ -440,6 +454,39 @@ private: |
| SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain); |
| }; |
| +/** |
| + * A shader for PDF gradients. This encapsulates the function shader |
| + * inside a tiling pattern while providing a common pattern interface. |
| + * The encapsulation allows the use of a SMask for transparency gradients. |
| + */ |
| +class SkPDFGradientShader : public SkPDFStream, public SkPDFShader { |
| +public: |
| + explicit SkPDFGradientShader(SkPDFShader::State* state); |
| + virtual ~SkPDFGradientShader() { |
| + if (isValid()) { |
| + RemoveShader(this); |
| + } |
| + } |
| + |
| + virtual bool isValid() { |
| + return fColorPattern.get() != NULL; |
| + } |
| + |
| +private: |
| + SkAutoTDelete<const SkPDFShader::State> fState; |
| + |
| + void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
| + SkTSet<SkPDFObject*>* newResourceObjects) { |
| + fResourceDict->getResources(knownResourceObjects, |
| + newResourceObjects); |
| + } |
| + |
| + SkAutoTUnref<SkPDFObject> fColorPattern; |
| + SkAutoTUnref<SkPDFGraphicState> fAlphaGs; |
| + |
| + SkAutoTUnref<SkPDFResourceDict> fResourceDict; |
| +}; |
| + |
| class SkPDFImageShader : public SkPDFStream, public SkPDFShader { |
| public: |
| explicit SkPDFImageShader(SkPDFShader::State* state); |
| @@ -465,21 +512,12 @@ private: |
| SkPDFShader::SkPDFShader() {} |
| // static |
| -void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
| - SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
| - ShaderCanonicalEntry entry(shader, NULL); |
| - int index = CanonicalShaders().find(entry); |
| - SkASSERT(index >= 0); |
| - CanonicalShaders().removeShuffle(index); |
| -} |
| - |
| -// static |
| -SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
| - const SkMatrix& matrix, |
| - const SkIRect& surfaceBBox) { |
| +// This is an internal method. CanonicalShadersMutex() should already be acquired. |
| +// This also takes ownership of inState, which is deleted on return. |
|
vandebo (ex-Chrome)
2013/07/09 17:47:44
nix "which is..." If it takes ownership than the c
ducky
2013/07/10 21:42:26
Done.
|
| +SkPDFObject* SkPDFShader::GetPDFShaderByState(State* inState) { |
| SkPDFObject* result; |
| - SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
| - SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox)); |
| + |
| + SkAutoTDelete<State> shaderState(inState); |
| if (shaderState.get()->fType == SkShader::kNone_GradientType && |
| shaderState.get()->fImage.isNull()) { |
| // TODO(vandebo) This drops SKComposeShader on the floor. We could |
| @@ -505,10 +543,17 @@ SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
| valid = imageShader->isValid(); |
| result = imageShader; |
| } else { |
| - SkPDFFunctionShader* functionShader = |
| - new SkPDFFunctionShader(shaderState.detach()); |
| - valid = functionShader->isValid(); |
| - result = functionShader; |
| + if (shaderState.get()->GradientHasAlpha()) { |
| + SkPDFGradientShader* gradientShader = |
| + new SkPDFGradientShader(shaderState.detach()); |
| + valid = gradientShader->isValid(); |
| + result = gradientShader; |
| + } else { |
| + SkPDFFunctionShader* functionShader = |
| + new SkPDFFunctionShader(shaderState.detach()); |
| + valid = functionShader->isValid(); |
| + result = functionShader; |
| + } |
| } |
| if (!valid) { |
| delete result; |
| @@ -520,6 +565,23 @@ SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
| } |
| // static |
| +void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
| + SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
| + ShaderCanonicalEntry entry(shader, NULL); |
| + int index = CanonicalShaders().find(entry); |
| + SkASSERT(index >= 0); |
| + CanonicalShaders().removeShuffle(index); |
| +} |
| + |
| +// static |
| +SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
| + const SkMatrix& matrix, |
| + const SkIRect& surfaceBBox) { |
| + SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
| + return GetPDFShaderByState(new State(shader, matrix, surfaceBBox)); |
| +} |
| + |
| +// static |
| SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() { |
| // This initialization is only thread safe with gcc. |
| static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders; |
| @@ -553,6 +615,93 @@ SkPDFObject* SkPDFFunctionShader::RangeObject() { |
| return range; |
| } |
| +static SkPDFResourceDict* get_gradient_resource_dict( |
| + SkPDFObject* functionShader, |
| + SkPDFObject* gState) { |
| + SkPDFResourceDict* dict = new SkPDFResourceDict(); |
| + |
| + if (functionShader != NULL) { |
| + dict->insertResourceAsRef("Pattern", "P0", functionShader); |
| + } |
| + if (gState != NULL) { |
| + dict->insertResourceAsRef("ExtGState", "G0", gState); |
| + } |
| + |
| + dict->insert("ProcSet", SkPDFUtils::CreateFullProcSetsArray())->unref(); |
| + |
| + return dict; |
| +} |
| + |
| +static void populate_tiling_pattern_dict(SkPDFDict* pattern, |
| + SkRect& bbox, SkPDFDict* resources, |
| + const SkMatrix& matrix) { |
| + pattern->insertName("Type", "Pattern"); |
| + pattern->insertInt("PatternType", 1); |
| + pattern->insertInt("PaintType", 1); |
| + pattern->insertInt("TilingType", 1); |
| + pattern->insert("BBox", SkPDFUtils::RectToArray(bbox))->unref(); |
| + pattern->insertScalar("XStep", bbox.width()); |
| + pattern->insertScalar("YStep", bbox.height()); |
| + pattern->insert("Resources", resources); |
| + if (!matrix.isIdentity()) { |
| + pattern->insert("Matrix", SkPDFUtils::MatrixToArray(matrix))->unref(); |
| + } |
| +} |
| + |
| +/** |
| + * Creates a content stream which fills the pattern P0 across bounds. |
| + * The argument setup may contain any relevant PDF content stream operators |
| + * and will be prepended to the output content stream. |
| + */ |
| +static SkData* create_pattern_fill_content(const char setup[], SkRect& bounds) { |
| + SkDynamicMemoryWStream content; |
| + if (NULL != setup) { |
| + content.writeText(setup); |
| + } |
| + content.writeText("/Pattern CS/Pattern cs/P0 SCN/P0 scn\n"); |
| + |
| + SkPDFUtils::AppendRectangle(bounds, &content); |
| + SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kEvenOdd_FillType, &content); |
| + |
| + return content.copyToData(); |
| +} |
| + |
| +/** |
| + * Creates a ExtGState with the SMask set to the luminosityShader in |
| + * luminosity mode. The shader pattern extends to the bbox. |
| + */ |
| +static SkPDFGraphicState* create_smask_graphic_state(SkPDFObject* luminosityShader, SkRect& bbox) { |
| + SkAutoTUnref<SkData> alphaStream(create_pattern_fill_content(NULL, bbox)); |
| + |
| + SkAutoTUnref<SkPDFResourceDict> |
| + resources(get_gradient_resource_dict(luminosityShader, NULL)); |
| + |
| + SkAutoTUnref<SkPDFFormXObject> alphaMask( |
| + new SkPDFFormXObject(alphaStream.get(), bbox, resources.get())); |
| + |
| + return SkPDFGraphicState::GetSMaskGraphicState( |
| + alphaMask.get(), false, |
| + SkPDFGraphicState::kLuminosity_SMaskMode); |
| +} |
| + |
| +SkPDFGradientShader::SkPDFGradientShader(SkPDFShader::State* state) : fState(state) { |
| + SkRect bbox; |
| + bbox.set(fState.get()->fBBox); |
| + |
| + fColorPattern.reset(SkPDFShader::GetPDFShaderByState(state->CreateOpaqueState())); |
| + |
| + SkAutoTUnref<SkPDFObject> luminosityShader(SkPDFShader::GetPDFShaderByState( |
| + state->CreateAlphaToLuminosityState())); |
| + fAlphaGs.reset(create_smask_graphic_state(luminosityShader.get(), bbox)); |
| + |
| + fResourceDict.reset(get_gradient_resource_dict(fColorPattern.get(), fAlphaGs.get())); |
| + |
| + SkAutoTUnref<SkData> colorStream(create_pattern_fill_content("/G0 gs", bbox)); |
| + setData(colorStream.get()); |
| + |
| + populate_tiling_pattern_dict(this, bbox, fResourceDict.get(), SkMatrix::I()); |
| +} |
| + |
| SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) |
| : SkPDFDict("Pattern"), |
| fState(state) { |
| @@ -828,27 +977,13 @@ SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { |
| } |
| } |
| - SkAutoTUnref<SkPDFArray> patternBBoxArray(new SkPDFArray); |
| - patternBBoxArray->reserve(4); |
| - patternBBoxArray->appendScalar(patternBBox.fLeft); |
| - patternBBoxArray->appendScalar(patternBBox.fTop); |
| - patternBBoxArray->appendScalar(patternBBox.fRight); |
| - patternBBoxArray->appendScalar(patternBBox.fBottom); |
| - |
| // Put the canvas into the pattern stream (fContent). |
| SkAutoTUnref<SkStream> content(pattern.content()); |
| setData(content.get()); |
| pattern.getResources(fResources, &fResources, false); |
| - insertName("Type", "Pattern"); |
| - insertInt("PatternType", 1); |
| - insertInt("PaintType", 1); |
| - insertInt("TilingType", 1); |
| - insert("BBox", patternBBoxArray.get()); |
| - insertScalar("XStep", patternBBox.width()); |
| - insertScalar("YStep", patternBBox.height()); |
| - insert("Resources", pattern.getResourceDict()); |
| - insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
| + populate_tiling_pattern_dict(this, patternBBox, |
| + pattern.getResourceDict(), finalMatrix); |
| fState.get()->fImage.unlockPixels(); |
| } |
| @@ -954,11 +1089,87 @@ SkPDFShader::State::State(const SkShader& shader, |
| SkASSERT(matrix.isIdentity()); |
| fPixelGeneration = fImage.getGenerationID(); |
| } else { |
| - fColorData.set(sk_malloc_throw( |
| - fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
| - fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
| - fInfo.fColorOffsets = |
| - reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
| + AllocateGradientInfoStorage(); |
| shader.asAGradient(&fInfo); |
| } |
| } |
| + |
| +SkPDFShader::State::State(const SkPDFShader::State& other) |
| + : fType(other.fType), |
| + fCanvasTransform(other.fCanvasTransform), |
| + fShaderTransform(other.fShaderTransform), |
| + fBBox(other.fBBox) |
| +{ |
| + // Only gradients supported for now, since that is all that is used. |
| + // If needed, image state copy constructor can be added here later. |
| + SkASSERT(fType != SkShader::kNone_GradientType); |
| + |
| + if (fType != SkShader::kNone_GradientType) { |
| + fInfo = other.fInfo; |
| + |
| + AllocateGradientInfoStorage(); |
| + for (int i = 0; i < fInfo.fColorCount; i++) { |
| + fInfo.fColors[i] = other.fInfo.fColors[i]; |
| + fInfo.fColorOffsets[i] = other.fInfo.fColorOffsets[i]; |
| + } |
| + } |
| +} |
| + |
| +/** |
| + * Create a copy of this gradient state with alpha assigned to RGB luminousity. |
| + * Only valid for gradient states! |
| + */ |
| +SkPDFShader::State* SkPDFShader::State::CreateAlphaToLuminosityState() const { |
| + SkASSERT(fType != SkShader::kNone_GradientType); |
| + |
| + SkPDFShader::State* newState = new SkPDFShader::State(*this); |
| + |
| + for (int i = 0; i < fInfo.fColorCount; i++) { |
| + SkAlpha alpha = SkColorGetA(fInfo.fColors[i]); |
| + newState->fInfo.fColors[i] = SkColorSetARGB(255, alpha, alpha, alpha); |
| + } |
| + |
| + return newState; |
| +} |
| + |
| +/** |
| + * Create a copy of this gradient state with alpha set to fully opaque |
| + * Only valid for gradient states. |
| + */ |
| +SkPDFShader::State* SkPDFShader::State::CreateOpaqueState() const { |
| + SkASSERT(fType != SkShader::kNone_GradientType); |
| + |
| + SkPDFShader::State* newState = new SkPDFShader::State(*this); |
| + SkAlpha opaque = SkColorGetA(SK_ColorBLACK); |
| + for (int i = 0; i < fInfo.fColorCount; i++) { |
| + newState->fInfo.fColors[i] = SkColorSetA(fInfo.fColors[i], opaque); |
| + } |
| + |
| + return newState; |
| +} |
| + |
| +/** |
| + * Returns true if state is a gradient and the gradient has alpha. |
| + */ |
| +bool SkPDFShader::State::GradientHasAlpha() const { |
| + if (fType == SkShader::kNone_GradientType) { |
| + return false; |
| + } |
| + |
| + SkAlpha opaque = SkColorGetA(SK_ColorBLACK); |
| + for (int i = 0; i < fInfo.fColorCount; i++) { |
| + SkAlpha alpha = SkColorGetA(fInfo.fColors[i]); |
| + if (alpha != opaque) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +void SkPDFShader::State::AllocateGradientInfoStorage() { |
| + fColorData.set(sk_malloc_throw( |
| + fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
| + fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
| + fInfo.fColorOffsets = |
| + reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
| +} |