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