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" |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame^] | 18 | #include "GrDrawState.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | |
| 20 | class GrAutoScratchTexture; |
| 21 | class GrContext; |
| 22 | class GrTexture; |
| 23 | class SkPath; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame^] | 24 | class GrDrawTarget; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * The GrSWMaskHelper helps generate clip masks using the software rendering |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 28 | * path. It is intended to be used as: |
| 29 | * |
| 30 | * GrSWMaskHelper helper(context); |
| 31 | * helper.init(...); |
| 32 | * |
| 33 | * draw one or more paths/rects specifying the required boolean ops |
| 34 | * |
| 35 | * toTexture(); // to get it from the internal bitmap to the GPU |
| 36 | * |
| 37 | * The result of this process will be the final mask (on the GPU) in the |
| 38 | * upper left hand corner of the texture. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 39 | */ |
| 40 | class GrSWMaskHelper : public GrNoncopyable { |
| 41 | public: |
| 42 | GrSWMaskHelper(GrContext* context) |
| 43 | : fContext(context) { |
| 44 | } |
| 45 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 46 | // set up the internal state in preparation for draws. Since many masks |
| 47 | // may be accumulated in the helper during creation, "resultBounds" |
| 48 | // allows the caller to specify the region of interest - to limit the |
| 49 | // amount of work. |
| 50 | bool init(const GrIRect& resultBounds, const GrMatrix* matrix); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 51 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 52 | // Draw a single rect into the accumulation bitmap using the specified op |
| 53 | void draw(const GrRect& rect, SkRegion::Op op, |
| 54 | bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 55 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 56 | // Draw a single path into the accumuation bitmap using the specified op |
| 57 | void draw(const SkPath& path, SkRegion::Op op, |
| 58 | GrPathFill fill, bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 59 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 60 | // Helper function to get a scratch texture suitable for capturing the |
| 61 | // result (i.e., right size & format) |
| 62 | bool getTexture(GrAutoScratchTexture* texture); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 63 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 64 | // Move the mask generation results from the internal bitmap to the gpu. |
| 65 | // The space outside of the mask is cleared using "alpha" |
| 66 | void toTexture(GrTexture* texture, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 67 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 68 | // Reset the internal bitmap |
| 69 | void clear(uint8_t alpha) { |
| 70 | fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 73 | // Canonical usage utility that draws a single path and uploads it |
| 74 | // to the GPU. The result is returned in "result". |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame^] | 75 | static GrTexture* DrawPathMaskToTexture(GrContext* context, |
| 76 | const SkPath& path, |
| 77 | const GrIRect& resultBounds, |
| 78 | GrPathFill fill, |
| 79 | bool antiAlias, |
| 80 | GrMatrix* matrix); |
| 81 | |
| 82 | // This utility routine is used to add a path's mask to some other draw. |
| 83 | // The ClipMaskManager uses it to accumulate clip masks while the |
| 84 | // GrSoftwarePathRenderer uses it to fulfill a drawPath call. |
| 85 | // It draws with "texture" as a path mask into "target" using "rect" as |
| 86 | // geometry and the current drawState. The current drawState is altered to |
| 87 | // accommodate the mask. |
| 88 | // Note that this method assumes that the GrPaint::kTotalStages slot in |
| 89 | // the draw state can be used to hold the mask texture stage. |
| 90 | // This method is really only intended to be used with the |
| 91 | // output of DrawPathMaskToTexture. |
| 92 | static void DrawToTargetWithPathMask(GrTexture* texture, |
| 93 | GrDrawTarget* target, |
| 94 | GrDrawState::StageMask stageMask, |
| 95 | const GrIRect& rect); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 96 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 97 | protected: |
| 98 | private: |
| 99 | GrContext* fContext; |
| 100 | GrMatrix fMatrix; |
| 101 | SkBitmap fBM; |
| 102 | SkDraw fDraw; |
| 103 | SkRasterClip fRasterClip; |
| 104 | |
| 105 | typedef GrNoncopyable INHERITED; |
| 106 | }; |
| 107 | |
robertphillips@google.com | fe65943 | 2012-06-28 01:01:53 +0000 | [diff] [blame] | 108 | #endif // GrSWMaskHelper_DEFINED |