1 /*
2  * Copyright 2013 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 SkPictureImageFilter_DEFINED
9 #define SkPictureImageFilter_DEFINED
10 
11 #include "SkImageFilter.h"
12 #include "SkPicture.h"
13 
14 class SK_API SkPictureImageFilter : public SkImageFilter {
15 public:
16     /**
17      *  Refs the passed-in picture.
18      */
19     static sk_sp<SkImageFilter> Make(sk_sp<SkPicture> picture);
20 
21     /**
22      *  Refs the passed-in picture. cropRect can be used to crop or expand the destination rect when
23      *  the picture is drawn. (No scaling is implied by the dest rect; only the CTM is applied.)
24      */
25     static sk_sp<SkImageFilter> Make(sk_sp<SkPicture> picture, const SkRect& cropRect);
26 
27     /**
28      *  Refs the passed-in picture. The picture is rasterized at a resolution that matches the
29      *  local coordinate space. If the picture needs to be resampled for drawing it into the
30      *  destination canvas, bilinear filtering will be used. cropRect can be used to crop or
31      *  expand the destination rect when the picture is drawn. (No scaling is implied by the
32      *  dest rect; only the CTM is applied.)
33      */
34     static sk_sp<SkImageFilter> MakeForLocalSpace(sk_sp<SkPicture> picture,
35                                                   const SkRect& cropRect,
36                                                   SkFilterQuality filterQuality);
37 
38     SK_TO_STRING_OVERRIDE()
39     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureImageFilter)
40 
41 protected:
42     enum PictureResolution {
43         kDeviceSpace_PictureResolution,
44         kLocalSpace_PictureResolution
45     };
46 
47     /*  Constructs an SkPictureImageFilter object from an SkReadBuffer.
48      *  Note: If the SkPictureImageFilter object construction requires bitmap
49      *  decoding, the decoder must be set on the SkReadBuffer parameter by calling
50      *  SkReadBuffer::setBitmapDecoder() before calling this constructor.
51      *  @param SkReadBuffer Serialized picture data.
52      */
53     void flatten(SkWriteBuffer&) const override;
54     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
55                                         SkIPoint* offset) const override;
56 
57 private:
58     explicit SkPictureImageFilter(sk_sp<SkPicture> picture);
59     SkPictureImageFilter(sk_sp<SkPicture> picture, const SkRect& cropRect,
60                          PictureResolution, SkFilterQuality);
61 
62     void drawPictureAtDeviceResolution(SkCanvas* canvas,
63                                        const SkIRect& deviceBounds,
64                                        const Context&) const;
65     void drawPictureAtLocalResolution(SkSpecialImage* source,
66                                       SkCanvas*,
67                                       const SkIRect& deviceBounds,
68                                       const Context&) const;
69 
70     sk_sp<SkPicture>      fPicture;
71     SkRect                fCropRect;
72     PictureResolution     fPictureResolution;
73     SkFilterQuality       fFilterQuality;
74 
75     typedef SkImageFilter INHERITED;
76 };
77 
78 #endif
79