blob: e17f835031bb3a16391b1328c9b7c2cb254a0379 [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
18#include "hwui/Canvas.h"
19#include "CanvasProperty.h"
20#include "DeferredLayerUpdater.h"
21#include "RenderNode.h"
22#include "VectorDrawable.h"
23
24#include <SkCanvas.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040025#include <SkTLazy.h>
Derek Sollenbergerc1908132016-07-15 10:28:16 -040026
27namespace android {
28
29// Holds an SkCanvas reference plus additional native data.
30class SkiaCanvas : public Canvas {
31public:
32 explicit SkiaCanvas(const SkBitmap& bitmap);
33
34 /**
35 * Create a new SkiaCanvas.
36 *
37 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
Mike Reed6acfe162016-11-18 17:21:09 -050038 * not be NULL. This constructor does not take ownership, so the caller
39 * must guarantee that it remains valid while the SkiaCanvas is valid.
Derek Sollenbergerc1908132016-07-15 10:28:16 -040040 */
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040041 explicit SkiaCanvas(SkCanvas* canvas);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040042
Stan Iliev021693b2016-10-17 16:26:15 -040043 virtual ~SkiaCanvas();
44
Derek Sollenbergerc1908132016-07-15 10:28:16 -040045 virtual SkCanvas* asSkCanvas() override {
Mike Reed6acfe162016-11-18 17:21:09 -050046 return mCanvas;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040047 }
48
Stan Ilievc0e7a902016-10-13 17:07:09 -040049 virtual void resetRecording(int width, int height,
50 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
73 virtual int saveLayer(float left, float top, float right, float bottom,
74 const SkPaint* paint, SaveFlags::Flags flags) override;
75 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
76 int alpha, SaveFlags::Flags flags) override;
77
78 virtual void getMatrix(SkMatrix* outMatrix) const override;
79 virtual void setMatrix(const SkMatrix& matrix) override;
80 virtual void concat(const SkMatrix& matrix) override;
81 virtual void rotate(float degrees) override;
82 virtual void scale(float sx, float sy) override;
83 virtual void skew(float sx, float sy) override;
84 virtual void translate(float dx, float dy) override;
85
86 virtual bool getClipBounds(SkRect* outRect) const override;
87 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
88 virtual bool quickRejectPath(const SkPath& path) const override;
89 virtual bool clipRect(float left, float top, float right, float bottom,
Mike Reed6e49c9f2016-12-02 15:36:59 -050090 SkClipOp op) override;
91 virtual bool clipPath(const SkPath* path, SkClipOp op) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -040092
93 virtual SkDrawFilter* getDrawFilter() override;
94 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
95
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,
104 const SkPaint& paint) override;
105 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
106 virtual void drawRect(float left, float top, float right, float bottom,
107 const SkPaint& paint) override;
108 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
109 virtual void drawRoundRect(float left, float top, float right, float bottom,
110 float rx, float ry, const SkPaint& paint) override;
111 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
112 virtual void drawOval(float left, float top, float right, float bottom,
113 const SkPaint& paint) override;
114 virtual void drawArc(float left, float top, float right, float bottom,
115 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
116 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Mike Reed826deef2017-04-04 15:32:04 -0400117 virtual void drawVertices(const SkVertices*, SkBlendMode, const SkPaint& paint) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400118
sergeyvaed7f582016-10-14 16:30:21 -0700119 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override;
sergeyvfc9999502016-10-17 13:07:38 -0700120 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override;
121 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400122 float srcRight, float srcBottom, float dstLeft, float dstTop,
123 float dstRight, float dstBottom, const SkPaint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700124 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400125 const float* vertices, const int* colors, const SkPaint* paint) override;
sergeyv5fd2a1c2016-10-20 15:04:28 -0700126 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400127 float dstLeft, float dstTop, float dstRight, float dstBottom,
128 const SkPaint* paint) override;
129
130 virtual bool drawTextAbsolutePos() const override { return true; }
131 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
132
133 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
134 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
135 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
136 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override;
137 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
138 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
139 uirenderer::CanvasPropertyPaint* paint) override;
140
141 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
142 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
143 virtual void callDrawGLFunction(Functor* functor,
144 uirenderer::GlFunctorLifecycleListener* listener) override;
145
146protected:
Stan Ilievf50806a2016-10-24 10:40:39 -0400147 SkiaCanvas();
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400148 void reset(SkCanvas* skiaCanvas);
149 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
150
Stan Iliev0b58d992017-03-30 18:22:27 -0400151 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
152 float y, float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400153 float totalAdvance) override;
Yuqian Liafc221492016-07-18 13:07:42 -0400154 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
155 const SkPaint& paint, const SkPath& path, size_t start, size_t end) override;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400156
157private:
158 struct SaveRec {
159 int saveCount;
160 SaveFlags::Flags saveFlags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400161 size_t clipIndex;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400162 };
163
Stan Ilievf50806a2016-10-24 10:40:39 -0400164 const SaveRec* currentSaveRec() const;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400165 void recordPartialSave(SaveFlags::Flags flags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400166
167 template<typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500168 void recordClip(const T&, SkClipOp);
Stan Ilievf50806a2016-10-24 10:40:39 -0400169 void applyPersistentClips(size_t clipStartIndex);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400170
171 void drawPoints(const float* points, int count, const SkPaint& paint,
172 SkCanvas::PointMode mode);
173
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400174 const SkPaint* addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
175 sk_sp<SkColorFilter> colorSpaceFilter);
176
Stan Ilievf50806a2016-10-24 10:40:39 -0400177 class Clip;
178
Matt Sarettea70d222017-03-29 16:25:10 -0400179 std::unique_ptr<SkCanvas> mCanvasWrapper; // might own a wrapper on the canvas
Mike Reed6acfe162016-11-18 17:21:09 -0500180 std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
181 SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us
182 // unless it is the same as mCanvasOwned.get()
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400183 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
Stan Ilievf50806a2016-10-24 10:40:39 -0400184 std::vector<Clip> mClipStack; // tracks persistent clips.
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400185};
186
187} // namespace android
188