blob: 71dec6357d9230308031d772040d16c7abb9d8b8 [file] [log] [blame]
robertphillips@google.com58b20212012-06-27 20:44:52 +00001/*
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.orga0b40282013-09-18 13:00:55 +000012#include "GrDrawState.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000013#include "SkBitmap.h"
14#include "SkDraw.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "SkMatrix.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016#include "SkRasterClip.h"
17#include "SkRegion.h"
krajcevski25a67bc2014-07-29 11:44:26 -070018#include "SkTextureCompressor.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000020
robertphillips@google.com58b20212012-06-27 20:44:52 +000021class GrContext;
22class GrTexture;
23class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000024class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000025class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000026
27/**
28 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000029 * 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.com58b20212012-06-27 20:44:52 +000040 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000041class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000042public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 GrSWMaskHelper(GrContext* context)
krajcevskib8ccc2f2014-08-07 08:15:14 -070044 : fContext(context)
45 , fCompressionMode(kNone_CompressionMode) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000046 }
47
robertphillips@google.com366f1c62012-06-29 21:38:47 +000048 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 // may be accumulated in the helper during creation, "resultBounds"
50 // allows the caller to specify the region of interest - to limit the
krajcevski71614ac2014-08-13 10:36:18 -070051 // amount of work. allowCompression should be set to false if you plan on using
52 // your own texture to draw into, and not a scratch texture via getTexture().
53 bool init(const SkIRect& resultBounds, const SkMatrix* matrix, bool allowCompression = true);
robertphillips@google.com58b20212012-06-27 20:44:52 +000054
robertphillips@google.com366f1c62012-06-29 21:38:47 +000055 // Draw a single rect into the accumulation bitmap using the specified op
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000056 void draw(const SkRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000057 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000058
robertphillips@google.com366f1c62012-06-29 21:38:47 +000059 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000060 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000061 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000062
robertphillips@google.com366f1c62012-06-29 21:38:47 +000063 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000064 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000065
jvanverthfa38a302014-10-06 05:59:05 -070066 // Convert mask generation results to a signed distance field
67 void toSDF(unsigned char* sdf);
68
robertphillips@google.com366f1c62012-06-29 21:38:47 +000069 // Reset the internal bitmap
70 void clear(uint8_t alpha) {
71 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000072 }
73
robertphillips@google.com366f1c62012-06-29 21:38:47 +000074 // Canonical usage utility that draws a single path and uploads it
jvanverthfa38a302014-10-06 05:59:05 -070075 // to the GPU. The result is returned.
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000076 static GrTexture* DrawPathMaskToTexture(GrContext* context,
77 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000078 const SkStrokeRec& stroke,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000079 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000080 bool antiAlias,
joshualitt8059eb92014-12-29 15:10:07 -080081 const SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000082
83 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000084 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000085 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000087 // geometry and the current drawState. The current drawState is altered to
88 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000089 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000090 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000092 // output of DrawPathMaskToTexture.
93 static void DrawToTargetWithPathMask(GrTexture* texture,
94 GrDrawTarget* target,
joshualitt9853cce2014-11-17 14:22:48 -080095 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -080096 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -080097 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000098 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000099
robertphillips@google.com58b20212012-06-27 20:44:52 +0000100private:
bsalomone3059732014-10-14 11:47:22 -0700101 // Helper function to get a scratch texture suitable for capturing the
102 // result (i.e., right size & format)
103 GrTexture* createTexture();
104
robertphillips@google.com58b20212012-06-27 20:44:52 +0000105 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000106 SkMatrix fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000107 SkBitmap fBM;
108 SkDraw fDraw;
109 SkRasterClip fRasterClip;
110
krajcevskib8ccc2f2014-08-07 08:15:14 -0700111 // This enum says whether or not we should compress the mask:
112 // kNone_CompressionMode: compression is not supported on this device.
113 // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
114 // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
115 enum CompressionMode {
116 kNone_CompressionMode,
117 kCompress_CompressionMode,
118 kBlitter_CompressionMode,
119 } fCompressionMode;
120
121 // This is the buffer into which we store our compressed data. This buffer is
122 // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
123 SkAutoMalloc fCompressedBuffer;
krajcevskib3abe902014-07-30 13:08:11 -0700124
125 // This is the desired format within which to compress the
krajcevskib8ccc2f2014-08-07 08:15:14 -0700126 // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
krajcevski25a67bc2014-07-29 11:44:26 -0700127 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700128
krajcevskifb4f5cb2014-06-12 09:20:38 -0700129 // Actually sends the texture data to the GPU. This is called from
130 // toTexture with the data filled in depending on the texture config.
bsalomonf2703d82014-10-28 14:33:06 -0700131 void sendTextureData(GrTexture *texture, const GrSurfaceDesc& desc,
bsalomonef3fcd82014-12-12 08:51:38 -0800132 const void *data, size_t rowbytes);
krajcevskifb4f5cb2014-06-12 09:20:38 -0700133
krajcevskib577c552014-07-16 13:26:43 -0700134 // Compresses the bitmap stored in fBM and sends the compressed data
135 // to the GPU to be stored in 'texture' using sendTextureData.
bsalomonf2703d82014-10-28 14:33:06 -0700136 void compressTextureData(GrTexture *texture, const GrSurfaceDesc& desc);
krajcevskib577c552014-07-16 13:26:43 -0700137
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000138 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000139};
140
robertphillips@google.comfe659432012-06-28 01:01:53 +0000141#endif // GrSWMaskHelper_DEFINED