• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/gpu/GpuTypes.h" // from Skia
22 
23 #include "TransformCanvas.h"
24 #include "SkiaDisplayList.h"
25 
26 using android::uirenderer::StretchMask;
27 
draw(GrRecordingContext * context,const StretchEffect & stretch,const SkRect & bounds,skiapipeline::SkiaDisplayList * displayList,SkCanvas * canvas)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,
41             skgpu::Budgeted::kYes,
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();
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();
58         maskCanvas->drawColor(0, SkBlendMode::kClear);
59         TransformCanvas transformCanvas(maskCanvas, SkBlendMode::kSrcOver);
60         displayList->draw(&transformCanvas);
61         maskCanvas->restore();
62         displayList->mParentMatrix = previousMatrix;
63     }
64 
65     sk_sp<SkImage> maskImage = mMaskSurface->makeImageSnapshot();
66     sk_sp<SkShader> maskStretchShader = stretch.getShader(width, height, maskImage, nullptr);
67 
68     SkPaint maskPaint;
69     maskPaint.setShader(maskStretchShader);
70     maskPaint.setBlendMode(SkBlendMode::kDstOut);
71     canvas->drawRect(bounds, maskPaint);
72 
73     mIsDirty = false;
74 }