blob: efae89b029ce4cb32a42cbf08b3cc30cc1b38391 [file] [log] [blame]
ajuma77b6ba32016-01-08 14:58:35 -08001/*
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
8#include "SkPaintImageFilter.h"
ajuma77b6ba32016-01-08 14:58:35 -08009#include "SkCanvas.h"
Matt Sarett31abf1f2017-04-07 16:54:04 -040010#include "SkColorSpaceXformer.h"
Cary Clark60aaeb22017-11-03 08:06:09 -040011#include "SkImageFilterPriv.h"
ajuma77b6ba32016-01-08 14:58:35 -080012#include "SkReadBuffer.h"
robertphillipse6163bf2016-03-09 10:17:41 -080013#include "SkSpecialImage.h"
14#include "SkSpecialSurface.h"
ajuma77b6ba32016-01-08 14:58:35 -080015#include "SkWriteBuffer.h"
16
robertphillips225db442016-04-17 14:27:05 -070017sk_sp<SkImageFilter> SkPaintImageFilter::Make(const SkPaint& paint,
18 const CropRect* cropRect) {
19 return sk_sp<SkImageFilter>(new SkPaintImageFilter(paint, cropRect));
20}
21
ajuma77b6ba32016-01-08 14:58:35 -080022SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cropRect)
robertphillips372177e2016-03-30 07:32:28 -070023 : INHERITED(nullptr, 0, cropRect)
24 , fPaint(paint) {
ajuma77b6ba32016-01-08 14:58:35 -080025}
26
reed60c9b582016-04-03 09:11:13 -070027sk_sp<SkFlattenable> SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) {
ajuma77b6ba32016-01-08 14:58:35 -080028 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
29 SkPaint paint;
30 buffer.readPaint(&paint);
reed60c9b582016-04-03 09:11:13 -070031 return SkPaintImageFilter::Make(paint, &common.cropRect());
ajuma77b6ba32016-01-08 14:58:35 -080032}
33
34void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const {
35 this->INHERITED::flatten(buffer);
36 buffer.writePaint(fPaint);
37}
38
robertphillips2302de92016-03-24 07:26:32 -070039sk_sp<SkSpecialImage> SkPaintImageFilter::onFilterImage(SkSpecialImage* source,
40 const Context& ctx,
41 SkIPoint* offset) const {
ajuma77b6ba32016-01-08 14:58:35 -080042 SkIRect bounds;
robertphillipse6163bf2016-03-09 10:17:41 -080043 const SkIRect srcBounds = SkIRect::MakeWH(source->width(), source->height());
44 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
45 return nullptr;
ajuma77b6ba32016-01-08 14:58:35 -080046 }
47
brianosmaneed6b0e2016-09-23 13:04:05 -070048 sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size()));
robertphillipse6163bf2016-03-09 10:17:41 -080049 if (!surf) {
50 return nullptr;
ajuma77b6ba32016-01-08 14:58:35 -080051 }
robertphillipse6163bf2016-03-09 10:17:41 -080052
53 SkCanvas* canvas = surf->getCanvas();
54 SkASSERT(canvas);
55
56 canvas->clear(0x0);
ajuma77b6ba32016-01-08 14:58:35 -080057
58 SkMatrix matrix(ctx.ctm());
59 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
robertphillipse6163bf2016-03-09 10:17:41 -080060 SkRect rect = SkRect::MakeIWH(bounds.width(), bounds.height());
ajuma77b6ba32016-01-08 14:58:35 -080061 SkMatrix inverse;
62 if (matrix.invert(&inverse)) {
63 inverse.mapRect(&rect);
64 }
robertphillipse6163bf2016-03-09 10:17:41 -080065 canvas->setMatrix(matrix);
66 canvas->drawRect(rect, fPaint);
ajuma77b6ba32016-01-08 14:58:35 -080067
ajuma77b6ba32016-01-08 14:58:35 -080068 offset->fX = bounds.fLeft;
69 offset->fY = bounds.fTop;
robertphillips2302de92016-03-24 07:26:32 -070070 return surf->makeImageSnapshot();
ajuma77b6ba32016-01-08 14:58:35 -080071}
72
Matt Sarett31abf1f2017-04-07 16:54:04 -040073sk_sp<SkImageFilter> SkPaintImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Mike Reed6d9f4292017-07-06 12:32:55 -040074 SkPaint paint = xformer->apply(fPaint);
75 if (paint != fPaint) {
76 return SkPaintImageFilter::Make(paint, this->getCropRectIfSet());
77 }
78 return this->refMe();
Matt Sarett31abf1f2017-04-07 16:54:04 -040079}
80
senorblanco6db0a7b2016-04-01 16:41:10 -070081bool SkPaintImageFilter::affectsTransparentBlack() const {
82 return true;
ajuma77b6ba32016-01-08 14:58:35 -080083}
84
85#ifndef SK_IGNORE_TO_STRING
86void SkPaintImageFilter::toString(SkString* str) const {
87 str->appendf("SkPaintImageFilter: (");
robertphillips372177e2016-03-30 07:32:28 -070088 fPaint.toString(str);
ajuma77b6ba32016-01-08 14:58:35 -080089 str->append(")");
90}
91#endif