| Index: include/core/SkShader.h
|
| diff --git a/include/core/SkShader.h b/include/core/SkShader.h
|
| index d0fac49f7e22cc30cbcb847c1538aada687fa18e..72bb9abed7811f3561d1594150eefdf41bf1f725 100644
|
| --- a/include/core/SkShader.h
|
| +++ b/include/core/SkShader.h
|
| @@ -6,8 +6,8 @@
|
| */
|
|
|
|
|
| -#ifndef SkShader_DEFINED
|
| -#define SkShader_DEFINED
|
| +#ifndef SkShaderGenerator_DEFINED
|
| +#define SkShaderGenerator_DEFINED
|
|
|
| #include "SkBitmap.h"
|
| #include "SkFlattenable.h"
|
| @@ -19,7 +19,7 @@ class SkPath;
|
| class GrContext;
|
| class GrEffectRef;
|
|
|
| -/** \class SkShader
|
| +/** \class SkShaderGenerator
|
| *
|
| * Shaders specify the source color(s) for what is being drawn. If a paint
|
| * has no shader, then the paint's color is used. If the paint has a
|
| @@ -28,32 +28,41 @@ class GrEffectRef;
|
| * once (e.g. bitmap tiling or gradient) and then change its transparency
|
| * w/o having to modify the original shader... only the paint's alpha needs
|
| * to be modified.
|
| + *
|
| + * FIXME: SkShader is transitioning to a model where it is actually a const
|
| + * generator of shaders.
|
| */
|
| -class SK_API SkShader : public SkFlattenable {
|
| +class SK_API SkShaderGenerator : public SkFlattenable {
|
| public:
|
| - SK_DECLARE_INST_COUNT(SkShader)
|
| + SK_DECLARE_INST_COUNT(SkShaderGenerator)
|
|
|
| - SkShader();
|
| - virtual ~SkShader();
|
| + SkShaderGenerator();
|
| + virtual ~SkShaderGenerator();
|
|
|
| /**
|
| - * Returns true if the local matrix is not an identity matrix.
|
| + * Returns true if the local matrix is not an identity matrix.
|
| + * FIXME: local matrix is moving to SkPaint.
|
| */
|
| bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
|
|
|
| /**
|
| * Returns the local matrix.
|
| + * FIXME: local matrix is moving to SkPaint.
|
| */
|
| const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
|
|
|
| /**
|
| * Set the shader's local matrix.
|
| * @param localM The shader's new local matrix.
|
| + *
|
| + * FIXME: local matrix is moving to SkPaint.
|
| */
|
| void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; }
|
|
|
| /**
|
| * Reset the shader's local matrix to identity.
|
| + *
|
| + * FIXME: local matrix is moving to SkPaint.
|
| */
|
| void resetLocalMatrix() { fLocalMatrix.reset(); }
|
|
|
| @@ -95,7 +104,7 @@ public:
|
| */
|
| kIntrinsicly16_Flag = 0x04,
|
|
|
| - /** set (after setContext) if the spans only vary in X (const in Y).
|
| + /** set if the spans only vary in X (const in Y).
|
| e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
|
| that varies from left-to-right. This flag specifies this for
|
| shadeSpan().
|
| @@ -111,84 +120,112 @@ public:
|
| };
|
|
|
| /**
|
| - * Called sometimes before drawing with this shader. Return the type of
|
| - * alpha your shader will return. The default implementation returns 0.
|
| - * Your subclass should override if it can (even sometimes) report a
|
| - * non-zero value, since that will enable various blitters to perform
|
| - * faster.
|
| - */
|
| - virtual uint32_t getFlags() { return 0; }
|
| -
|
| - /**
|
| * Returns true if the shader is guaranteed to produce only opaque
|
| * colors, subject to the SkPaint using the shader to apply an opaque
|
| * alpha value. Subclasses should override this to allow some
|
| - * optimizations. isOpaque() can be called at any time, unlike getFlags,
|
| - * which only works properly when the context is set.
|
| + * optimizations.
|
| */
|
| virtual bool isOpaque() const { return false; }
|
|
|
| /**
|
| - * Return the alpha associated with the data returned by shadeSpan16(). If
|
| - * kHasSpan16_Flag is not set, this value is meaningless.
|
| - */
|
| - virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
|
| -
|
| - /**
|
| * Called once before drawing, with the current paint and device matrix.
|
| * Return true if your shader supports these parameters, or false if not.
|
| - * If false is returned, nothing will be drawn. If true is returned, then
|
| - * a balancing call to endContext() will be made before the next call to
|
| - * setContext.
|
| + * If false is returned, createShaderImpl will not be called.
|
| *
|
| - * Subclasses should be sure to call their INHERITED::setContext() if they
|
| - * override this method.
|
| + * Subclasses should be sure to call their INHERITED::validContext() if
|
| + * they override this method.
|
| + * FIXME: Make sure device is still needed.
|
| */
|
| - virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
|
| - const SkMatrix& matrix);
|
| + virtual bool validContext(const SkBitmap& device, const SkPaint& paint,
|
| + const SkMatrix& matrix) const;
|
|
|
| /**
|
| - * Assuming setContext returned true, endContext() will be called when
|
| - * the draw using the shader has completed. It is an error for setContext
|
| - * to be called twice w/o an intervening call to endContext().
|
| - *
|
| - * Subclasses should be sure to call their INHERITED::endContext() if they
|
| - * override this method.
|
| + * Called internally to create the actual object that does the shading.
|
| + * Only valid if validContext() returned true.
|
| + * Size of storage must be >= shaderImplSize.
|
| */
|
| - virtual void endContext();
|
| -
|
| - SkDEBUGCODE(bool setContextHasBeenCalled() const { return SkToBool(fInSetContext); })
|
| + virtual ShaderImpl* createShaderImpl(const SkBitmap& device,
|
| + const SkPaint& paint,
|
| + const SkMatrix& matrix,
|
| + void* storage) const = 0;
|
|
|
| /**
|
| - * Called for each span of the object being drawn. Your subclass should
|
| - * set the appropriate colors (with premultiplied alpha) that correspond
|
| - * to the specified device coordinates.
|
| + * Return the size of a ShaderImpl returned by createShaderImpl.
|
| */
|
| - virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
|
| + virtual size_t shaderImplSize() const = 0;
|
|
|
| - typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
|
| - virtual ShadeProc asAShadeProc(void** ctx);
|
| + class ShaderImpl : public SkNoncopyable {
|
| + public:
|
| + ShaderImpl(const SkShaderGenerator& shader, const SkBitmap& device,
|
| + const SkPaint& paint, const SkMatrix& matrix);
|
|
|
| - /**
|
| - * Called only for 16bit devices when getFlags() returns
|
| - * kOpaqueAlphaFlag | kHasSpan16_Flag
|
| - */
|
| - virtual void shadeSpan16(int x, int y, uint16_t[], int count);
|
| + virtual ~ShaderImpl();
|
|
|
| - /**
|
| - * Similar to shadeSpan, but only returns the alpha-channel for a span.
|
| - * The default implementation calls shadeSpan() and then extracts the alpha
|
| - * values from the returned colors.
|
| - */
|
| - virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
|
| + /**
|
| + * Called sometimes before drawing with this shader. Return the type of
|
| + * alpha your shader will return. The default implementation returns 0.
|
| + * Your subclass should override if it can (even sometimes) report a
|
| + * non-zero value, since that will enable various blitters to perform
|
| + * faster.
|
| + */
|
| + virtual uint32_t getFlags() { return 0; }
|
|
|
| - /**
|
| - * Helper function that returns true if this shader's shadeSpan16() method
|
| - * can be called.
|
| - */
|
| - bool canCallShadeSpan16() {
|
| - return SkShader::CanCallShadeSpan16(this->getFlags());
|
| - }
|
| + /**
|
| + * Return the alpha associated with the data returned by shadeSpan16(). If
|
| + * kHasSpan16_Flag is not set, this value is meaningless.
|
| + */
|
| + virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
|
| +
|
| + /**
|
| + * Called for each span of the object being drawn. Your subclass should
|
| + * set the appropriate colors (with premultiplied alpha) that correspond
|
| + * to the specified device coordinates.
|
| + */
|
| + virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
|
| +
|
| + typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
|
| + virtual ShadeProc asAShadeProc(void** ctx);
|
| +
|
| + /**
|
| + * Called only for 16bit devices when getFlags() returns
|
| + * kOpaqueAlphaFlag | kHasSpan16_Flag
|
| + */
|
| + virtual void shadeSpan16(int x, int y, uint16_t[], int count);
|
| +
|
| + /**
|
| + * Similar to shadeSpan, but only returns the alpha-channel for a span.
|
| + * The default implementation calls shadeSpan() and then extracts the alpha
|
| + * values from the returned colors.
|
| + */
|
| + virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
|
| +
|
| + /**
|
| + * Helper function that returns true if this shader's shadeSpan16() method
|
| + * can be called.
|
| + */
|
| + bool canCallShadeSpan16() {
|
| + return SkShaderGenerator::CanCallShadeSpan16(this->getFlags());
|
| + }
|
| +
|
| + protected:
|
| + // Reference to generator, so we don't have to dupe information.
|
| + const SkShaderGenerator& fShader;
|
| +
|
| + enum MatrixClass {
|
| + kLinear_MatrixClass, // no perspective
|
| + kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each scanline
|
| + kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
|
| + };
|
| + static MatrixClass ComputeMatrixClass(const SkMatrix&);
|
| +
|
| + uint8_t getPaintAlpha() const { return fPaintAlpha; }
|
| + const SkMatrix& getTotalInverse() const { return fTotalInverse; }
|
| + MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
|
| + private:
|
| + SkMatrix fTotalInverse;
|
| + uint8_t fPaintAlpha;
|
| + uint8_t fTotalInverseClass;
|
| + };
|
|
|
| /**
|
| * Helper to check the flags to know if it is legal to call shadeSpan16()
|
| @@ -319,9 +356,9 @@ public:
|
| /**
|
| * If the shader subclass has a GrEffect implementation, this resturns the effect to install.
|
| * The incoming color to the effect has r=g=b=a all extracted from the SkPaint's alpha.
|
| - * The output color should be the computed SkShader premul color modulated by the incoming
|
| + * The output color should be the computed SkShaderGenerator premul color modulated by the incoming
|
| * color. The GrContext may be used by the effect to create textures. The GPU device does not
|
| - * call setContext. Instead we pass the SkPaint here in case the shader needs paint info.
|
| + * call createShaderImpl. Instead we pass the SkPaint here in case the shader needs paint info.
|
| */
|
| virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) const;
|
|
|
| @@ -342,34 +379,20 @@ public:
|
| * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
|
| * @return Returns a new shader object. Note: this function never returns null.
|
| */
|
| - static SkShader* CreateBitmapShader(const SkBitmap& src,
|
| - TileMode tmx, TileMode tmy);
|
| + static SkShaderGenerator* CreateBitmapShader(const SkBitmap& src,
|
| + TileMode tmx, TileMode tmy);
|
|
|
| SkDEVCODE(virtual void toString(SkString* str) const;)
|
|
|
| - SK_DEFINE_FLATTENABLE_TYPE(SkShader)
|
| + SK_DEFINE_FLATTENABLE_TYPE(SkShaderGenerator)
|
|
|
| protected:
|
| - enum MatrixClass {
|
| - kLinear_MatrixClass, // no perspective
|
| - kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each scanline
|
| - kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
|
| - };
|
| - static MatrixClass ComputeMatrixClass(const SkMatrix&);
|
| -
|
| - // These can be called by your subclass after setContext() has been called
|
| - uint8_t getPaintAlpha() const { return fPaintAlpha; }
|
| - const SkMatrix& getTotalInverse() const { return fTotalInverse; }
|
| - MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
|
|
|
| - SkShader(SkReadBuffer& );
|
| + SkShaderGenerator(SkReadBuffer& );
|
| virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
|
| private:
|
| + // FIXME: Move to SkPaint.
|
| SkMatrix fLocalMatrix;
|
| - SkMatrix fTotalInverse;
|
| - uint8_t fPaintAlpha;
|
| - uint8_t fTotalInverseClass;
|
| - SkDEBUGCODE(SkBool8 fInSetContext;)
|
|
|
| typedef SkFlattenable INHERITED;
|
| };
|
|
|