blob: 3fe2bce06b41202fb628c93feb1e3f72bf2563b6 [file] [log] [blame]
Derek Sollenbergerc1908132016-07-15 10:28:16 -04001/*
2 * Copyright (C) 2016 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#pragma once
17
Derek Sollenbergerc1908132016-07-15 10:28:16 -040018#include "CanvasProperty.h"
19#include "DeferredLayerUpdater.h"
20#include "RenderNode.h"
21#include "VectorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "hwui/Canvas.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040023
24#include <SkCanvas.h>
Ben Wagner0ed10be2018-06-28 17:08:16 -040025
26#include <cassert>
27#include <optional>
Derek Sollenbergerc1908132016-07-15 10:28:16 -040028
29namespace android {
30
31// Holds an SkCanvas reference plus additional native data.
32class SkiaCanvas : public Canvas {
33public:
34 explicit SkiaCanvas(const SkBitmap& bitmap);
35
36 /**
37 * Create a new SkiaCanvas.
38 *
39 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
Mike Reed6acfe162016-11-18 17:21:09 -050040 * not be NULL. This constructor does not take ownership, so the caller
41 * must guarantee that it remains valid while the SkiaCanvas is valid.
Derek Sollenbergerc1908132016-07-15 10:28:16 -040042 */
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040043 explicit SkiaCanvas(SkCanvas* canvas);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040044
Stan Iliev021693b2016-10-17 16:26:15 -040045 virtual ~SkiaCanvas();
46
John Reck1bcacfd2017-11-03 10:12:19 -070047 virtual SkCanvas* asSkCanvas() override { return mCanvas; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040048
Stan Ilievc0e7a902016-10-13 17:07:09 -040049 virtual void resetRecording(int width, int height,
John Reck1bcacfd2017-11-03 10:12:19 -070050 uirenderer::RenderNode* renderNode) override {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040051 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
52 }
53
54 virtual uirenderer::DisplayList* finishRecording() override {
55 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
56 return nullptr;
57 }
58 virtual void insertReorderBarrier(bool enableReorder) override {
59 LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers");
60 }
61
62 virtual void setBitmap(const SkBitmap& bitmap) override;
63
64 virtual bool isOpaque() override;
65 virtual int width() override;
66 virtual int height() override;
67
Derek Sollenbergerc1908132016-07-15 10:28:16 -040068 virtual int getSaveCount() const override;
69 virtual int save(SaveFlags::Flags flags) override;
70 virtual void restore() override;
71 virtual void restoreToCount(int saveCount) override;
72
John Reck1bcacfd2017-11-03 10:12:19 -070073 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
74 SaveFlags::Flags flags) override;
75 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
76 SaveFlags::Flags flags) override;
Derek Sollenberger24fc9012018-12-07 14:12:12 -050077 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040078
79 virtual void getMatrix(SkMatrix* outMatrix) const override;
80 virtual void setMatrix(const SkMatrix& matrix) override;
81 virtual void concat(const SkMatrix& matrix) override;
82 virtual void rotate(float degrees) override;
83 virtual void scale(float sx, float sy) override;
84 virtual void skew(float sx, float sy) override;
85 virtual void translate(float dx, float dy) override;
86
87 virtual bool getClipBounds(SkRect* outRect) const override;
88 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
89 virtual bool quickRejectPath(const SkPath& path) const override;
John Reck1bcacfd2017-11-03 10:12:19 -070090 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override;
Mike Reed6e49c9f2016-12-02 15:36:59 -050091 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040092
Ben Wagner0ed10be2018-06-28 17:08:16 -040093 virtual PaintFilter* getPaintFilter() override;
94 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040095
Matt Sarettd0814db2017-04-13 09:33:18 -040096 virtual SkCanvasState* captureCanvasState() const override;
97
Mike Reed260ab722016-10-07 15:59:20 -040098 virtual void drawColor(int color, SkBlendMode mode) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040099 virtual void drawPaint(const SkPaint& paint) override;
100
101 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
102 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
103 virtual void drawLine(float startX, float startY, float stopX, float stopY,
John Reck1bcacfd2017-11-03 10:12:19 -0700104 const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400105 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
106 virtual void drawRect(float left, float top, float right, float bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700107 const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400108 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700109 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
110 const SkPaint& paint) override;
Nader Jawadadfe1d92018-09-27 12:27:36 -0700111
112 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
113 const SkPaint& paint) override;
114
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400115 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
116 virtual void drawOval(float left, float top, float right, float bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700117 const SkPaint& paint) override;
118 virtual void drawArc(float left, float top, float right, float bottom, float startAngle,
119 float sweepAngle, bool useCenter, const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400120 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Mike Reed826deef2017-04-04 15:32:04 -0400121 virtual void drawVertices(const SkVertices*, SkBlendMode, const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400122
sergeyvaed7f582016-10-14 16:30:21 -0700123 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override;
sergeyvfc9999502016-10-17 13:07:38 -0700124 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override;
John Reck1bcacfd2017-11-03 10:12:19 -0700125 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
126 float srcBottom, float dstLeft, float dstTop, float dstRight,
127 float dstBottom, const SkPaint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700128 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700129 const float* vertices, const int* colors,
130 const SkPaint* paint) override;
131 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft,
132 float dstTop, float dstRight, float dstBottom,
133 const SkPaint* paint) override;
Derek Sollenberger2d142132018-01-22 10:25:26 -0500134 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400135
John Reck1bcacfd2017-11-03 10:12:19 -0700136 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400137 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
138
139 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700140 uirenderer::CanvasPropertyPrimitive* top,
141 uirenderer::CanvasPropertyPrimitive* right,
142 uirenderer::CanvasPropertyPrimitive* bottom,
143 uirenderer::CanvasPropertyPrimitive* rx,
144 uirenderer::CanvasPropertyPrimitive* ry,
145 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400146 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
John Reck1bcacfd2017-11-03 10:12:19 -0700147 uirenderer::CanvasPropertyPrimitive* y,
148 uirenderer::CanvasPropertyPrimitive* radius,
149 uirenderer::CanvasPropertyPaint* paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400150
151 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
152 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
153 virtual void callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700154 uirenderer::GlFunctorLifecycleListener* listener) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400155
156protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400157 SkiaCanvas();
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400158 void reset(SkCanvas* skiaCanvas);
159 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
160
Mike Reed2dfd55d2019-01-08 16:19:03 -0500161 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700162 float y, float boundsLeft, float boundsTop, float boundsRight,
163 float boundsBottom, float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400164 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500165 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700166 size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400167
Ben Wagner0ed10be2018-06-28 17:08:16 -0400168 /** This class acts as a copy on write SkPaint.
169 *
170 * Initially this will be the SkPaint passed to the contructor.
171 * The first time writable() is called this will become a copy of the
172 * initial SkPaint (or a default SkPaint if nullptr).
173 */
174 struct PaintCoW {
175 PaintCoW(const SkPaint& that) : mPtr(&that) {}
176 PaintCoW(const SkPaint* ptr) : mPtr(ptr) {}
177 PaintCoW(const PaintCoW&) = delete;
178 PaintCoW(PaintCoW&&) = delete;
179 PaintCoW& operator=(const PaintCoW&) = delete;
180 PaintCoW& operator=(PaintCoW&&) = delete;
181 SkPaint& writeable() {
182 if (!mStorage) {
183 if (!mPtr) {
184 mStorage.emplace();
185 } else {
186 mStorage.emplace(*mPtr);
187 }
188 mPtr = &*mStorage;
189 }
190 return *mStorage;
191 }
192 operator const SkPaint*() const { return mPtr; }
193 const SkPaint* operator->() const { assert(mPtr); return mPtr; }
194 const SkPaint& operator*() const { assert(mPtr); return *mPtr; }
195 explicit operator bool() { return mPtr != nullptr; }
196 private:
197 const SkPaint* mPtr;
198 std::optional<SkPaint> mStorage;
199 };
200
201 /** Filters the paint using the current paint filter.
202 *
203 * @param paint the paint to filter. Will be initialized with the default
204 * SkPaint before filtering if filtering is required.
205 */
206 PaintCoW&& filterPaint(PaintCoW&& paint) const;
207
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400208private:
209 struct SaveRec {
John Reck1bcacfd2017-11-03 10:12:19 -0700210 int saveCount;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400211 SaveFlags::Flags saveFlags;
John Reck1bcacfd2017-11-03 10:12:19 -0700212 size_t clipIndex;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400213 };
214
Stan Ilievf50806a2016-10-24 10:40:39 -0400215 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400216 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400217
John Reck1bcacfd2017-11-03 10:12:19 -0700218 template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500219 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400220 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400221
John Reck1bcacfd2017-11-03 10:12:19 -0700222 void drawPoints(const float* points, int count, const SkPaint& paint, SkCanvas::PointMode mode);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400223
Ben Wagner0ed10be2018-06-28 17:08:16 -0400224 /** Filters the paint for bitmap drawing.
225 *
226 * After filtering the paint for bitmap drawing,
227 * also calls filterPaint on the paint.
228 *
229 * @param paint the paint to filter. Will be initialized with the default
230 * SkPaint before filtering if filtering is required.
231 */
232 PaintCoW&& filterBitmap(PaintCoW&& paint, sk_sp<SkColorFilter> colorSpaceFilter) const;
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400233
Stan Ilievf50806a2016-10-24 10:40:39 -0400234 class Clip;
235
John Reck1bcacfd2017-11-03 10:12:19 -0700236 std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
237 SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us
238 // unless it is the same as mCanvasOwned.get()
239 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
240 std::vector<Clip> mClipStack; // tracks persistent clips.
Ben Wagner0ed10be2018-06-28 17:08:16 -0400241 sk_sp<PaintFilter> mPaintFilter;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400242};
243
John Reck1bcacfd2017-11-03 10:12:19 -0700244} // namespace android