michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkShader_DEFINED michael@0: #define SkShader_DEFINED michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkFlattenable.h" michael@0: #include "SkMask.h" michael@0: #include "SkMatrix.h" michael@0: #include "SkPaint.h" michael@0: michael@0: class SkPath; michael@0: class GrContext; michael@0: class GrEffectRef; michael@0: michael@0: /** \class SkShader michael@0: * michael@0: * Shaders specify the source color(s) for what is being drawn. If a paint michael@0: * has no shader, then the paint's color is used. If the paint has a michael@0: * shader, then the shader's color(s) are use instead, but they are michael@0: * modulated by the paint's alpha. This makes it easy to create a shader michael@0: * once (e.g. bitmap tiling or gradient) and then change its transparency michael@0: * w/o having to modify the original shader... only the paint's alpha needs michael@0: * to be modified. michael@0: */ michael@0: class SK_API SkShader : public SkFlattenable { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkShader) michael@0: michael@0: SkShader(); michael@0: virtual ~SkShader(); michael@0: michael@0: /** michael@0: * Returns true if the local matrix is not an identity matrix. michael@0: */ michael@0: bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); } michael@0: michael@0: /** michael@0: * Returns the local matrix. michael@0: */ michael@0: const SkMatrix& getLocalMatrix() const { return fLocalMatrix; } michael@0: michael@0: /** michael@0: * Set the shader's local matrix. michael@0: * @param localM The shader's new local matrix. michael@0: */ michael@0: void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; } michael@0: michael@0: /** michael@0: * Reset the shader's local matrix to identity. michael@0: */ michael@0: void resetLocalMatrix() { fLocalMatrix.reset(); } michael@0: michael@0: enum TileMode { michael@0: /** replicate the edge color if the shader draws outside of its michael@0: * original bounds michael@0: */ michael@0: kClamp_TileMode, michael@0: michael@0: /** repeat the shader's image horizontally and vertically */ michael@0: kRepeat_TileMode, michael@0: michael@0: /** repeat the shader's image horizontally and vertically, alternating michael@0: * mirror images so that adjacent images always seam michael@0: */ michael@0: kMirror_TileMode, michael@0: michael@0: #if 0 michael@0: /** only draw within the original domain, return 0 everywhere else */ michael@0: kDecal_TileMode, michael@0: #endif michael@0: michael@0: kTileModeCount michael@0: }; michael@0: michael@0: // override these in your subclass michael@0: michael@0: enum Flags { michael@0: //!< set if all of the colors will be opaque michael@0: kOpaqueAlpha_Flag = 0x01, michael@0: michael@0: //! set if this shader's shadeSpan16() method can be called michael@0: kHasSpan16_Flag = 0x02, michael@0: michael@0: /** Set this bit if the shader's native data type is instrinsically 16 michael@0: bit, meaning that calling the 32bit shadeSpan() entry point will michael@0: mean the the impl has to up-sample 16bit data into 32bit. Used as a michael@0: a means of clearing a dither request if the it will have no effect michael@0: */ michael@0: kIntrinsicly16_Flag = 0x04, michael@0: michael@0: /** set (after setContext) if the spans only vary in X (const in Y). michael@0: e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient michael@0: that varies from left-to-right. This flag specifies this for michael@0: shadeSpan(). michael@0: */ michael@0: kConstInY32_Flag = 0x08, michael@0: michael@0: /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16 michael@0: which may not always be the case, since shadeSpan16 may be michael@0: predithered, which would mean it was not const in Y, even though michael@0: the 32bit shadeSpan() would be const. michael@0: */ michael@0: kConstInY16_Flag = 0x10 michael@0: }; michael@0: michael@0: /** michael@0: * Called sometimes before drawing with this shader. Return the type of michael@0: * alpha your shader will return. The default implementation returns 0. michael@0: * Your subclass should override if it can (even sometimes) report a michael@0: * non-zero value, since that will enable various blitters to perform michael@0: * faster. michael@0: */ michael@0: virtual uint32_t getFlags() { return 0; } michael@0: michael@0: /** michael@0: * Returns true if the shader is guaranteed to produce only opaque michael@0: * colors, subject to the SkPaint using the shader to apply an opaque michael@0: * alpha value. Subclasses should override this to allow some michael@0: * optimizations. isOpaque() can be called at any time, unlike getFlags, michael@0: * which only works properly when the context is set. michael@0: */ michael@0: virtual bool isOpaque() const { return false; } michael@0: michael@0: /** michael@0: * Return the alpha associated with the data returned by shadeSpan16(). If michael@0: * kHasSpan16_Flag is not set, this value is meaningless. michael@0: */ michael@0: virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; } michael@0: michael@0: /** michael@0: * Called once before drawing, with the current paint and device matrix. michael@0: * Return true if your shader supports these parameters, or false if not. michael@0: * If false is returned, nothing will be drawn. If true is returned, then michael@0: * a balancing call to endContext() will be made before the next call to michael@0: * setContext. michael@0: * michael@0: * Subclasses should be sure to call their INHERITED::setContext() if they michael@0: * override this method. michael@0: */ michael@0: virtual bool setContext(const SkBitmap& device, const SkPaint& paint, michael@0: const SkMatrix& matrix); michael@0: michael@0: /** michael@0: * Assuming setContext returned true, endContext() will be called when michael@0: * the draw using the shader has completed. It is an error for setContext michael@0: * to be called twice w/o an intervening call to endContext(). michael@0: * michael@0: * Subclasses should be sure to call their INHERITED::endContext() if they michael@0: * override this method. michael@0: */ michael@0: virtual void endContext(); michael@0: michael@0: SkDEBUGCODE(bool setContextHasBeenCalled() const { return SkToBool(fInSetContext); }) michael@0: michael@0: /** michael@0: * Called for each span of the object being drawn. Your subclass should michael@0: * set the appropriate colors (with premultiplied alpha) that correspond michael@0: * to the specified device coordinates. michael@0: */ michael@0: virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; michael@0: michael@0: typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count); michael@0: virtual ShadeProc asAShadeProc(void** ctx); michael@0: michael@0: /** michael@0: * Called only for 16bit devices when getFlags() returns michael@0: * kOpaqueAlphaFlag | kHasSpan16_Flag michael@0: */ michael@0: virtual void shadeSpan16(int x, int y, uint16_t[], int count); michael@0: michael@0: /** michael@0: * Similar to shadeSpan, but only returns the alpha-channel for a span. michael@0: * The default implementation calls shadeSpan() and then extracts the alpha michael@0: * values from the returned colors. michael@0: */ michael@0: virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count); michael@0: michael@0: /** michael@0: * Helper function that returns true if this shader's shadeSpan16() method michael@0: * can be called. michael@0: */ michael@0: bool canCallShadeSpan16() { michael@0: return SkShader::CanCallShadeSpan16(this->getFlags()); michael@0: } michael@0: michael@0: /** michael@0: * Helper to check the flags to know if it is legal to call shadeSpan16() michael@0: */ michael@0: static bool CanCallShadeSpan16(uint32_t flags) { michael@0: return (flags & kHasSpan16_Flag) != 0; michael@0: } michael@0: michael@0: /** michael@0: Gives method bitmap should be read to implement a shader. michael@0: Also determines number and interpretation of "extra" parameters returned michael@0: by asABitmap michael@0: */ michael@0: enum BitmapType { michael@0: kNone_BitmapType, //