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