robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 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 "GrColor.h" |
| 12 | #include "GrMatrix.h" |
| 13 | #include "GrNoncopyable.h" |
| 14 | #include "SkBitmap.h" |
| 15 | #include "SkDraw.h" |
| 16 | #include "SkRasterClip.h" |
| 17 | #include "SkRegion.h" |
| 18 | |
| 19 | class GrAutoScratchTexture; |
| 20 | class GrContext; |
| 21 | class GrTexture; |
| 22 | class SkPath; |
| 23 | |
| 24 | /** |
| 25 | * The GrSWMaskHelper helps generate clip masks using the software rendering |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 26 | * path. It is intended to be used as: |
| 27 | * |
| 28 | * GrSWMaskHelper helper(context); |
| 29 | * helper.init(...); |
| 30 | * |
| 31 | * draw one or more paths/rects specifying the required boolean ops |
| 32 | * |
| 33 | * toTexture(); // to get it from the internal bitmap to the GPU |
| 34 | * |
| 35 | * The result of this process will be the final mask (on the GPU) in the |
| 36 | * upper left hand corner of the texture. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 37 | */ |
| 38 | class GrSWMaskHelper : public GrNoncopyable { |
| 39 | public: |
| 40 | GrSWMaskHelper(GrContext* context) |
| 41 | : fContext(context) { |
| 42 | } |
| 43 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 44 | // set up the internal state in preparation for draws. Since many masks |
| 45 | // may be accumulated in the helper during creation, "resultBounds" |
| 46 | // allows the caller to specify the region of interest - to limit the |
| 47 | // amount of work. |
| 48 | bool init(const GrIRect& resultBounds, const GrMatrix* matrix); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 49 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 50 | // Draw a single rect into the accumulation bitmap using the specified op |
| 51 | void draw(const GrRect& rect, SkRegion::Op op, |
| 52 | bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 53 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 54 | // Draw a single path into the accumuation bitmap using the specified op |
| 55 | void draw(const SkPath& path, SkRegion::Op op, |
| 56 | GrPathFill fill, bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 57 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 58 | // Helper function to get a scratch texture suitable for capturing the |
| 59 | // result (i.e., right size & format) |
| 60 | bool getTexture(GrAutoScratchTexture* texture); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 61 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 62 | // Move the mask generation results from the internal bitmap to the gpu. |
| 63 | // The space outside of the mask is cleared using "alpha" |
| 64 | void toTexture(GrTexture* texture, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 65 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 66 | // Reset the internal bitmap |
| 67 | void clear(uint8_t alpha) { |
| 68 | fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 69 | } |
| 70 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame^] | 71 | // Canonical usage utility that draws a single path and uploads it |
| 72 | // to the GPU. The result is returned in "result". |
| 73 | static bool DrawToTexture(GrContext* context, |
| 74 | const SkPath& path, |
| 75 | const GrIRect& resultBounds, |
| 76 | GrPathFill fill, |
| 77 | GrAutoScratchTexture* result, |
| 78 | bool antiAlias, |
| 79 | GrMatrix* matrix); |
| 80 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 81 | protected: |
| 82 | private: |
| 83 | GrContext* fContext; |
| 84 | GrMatrix fMatrix; |
| 85 | SkBitmap fBM; |
| 86 | SkDraw fDraw; |
| 87 | SkRasterClip fRasterClip; |
| 88 | |
| 89 | typedef GrNoncopyable INHERITED; |
| 90 | }; |
| 91 | |
robertphillips@google.com | fe65943 | 2012-06-28 01:01:53 +0000 | [diff] [blame] | 92 | #endif // GrSWMaskHelper_DEFINED |