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" |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 13 | #include "SkAutoPixmapStorage.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 | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 22 | class GrPaint; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 23 | class GrShape; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 24 | class GrStyle; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 25 | class GrTexture; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 26 | struct GrUserStencilSettings; |
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: |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 44 | GrSWMaskHelper() { } |
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 |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 53 | void drawRect(const SkRect& rect, SkRegion::Op op, GrAA, 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 |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 56 | void drawShape(const GrShape&, SkRegion::Op op, GrAA, uint8_t alpha); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 57 | |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 58 | sk_sp<GrTextureProxy> toTextureProxy(GrContext*, SkBackingFit fit); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 59 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 60 | // Convert mask generation results to a signed distance field |
| 61 | void toSDF(unsigned char* sdf); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 63 | // Reset the internal bitmap |
| 64 | void clear(uint8_t alpha) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 65 | fPixels.erase(SkColorSetARGB(alpha, 0xFF, 0xFF, 0xFF)); |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 66 | } |
| 67 | |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 68 | // Canonical usage utility that draws a single path and uploads it |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 69 | // to the GPU. The result is returned. |
Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 70 | static sk_sp<GrTextureProxy> DrawShapeMaskToTexture(GrContext*, |
| 71 | const GrShape&, |
| 72 | const SkIRect& resultBounds, |
| 73 | GrAA, |
| 74 | SkBackingFit, |
| 75 | const SkMatrix* matrix); |
robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 76 | |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 77 | // This utility draws a path mask generated by DrawShapeMaskToTexture using a provided paint. |
| 78 | // The rectangle is drawn in device space. The 'viewMatrix' will be used to ensure the correct |
| 79 | // local coords are provided to any fragment processors in the paint. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 80 | static void DrawToTargetWithShapeMask(sk_sp<GrTextureProxy>, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 81 | GrRenderTargetContext*, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 82 | GrPaint&& paint, |
robertphillips | d2b6d64 | 2016-07-21 08:55:08 -0700 | [diff] [blame] | 83 | const GrUserStencilSettings& userStencilSettings, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 84 | const GrClip&, |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 85 | const SkMatrix& viewMatrix, |
bsalomon | 39ef7fb | 2016-09-21 11:16:05 -0700 | [diff] [blame] | 86 | const SkIPoint& textureOriginInDeviceSpace, |
| 87 | const SkIRect& deviceSpaceRectToDraw); |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 88 | |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 89 | private: |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 90 | SkMatrix fMatrix; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 91 | SkAutoPixmapStorage fPixels; |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 92 | SkDraw fDraw; |
| 93 | SkRasterClip fRasterClip; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 95 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
robertphillips@google.com | fe65943 | 2012-06-28 01:01:53 +0000 | [diff] [blame] | 98 | #endif // GrSWMaskHelper_DEFINED |