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