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
18 #include "SkBlendMode.h"
19 #include "SkCanvas.h"
20 #include "SkSurface.h"
21
22 #include "TransformCanvas.h"
23 #include "SkiaDisplayList.h"
24
25 using android::uirenderer::StretchMask;
26
draw(GrRecordingContext *,const StretchEffect & stretch,const SkRect & bounds,skiapipeline::SkiaDisplayList * displayList,SkCanvas * canvas)27 void StretchMask::draw(GrRecordingContext*,
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
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 ));
45 mIsDirty = true;
46 }
47
48 if (mIsDirty) {
49 SkCanvas* maskCanvas = mMaskSurface->getCanvas();
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;
53 displayList->mParentMatrix = maskCanvas->getLocalToDeviceAs3x3();
54 maskCanvas->save();
55 maskCanvas->drawColor(0, SkBlendMode::kClear);
56 TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver);
57 displayList->draw(&transformCanvas);
58 maskCanvas->restore();
59 displayList->mParentMatrix = previousMatrix;
60 }
61
62 sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot();
63 sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr);
64
65 SkPaint maskPaint;
66 maskPaint.setShader(maskStretchShader);
67 maskPaint.setBlendMode(SkBlendMode::kDstOut);
68 canvas->drawRect(bounds, maskPaint);
69
70 mIsDirty = false;
71 }