123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- * Copyright 2013 The Android Open Source Project
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
- #include "include/effects/SkPictureImageFilter.h"
- #include "include/core/SkCanvas.h"
- #include "include/effects/SkImageSource.h"
- #include "src/core/SkPicturePriv.h"
- #include "src/core/SkReadBuffer.h"
- #include "src/core/SkSpecialImage.h"
- #include "src/core/SkSpecialSurface.h"
- #include "src/core/SkValidationUtils.h"
- #include "src/core/SkWriteBuffer.h"
- sk_sp<SkImageFilter> SkPictureImageFilter::Make(sk_sp<SkPicture> picture) {
- return sk_sp<SkImageFilter>(new SkPictureImageFilter(std::move(picture)));
- }
- sk_sp<SkImageFilter> SkPictureImageFilter::Make(sk_sp<SkPicture> picture,
- const SkRect& cropRect) {
- return sk_sp<SkImageFilter>(new SkPictureImageFilter(std::move(picture), cropRect));
- }
- SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture)
- : INHERITED(nullptr, 0, nullptr)
- , fPicture(std::move(picture))
- , fCropRect(fPicture ? fPicture->cullRect() : SkRect::MakeEmpty()) {
- }
- SkPictureImageFilter::SkPictureImageFilter(sk_sp<SkPicture> picture, const SkRect& cropRect)
- : INHERITED(nullptr, 0, nullptr)
- , fPicture(std::move(picture))
- , fCropRect(cropRect) {
- }
- enum PictureResolution {
- kDeviceSpace_PictureResolution,
- kLocalSpace_PictureResolution
- };
- static sk_sp<SkImageFilter> make_localspace_filter(sk_sp<SkPicture> pic, const SkRect& cropRect,
- SkFilterQuality fq) {
- SkISize dim = { SkScalarRoundToInt(cropRect.width()), SkScalarRoundToInt(cropRect.height()) };
- auto img = SkImage::MakeFromPicture(std::move(pic), dim, nullptr, nullptr,
- SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
- return SkImageSource::Make(img, cropRect, cropRect, fq);
- }
- sk_sp<SkFlattenable> SkPictureImageFilter::CreateProc(SkReadBuffer& buffer) {
- sk_sp<SkPicture> picture;
- SkRect cropRect;
- if (buffer.readBool()) {
- picture = SkPicturePriv::MakeFromBuffer(buffer);
- }
- buffer.readRect(&cropRect);
- if (buffer.isVersionLT(SkReadBuffer::kRemovePictureImageFilterLocalSpace)) {
- PictureResolution pictureResolution = buffer.checkRange<PictureResolution>(
- kDeviceSpace_PictureResolution, kLocalSpace_PictureResolution);
- if (kLocalSpace_PictureResolution == pictureResolution) {
- return make_localspace_filter(std::move(picture), cropRect,
- buffer.checkFilterQuality());
- }
- }
- return sk_sp<SkImageFilter>(new SkPictureImageFilter(picture, cropRect));
- }
- void SkPictureImageFilter::flatten(SkWriteBuffer& buffer) const {
- bool hasPicture = (fPicture != nullptr);
- buffer.writeBool(hasPicture);
- if (hasPicture) {
- SkPicturePriv::Flatten(fPicture, buffer);
- }
- buffer.writeRect(fCropRect);
- }
- sk_sp<SkSpecialImage> SkPictureImageFilter::onFilterImage(SkSpecialImage* source,
- const Context& ctx,
- SkIPoint* offset) const {
- if (!fPicture) {
- return nullptr;
- }
- SkRect floatBounds;
- ctx.ctm().mapRect(&floatBounds, fCropRect);
- SkIRect bounds = floatBounds.roundOut();
- if (!bounds.intersect(ctx.clipBounds())) {
- return nullptr;
- }
- SkASSERT(!bounds.isEmpty());
- // Given the standard usage of the picture image filter (i.e., to render content at a fixed
- // resolution that, most likely, differs from the screen's) disable LCD text.
- SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
- sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size(),
- kPremul_SkAlphaType, &props));
- if (!surf) {
- return nullptr;
- }
- SkASSERT(kUnknown_SkPixelGeometry == surf->props().pixelGeometry());
- SkCanvas* canvas = surf->getCanvas();
- SkASSERT(canvas);
- canvas->clear(0x0);
- canvas->translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop));
- canvas->concat(ctx.ctm());
- canvas->drawPicture(fPicture);
- offset->fX = bounds.fLeft;
- offset->fY = bounds.fTop;
- return surf->makeImageSnapshot();
- }
|