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