void PathText::Glyph::reset(SkRandom& rand, int w, int h) { int screensize = SkTMax(w, h); const SkRect& bounds = fPath.getBounds(); SkScalar t; fPosition = {rand.nextF() * w, rand.nextF() * h}; t = pow(rand.nextF(), 100); fZoom = ((1 - t) * screensize / 50 + t * screensize / 3) / SkTMax(bounds.width(), bounds.height()); fSpin = rand.nextF() * 360; fMidpt = {bounds.centerX(), bounds.centerY()}; }
void onDraw(SkCanvas* canvas) override { if (nullptr == fImage) { fImage = makebm(gSurfaceSize, gSurfaceSize); } const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2); constexpr int kPadX = 30; constexpr int kPadY = 40; int rowCount = 0; canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); canvas->save(); SkRandom random; SkPaint paint; paint.setAntiAlias(fAA); for (int w = 1; w <= kMaxSrcRectSize; w *= 3) { for (int h = 1; h <= kMaxSrcRectSize; h *= 3) { const SkIRect srcRect = SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h); canvas->save(); switch (random.nextU() % 3) { case 0: canvas->rotate(random.nextF() * 10.f); break; case 1: canvas->rotate(-random.nextF() * 10.f); break; case 2: // rect stays rect break; } canvas->drawImageRect(fImage.get(), srcRect, dstRect, &paint, SkCanvas::kFast_SrcRectConstraint); canvas->restore(); canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); ++rowCount; if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) { canvas->restore(); canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); canvas->save(); rowCount = 0; } } } canvas->restore(); }
void WavyPathText::Waves::reset(SkRandom& rand, int w, int h) { const double pixelsPerMeter = 0.06 * SkTMax(w, h); const double medianWavelength = 8 * pixelsPerMeter; const double medianWaveAmplitude = 0.05 * 4 * pixelsPerMeter; const double gravity = 9.8 * pixelsPerMeter; for (int i = 0; i < 4; ++i) { const double offsetAngle = (rand.nextF() * 2 - 1) * kMaxOffsetAngle; const double intensity = pow(2, rand.nextF() * 2 - 1); const double wavelength = intensity * medianWavelength; fAmplitudes[i] = intensity * medianWaveAmplitude; fFrequencies[i] = 2 * SK_ScalarPI / wavelength; fDirsX[i] = cosf(kAverageAngle + offsetAngle); fDirsY[i] = sinf(kAverageAngle + offsetAngle); fSpeeds[i] = -sqrt(gravity * 2 * SK_ScalarPI / wavelength); fOffsets[i] = rand.nextF() * 2 * SK_ScalarPI; } }
// SkMatrix44::setConcat() has a fast path for matrices that are at most scale+translate. SetConcatMatrix44Bench(bool fastPath) : INHERITED(fastPath ? "setconcat_fast" : "setconcat_general") { if (fastPath) { const SkMScalar v = SkDoubleToMScalar(1.5); fM1.setScale(v,v,v); fM2.setTranslate(v,v,v); } else { SkRandom rand; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { fM1.setFloat(x,y, rand.nextF()); fM2.setFloat(x,y, rand.nextF()); }} } }
void onDelayedSetup() override { SkScalar w = SkIntToScalar(fShapesSize.width()); SkScalar h = SkIntToScalar(fShapesSize.height()); fRect.setRect(SkRect::MakeXYWH(-w / 2, -h / 2, w, h)); fOval.setOval(fRect.rect()); fRRect.setNinePatch(fRect.rect(), w / 8, h / 13, w / 11, h / 7); if (kNone_ShapesType != fInnerShapesType) { fRect.inset(w / 7, h / 11, &fInnerRect); fInnerRect.offset(w / 28, h / 44); fInnerOval.setOval(fInnerRect.rect()); fInnerRRect.setRectXY(fInnerRect.rect(), w / 13, w / 7); } SkRandom rand; fShapes.push_back_n(fNumShapes); for (int i = 0; i < fNumShapes; i++) { float pad = sqrtf(static_cast<float>(fShapesSize.width() * fShapesSize.width()) + static_cast<float>(fShapesSize.height() * fShapesSize.height())); fShapes[i].fMatrix.setTranslate(0.5f * pad + rand.nextF() * (kBenchWidth - pad), 0.5f * pad + rand.nextF() * (kBenchHeight - pad)); fShapes[i].fMatrix.preRotate(rand.nextF() * 360.0f); if (fPerspective) { fShapes[i].fMatrix.setPerspX(0.00015f); fShapes[i].fMatrix.setPerspY(-0.00015f); } fShapes[i].fColor = rand.nextU() | 0xff808080; } for (int i = 0; i < fNumShapes; i++) { // Do this in a separate loop so mixed shapes get the same random numbers during // placement as non-mixed do. int shapeType = fShapesType; if (kMixed_ShapesType == shapeType) { shapeType = rand.nextRangeU(kRect_ShapesType, kRRect_ShapesType); } int innerShapeType = fInnerShapesType; if (kMixed_ShapesType == innerShapeType) { innerShapeType = rand.nextRangeU(kRect_ShapesType, kRRect_ShapesType); } if (kNone_ShapesType == innerShapeType) { switch (shapeType) { using namespace std; using namespace std::placeholders; case kRect_ShapesType: fShapes[i].fDraw = bind(&SkCanvas::drawRect, _1, cref(fRect.rect()), _2); break; case kOval_ShapesType: fShapes[i].fDraw = bind(&SkCanvas::drawOval, _1, cref(fOval.rect()), _2); break; case kRRect_ShapesType: fShapes[i].fDraw = bind(&SkCanvas::drawRRect, _1, cref(fRRect), _2); break; } } else { const SkRRect* outer; switch (shapeType) { case kRect_ShapesType: outer = &fRect; break; case kOval_ShapesType: outer = &fOval; break; case kRRect_ShapesType: outer = &fRRect; break; } const SkRRect* inner; switch (innerShapeType) { case kRect_ShapesType: inner = &fInnerRect; break; case kOval_ShapesType: inner = &fInnerOval; break; case kRRect_ShapesType: inner = &fInnerRRect; break; } fShapes[i].fDraw = std::bind(&SkCanvas::drawDRRect, std::placeholders::_1, std::cref(*outer), std::cref(*inner), std::placeholders::_2); } } }