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 | #include "include/gpu/GpuTypes.h" // from Skia |
| 22 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 23 | #include "TransformCanvas.h" |
| 24 | #include "SkiaDisplayList.h" |
| 25 | |
| 26 | using android::uirenderer::StretchMask; |
| 27 | |
| 28 | void 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 Lubick | 8099b67 | 2023-01-09 14:48:45 +0000 | [diff] [blame^] | 41 | skgpu::Budgeted::kYes, |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 42 | 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 Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 53 | // 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 Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 58 | maskCanvas->drawColor(0, SkBlendMode::kClear); |
| 59 | TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver); |
| 60 | displayList->draw(&transformCanvas); |
Nader Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 61 | maskCanvas->restore(); |
| 62 | displayList->mParentMatrix = previousMatrix; |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot(); |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame] | 66 | sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 67 | |
| 68 | SkPaint maskPaint; |
| 69 | maskPaint.setShader(maskStretchShader); |
| 70 | maskPaint.setBlendMode(SkBlendMode::kDstOut); |
| 71 | canvas->drawRect(bounds, maskPaint); |
| 72 | |
| 73 | mIsDirty = false; |
| 74 | } |