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 "TransformCanvas.h"
17
18 #include "FunctorDrawable.h"
19 #include "HolePunch.h"
20 #include "SkData.h"
21 #include "SkDrawable.h"
22 #include "SkMatrix.h"
23 #include "SkPaint.h"
24 #include "SkRect.h"
25 #include "SkRRect.h"
26
27 using namespace android::uirenderer::skiapipeline;
28
onDrawAnnotation(const SkRect & rect,const char * key,SkData * value)29 void TransformCanvas::onDrawAnnotation(const SkRect& rect, const char* key, SkData* value) {
30 if (HOLE_PUNCH_ANNOTATION == key) {
31 auto* rectParams = reinterpret_cast<const float*>(value->data());
32 const float radiusX = rectParams[0];
33 const float radiusY = rectParams[1];
34 const float alpha = rectParams[2];
35 SkRRect roundRect = SkRRect::MakeRectXY(rect, radiusX, radiusY);
36
37 SkPaint paint;
38 paint.setColor(SkColors::kBlack);
39 paint.setBlendMode(mHolePunchBlendMode);
40 paint.setAlphaf(alpha);
41 mWrappedCanvas->drawRRect(roundRect, paint);
42 }
43 }
44
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)45 void TransformCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
46 // TransformCanvas filters all drawing commands while maintaining the current
47 // clip stack and transformation. We need to draw most SkDrawables, since their
48 // draw calls may call methods that affect the clip stack and transformation. (Any
49 // actual draw commands will then be filtered out.) But FunctorDrawables are used
50 // as leaf nodes which issue self-contained OpenGL/Vulkan commands. These won't
51 // affect the clip stack + transformation, and in some cases cause problems (e.g. if
52 // the surface only has an alpha channel). See b/203960959
53 const auto* drawableName = drawable->getTypeName();
54 if (drawableName == nullptr || strcmp(drawableName, FunctorDrawable::TYPE_NAME) != 0) {
55 drawable->draw(this, matrix);
56 }
57 }
58
onFilter(SkPaint & paint) const59 bool TransformCanvas::onFilter(SkPaint& paint) const {
60 return false;
61 }
62