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