blob: 1676787ef67113c4fc53f01692ccdd0cdf367bad [file] [log] [blame]
Nader Jawad197743f2021-04-19 19:45:13 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "StretchMask.h"
Kevin Lubick4e8ce462022-12-01 20:29:16 +000017
18#include "SkBlendMode.h"
Nader Jawad197743f2021-04-19 19:45:13 -070019#include "SkCanvas.h"
Kevin Lubick4e8ce462022-12-01 20:29:16 +000020#include "SkSurface.h"
Kevin Lubick8099b672023-01-09 14:48:45 +000021
Nader Jawad197743f2021-04-19 19:45:13 -070022#include "TransformCanvas.h"
23#include "SkiaDisplayList.h"
24
25using android::uirenderer::StretchMask;
26
Kevin Lubicka0fccc32023-05-02 13:31:00 +000027void StretchMask::draw(GrRecordingContext*,
Nader Jawad197743f2021-04-19 19:45:13 -070028 const StretchEffect& stretch,
29 const SkRect& bounds,
30 skiapipeline::SkiaDisplayList* displayList,
31 SkCanvas* canvas) {
32 float width = bounds.width();
33 float height = bounds.height();
34 if (mMaskSurface == nullptr || mMaskSurface->width() != width ||
35 mMaskSurface->height() != height) {
36 // Create a new surface if we don't have one or our existing size does
Kevin Lubicka0fccc32023-05-02 13:31:00 +000037 // not match. SkCanvas::makeSurface returns a new surface that will
38 // be GPU-backed if canvas was also.
39 mMaskSurface = canvas->makeSurface(SkImageInfo::Make(
40 width,
41 height,
42 SkColorType::kAlpha_8_SkColorType,
43 SkAlphaType::kPremul_SkAlphaType
44 ));
Nader Jawad197743f2021-04-19 19:45:13 -070045 mIsDirty = true;
46 }
47
48 if (mIsDirty) {
49 SkCanvas* maskCanvas = mMaskSurface->getCanvas();
Nader Jawad9238f592021-05-11 20:13:29 -070050 // Make sure to apply target transformation to the mask canvas
51 // to ensure the replayed drawing commands generate the same result
52 auto previousMatrix = displayList->mParentMatrix;
Kevin Lubicka0fccc32023-05-02 13:31:00 +000053 displayList->mParentMatrix = maskCanvas->getLocalToDeviceAs3x3();
Nader Jawad9238f592021-05-11 20:13:29 -070054 maskCanvas->save();
Nader Jawad197743f2021-04-19 19:45:13 -070055 maskCanvas->drawColor(0, SkBlendMode::kClear);
56 TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver);
57 displayList->draw(&transformCanvas);
Nader Jawad9238f592021-05-11 20:13:29 -070058 maskCanvas->restore();
59 displayList->mParentMatrix = previousMatrix;
Nader Jawad197743f2021-04-19 19:45:13 -070060 }
61
62 sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot();
Nader Jawad6aff4812021-06-09 10:14:43 -070063 sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr);
Nader Jawad197743f2021-04-19 19:45:13 -070064
65 SkPaint maskPaint;
66 maskPaint.setShader(maskStretchShader);
67 maskPaint.setBlendMode(SkBlendMode::kDstOut);
68 canvas->drawRect(bounds, maskPaint);
69
70 mIsDirty = false;
71}