virtual void onDraw(SkCanvas* canvas) { SkRect r; r.setWH(SkIntToScalar(fRectWidth), SkIntToScalar(fRectHeight)); SkISize canvas_size = canvas->getDeviceSize(); int center_x = (canvas_size.fWidth - (int)(r.width()))/2; int center_y = (canvas_size.fHeight - (int)(r.height()))/2; SkMask mask; if (!this->makeMask(&mask, r)) { SkPaint paint; r.offset( SkIntToScalar(center_x), SkIntToScalar(center_y) ); canvas->drawRect(r,paint); return; } SkAutoMaskFreeImage amfi(mask.fImage); SkBitmap bm; bm.installMaskPixels(mask); center_x = (canvas_size.fWidth - mask.fBounds.width())/2; center_y = (canvas_size.fHeight - mask.fBounds.height())/2; canvas->drawBitmap(bm, SkIntToScalar(center_x), SkIntToScalar(center_y), NULL); }
bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix, const SkIRect* clipBounds, SkMask* mask, SkMask::CreateMode mode) const { SkASSERT(fLayers); if (fLayers->empty()) { return false; } if (SkMask::kJustRenderImage_CreateMode != mode) { if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds)) return false; } if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { mask->fFormat = SkMask::kA8_Format; mask->fRowBytes = mask->fBounds.width(); size_t size = mask->computeImageSize(); if (0 == size) { return false; // too big to allocate, abort } mask->fImage = SkMask::AllocImage(size); memset(mask->fImage, 0, size); } if (SkMask::kJustComputeBounds_CreateMode != mode) { SkBitmap device; SkRasterClip rectClip; SkDraw draw; SkMatrix translatedMatrix; // this translates us to our local pixels SkMatrix drawMatrix; // this translates the path by each layer's offset rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height())); translatedMatrix = matrix; translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft), -SkIntToScalar(mask->fBounds.fTop)); device.installMaskPixels(*mask); draw.fBitmap = &device; draw.fMatrix = &drawMatrix; draw.fRC = &rectClip; draw.fClip = &rectClip.bwRgn(); // we set the matrixproc in the loop, as the matrix changes each time (potentially) SkDeque::F2BIter iter(*fLayers); SkLayerRasterizer_Rec* rec; while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) { drawMatrix = translatedMatrix; drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY); draw.drawPath(path, rec->fPaint); } } return true; }
virtual void onDraw(SkCanvas* canvas) { const int widths[] = {25, 5, 5, 100, 150, 25}; const int heights[] = {100, 100, 5, 25, 150, 25}; const SkBlurStyle styles[] = {kNormal_SkBlurStyle, kInner_SkBlurStyle, kOuter_SkBlurStyle}; const float radii[] = {20, 5, 10}; canvas->translate(50,20); int cur_x = 0; int cur_y = 0; int max_height = 0; for (size_t i = 0 ; i < SK_ARRAY_COUNT(widths) ; i++) { int width = widths[i]; int height = heights[i]; SkRect r; r.setWH(SkIntToScalar(width), SkIntToScalar(height)); SkAutoCanvasRestore autoRestore(canvas, true); for (size_t j = 0 ; j < SK_ARRAY_COUNT(radii) ; j++) { float radius = radii[j]; for (size_t k = 0 ; k < SK_ARRAY_COUNT(styles) ; k++) { SkBlurStyle style = styles[k]; SkMask mask; SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(radius), &mask, r, style); SkAutoMaskFreeImage amfi(mask.fImage); SkBitmap bm; bm.installMaskPixels(mask); if (cur_x + bm.width() >= fGMWidth - fMargin) { cur_x = 0; cur_y += max_height + fPadding; max_height = 0; } canvas->save(); canvas->translate((SkScalar)cur_x, (SkScalar)cur_y); canvas->translate(-(bm.width() - r.width())/2, -(bm.height()-r.height())/2); canvas->drawBitmap(bm, 0.f, 0.f, NULL); canvas->restore(); cur_x += bm.width() + fPadding; if (bm.height() > max_height) max_height = bm.height(); } } } }
static void drawClip(SkCanvas* canvas, const SkAAClip& clip) { SkMask mask; SkBitmap bm; clip.copyToMask(&mask); SkAutoMaskFreeImage amfi(mask.fImage); bm.installMaskPixels(mask); SkPaint paint; canvas->drawBitmap(bm, SK_Scalar1 * mask.fBounds.fLeft, SK_Scalar1 * mask.fBounds.fTop, &paint); }