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

Unified Diff: src/core/SkShader.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: totalInverse param optional; SkShader::computeTotalInverse; SkBitmapProcShader::validInternal 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/SkShader.cpp
diff --git a/src/core/SkShader.cpp b/src/core/SkShader.cpp
index b06e0c26ff6391e3125d21d847746b05d76c7aa0..24ac4ef02b057ca93f0747b594b0b690ea1ad9f1 100644
--- a/src/core/SkShader.cpp
+++ b/src/core/SkShader.cpp
@@ -15,7 +15,6 @@
SkShader::SkShader() {
fLocalMatrix.reset();
- SkDEBUGCODE(fInSetContext = false;)
}
SkShader::SkShader(SkReadBuffer& buffer)
@@ -25,12 +24,9 @@ SkShader::SkShader(SkReadBuffer& buffer)
} else {
fLocalMatrix.reset();
}
-
- SkDEBUGCODE(fInSetContext = false;)
}
SkShader::~SkShader() {
- SkASSERT(!fInSetContext);
}
void SkShader::flatten(SkWriteBuffer& buffer) const {
@@ -42,39 +38,46 @@ void SkShader::flatten(SkWriteBuffer& buffer) const {
}
}
-bool SkShader::setContext(const SkBitmap& device,
- const SkPaint& paint,
- const SkMatrix& matrix) {
- SkASSERT(!this->setContextHasBeenCalled());
-
+bool SkShader::computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInverse) const {
const SkMatrix* m = &matrix;
SkMatrix total;
- fPaintAlpha = paint.getAlpha();
if (this->hasLocalMatrix()) {
total.setConcat(matrix, this->getLocalMatrix());
m = &total;
}
- if (m->invert(&fTotalInverse)) {
- fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
- SkDEBUGCODE(fInSetContext = true;)
- return true;
- }
- return false;
+
+ return m->invert(totalInverse);
+}
+
+bool SkShader::validContext(const SkBitmap& device,
+ const SkPaint& paint,
+ const SkMatrix& matrix,
+ SkMatrix* totalInverse) const {
+ return this->computeTotalInverse(matrix, totalInverse);
}
-void SkShader::endContext() {
- SkASSERT(fInSetContext);
- SkDEBUGCODE(fInSetContext = false;)
+SkShader::Context::Context(const SkShader& shader, const SkBitmap& device,
+ const SkPaint& paint, const SkMatrix& matrix)
+ : fShader(shader)
+{
+ SkASSERT(fShader.validContext(device, paint, matrix));
+
+ SkAssertResult(fShader.computeTotalInverse(matrix, &fTotalInverse));
scroggo 2014/03/28 20:50:08 This looks much better. A comment would be nice, e
Dominik Grewe 2014/04/01 10:49:55 Done.
+ fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
+
+ fPaintAlpha = paint.getAlpha();
}
-SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) {
+SkShader::Context::~Context() {}
+
+SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
return NULL;
}
#include "SkColorPriv.h"
-void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
+void SkShader::Context::shadeSpan16(int x, int y, uint16_t span16[], int count) {
SkASSERT(span16);
SkASSERT(count > 0);
SkASSERT(this->canCallShadeSpan16());
@@ -92,7 +95,7 @@ void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) {
#define SkU32BitShiftToByteOffset(shift) ((shift) >> 3)
#endif
-void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
+void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
SkASSERT(count > 0);
SkPMColor colors[kTempColorCount];
@@ -146,7 +149,8 @@ void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
#endif
}
-SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
+SkShader::Context::MatrixClass
+SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
MatrixClass mc = kLinear_MatrixClass;
if (mat.hasPerspective()) {
@@ -161,8 +165,7 @@ SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) {
//////////////////////////////////////////////////////////////////////////////
-SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
- TileMode*) const {
+SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) const {
return kNone_BitmapType;
}
@@ -193,19 +196,16 @@ void SkShader::toString(SkString* str) const {
#include "SkColorShader.h"
#include "SkUtils.h"
-SkColorShader::SkColorShader() {
- fFlags = 0;
- fInheritColor = true;
+SkColorShader::SkColorShader()
+ : fColor()
+ , fInheritColor(true) {
}
-SkColorShader::SkColorShader(SkColor c) {
- fFlags = 0;
- fColor = c;
- fInheritColor = false;
+SkColorShader::SkColorShader(SkColor c)
+ : fColor(c)
+ , fInheritColor(false) {
}
-SkColorShader::~SkColorShader() {}
-
bool SkColorShader::isOpaque() const {
if (fInheritColor) {
return true; // using paint's alpha
@@ -213,14 +213,11 @@ bool SkColorShader::isOpaque() const {
return SkColorGetA(fColor) == 255;
}
-SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) {
- fFlags = 0; // computed in setContext
-
- fInheritColor = b.readBool();
- if (fInheritColor) {
- return;
+SkFlattenable* SkColorShader::CreateProc(SkReadBuffer& b) {
+ if (b.readBool()) {
+ return SkNEW(SkColorShader);
}
- fColor = b.readColor();
+ return SkNEW_ARGS(SkColorShader, (b.readColor()));
}
void SkColorShader::flatten(SkWriteBuffer& buffer) const {
@@ -232,32 +229,43 @@ void SkColorShader::flatten(SkWriteBuffer& buffer) const {
buffer.writeColor(fColor);
}
-uint32_t SkColorShader::getFlags() {
+uint32_t SkColorShader::ColorShaderContext::getFlags() const {
return fFlags;
}
-uint8_t SkColorShader::getSpan16Alpha() const {
+uint8_t SkColorShader::ColorShaderContext::getSpan16Alpha() const {
return SkGetPackedA32(fPMColor);
}
-bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
- const SkMatrix& matrix) {
- if (!this->INHERITED::setContext(device, paint, matrix)) {
- return false;
+SkShader::Context* SkColorShader::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, ColorShaderContext, (*this, device, paint, matrix));
+}
+
+SkColorShader::ColorShaderContext::ColorShaderContext(const SkColorShader& shader,
+ const SkBitmap& device,
+ const SkPaint& paint,
+ const SkMatrix& matrix)
+ : INHERITED(shader, device, paint, matrix)
+{
unsigned a;
- if (fInheritColor) {
- fColor = paint.getColor();
- a = SkColorGetA(fColor);
+ SkColor color;
+ if (shader.fInheritColor) {
+ color = paint.getColor();
+ a = SkColorGetA(color);
} else {
- a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha()));
+ color = shader.fColor;
+ a = SkAlphaMul(SkColorGetA(color), SkAlpha255To256(paint.getAlpha()));
}
- unsigned r = SkColorGetR(fColor);
- unsigned g = SkColorGetG(fColor);
- unsigned b = SkColorGetB(fColor);
+ unsigned r = SkColorGetR(color);
+ unsigned g = SkColorGetG(color);
+ unsigned b = SkColorGetB(color);
// we want this before we apply any alpha
fColor16 = SkPack888ToRGB16(r, g, b);
@@ -276,19 +284,17 @@ bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint,
fFlags |= kHasSpan16_Flag;
}
}
-
- return true;
}
-void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
+void SkColorShader::ColorShaderContext::shadeSpan(int x, int y, SkPMColor span[], int count) {
sk_memset32(span, fPMColor, count);
}
-void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
+void SkColorShader::ColorShaderContext::shadeSpan16(int x, int y, uint16_t span[], int count) {
sk_memset16(span, fColor16, count);
}
-void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
+void SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
memset(alpha, SkGetPackedA32(fPMColor), count);
}
@@ -330,24 +336,6 @@ void SkColorShader::toString(SkString* str) const {
#include "SkEmptyShader.h"
-uint32_t SkEmptyShader::getFlags() { return 0; }
-uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; }
-
-bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&,
- const SkMatrix&) { return false; }
-
-void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
- SkDEBUGFAIL("should never get called, since setContext() returned false");
-}
-
-void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
- SkDEBUGFAIL("should never get called, since setContext() returned false");
-}
-
-void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
- SkDEBUGFAIL("should never get called, since setContext() returned false");
-}
-
#ifdef SK_DEVELOPER
void SkEmptyShader::toString(SkString* str) const {
str->append("SkEmptyShader: (");

Powered by Google App Engine
This is Rietveld 408576698