blob: b169c9200e887f1ed56f3f985abfa677c9697b5e [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"
Nader Jawad197743f2021-04-19 19:45:13 -070021#include "TransformCanvas.h"
22#include "SkiaDisplayList.h"
23
24using android::uirenderer::StretchMask;
25
26void StretchMask::draw(GrRecordingContext* context,
27 const StretchEffect& stretch,
28 const SkRect& bounds,
29 skiapipeline::SkiaDisplayList* displayList,
30 SkCanvas* canvas) {
31 float width = bounds.width();
32 float height = bounds.height();
33 if (mMaskSurface == nullptr || mMaskSurface->width() != width ||
34 mMaskSurface->height() != height) {
35 // Create a new surface if we don't have one or our existing size does
36 // not match.
37 mMaskSurface = SkSurface::MakeRenderTarget(
38 context,
39 SkBudgeted::kYes,
40 SkImageInfo::Make(
41 width,
42 height,
43 SkColorType::kAlpha_8_SkColorType,
44 SkAlphaType::kPremul_SkAlphaType)
45 );
46 mIsDirty = true;
47 }
48
49 if (mIsDirty) {
50 SkCanvas* maskCanvas = mMaskSurface->getCanvas();
Nader Jawad9238f592021-05-11 20:13:29 -070051 // Make sure to apply target transformation to the mask canvas
52 // to ensure the replayed drawing commands generate the same result
53 auto previousMatrix = displayList->mParentMatrix;
54 displayList->mParentMatrix = maskCanvas->getTotalMatrix();
55 maskCanvas->save();
Nader Jawad197743f2021-04-19 19:45:13 -070056 maskCanvas->drawColor(0, SkBlendMode::kClear);
57 TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver);
58 displayList->draw(&transformCanvas);
Nader Jawad9238f592021-05-11 20:13:29 -070059 maskCanvas->restore();
60 displayList->mParentMatrix = previousMatrix;
Nader Jawad197743f2021-04-19 19:45:13 -070061 }
62
63 sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot();
Nader Jawad6aff4812021-06-09 10:14:43 -070064 sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr);
Nader Jawad197743f2021-04-19 19:45:13 -070065
66 SkPaint maskPaint;
67 maskPaint.setShader(maskStretchShader);
68 maskPaint.setBlendMode(SkBlendMode::kDstOut);
69 canvas->drawRect(bounds, maskPaint);
70
71 mIsDirty = false;
72}