1 /*
2  * Copyright 2012 Google Inc.
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 GrSWMaskHelper_DEFINED
9 #define GrSWMaskHelper_DEFINED
10 
11 #include "SkAutoPixmapStorage.h"
12 #include "SkDraw.h"
13 #include "SkMatrix.h"
14 #include "SkRasterClip.h"
15 #include "SkRegion.h"
16 #include "SkTypes.h"
17 
18 class GrShape;
19 class GrTextureProxy;
20 
21 /**
22  * The GrSWMaskHelper helps generate clip masks using the software rendering
23  * path. It is intended to be used as:
24  *
25  *   GrSWMaskHelper helper(context);
26  *   helper.init(...);
27  *
28  *      draw one or more paths/rects specifying the required boolean ops
29  *
30  *   toTextureProxy();   // to get it from the internal bitmap to the GPU
31  *
32  * The result of this process will be the final mask (on the GPU) in the
33  * upper left hand corner of the texture.
34  */
35 class GrSWMaskHelper : SkNoncopyable {
36 public:
37     GrSWMaskHelper(SkAutoPixmapStorage* pixels = nullptr)
38             : fPixels(pixels ? pixels : &fPixelsStorage) { }
39 
40     // set up the internal state in preparation for draws. Since many masks
41     // may be accumulated in the helper during creation, "resultBounds"
42     // allows the caller to specify the region of interest - to limit the
43     // amount of work.
44     bool init(const SkIRect& resultBounds);
45 
46     // Draw a single rect into the accumulation bitmap using the specified op
47     void drawRect(const SkRect& rect, const SkMatrix& matrix, SkRegion::Op op, GrAA, uint8_t alpha);
48 
49     // Draw a single path into the accumuation bitmap using the specified op
50     void drawShape(const GrShape&, const SkMatrix& matrix, SkRegion::Op op, GrAA, uint8_t alpha);
51 
52     sk_sp<GrTextureProxy> toTextureProxy(GrContext*, SkBackingFit fit);
53 
54     // Reset the internal bitmap
55     void clear(uint8_t alpha) {
56         fPixels->erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF));
57     }
58 
59 private:
60     SkVector             fTranslate;
61     SkAutoPixmapStorage* fPixels;
62     SkAutoPixmapStorage  fPixelsStorage;
63     SkDraw               fDraw;
64     SkRasterClip         fRasterClip;
65 
66     typedef SkNoncopyable INHERITED;
67 };
68 
69 #endif // GrSWMaskHelper_DEFINED
70