SkLayerDrawLooper.h revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkLayerDrawLooper_DEFINED
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkLayerDrawLooper_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDrawLooper.h"
120716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com#include "SkXfermode.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct SkPoint;
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168c3ff17e2cab6f7c798b9f8ff4515c4a3d3fd9d1bsalomon@google.comclass SK_API SkLayerDrawLooper : public SkDrawLooper {
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkLayerDrawLooper();
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual ~SkLayerDrawLooper();
200e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
210716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    /**
220716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Bits specifies which aspects of the layer's paint should replace the
230716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  corresponding aspects on the draw's paint.
240716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  kEntirePaint_Bits means use the layer's paint completely.
250716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  0 means ignore the layer's paint.
260716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     */
270e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    enum Bits {
280716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kStyle_Bit      = 1 << 0,   //!< use this layer's Style/stroke settings
290716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kTextSkewX_Bit  = 1 << 1,   //!< use this layer's textskewx
300716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kPathEffect_Bit = 1 << 2,   //!< use this layer's patheffect
310716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kMaskFilter_Bit = 1 << 3,   //!< use this layer's maskfilter
320716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kShader_Bit     = 1 << 4,   //!< use this layer's shader
330716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kColorFilter_Bit = 1 << 5,  //!< use this layer's colorfilter
340716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        kXfermode_Bit   = 1 << 6,   //!< use this layer's xfermode
350e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
360e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org        kEntirePaint_Bits = -1,      //!< use this layer's paint entirely
370e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    };
380e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    typedef int32_t BitFlags;
390716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
400716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    /**
410716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Info for how to apply the layer's paint and offset.
420716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *
43a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *  fFlagsMask selects which flags in the layer's paint should be applied.
44a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *      result = (draw-flags & ~fFlagsMask) | (layer-flags & fFlagsMask)
45a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *  In the extreme:
46a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *      If fFlagsMask is 0, we ignore all of the layer's flags
47a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *      If fFlagsMask is -1, we use all of the layer's flags
48a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *
490716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  fColorMode controls how we compute the final color for the layer:
500716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      The layer's paint's color is treated as the SRC
510716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      The draw's paint's color is treated as the DST
520716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      final-color = Mode(layers-color, draws-color);
530716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *  Any SkXfermode::Mode will work. Two common choices are:
540716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      kSrc_Mode: to use the layer's color, ignoring the draw's
550716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     *      kDst_Mode: to just keep the draw's color, ignoring the layer's
560716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     */
578c3ff17e2cab6f7c798b9f8ff4515c4a3d3fd9d1bsalomon@google.com    struct SK_API LayerInfo {
58a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org        uint32_t            fFlagsMask; // SkPaint::Flags
590716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        BitFlags            fPaintBits;
600716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        SkXfermode::Mode    fColorMode;
610716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        SkVector            fOffset;
620716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        bool                fPostTranslate; //!< applies to fOffset
630716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
640716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        /**
650716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *  Initial the LayerInfo. Defaults to settings that will draw the
660716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *  layer with no changes: e.g.
670716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fPaintBits == 0
680716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fColorMode == kDst_Mode
690716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         *      fOffset == (0, 0)
700716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com         */
710716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        LayerInfo();
720716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    };
730716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
740e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    /**
750e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org     *  Call for each layer you want to add (from top to bottom).
760e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org     *  This returns a paint you can modify, but that ptr is only valid until
77a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *  the next call made to addLayer().
780716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com     */
790716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    SkPaint* addLayer(const LayerInfo&);
800716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
810716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com    /**
82a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *  This layer will draw with the original paint, ad the specified offset
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
84a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    void addLayer(SkScalar dx, SkScalar dy);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
860e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org    /**
87a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org     *  This layer will with the original paint and no offset.
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
89a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    void addLayer() { this->addLayer(0, 0); }
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // overrides from SkDrawLooper
924e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    virtual void init(SkCanvas*);
934e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    virtual bool next(SkCanvas*, SkPaint* paint);
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // must be public for Registrar :(
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return SkNEW_ARGS(SkLayerDrawLooper, (buffer));
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkLayerDrawLooper(SkFlattenableReadBuffer&);
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // overrides from SkFlattenable
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual void flatten(SkFlattenableWriteBuffer& );
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual Factory getFactory() { return CreateProc; }
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    struct Rec {
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        Rec*    fNext;
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint fPaint;
1110716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com        LayerInfo fInfo;
1120716c63332a64c3cc77a9afb87ae2fd9614f0c4freed@google.com
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        static Rec* Reverse(Rec*);
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Rec*    fRecs;
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int     fCount;
1174e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com
1184e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    // state-machine during the init/next cycle
1194e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    Rec* fCurrRec;
1200e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
121a8282ef8f9e63e60c5665af0cdfc8fdd11c089f6mike@reedtribe.org    static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
1220e2810be95d3f1aa95c341521d3f514eb9e9ebdemike@reedtribe.org
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    class MyRegistrar : public SkFlattenable::Registrar {
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public:
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        MyRegistrar();
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkDrawLooper INHERITED;
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
133