Home | History | Annotate | Download | only in effects
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkLayerRasterizer_DEFINED
     11 #define SkLayerRasterizer_DEFINED
     12 
     13 #include "SkRasterizer.h"
     14 #include "SkDeque.h"
     15 #include "SkScalar.h"
     16 
     17 class SkPaint;
     18 
     19 class SK_API SkLayerRasterizer : public SkRasterizer {
     20 public:
     21     virtual ~SkLayerRasterizer();
     22 
     23     class SK_API Builder {
     24     public:
     25         Builder();
     26         ~Builder();
     27 
     28         void addLayer(const SkPaint& paint) {
     29             this->addLayer(paint, 0, 0);
     30         }
     31 
     32         /**
     33           *  Add a new layer (above any previous layers) to the rasterizer.
     34           *  The layer will extract those fields that affect the mask from
     35           *  the specified paint, but will not retain a reference to the paint
     36           *  object itself, so it may be reused without danger of side-effects.
     37           */
     38         void addLayer(const SkPaint& paint, SkScalar dx, SkScalar dy);
     39 
     40         /**
     41           *  Pass queue of layers on to newly created layer rasterizer and return it. The builder
     42           *  *cannot* be used any more after calling this function. If no layers have been added,
     43           *  returns NULL.
     44           *
     45           *  The caller is responsible for calling unref() on the returned object, if non NULL.
     46           */
     47         SkLayerRasterizer* detachRasterizer();
     48 
     49         /**
     50           *  Create and return a new immutable SkLayerRasterizer that contains a shapshot of the
     51           *  layers that were added to the Builder, without modifying the Builder. The Builder
     52           *  *may* be used after calling this function. It will continue to hold any layers
     53           *  previously added, so consecutive calls to this function will return identical objects,
     54           *  and objects returned by future calls to this function contain all the layers in
     55           *  previously returned objects. If no layers have been added, returns NULL.
     56           *
     57           *  Future calls to addLayer will not affect rasterizers previously returned by this call.
     58           *
     59           *  The caller is responsible for calling unref() on the returned object, if non NULL.
     60           */
     61         SkLayerRasterizer* snapshotRasterizer() const;
     62 
     63     private:
     64         SkDeque* fLayers;
     65     };
     66 
     67 #ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API
     68     void addLayer(const SkPaint& paint) {
     69         this->addLayer(paint, 0, 0);
     70     }
     71 
     72     /**    Add a new layer (above any previous layers) to the rasterizer.
     73         The layer will extract those fields that affect the mask from
     74         the specified paint, but will not retain a reference to the paint
     75         object itself, so it may be reused without danger of side-effects.
     76     */
     77     void addLayer(const SkPaint& paint, SkScalar dx, SkScalar dy);
     78 #endif
     79 
     80     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLayerRasterizer)
     81 
     82 protected:
     83     SkLayerRasterizer(SkDeque* layers);
     84     SkLayerRasterizer(SkReadBuffer&);
     85     virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
     86 
     87     // override from SkRasterizer
     88     virtual bool onRasterize(const SkPath& path, const SkMatrix& matrix,
     89                              const SkIRect* clipBounds,
     90                              SkMask* mask, SkMask::CreateMode mode) const;
     91 
     92 #ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API
     93 public:
     94 #endif
     95     SkLayerRasterizer();
     96 
     97 private:
     98 #ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API
     99     SkDeque* fLayers;
    100 #else
    101     const SkDeque* const fLayers;
    102 #endif
    103 
    104     static SkDeque* ReadLayers(SkReadBuffer& buffer);
    105 
    106     friend class LayerRasterizerTester;
    107 
    108     typedef SkRasterizer INHERITED;
    109 };
    110 
    111 #endif
    112