1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.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.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDrawLooper_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkDrawLooper_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkBlurTypes.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkFlattenable.h"
15daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkPoint.h"
16daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com#include "SkColor.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1873fe7b07e6391da81dfc48d600d7b3141be1f471Herb Derbyclass  SkArenaAlloc;
1973fe7b07e6391da81dfc48d600d7b3141be1f471Herb Derbyclass  SkCanvas;
20cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarettclass  SkColorSpaceXformer;
2173fe7b07e6391da81dfc48d600d7b3141be1f471Herb Derbyclass  SkPaint;
22c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.comstruct SkRect;
2373fe7b07e6391da81dfc48d600d7b3141be1f471Herb Derbyclass  SkString;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkDrawLooper
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Subclasses of SkDrawLooper can be attached to a SkPaint. Where they are,
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    and something is drawn to a canvas with that paint, the looper subclass will
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    be called, allowing it to modify the canvas and/or paint for that draw call.
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    More than that, via the next() method, the looper can modify the draw to be
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    invoked multiple times (hence the name loop-er), allow it to perform effects
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    like shadows or frame/fills, that require more than one pass.
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
337ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkDrawLooper : public SkFlattenable {
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
354e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
3679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Holds state during a draw. Users call next() until it returns false.
3779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *
3879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Subclasses of SkDrawLooper should create a subclass of this object to
3979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  hold state specific to their subclass.
404e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
41e3beb6bd7de7fa211681abbb0be58e80b19885e0commit-bot@chromium.org    class SK_API Context : ::SkNoncopyable {
4279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    public:
4379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        Context() {}
4479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual ~Context() {}
4579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org
4679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        /**
4779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Called in a loop on objects returned by SkDrawLooper::createContext().
4879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  Each time true is returned, the object is drawn (possibly with a modified
4979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas and/or paint). When false is finally returned, drawing for the object
5079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  stops.
5179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5279fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  On each call, the paint will be in its original state, but the
5379fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  canvas will be as it was following the previous call to next() or
5479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  createContext().
5579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *
5679fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  The implementation must ensure that, when next() finally returns
5779fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  false, the canvas has been restored to the state it was
5879fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         *  initially, before createContext() was first called.
5979fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org         */
6079fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org        virtual bool next(SkCanvas* canvas, SkPaint* paint) = 0;
6179fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org    };
624e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com
634e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com    /**
6479fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  Called right before something is being drawn. Returns a Context
6579fbb40bca9d815ef79b896b31ba6ee736817e0fcommit-bot@chromium.org     *  whose next() method should be called until it returns false.
664e2b3d3fb1288c6dc0f3ea1c0aa4a0d7c603bd7breed@google.com     */
6773fe7b07e6391da81dfc48d600d7b3141be1f471Herb Derby    virtual Context* makeContext(SkCanvas*, SkArenaAlloc*) const = 0;
68fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
699efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com    /**
709efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * The fast bounds functions are used to enable the paint to be culled early
719efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * in the drawing pipeline. If a subclass can support this feature it must
729efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * return true for the canComputeFastBounds() function.  If that function
739efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * returns false then computeFastBounds behavior is undefined otherwise it
749efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * is expected to have the following behavior. Given the parent paint and
759efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * the parent's bounding rect the subclass must fill in and return the
769efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * storage rect, where the storage rect is with the union of the src rect
779efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     * and the looper's bounding rect.
789efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com     */
7905d611574ac7dd07c055c9a7426199b804e6061fmtklein    bool canComputeFastBounds(const SkPaint& paint) const;
8005d611574ac7dd07c055c9a7426199b804e6061fmtklein    void computeFastBounds(const SkPaint& paint, const SkRect& src, SkRect* dst) const;
819efd9a048aebaa6681afb76b18e1a7dd642078d3reed@google.com
82daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    struct BlurShadowRec {
83daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkScalar        fSigma;
84daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkVector        fOffset;
85daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkColor         fColor;
86daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurStyle     fStyle;
87daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com        SkBlurQuality   fQuality;
88daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    };
89daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    /**
90daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If this looper can be interpreted as having two layers, such that
91daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      1. The first layer (bottom most) just has a blur and translate
92daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      2. The second layer has no modifications to either paint or canvas
93daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *      3. No other layers.
94daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  then return true, and if not null, fill out the BlurShadowRec).
95daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *
96daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     *  If any of the above are not met, return false and ignore the BlurShadowRec parameter.
97daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com     */
98daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com    virtual bool asABlurShadow(BlurShadowRec*) const;
99daaafa6e81860e3dc52660ba019c336f0a43f1e7reed@google.com
1000f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_PUREVIRT()
1013b37545bc594a96de45eba62dea0ce478750f2a9mtklein    SK_DEFINE_FLATTENABLE_TYPE(SkDrawLooper)
1024991b8f23482afc1494fd17647421ce68de53331robertphillips@google.com
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
104cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett    sk_sp<SkDrawLooper> makeColorSpace(SkColorSpaceXformer* xformer) const {
105cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett        return this->onMakeColorSpace(xformer);
106cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett    }
107cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett    virtual sk_sp<SkDrawLooper> onMakeColorSpace(SkColorSpaceXformer*) const = 0;
108cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkDrawLooper() {}
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
112cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett    friend class SkColorSpaceXformer;
113cdc651d29d88e87ef84d5a586879db2c16ff4579Matt Sarett
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFlattenable INHERITED;
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
118