1 /*
2  * Copyright (C) 2020 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 
17 #pragma once
18 
19 #include <SkCanvasVirtualEnforcer.h>
20 #include <SkNoDrawCanvas.h>
21 
22 enum class SkBlendMode;
23 
24 namespace android {
25 namespace uirenderer {
26 namespace test {
27 
28 class CallCountingCanvas final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
29 private:
30     int START_MARKER;
31 public:
CallCountingCanvas()32     CallCountingCanvas() : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(1, 1) {}
33 
sumTotalDrawCalls()34     int sumTotalDrawCalls() {
35         // Dirty hack assumes we're nothing but ints between START_MARKET and END_MARKER
36         int* cur = &START_MARKER + 1;
37         int* end = &END_MARKER;
38         int sum = 0;
39         while (cur != end) {
40             sum += *cur;
41             cur++;
42         }
43         return sum;
44     }
45 
46     int drawPaintCount = 0;
onDrawPaint(const SkPaint & paint)47     void onDrawPaint(const SkPaint& paint) override {
48         drawPaintCount++;
49     }
50 
51     int drawBehindCount = 0;
onDrawBehind(const SkPaint &)52     void onDrawBehind(const SkPaint&) override {
53         drawBehindCount++;
54     }
55 
56     int drawRectCount = 0;
onDrawRect(const SkRect & rect,const SkPaint & paint)57     void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
58         drawRectCount++;
59     }
60 
61     int drawRRectCount = 0;
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)62     void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override {
63         drawRRectCount++;
64     }
65 
66     int drawDRRectCount = 0;
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)67     void onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
68                       const SkPaint& paint) override {
69         drawDRRectCount++;
70     }
71 
72     int drawOvalCount = 0;
onDrawOval(const SkRect & rect,const SkPaint & paint)73     void onDrawOval(const SkRect& rect, const SkPaint& paint) override {
74         drawOvalCount++;
75     }
76 
77     int drawArcCount = 0;
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)78     void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
79                    const SkPaint& paint) override {
80         drawArcCount++;
81     }
82 
83     int drawPathCount = 0;
onDrawPath(const SkPath & path,const SkPaint & paint)84     void onDrawPath(const SkPath& path, const SkPaint& paint) override {
85         drawPathCount++;
86     }
87 
88     int drawRegionCount = 0;
onDrawRegion(const SkRegion & region,const SkPaint & paint)89     void onDrawRegion(const SkRegion& region, const SkPaint& paint) override {
90         drawRegionCount++;
91     }
92 
93     int drawTextBlobCount = 0;
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)94     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
95                         const SkPaint& paint) override {
96         drawTextBlobCount++;
97     }
98 
99     int drawPatchCount = 0;
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode mode,const SkPaint & paint)100     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
101                      const SkPoint texCoords[4], SkBlendMode mode,
102                      const SkPaint& paint) override {
103         drawPatchCount++;
104     }
105 
106     int drawPoints = 0;
onDrawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)107     void onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
108                       const SkPaint& paint) override {
109         drawPoints++;
110     }
111 
112     int drawImageRectCount = 0;
onDrawImageRect2(const SkImage *,const SkRect &,const SkRect &,const SkSamplingOptions &,const SkPaint *,SkCanvas::SrcRectConstraint)113     void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
114                           const SkPaint*, SkCanvas::SrcRectConstraint) override {
115         drawImageRectCount++;
116     }
117 
118     int drawImageLatticeCount = 0;
onDrawImageLattice2(const SkImage * image,const SkCanvas::Lattice & lattice,const SkRect & dst,SkFilterMode,const SkPaint * paint)119     void onDrawImageLattice2(const SkImage* image, const SkCanvas::Lattice& lattice,
120                              const SkRect& dst, SkFilterMode, const SkPaint* paint) override {
121         drawImageLatticeCount++;
122     }
123 
124     int drawAtlasCount = 0;
onDrawAtlas2(const SkImage * atlas,const SkRSXform xform[],const SkRect rect[],const SkColor colors[],int count,SkBlendMode mode,const SkSamplingOptions &,const SkRect * cull,const SkPaint * paint)125     void onDrawAtlas2(const SkImage* atlas, const SkRSXform xform[], const SkRect rect[],
126                       const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions&,
127                       const SkRect* cull, const SkPaint* paint) override {
128         drawAtlasCount++;
129     }
130 
131     int drawAnnotationCount = 0;
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)132     void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override {
133         drawAnnotationCount++;
134     }
135 
136     int drawShadowRecCount = 0;
onDrawShadowRec(const SkPath &,const SkDrawShadowRec &)137     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override {
138         drawShadowRecCount++;
139     }
140 
141     int drawDrawableCount = 0;
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)142     void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override {
143         drawDrawableCount++;
144     }
145 
146     int drawPictureCount = 0;
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)147     void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
148                        const SkPaint* paint) override {
149         drawPictureCount++;
150     }
151 
152     int drawVerticesCount = 0;
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)153     void onDrawVerticesObject (const SkVertices *vertices, SkBlendMode mode,
154                         const SkPaint &paint) override {
155         drawVerticesCount++;
156     }
157 
158 private:
159     int END_MARKER;
160 };
161 
162 } /* namespace test */
163 } /* namespace uirenderer */
164 } /* namespace android */
165