blob: f8cce8b27717f3d0af5163ff67b23873f5742c84 [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
21class GrAutoScratchTexture;
22class GrContext;
23class GrTexture;
24class SkPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025class SkStrokeRec;
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000026class GrDrawTarget;
robertphillips@google.com58b20212012-06-27 20:44:52 +000027
28/**
29 * The GrSWMaskHelper helps generate clip masks using the software rendering
robertphillips@google.com366f1c62012-06-29 21:38:47 +000030 * 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.com58b20212012-06-27 20:44:52 +000041 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000042class GrSWMaskHelper : SkNoncopyable {
robertphillips@google.com58b20212012-06-27 20:44:52 +000043public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000044 GrSWMaskHelper(GrContext* context)
krajcevskib8ccc2f2014-08-07 08:15:14 -070045 : fContext(context)
46 , fCompressionMode(kNone_CompressionMode) {
robertphillips@google.com58b20212012-06-27 20:44:52 +000047 }
48
robertphillips@google.com366f1c62012-06-29 21:38:47 +000049 // set up the internal state in preparation for draws. Since many masks
rmistry@google.comd6176b02012-08-23 18:14:13 +000050 // may be accumulated in the helper during creation, "resultBounds"
51 // allows the caller to specify the region of interest - to limit the
krajcevski71614ac2014-08-13 10:36:18 -070052 // amount of work. allowCompression should be set to false if you plan on using
53 // your own texture to draw into, and not a scratch texture via getTexture().
54 bool init(const SkIRect& resultBounds, const SkMatrix* matrix, bool allowCompression = true);
robertphillips@google.com58b20212012-06-27 20:44:52 +000055
robertphillips@google.com366f1c62012-06-29 21:38:47 +000056 // Draw a single rect into the accumulation bitmap using the specified op
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000057 void draw(const SkRect& rect, SkRegion::Op op,
robertphillips@google.com366f1c62012-06-29 21:38:47 +000058 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000059
robertphillips@google.com366f1c62012-06-29 21:38:47 +000060 // Draw a single path into the accumuation bitmap using the specified op
sugoi@google.com5f74cf82012-12-17 21:16:45 +000061 void draw(const SkPath& path, const SkStrokeRec& stroke, SkRegion::Op op,
sugoi@google.com12b4e272012-12-06 20:13:11 +000062 bool antiAlias, uint8_t alpha);
robertphillips@google.com58b20212012-06-27 20:44:52 +000063
robertphillips@google.com366f1c62012-06-29 21:38:47 +000064 // Helper function to get a scratch texture suitable for capturing the
65 // result (i.e., right size & format)
66 bool getTexture(GrAutoScratchTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000067
robertphillips@google.com366f1c62012-06-29 21:38:47 +000068 // Move the mask generation results from the internal bitmap to the gpu.
robertphillips@google.comd92cf2e2013-07-19 18:13:02 +000069 void toTexture(GrTexture* texture);
robertphillips@google.com58b20212012-06-27 20:44:52 +000070
robertphillips@google.com366f1c62012-06-29 21:38:47 +000071 // Reset the internal bitmap
72 void clear(uint8_t alpha) {
73 fBM.eraseColor(SkColorSetARGB(alpha, alpha, alpha, alpha));
robertphillips@google.com58b20212012-06-27 20:44:52 +000074 }
75
robertphillips@google.com366f1c62012-06-29 21:38:47 +000076 // Canonical usage utility that draws a single path and uploads it
77 // to the GPU. The result is returned in "result".
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000078 static GrTexture* DrawPathMaskToTexture(GrContext* context,
79 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000080 const SkStrokeRec& stroke,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000081 const SkIRect& resultBounds,
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000082 bool antiAlias,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000083 SkMatrix* matrix);
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000084
85 // This utility routine is used to add a path's mask to some other draw.
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 // The ClipMaskManager uses it to accumulate clip masks while the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000087 // GrSoftwarePathRenderer uses it to fulfill a drawPath call.
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 // It draws with "texture" as a path mask into "target" using "rect" as
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000089 // geometry and the current drawState. The current drawState is altered to
90 // accommodate the mask.
rmistry@google.comd6176b02012-08-23 18:14:13 +000091 // Note that this method assumes that the GrPaint::kTotalStages slot in
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000092 // the draw state can be used to hold the mask texture stage.
rmistry@google.comd6176b02012-08-23 18:14:13 +000093 // This method is really only intended to be used with the
robertphillips@google.com5dfb6722012-07-09 16:32:28 +000094 // output of DrawPathMaskToTexture.
95 static void DrawToTargetWithPathMask(GrTexture* texture,
96 GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000097 const SkIRect& rect);
robertphillips@google.com366f1c62012-06-29 21:38:47 +000098
robertphillips@google.com58b20212012-06-27 20:44:52 +000099private:
100 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000101 SkMatrix fMatrix;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000102 SkBitmap fBM;
103 SkDraw fDraw;
104 SkRasterClip fRasterClip;
105
krajcevskib8ccc2f2014-08-07 08:15:14 -0700106 // This enum says whether or not we should compress the mask:
107 // kNone_CompressionMode: compression is not supported on this device.
108 // kCompress_CompressionMode: compress the bitmap before it gets sent to the gpu
109 // kBlitter_CompressionMode: write to the bitmap using a special compressed blitter.
110 enum CompressionMode {
111 kNone_CompressionMode,
112 kCompress_CompressionMode,
113 kBlitter_CompressionMode,
114 } fCompressionMode;
115
116 // This is the buffer into which we store our compressed data. This buffer is
117 // only allocated (non-null) if fCompressionMode is kBlitter_CompressionMode
118 SkAutoMalloc fCompressedBuffer;
krajcevskib3abe902014-07-30 13:08:11 -0700119
120 // This is the desired format within which to compress the
krajcevskib8ccc2f2014-08-07 08:15:14 -0700121 // texture. This value is only valid if fCompressionMode is not kNone_CompressionMode.
krajcevski25a67bc2014-07-29 11:44:26 -0700122 SkTextureCompressor::Format fCompressedFormat;
krajcevski25a67bc2014-07-29 11:44:26 -0700123
krajcevskifb4f5cb2014-06-12 09:20:38 -0700124 // Actually sends the texture data to the GPU. This is called from
125 // toTexture with the data filled in depending on the texture config.
126 void sendTextureData(GrTexture *texture, const GrTextureDesc& desc,
127 const void *data, int rowbytes);
128
krajcevskib577c552014-07-16 13:26:43 -0700129 // Compresses the bitmap stored in fBM and sends the compressed data
130 // to the GPU to be stored in 'texture' using sendTextureData.
131 void compressTextureData(GrTexture *texture, const GrTextureDesc& desc);
132
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000133 typedef SkNoncopyable INHERITED;
robertphillips@google.com58b20212012-06-27 20:44:52 +0000134};
135
robertphillips@google.comfe659432012-06-28 01:01:53 +0000136#endif // GrSWMaskHelper_DEFINED