Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/ShadowRRectOp.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkRRectPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrMemoryPool.h" |
| 13 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrProgramInfo.h" |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrThreadSafeCache.h" |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 18 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/effects/GrShadowGeoProc.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 20 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 21 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 22 | namespace { |
| 23 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 24 | /////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 25 | // Circle Data |
| 26 | // |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 27 | // We have two possible cases for geometry for a circle: |
| 28 | |
| 29 | // In the case of a normal fill, we draw geometry for the circle as an octagon. |
| 30 | static const uint16_t gFillCircleIndices[] = { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 31 | // enter the octagon |
| 32 | // clang-format off |
| 33 | 0, 1, 8, 1, 2, 8, |
| 34 | 2, 3, 8, 3, 4, 8, |
| 35 | 4, 5, 8, 5, 6, 8, |
| 36 | 6, 7, 8, 7, 0, 8, |
| 37 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // For stroked circles, we use two nested octagons. |
| 41 | static const uint16_t gStrokeCircleIndices[] = { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 42 | // enter the octagon |
| 43 | // clang-format off |
| 44 | 0, 1, 9, 0, 9, 8, |
| 45 | 1, 2, 10, 1, 10, 9, |
| 46 | 2, 3, 11, 2, 11, 10, |
| 47 | 3, 4, 12, 3, 12, 11, |
| 48 | 4, 5, 13, 4, 13, 12, |
| 49 | 5, 6, 14, 5, 14, 13, |
| 50 | 6, 7, 15, 6, 15, 14, |
| 51 | 7, 0, 8, 7, 8, 15, |
| 52 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static const int kIndicesPerFillCircle = SK_ARRAY_COUNT(gFillCircleIndices); |
| 56 | static const int kIndicesPerStrokeCircle = SK_ARRAY_COUNT(gStrokeCircleIndices); |
| 57 | static const int kVertsPerStrokeCircle = 16; |
| 58 | static const int kVertsPerFillCircle = 9; |
| 59 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 60 | int circle_type_to_vert_count(bool stroked) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 61 | return stroked ? kVertsPerStrokeCircle : kVertsPerFillCircle; |
| 62 | } |
| 63 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 64 | int circle_type_to_index_count(bool stroked) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 65 | return stroked ? kIndicesPerStrokeCircle : kIndicesPerFillCircle; |
| 66 | } |
| 67 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 68 | const uint16_t* circle_type_to_indices(bool stroked) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 69 | return stroked ? gStrokeCircleIndices : gFillCircleIndices; |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 73 | // RoundRect Data |
| 74 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 75 | // The geometry for a shadow roundrect is similar to a 9-patch: |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 76 | // ____________ |
| 77 | // |_|________|_| |
| 78 | // | | | | |
| 79 | // | | | | |
| 80 | // | | | | |
| 81 | // |_|________|_| |
| 82 | // |_|________|_| |
| 83 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 84 | // However, each corner is rendered as a fan rather than a simple quad, as below. (The diagram |
| 85 | // shows the upper part of the upper left corner. The bottom triangle would similarly be split |
| 86 | // into two triangles.) |
| 87 | // ________ |
| 88 | // |\ \ | |
| 89 | // | \ \ | |
| 90 | // | \\ | |
| 91 | // | \| |
| 92 | // -------- |
| 93 | // |
| 94 | // The center of the fan handles the curve of the corner. For roundrects where the stroke width |
| 95 | // is greater than the corner radius, the outer triangles blend from the curve to the straight |
| 96 | // sides. Otherwise these triangles will be degenerate. |
| 97 | // |
| 98 | // In the case where the stroke width is greater than the corner radius and the |
| 99 | // blur radius (overstroke), we add additional geometry to mark out the rectangle in the center. |
| 100 | // This rectangle extends the coverage values of the center edges of the 9-patch. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 101 | // ____________ |
| 102 | // |_|________|_| |
| 103 | // | |\ ____ /| | |
| 104 | // | | | | | | |
| 105 | // | | |____| | | |
| 106 | // |_|/______\|_| |
| 107 | // |_|________|_| |
| 108 | // |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 109 | // For filled rrects we reuse the stroke geometry but add an additional quad to the center. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 110 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 111 | static const uint16_t gRRectIndices[] = { |
| 112 | // clang-format off |
| 113 | // overstroke quads |
| 114 | // we place this at the beginning so that we can skip these indices when rendering as filled |
| 115 | 0, 6, 25, 0, 25, 24, |
| 116 | 6, 18, 27, 6, 27, 25, |
| 117 | 18, 12, 26, 18, 26, 27, |
| 118 | 12, 0, 24, 12, 24, 26, |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 119 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 120 | // corners |
| 121 | 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, |
| 122 | 6, 11, 10, 6, 10, 9, 6, 9, 8, 6, 8, 7, |
| 123 | 12, 17, 16, 12, 16, 15, 12, 15, 14, 12, 14, 13, |
| 124 | 18, 19, 20, 18, 20, 21, 18, 21, 22, 18, 22, 23, |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 125 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 126 | // edges |
| 127 | 0, 5, 11, 0, 11, 6, |
| 128 | 6, 7, 19, 6, 19, 18, |
| 129 | 18, 23, 17, 18, 17, 12, |
| 130 | 12, 13, 1, 12, 1, 0, |
| 131 | |
| 132 | // fill quad |
| 133 | // we place this at the end so that we can skip these indices when rendering as stroked |
| 134 | 0, 6, 18, 0, 18, 12, |
| 135 | // clang-format on |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 136 | }; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 137 | |
| 138 | // overstroke count |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 139 | static const int kIndicesPerOverstrokeRRect = SK_ARRAY_COUNT(gRRectIndices) - 6; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 140 | // simple stroke count skips overstroke indices |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 141 | static const int kIndicesPerStrokeRRect = kIndicesPerOverstrokeRRect - 6*4; |
| 142 | // fill count adds final quad to stroke count |
| 143 | static const int kIndicesPerFillRRect = kIndicesPerStrokeRRect + 6; |
| 144 | static const int kVertsPerStrokeRRect = 24; |
| 145 | static const int kVertsPerOverstrokeRRect = 28; |
| 146 | static const int kVertsPerFillRRect = 24; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 147 | |
| 148 | enum RRectType { |
| 149 | kFill_RRectType, |
| 150 | kStroke_RRectType, |
| 151 | kOverstroke_RRectType, |
| 152 | }; |
| 153 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 154 | int rrect_type_to_vert_count(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 155 | switch (type) { |
| 156 | case kFill_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 157 | return kVertsPerFillRRect; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 158 | case kStroke_RRectType: |
| 159 | return kVertsPerStrokeRRect; |
| 160 | case kOverstroke_RRectType: |
| 161 | return kVertsPerOverstrokeRRect; |
| 162 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 163 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 166 | int rrect_type_to_index_count(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 167 | switch (type) { |
| 168 | case kFill_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 169 | return kIndicesPerFillRRect; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 170 | case kStroke_RRectType: |
| 171 | return kIndicesPerStrokeRRect; |
| 172 | case kOverstroke_RRectType: |
| 173 | return kIndicesPerOverstrokeRRect; |
| 174 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 175 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 178 | const uint16_t* rrect_type_to_indices(RRectType type) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 179 | switch (type) { |
| 180 | case kFill_RRectType: |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 181 | case kStroke_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 182 | return gRRectIndices + 6*4; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 183 | case kOverstroke_RRectType: |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 184 | return gRRectIndices; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 185 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 186 | SK_ABORT("Invalid type"); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 191 | class ShadowCircularRRectOp final : public GrMeshDrawOp { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 192 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 193 | DEFINE_OP_CLASS_ID |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 194 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 195 | // An insetWidth > 1/2 rect width or height indicates a simple fill. |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 196 | ShadowCircularRRectOp(GrColor color, const SkRect& devRect, |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 197 | float devRadius, bool isCircle, float blurRadius, float insetWidth, |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 198 | GrSurfaceProxyView falloffView) |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 199 | : INHERITED(ClassID()) |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 200 | , fFalloffView(std::move(falloffView)) { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 201 | SkRect bounds = devRect; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 202 | SkASSERT(insetWidth > 0); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 203 | SkScalar innerRadius = 0.0f; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 204 | SkScalar outerRadius = devRadius; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 205 | SkScalar umbraInset; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 206 | |
| 207 | RRectType type = kFill_RRectType; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 208 | if (isCircle) { |
| 209 | umbraInset = 0; |
| 210 | } else { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 211 | umbraInset = std::max(outerRadius, blurRadius); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 214 | // If stroke is greater than width or height, this is still a fill, |
| 215 | // otherwise we compute stroke params. |
| 216 | if (isCircle) { |
| 217 | innerRadius = devRadius - insetWidth; |
| 218 | type = innerRadius > 0 ? kStroke_RRectType : kFill_RRectType; |
| 219 | } else { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 220 | if (insetWidth <= 0.5f*std::min(devRect.width(), devRect.height())) { |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 221 | // We don't worry about a real inner radius, we just need to know if we |
| 222 | // need to create overstroke vertices. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 223 | innerRadius = std::max(insetWidth - umbraInset, 0.0f); |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 224 | type = innerRadius > 0 ? kOverstroke_RRectType : kStroke_RRectType; |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 225 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 228 | this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 229 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 230 | fGeoData.emplace_back(Geometry{color, outerRadius, umbraInset, innerRadius, |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 231 | blurRadius, bounds, type, isCircle}); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 232 | if (isCircle) { |
| 233 | fVertCount = circle_type_to_vert_count(kStroke_RRectType == type); |
| 234 | fIndexCount = circle_type_to_index_count(kStroke_RRectType == type); |
| 235 | } else { |
| 236 | fVertCount = rrect_type_to_vert_count(type); |
| 237 | fIndexCount = rrect_type_to_index_count(type); |
| 238 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 241 | const char* name() const override { return "ShadowCircularRRectOp"; } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 242 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 243 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 244 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 245 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 246 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 249 | private: |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 250 | struct Geometry { |
| 251 | GrColor fColor; |
| 252 | SkScalar fOuterRadius; |
| 253 | SkScalar fUmbraInset; |
| 254 | SkScalar fInnerRadius; |
| 255 | SkScalar fBlurRadius; |
| 256 | SkRect fDevBounds; |
| 257 | RRectType fType; |
| 258 | bool fIsCircle; |
| 259 | }; |
| 260 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 261 | struct CircleVertex { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 262 | SkPoint fPos; |
| 263 | GrColor fColor; |
| 264 | SkPoint fOffset; |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 265 | SkScalar fDistanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 266 | }; |
| 267 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 268 | void fillInCircleVerts(const Geometry& args, bool isStroked, CircleVertex** verts) const { |
| 269 | |
| 270 | GrColor color = args.fColor; |
| 271 | SkScalar outerRadius = args.fOuterRadius; |
| 272 | SkScalar innerRadius = args.fInnerRadius; |
| 273 | SkScalar blurRadius = args.fBlurRadius; |
| 274 | SkScalar distanceCorrection = outerRadius / blurRadius; |
| 275 | |
| 276 | const SkRect& bounds = args.fDevBounds; |
| 277 | |
| 278 | // The inner radius in the vertex data must be specified in normalized space. |
| 279 | innerRadius = innerRadius / outerRadius; |
| 280 | |
| 281 | SkPoint center = SkPoint::Make(bounds.centerX(), bounds.centerY()); |
| 282 | SkScalar halfWidth = 0.5f * bounds.width(); |
| 283 | SkScalar octOffset = 0.41421356237f; // sqrt(2) - 1 |
| 284 | |
| 285 | (*verts)->fPos = center + SkPoint::Make(-octOffset * halfWidth, -halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 286 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 287 | (*verts)->fOffset = SkPoint::Make(-octOffset, -1); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 288 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 289 | (*verts)++; |
| 290 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 291 | (*verts)->fPos = center + SkPoint::Make(octOffset * halfWidth, -halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 292 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 293 | (*verts)->fOffset = SkPoint::Make(octOffset, -1); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 294 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 295 | (*verts)++; |
| 296 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 297 | (*verts)->fPos = center + SkPoint::Make(halfWidth, -octOffset * halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 298 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 299 | (*verts)->fOffset = SkPoint::Make(1, -octOffset); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 300 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 301 | (*verts)++; |
| 302 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 303 | (*verts)->fPos = center + SkPoint::Make(halfWidth, octOffset * halfWidth); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 304 | (*verts)->fColor = color; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 305 | (*verts)->fOffset = SkPoint::Make(1, octOffset); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 306 | (*verts)->fDistanceCorrection = distanceCorrection; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 307 | (*verts)++; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 308 | |
| 309 | (*verts)->fPos = center + SkPoint::Make(octOffset * halfWidth, halfWidth); |
| 310 | (*verts)->fColor = color; |
| 311 | (*verts)->fOffset = SkPoint::Make(octOffset, 1); |
| 312 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 313 | (*verts)++; |
| 314 | |
| 315 | (*verts)->fPos = center + SkPoint::Make(-octOffset * halfWidth, halfWidth); |
| 316 | (*verts)->fColor = color; |
| 317 | (*verts)->fOffset = SkPoint::Make(-octOffset, 1); |
| 318 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 319 | (*verts)++; |
| 320 | |
| 321 | (*verts)->fPos = center + SkPoint::Make(-halfWidth, octOffset * halfWidth); |
| 322 | (*verts)->fColor = color; |
| 323 | (*verts)->fOffset = SkPoint::Make(-1, octOffset); |
| 324 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 325 | (*verts)++; |
| 326 | |
| 327 | (*verts)->fPos = center + SkPoint::Make(-halfWidth, -octOffset * halfWidth); |
| 328 | (*verts)->fColor = color; |
| 329 | (*verts)->fOffset = SkPoint::Make(-1, -octOffset); |
| 330 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 331 | (*verts)++; |
| 332 | |
| 333 | if (isStroked) { |
| 334 | // compute the inner ring |
| 335 | |
| 336 | // cosine and sine of pi/8 |
| 337 | SkScalar c = 0.923579533f; |
| 338 | SkScalar s = 0.382683432f; |
| 339 | SkScalar r = args.fInnerRadius; |
| 340 | |
| 341 | (*verts)->fPos = center + SkPoint::Make(-s * r, -c * r); |
| 342 | (*verts)->fColor = color; |
| 343 | (*verts)->fOffset = SkPoint::Make(-s * innerRadius, -c * innerRadius); |
| 344 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 345 | (*verts)++; |
| 346 | |
| 347 | (*verts)->fPos = center + SkPoint::Make(s * r, -c * r); |
| 348 | (*verts)->fColor = color; |
| 349 | (*verts)->fOffset = SkPoint::Make(s * innerRadius, -c * innerRadius); |
| 350 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 351 | (*verts)++; |
| 352 | |
| 353 | (*verts)->fPos = center + SkPoint::Make(c * r, -s * r); |
| 354 | (*verts)->fColor = color; |
| 355 | (*verts)->fOffset = SkPoint::Make(c * innerRadius, -s * innerRadius); |
| 356 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 357 | (*verts)++; |
| 358 | |
| 359 | (*verts)->fPos = center + SkPoint::Make(c * r, s * r); |
| 360 | (*verts)->fColor = color; |
| 361 | (*verts)->fOffset = SkPoint::Make(c * innerRadius, s * innerRadius); |
| 362 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 363 | (*verts)++; |
| 364 | |
| 365 | (*verts)->fPos = center + SkPoint::Make(s * r, c * r); |
| 366 | (*verts)->fColor = color; |
| 367 | (*verts)->fOffset = SkPoint::Make(s * innerRadius, c * innerRadius); |
| 368 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 369 | (*verts)++; |
| 370 | |
| 371 | (*verts)->fPos = center + SkPoint::Make(-s * r, c * r); |
| 372 | (*verts)->fColor = color; |
| 373 | (*verts)->fOffset = SkPoint::Make(-s * innerRadius, c * innerRadius); |
| 374 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 375 | (*verts)++; |
| 376 | |
| 377 | (*verts)->fPos = center + SkPoint::Make(-c * r, s * r); |
| 378 | (*verts)->fColor = color; |
| 379 | (*verts)->fOffset = SkPoint::Make(-c * innerRadius, s * innerRadius); |
| 380 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 381 | (*verts)++; |
| 382 | |
| 383 | (*verts)->fPos = center + SkPoint::Make(-c * r, -s * r); |
| 384 | (*verts)->fColor = color; |
| 385 | (*verts)->fOffset = SkPoint::Make(-c * innerRadius, -s * innerRadius); |
| 386 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 387 | (*verts)++; |
| 388 | } else { |
| 389 | // filled |
| 390 | (*verts)->fPos = center; |
| 391 | (*verts)->fColor = color; |
| 392 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 393 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 394 | (*verts)++; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | void fillInRRectVerts(const Geometry& args, CircleVertex** verts) const { |
| 399 | GrColor color = args.fColor; |
| 400 | SkScalar outerRadius = args.fOuterRadius; |
| 401 | |
| 402 | const SkRect& bounds = args.fDevBounds; |
| 403 | |
| 404 | SkScalar umbraInset = args.fUmbraInset; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 405 | SkScalar minDim = 0.5f*std::min(bounds.width(), bounds.height()); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 406 | if (umbraInset > minDim) { |
| 407 | umbraInset = minDim; |
| 408 | } |
| 409 | |
| 410 | SkScalar xInner[4] = { bounds.fLeft + umbraInset, bounds.fRight - umbraInset, |
| 411 | bounds.fLeft + umbraInset, bounds.fRight - umbraInset }; |
| 412 | SkScalar xMid[4] = { bounds.fLeft + outerRadius, bounds.fRight - outerRadius, |
| 413 | bounds.fLeft + outerRadius, bounds.fRight - outerRadius }; |
| 414 | SkScalar xOuter[4] = { bounds.fLeft, bounds.fRight, |
| 415 | bounds.fLeft, bounds.fRight }; |
| 416 | SkScalar yInner[4] = { bounds.fTop + umbraInset, bounds.fTop + umbraInset, |
| 417 | bounds.fBottom - umbraInset, bounds.fBottom - umbraInset }; |
| 418 | SkScalar yMid[4] = { bounds.fTop + outerRadius, bounds.fTop + outerRadius, |
| 419 | bounds.fBottom - outerRadius, bounds.fBottom - outerRadius }; |
| 420 | SkScalar yOuter[4] = { bounds.fTop, bounds.fTop, |
| 421 | bounds.fBottom, bounds.fBottom }; |
| 422 | |
| 423 | SkScalar blurRadius = args.fBlurRadius; |
| 424 | |
| 425 | // In the case where we have to inset more for the umbra, our two triangles in the |
| 426 | // corner get skewed to a diamond rather than a square. To correct for that, |
| 427 | // we also skew the vectors we send to the shader that help define the circle. |
| 428 | // By doing so, we end up with a quarter circle in the corner rather than the |
| 429 | // elliptical curve. |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 430 | |
| 431 | // This is a bit magical, but it gives us the correct results at extrema: |
| 432 | // a) umbraInset == outerRadius produces an orthogonal vector |
| 433 | // b) outerRadius == 0 produces a diagonal vector |
| 434 | // And visually the corner looks correct. |
| 435 | SkVector outerVec = SkVector::Make(outerRadius - umbraInset, -outerRadius - umbraInset); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 436 | outerVec.normalize(); |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 437 | // We want the circle edge to fall fractionally along the diagonal at |
| 438 | // (sqrt(2)*(umbraInset - outerRadius) + outerRadius)/sqrt(2)*umbraInset |
| 439 | // |
| 440 | // Setting the components of the diagonal offset to the following value will give us that. |
| 441 | SkScalar diagVal = umbraInset / (SK_ScalarSqrt2*(outerRadius - umbraInset) - outerRadius); |
| 442 | SkVector diagVec = SkVector::Make(diagVal, diagVal); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 443 | SkScalar distanceCorrection = umbraInset / blurRadius; |
| 444 | |
| 445 | // build corner by corner |
| 446 | for (int i = 0; i < 4; ++i) { |
| 447 | // inner point |
| 448 | (*verts)->fPos = SkPoint::Make(xInner[i], yInner[i]); |
| 449 | (*verts)->fColor = color; |
| 450 | (*verts)->fOffset = SkVector::Make(0, 0); |
| 451 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 452 | (*verts)++; |
| 453 | |
| 454 | // outer points |
| 455 | (*verts)->fPos = SkPoint::Make(xOuter[i], yInner[i]); |
| 456 | (*verts)->fColor = color; |
| 457 | (*verts)->fOffset = SkVector::Make(0, -1); |
| 458 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 459 | (*verts)++; |
| 460 | |
| 461 | (*verts)->fPos = SkPoint::Make(xOuter[i], yMid[i]); |
| 462 | (*verts)->fColor = color; |
| 463 | (*verts)->fOffset = outerVec; |
| 464 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 465 | (*verts)++; |
| 466 | |
| 467 | (*verts)->fPos = SkPoint::Make(xOuter[i], yOuter[i]); |
| 468 | (*verts)->fColor = color; |
| 469 | (*verts)->fOffset = diagVec; |
| 470 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 471 | (*verts)++; |
| 472 | |
| 473 | (*verts)->fPos = SkPoint::Make(xMid[i], yOuter[i]); |
| 474 | (*verts)->fColor = color; |
| 475 | (*verts)->fOffset = outerVec; |
| 476 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 477 | (*verts)++; |
| 478 | |
| 479 | (*verts)->fPos = SkPoint::Make(xInner[i], yOuter[i]); |
| 480 | (*verts)->fColor = color; |
| 481 | (*verts)->fOffset = SkVector::Make(0, -1); |
| 482 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 483 | (*verts)++; |
| 484 | } |
| 485 | |
| 486 | // Add the additional vertices for overstroked rrects. |
| 487 | // Effectively this is an additional stroked rrect, with its |
| 488 | // parameters equal to those in the center of the 9-patch. This will |
| 489 | // give constant values across this inner ring. |
| 490 | if (kOverstroke_RRectType == args.fType) { |
| 491 | SkASSERT(args.fInnerRadius > 0.0f); |
| 492 | |
| 493 | SkScalar inset = umbraInset + args.fInnerRadius; |
| 494 | |
| 495 | // TL |
| 496 | (*verts)->fPos = SkPoint::Make(bounds.fLeft + inset, bounds.fTop + inset); |
| 497 | (*verts)->fColor = color; |
| 498 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 499 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 500 | (*verts)++; |
| 501 | |
| 502 | // TR |
| 503 | (*verts)->fPos = SkPoint::Make(bounds.fRight - inset, bounds.fTop + inset); |
| 504 | (*verts)->fColor = color; |
| 505 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 506 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 507 | (*verts)++; |
| 508 | |
| 509 | // BL |
| 510 | (*verts)->fPos = SkPoint::Make(bounds.fLeft + inset, bounds.fBottom - inset); |
| 511 | (*verts)->fColor = color; |
| 512 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 513 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 514 | (*verts)++; |
| 515 | |
| 516 | // BR |
| 517 | (*verts)->fPos = SkPoint::Make(bounds.fRight - inset, bounds.fBottom - inset); |
| 518 | (*verts)->fColor = color; |
| 519 | (*verts)->fOffset = SkPoint::Make(0, 0); |
| 520 | (*verts)->fDistanceCorrection = distanceCorrection; |
| 521 | (*verts)++; |
| 522 | } |
| 523 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 526 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 527 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 528 | void onCreateProgramInfo(const GrCaps* caps, |
| 529 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 530 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 531 | bool usesMSAASurface, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 532 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 533 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 534 | GrXferBarrierFlags renderPassXferBarriers, |
| 535 | GrLoadOp colorLoadOp) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 536 | GrGeometryProcessor* gp = GrRRectShadowGeoProc::Make(arena, fFalloffView); |
| 537 | SkASSERT(sizeof(CircleVertex) == gp->vertexStride()); |
| 538 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 539 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame] | 540 | usesMSAASurface, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 541 | std::move(appliedClip), |
| 542 | dstProxyView, gp, |
| 543 | GrProcessorSet::MakeEmptySet(), |
| 544 | GrPrimitiveType::kTriangles, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 545 | renderPassXferBarriers, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 546 | colorLoadOp, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 547 | GrPipeline::InputFlags::kNone, |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 548 | &GrUserStencilSettings::kUnused); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 551 | void onPrepareDraws(GrMeshDrawTarget* target) override { |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 552 | int instanceCount = fGeoData.count(); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 553 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 554 | sk_sp<const GrBuffer> vertexBuffer; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 555 | int firstVertex; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 556 | CircleVertex* verts = (CircleVertex*)target->makeVertexSpace( |
| 557 | sizeof(CircleVertex), fVertCount, &vertexBuffer, &firstVertex); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 558 | if (!verts) { |
| 559 | SkDebugf("Could not allocate vertices\n"); |
| 560 | return; |
| 561 | } |
| 562 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 563 | sk_sp<const GrBuffer> indexBuffer; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 564 | int firstIndex = 0; |
| 565 | uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
| 566 | if (!indices) { |
| 567 | SkDebugf("Could not allocate indices\n"); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | int currStartVertex = 0; |
| 572 | for (int i = 0; i < instanceCount; i++) { |
| 573 | const Geometry& args = fGeoData[i]; |
| 574 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 575 | if (args.fIsCircle) { |
| 576 | bool isStroked = SkToBool(kStroke_RRectType == args.fType); |
| 577 | this->fillInCircleVerts(args, isStroked, &verts); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 578 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 579 | const uint16_t* primIndices = circle_type_to_indices(isStroked); |
| 580 | const int primIndexCount = circle_type_to_index_count(isStroked); |
John Stiles | 7c3fa4e | 2021-08-10 17:00:56 -0400 | [diff] [blame] | 581 | for (int j = 0; j < primIndexCount; ++j) { |
| 582 | *indices++ = primIndices[j] + currStartVertex; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 583 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 584 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 585 | currStartVertex += circle_type_to_vert_count(isStroked); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 586 | |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 587 | } else { |
| 588 | this->fillInRRectVerts(args, &verts); |
| 589 | |
| 590 | const uint16_t* primIndices = rrect_type_to_indices(args.fType); |
| 591 | const int primIndexCount = rrect_type_to_index_count(args.fType); |
John Stiles | 7c3fa4e | 2021-08-10 17:00:56 -0400 | [diff] [blame] | 592 | for (int j = 0; j < primIndexCount; ++j) { |
| 593 | *indices++ = primIndices[j] + currStartVertex; |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | currStartVertex += rrect_type_to_vert_count(args.fType); |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 597 | } |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 600 | fMesh = target->allocMesh(); |
| 601 | fMesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 602 | GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 606 | if (!fProgramInfo) { |
| 607 | this->createProgramInfo(flushState); |
| 608 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 609 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 610 | if (!fProgramInfo || !fMesh) { |
| 611 | return; |
| 612 | } |
| 613 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 614 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 615 | flushState->bindTextures(fProgramInfo->geomProc(), *fFalloffView.proxy(), |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 616 | fProgramInfo->pipeline()); |
| 617 | flushState->drawMesh(*fMesh); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 618 | } |
| 619 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 620 | CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 621 | ShadowCircularRRectOp* that = t->cast<ShadowCircularRRectOp>(); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 622 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 623 | fVertCount += that->fVertCount; |
| 624 | fIndexCount += that->fIndexCount; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 625 | return CombineResult::kMerged; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 626 | } |
| 627 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 628 | #if GR_TEST_UTILS |
| 629 | SkString onDumpInfo() const override { |
| 630 | SkString string; |
| 631 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 632 | string.appendf( |
| 633 | "Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]," |
| 634 | "OuterRad: %.2f, Umbra: %.2f, InnerRad: %.2f, BlurRad: %.2f\n", |
| 635 | fGeoData[i].fColor, fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop, |
| 636 | fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom, |
| 637 | fGeoData[i].fOuterRadius, fGeoData[i].fUmbraInset, |
| 638 | fGeoData[i].fInnerRadius, fGeoData[i].fBlurRadius); |
| 639 | } |
| 640 | return string; |
| 641 | } |
| 642 | #endif |
| 643 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 644 | void visitProxies(const GrVisitProxyFunc& func) const override { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 645 | func(fFalloffView.proxy(), GrMipmapped(false)); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 646 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 647 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 648 | } |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 649 | } |
| 650 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 651 | SkSTArray<1, Geometry, true> fGeoData; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 652 | int fVertCount; |
| 653 | int fIndexCount; |
Greg Daniel | ad994cd | 2019-12-10 09:35:16 -0500 | [diff] [blame] | 654 | GrSurfaceProxyView fFalloffView; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 655 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 656 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 657 | GrProgramInfo* fProgramInfo = nullptr; |
| 658 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 659 | using INHERITED = GrMeshDrawOp; |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 660 | }; |
| 661 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 662 | } // anonymous namespace |
| 663 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 664 | /////////////////////////////////////////////////////////////////////////////// |
| 665 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 666 | namespace skgpu::v1::ShadowRRectOp { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 667 | |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 668 | static GrSurfaceProxyView create_falloff_texture(GrRecordingContext* rContext) { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 669 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 670 | GrUniqueKey key; |
| 671 | GrUniqueKey::Builder builder(&key, kDomain, 0, "Shadow Gaussian Falloff"); |
| 672 | builder.finish(); |
| 673 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 674 | auto threadSafeCache = rContext->priv().threadSafeCache(); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 675 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 676 | GrSurfaceProxyView view = threadSafeCache->find(key); |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 677 | if (view) { |
| 678 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
| 679 | return view; |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 682 | static const int kWidth = 128; |
| 683 | static const size_t kRowBytes = kWidth * GrColorTypeBytesPerPixel(GrColorType::kAlpha_8); |
| 684 | SkImageInfo ii = SkImageInfo::MakeA8(kWidth, 1); |
| 685 | |
| 686 | SkBitmap bitmap; |
| 687 | bitmap.allocPixels(ii, kRowBytes); |
| 688 | |
| 689 | unsigned char* values = (unsigned char*)bitmap.getPixels(); |
| 690 | for (int i = 0; i < 128; ++i) { |
| 691 | SkScalar d = SK_Scalar1 - i / SkIntToScalar(127); |
| 692 | values[i] = SkScalarRoundToInt((SkScalarExp(-4 * d * d) - 0.018f) * 255); |
| 693 | } |
| 694 | bitmap.setImmutable(); |
| 695 | |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 696 | view = std::get<0>(GrMakeUncachedBitmapProxyView(rContext, bitmap)); |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 697 | if (!view) { |
| 698 | return {}; |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 699 | } |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 700 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 701 | view = threadSafeCache->add(key, view); |
Robert Phillips | 692915e | 2020-09-30 13:56:51 -0400 | [diff] [blame] | 702 | SkASSERT(view.origin() == kTopLeft_GrSurfaceOrigin); |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 703 | return view; |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 706 | GrOp::Owner Make(GrRecordingContext* context, |
| 707 | GrColor color, |
| 708 | const SkMatrix& viewMatrix, |
| 709 | const SkRRect& rrect, |
| 710 | SkScalar blurWidth, |
| 711 | SkScalar insetWidth) { |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 712 | // Shadow rrect ops only handle simple circular rrects. |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 713 | SkASSERT(viewMatrix.isSimilarity() && SkRRectPriv::EqualRadii(rrect)); |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 714 | |
Greg Daniel | 9f0dfbd | 2020-02-10 11:47:11 -0500 | [diff] [blame] | 715 | GrSurfaceProxyView falloffView = create_falloff_texture(context); |
| 716 | if (!falloffView) { |
Jim Van Verth | 7da048b | 2019-10-29 13:28:14 -0400 | [diff] [blame] | 717 | return nullptr; |
| 718 | } |
| 719 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 720 | // Do any matrix crunching before we reset the draw state for device coords. |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 721 | const SkRect& rrectBounds = rrect.getBounds(); |
| 722 | SkRect bounds; |
| 723 | viewMatrix.mapRect(&bounds, rrectBounds); |
| 724 | |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 725 | // Map radius and inset. As the matrix is a similarity matrix, this should be isotropic. |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 726 | SkScalar radius = SkRRectPriv::GetSimpleRadii(rrect).fX; |
Jim Van Verth | 8d1e0ac | 2017-05-05 15:53:23 -0400 | [diff] [blame] | 727 | SkScalar matrixFactor = viewMatrix[SkMatrix::kMScaleX] + viewMatrix[SkMatrix::kMSkewX]; |
| 728 | SkScalar scaledRadius = SkScalarAbs(radius*matrixFactor); |
| 729 | SkScalar scaledInsetWidth = SkScalarAbs(insetWidth*matrixFactor); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 730 | |
Robert Phillips | e576378 | 2019-04-17 14:38:24 -0400 | [diff] [blame] | 731 | if (scaledInsetWidth <= 0) { |
| 732 | return nullptr; |
| 733 | } |
| 734 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 735 | return GrOp::Make<ShadowCircularRRectOp>(context, |
| 736 | color, |
| 737 | bounds, |
| 738 | scaledRadius, |
| 739 | rrect.isOval(), |
| 740 | blurWidth, |
| 741 | scaledInsetWidth, |
| 742 | std::move(falloffView)); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 743 | } |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 744 | |
| 745 | } // namespace skgpu::v1::ShadowRRectOp |
Jim Van Verth | 57061ee | 2017-04-28 17:30:30 -0400 | [diff] [blame] | 746 | |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 747 | /////////////////////////////////////////////////////////////////////////////// |
| 748 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 749 | #if GR_TEST_UTILS |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 750 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 751 | #include "src/gpu/GrDrawOpTest.h" |
| 752 | |
Brian Salomon | 0596909 | 2017-07-13 11:20:51 -0400 | [diff] [blame] | 753 | GR_DRAW_OP_TEST_DEFINE(ShadowRRectOp) { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 754 | // We may choose matrix and inset values that cause the factory to fail. We loop until we find |
| 755 | // an acceptable combination. |
Brian Osman | 4462c04 | 2018-06-08 16:35:44 -0400 | [diff] [blame] | 756 | do { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 757 | // create a similarity matrix |
| 758 | SkScalar rotate = random->nextSScalar1() * 360.f; |
| 759 | SkScalar translateX = random->nextSScalar1() * 1000.f; |
| 760 | SkScalar translateY = random->nextSScalar1() * 1000.f; |
| 761 | SkScalar scale = random->nextSScalar1() * 100.f; |
| 762 | SkMatrix viewMatrix; |
| 763 | viewMatrix.setRotate(rotate); |
| 764 | viewMatrix.postTranslate(translateX, translateY); |
| 765 | viewMatrix.postScale(scale, scale); |
| 766 | SkScalar insetWidth = random->nextSScalar1() * 72.f; |
| 767 | SkScalar blurWidth = random->nextSScalar1() * 72.f; |
| 768 | bool isCircle = random->nextBool(); |
| 769 | // This op doesn't use a full GrPaint, just a color. |
| 770 | GrColor color = paint.getColor4f().toBytes_RGBA(); |
| 771 | if (isCircle) { |
| 772 | SkRect circle = GrTest::TestSquare(random); |
| 773 | SkRRect rrect = SkRRect::MakeOval(circle); |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 774 | if (auto op = skgpu::v1::ShadowRRectOp::Make( |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 775 | context, color, viewMatrix, rrect, blurWidth, insetWidth)) { |
| 776 | return op; |
| 777 | } |
| 778 | } else { |
| 779 | SkRRect rrect; |
| 780 | do { |
| 781 | // This may return a rrect with elliptical corners, which will cause an assert. |
| 782 | rrect = GrTest::TestRRectSimple(random); |
| 783 | } while (!SkRRectPriv::IsSimpleCircular(rrect)); |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 784 | if (auto op = skgpu::v1::ShadowRRectOp::Make( |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 785 | context, color, viewMatrix, rrect, blurWidth, insetWidth)) { |
| 786 | return op; |
| 787 | } |
| 788 | } |
| 789 | } while (true); |
Jim Van Verth | c590341 | 2016-11-17 15:27:09 -0500 | [diff] [blame] | 790 | } |
| 791 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 792 | #endif // GR_TEST_UTILS |