| 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" |
| krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 18 | #include "SkTextureCompressor.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 | |
| 21 | class GrAutoScratchTexture; |
| 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: |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 44 | GrSWMaskHelper(GrContext* context) |
| krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame^] | 45 | : fContext(context), fCompressMask(false) { |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 48 | // 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] | 49 | // may be accumulated in the helper during creation, "resultBounds" |
| 50 | // 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] | 51 | // amount of work. |
| reed@google.com | 84e922b | 2013-11-04 20:57:36 +0000 | [diff] [blame] | 52 | bool init(const SkIRect& resultBounds, const SkMatrix* matrix); |
| 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 rect into the accumulation bitmap using the specified op |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 55 | void draw(const SkRect& rect, SkRegion::Op op, |
| robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 56 | 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 | // 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] | 59 | void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op, |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 60 | bool antiAlias, uint8_t alpha); |
| 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 | // Helper function to get a scratch texture suitable for capturing the |
| 63 | // result (i.e., right size & format) |
| 64 | bool getTexture(GrAutoScratchTexture* texture); |
| 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 | // 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] | 67 | void toTexture(GrTexture* texture); |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 68 | |
| robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 69 | // Reset the internal bitmap |
| 70 | void clear(uint8_t alpha) { |
| 71 | fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha)); |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 74 | // Canonical usage utility that draws a single path and uploads it |
| 75 | // to the GPU. The result is returned in "result". |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 76 | static GrTexture* DrawPathMaskToTexture(GrContext* context, |
| 77 | const SkPath& path, |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 78 | const SkStrokeRec& stroke, |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 79 | const SkIRect& resultBounds, |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 80 | bool antiAlias, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 81 | SkMatrix* matrix); |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 82 | |
| 83 | // 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] | 84 | // The ClipMaskManager uses it to accumulate clip masks while the |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 85 | // GrSoftwarePathRenderer uses it to fulfill a drawPath call. |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 86 | // 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] | 87 | // geometry and the current drawState. The current drawState is altered to |
| 88 | // accommodate the mask. |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | // Note that this method assumes that the GrPaint::kTotalStages slot in |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 90 | // 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] | 91 | // This method is really only intended to be used with the |
| robertphillips@google.com | 5dfb672 | 2012-07-09 16:32:28 +0000 | [diff] [blame] | 92 | // output of DrawPathMaskToTexture. |
| 93 | static void DrawToTargetWithPathMask(GrTexture* texture, |
| 94 | GrDrawTarget* target, |
| 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 | protected: |
| 98 | private: |
| 99 | GrContext* fContext; |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 100 | SkMatrix fMatrix; |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 101 | SkBitmap fBM; |
| 102 | SkDraw fDraw; |
| 103 | SkRasterClip fRasterClip; |
| 104 | |
| krajcevski | b3abe90 | 2014-07-30 13:08:11 -0700 | [diff] [blame^] | 105 | // This flag says whether or not we should compress the mask. If |
| 106 | // it is true, then fCompressedFormat is always valid. |
| 107 | bool fCompressMask; |
| 108 | |
| 109 | // This is the desired format within which to compress the |
| 110 | // texture. This value is only valid if fCompressMask is true. |
| krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 111 | SkTextureCompressor::Format fCompressedFormat; |
| krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 112 | |
| krajcevski | fb4f5cb | 2014-06-12 09:20:38 -0700 | [diff] [blame] | 113 | // Actually sends the texture data to the GPU. This is called from |
| 114 | // toTexture with the data filled in depending on the texture config. |
| 115 | void sendTextureData(GrTexture *texture, const GrTextureDesc& desc, |
| 116 | const void *data, int rowbytes); |
| 117 | |
| krajcevski | b577c55 | 2014-07-16 13:26:43 -0700 | [diff] [blame] | 118 | // Compresses the bitmap stored in fBM and sends the compressed data |
| 119 | // to the GPU to be stored in 'texture' using sendTextureData. |
| 120 | void compressTextureData(GrTexture *texture, const GrTextureDesc& desc); |
| 121 | |
| commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 122 | typedef SkNoncopyable INHERITED; |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| robertphillips@google.com | fe65943 | 2012-06-28 01:01:53 +0000 | [diff] [blame] | 125 | #endif // GrSWMaskHelper_DEFINED |