Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Lubick | 4e8ce46 | 2022-12-01 20:29:16 +0000 | [diff] [blame] | 17 | |
| 18 | #include "SkBlendMode.h" |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 19 | #include "SkCanvas.h" |
Kevin Lubick | 4e8ce46 | 2022-12-01 20:29:16 +0000 | [diff] [blame] | 20 | #include "SkSurface.h" |
Kevin Lubick | 8099b67 | 2023-01-09 14:48:45 +0000 | [diff] [blame] | 21 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 22 | #include "TransformCanvas.h" |
| 23 | #include "SkiaDisplayList.h" |
| 24 | |
| 25 | using android::uirenderer::StretchMask; |
| 26 | |
Kevin Lubick | a0fccc3 | 2023-05-02 13:31:00 +0000 | [diff] [blame] | 27 | void StretchMask::draw(GrRecordingContext*, |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 28 | 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 Lubick | a0fccc3 | 2023-05-02 13:31:00 +0000 | [diff] [blame] | 37 | // 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 Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 45 | mIsDirty = true; |
| 46 | } |
| 47 | |
| 48 | if (mIsDirty) { |
| 49 | SkCanvas* maskCanvas = mMaskSurface->getCanvas(); |
Nader Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 50 | // 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 Lubick | a0fccc3 | 2023-05-02 13:31:00 +0000 | [diff] [blame] | 53 | displayList->mParentMatrix = maskCanvas->getLocalToDeviceAs3x3(); |
Nader Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 54 | maskCanvas->save(); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 55 | maskCanvas->drawColor(0, SkBlendMode::kClear); |
| 56 | TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver); |
| 57 | displayList->draw(&transformCanvas); |
Nader Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 58 | maskCanvas->restore(); |
| 59 | displayList->mParentMatrix = previousMatrix; |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot(); |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 63 | sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 64 | |
| 65 | SkPaint maskPaint; |
| 66 | maskPaint.setShader(maskStretchShader); |
| 67 | maskPaint.setBlendMode(SkBlendMode::kDstOut); |
| 68 | canvas->drawRect(bounds, maskPaint); |
| 69 | |
| 70 | mIsDirty = false; |
| 71 | } |