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 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 11 | #include "SkAutoPixmapStorage.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 12 | #include "GrColor.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 13 | #include "GrPipelineBuilder.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 14 | #include "SkBitmap.h" |
| 15 | #include "SkDraw.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 16 | #include "SkMatrix.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 17 | #include "SkRasterClip.h" |
| 18 | #include "SkRegion.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 21 | class GrClip; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 22 | class GrContext; |
| 23 | class GrTexture; |
| 24 | class SkPath; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 25 | class SkStrokeRec; |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 26 | class GrDrawTarget; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * The GrSWMaskHelper helps generate clip masks using the software rendering |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 30 | * path. It is intended to be used as: |
| 31 | * |
| 32 | * GrSWMaskHelper helper(context); |
| 33 | * helper.init(...); |
| 34 | * |
| 35 | * draw one or more paths/rects specifying the required boolean ops |
| 36 | * |
| 37 | * toTexture(); // to get it from the internal bitmap to the GPU |
| 38 | * |
| 39 | * The result of this process will be the final mask (on the GPU) in the |
| 40 | * upper left hand corner of the texture. |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 41 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 42 | class GrSWMaskHelper : SkNoncopyable { |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 43 | public: |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 44 | GrSWMaskHelper(GrContext* context) : fContext(context) { } |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 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 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | // may be accumulated in the helper during creation, "resultBounds" |
| 48 | // allows the caller to specify the region of interest - to limit the |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 49 | // amount of work. |
| 50 | bool init(const SkIRect& resultBounds, const SkMatrix* 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 |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 53 | void drawRect(const SkRect& rect, SkRegion::Op op, bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 54 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 55 | // Draw a single path into the accumuation bitmap using the specified op |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 56 | void drawPath(const SkPath& path, const GrStyle& style, SkRegion::Op op, |
| 57 | bool antiAlias, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 58 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 59 | // 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] | 60 | void toTexture(GrTexture* texture); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 61 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 62 | // Convert mask generation results to a signed distance field |
| 63 | void toSDF(unsigned char* sdf); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 64 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 65 | // Reset the internal bitmap |
| 66 | void clear(uint8_t alpha) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 67 | fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 68 | } |
| 69 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 70 | // Canonical usage utility that draws a single path and uploads it |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 71 | // to the GPU. The result is returned. |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 72 | static GrTexture* DrawPathMaskToTexture(GrContext* context, |
| 73 | const SkPath& path, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 74 | const GrStyle& style, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 75 | const SkIRect& resultBounds, |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 76 | bool antiAlias, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 77 | const SkMatrix* matrix); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 78 | |
| 79 | // 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] | 80 | // The ClipMaskManager uses it to accumulate clip masks while the |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 81 | // GrSoftwarePathRenderer uses it to fulfill a drawPath call. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 82 | // 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] | 83 | // geometry and the current drawState. The current drawState is altered to |
| 84 | // accommodate the mask. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 85 | // Note that this method assumes that the GrPaint::kTotalStages slot in |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 86 | // 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] | 87 | // This method is really only intended to be used with the |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 88 | // output of DrawPathMaskToTexture. |
| 89 | static void DrawToTargetWithPathMask(GrTexture* texture, |
| 90 | GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 91 | GrPipelineBuilder* pipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 92 | const GrClip&, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 93 | GrColor, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 94 | const SkMatrix& viewMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 95 | const SkIRect& 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 | private: |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 98 | // Helper function to get a scratch texture suitable for capturing the |
| 99 | // result (i.e., right size & format) |
| 100 | GrTexture* createTexture(); |
| 101 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 102 | GrContext* fContext; |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 103 | SkMatrix fMatrix; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 104 | SkAutoPixmapStorage fPixels; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 105 | SkDraw fDraw; |
| 106 | SkRasterClip fRasterClip; |
| 107 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 108 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
robertphillips@google.com | fe65943 | 2012-06-28 01:01:53 +0000 | [diff] [blame] | 111 | #endif // GrSWMaskHelper_DEFINED |