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" |
| 17 | #include "SkSurface.h" |
| 18 | #include "SkCanvas.h" |
| 19 | #include "TransformCanvas.h" |
| 20 | #include "SkiaDisplayList.h" |
| 21 | |
| 22 | using android::uirenderer::StretchMask; |
| 23 | |
| 24 | void 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 Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 49 | // 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 Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 54 | maskCanvas->drawColor(0, SkBlendMode::kClear); |
| 55 | TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver); |
| 56 | displayList->draw(&transformCanvas); |
Nader Jawad | 9238f59 | 2021-05-11 20:13:29 -0700 | [diff] [blame] | 57 | maskCanvas->restore(); |
| 58 | displayList->mParentMatrix = previousMatrix; |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot(); |
Nader Jawad | 6aff481 | 2021-06-09 10:14:43 -0700 | [diff] [blame^] | 62 | sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 63 | |
| 64 | SkPaint maskPaint; |
| 65 | maskPaint.setShader(maskStretchShader); |
| 66 | maskPaint.setBlendMode(SkBlendMode::kDstOut); |
| 67 | canvas->drawRect(bounds, maskPaint); |
| 68 | |
| 69 | mIsDirty = false; |
| 70 | } |