Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(731)

Unified Diff: src/core/SkDraw.cpp

Issue 207683004: Extract most of the mutable state of SkShader into a separate Context object. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: SkGradientShader Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkDraw.cpp
diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp
index 05b06453dbf200df8cebccfc3aee4c69825d5143..7c3737654e4bdb6891e10d5a5aa49249a9058338 100644
--- a/src/core/SkDraw.cpp
+++ b/src/core/SkDraw.cpp
@@ -2355,13 +2355,35 @@ static bool texture_to_matrix(const VertState& state, const SkPoint verts[],
return matrix->setPolyToPoly(src, dst, 3);
}
+/**
+ * This class is NOT immutable due to the setup() method being called repeatedly after
scroggo 2014/04/02 20:16:53 Now that setup is on the context, isn't this comme
Dominik Grewe 2014/04/03 12:12:59 You're right. Thanks for spotting.
+ * the shader has been associated with a paint. This is okay because we only ever
+ * associate this shader with temporary paints.
+ */
class SkTriColorShader : public SkShader {
public:
SkTriColorShader() {}
- bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
+ virtual SkShader::Context* createContext(
+ const SkBitmap&, const SkPaint&, const SkMatrix&, void*) const SK_OVERRIDE;
+ virtual size_t contextSize() const SK_OVERRIDE;
+
+ class TriColorShaderContext : public SkShader::Context {
+ public:
+ TriColorShaderContext(const SkTriColorShader& shader, const SkBitmap& device,
+ const SkPaint& paint, const SkMatrix& matrix);
+ virtual ~TriColorShaderContext();
+
+ bool setup(const SkPoint pts[], const SkColor colors[], int, int, int);
+
+ virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
+
+ private:
+ SkMatrix fDstToUnit;
+ SkPMColor fColors[3];
- virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
+ typedef SkShader::Context INHERITED;
+ };
SK_DEVELOPER_TO_STRING()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTriColorShader)
@@ -2369,15 +2391,20 @@ public:
protected:
SkTriColorShader(SkReadBuffer& buffer) : SkShader(buffer) {}
-private:
- SkMatrix fDstToUnit;
- SkPMColor fColors[3];
-
typedef SkShader INHERITED;
};
-bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[],
- int index0, int index1, int index2) {
+SkShader::Context* SkTriColorShader::createContext(const SkBitmap& device, const SkPaint& paint,
+ const SkMatrix& matrix, void* storage) const {
+ if (!this->validContext(device, paint, matrix)) {
+ return NULL;
+ }
+
+ return SkNEW_PLACEMENT_ARGS(storage, TriColorShaderContext, (*this, device, paint, matrix));
+}
+
+bool SkTriColorShader::TriColorShaderContext::setup(const SkPoint pts[], const SkColor colors[],
+ int index0, int index1, int index2) {
fColors[0] = SkPreMultiplyColor(colors[index0]);
fColors[1] = SkPreMultiplyColor(colors[index1]);
@@ -2411,7 +2438,18 @@ static int ScalarTo256(SkScalar v) {
return SkAlpha255To256(scale);
}
-void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
+
+SkTriColorShader::TriColorShaderContext::TriColorShaderContext(
+ const SkTriColorShader& shader, const SkBitmap& device,
+ const SkPaint& paint, const SkMatrix& matrix)
+ : INHERITED(shader, device, paint, matrix) {}
+
+SkTriColorShader::TriColorShaderContext::~TriColorShaderContext() {}
+
+size_t SkTriColorShader::contextSize() const {
+ return sizeof(TriColorShaderContext);
+}
+void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) {
SkPoint src;
for (int i = 0; i < count; i++) {
@@ -2518,9 +2556,7 @@ void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
}
SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p);
- // important that we abort early, as below we may manipulate the shader
- // and that is only valid if the shader returned true from setContext.
- // If it returned false, then our blitter will be the NullBlitter.
+ // Abort early if we failed to create a shader context.
if (blitter->isNullBlitter()) {
return;
}
@@ -2536,30 +2572,36 @@ void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
savedLocalM = shader->getLocalMatrix();
}
- // setContext has already been called and verified to return true
- // by the constructor of SkAutoBlitterChoose
- bool prevContextSuccess = true;
while (vertProc(&state)) {
if (NULL != textures) {
if (texture_to_matrix(state, vertices, textures, &tempM)) {
tempM.postConcat(savedLocalM);
shader->setLocalMatrix(tempM);
- // Need to recall setContext since we changed the local matrix.
- // However, we also need to balance the calls this with a
- // call to endContext which requires tracking the result of
- // the previous call to setContext.
- if (prevContextSuccess) {
- shader->endContext();
- }
- prevContextSuccess = shader->setContext(*fBitmap, p, *fMatrix);
- if (!prevContextSuccess) {
+ if (!blitter->resetShaderContext(*fBitmap, p, *fMatrix)) {
continue;
}
}
}
if (NULL != colors) {
- if (!triShader.setup(vertices, colors,
- state.f0, state.f1, state.f2)) {
+ // Find the context for triShader.
+ SkTriColorShader::TriColorShaderContext* triColorShaderContext;
+
+ SkShader::Context* shaderContext = blitter->getShaderContext();
+ SkASSERT(shaderContext);
+ if (p.getShader() == &triShader) {
+ triColorShaderContext =
+ static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContext);
+ } else {
+ // The shader is a compose shader and triShader is its first shader.
+ SkComposeShader::ComposeShaderContext* composeShaderContext =
+ static_cast<SkComposeShader::ComposeShaderContext*>(shaderContext);
scroggo 2014/04/02 20:16:53 This code makes a lot of assumptions about the cod
Dominik Grewe 2014/04/03 12:12:59 Good point. I've added the asserts.
+ SkShader::Context* shaderContextA = composeShaderContext->getShaderContextA();
+ triColorShaderContext =
+ static_cast<SkTriColorShader::TriColorShaderContext*>(shaderContextA);
+ }
+
+ if (!triColorShaderContext->setup(vertices, colors,
+ state.f0, state.f1, state.f2)) {
continue;
}
}
@@ -2574,13 +2616,6 @@ void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count,
if (NULL != shader) {
shader->setLocalMatrix(savedLocalM);
}
-
- // If the final call to setContext fails we must make it suceed so that the
- // call to endContext in the destructor for SkAutoBlitterChoose is balanced.
- if (!prevContextSuccess) {
- prevContextSuccess = shader->setContext(*fBitmap, paint, SkMatrix::I());
- SkASSERT(prevContextSuccess);
- }
} else {
// no colors[] and no texture
HairProc hairProc = ChooseHairProc(paint.isAntiAlias());

Powered by Google App Engine
This is Rietveld 408576698