1 /* 2 * Copyright (C) 2014 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 <SaveFlags.h> 20 #include <SkBitmap.h> 21 #include <SkCanvas.h> 22 #include <SkMatrix.h> 23 #include <androidfw/ResourceTypes.h> 24 #include <cutils/compiler.h> 25 #include <utils/Functor.h> 26 27 #include "Properties.h" 28 #include "pipeline/skia/AnimatedDrawables.h" 29 #include "utils/Macros.h" 30 31 class SkAnimatedImage; 32 enum class SkBlendMode; 33 class SkCanvasState; 34 class SkRRect; 35 class SkRuntimeShaderBuilder; 36 class SkVertices; 37 class Mesh; 38 39 namespace minikin { 40 class Font; 41 class Layout; 42 class MeasuredText; 43 enum class Bidi : uint8_t; 44 } 45 46 namespace android { 47 class PaintFilter; 48 49 namespace uirenderer { 50 class CanvasPropertyPaint; 51 class CanvasPropertyPrimitive; 52 class DeferredLayerUpdater; 53 class RenderNode; 54 namespace VectorDrawable { 55 class Tree; 56 } 57 } 58 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 59 60 typedef std::function<void(uint16_t* text, float* positions)> ReadGlyphFunc; 61 62 class AnimatedImageDrawable; 63 class Bitmap; 64 class Paint; 65 struct Typeface; 66 67 enum class DrawTextBlobMode { 68 Normal, 69 HctOutline, 70 HctInner, 71 }; 72 73 inline DrawTextBlobMode gDrawTextBlobMode = DrawTextBlobMode::Normal; 74 75 class ANDROID_API Canvas { 76 public: ~Canvas()77 virtual ~Canvas(){}; 78 79 static Canvas* create_canvas(const SkBitmap& bitmap); 80 81 /** 82 * Create a new Canvas object that records view system drawing operations for deferred 83 * rendering. A canvas returned by this call supports calls to the resetRecording(...) and 84 * finishRecording() calls. The latter call returns a DisplayList that is specific to the 85 * RenderPipeline defined by Properties::getRenderPipelineType(). 86 * 87 * @param width of the requested Canvas. 88 * @param height of the requested Canvas. 89 * @param renderNode is an optional parameter that specifies the node that will consume the 90 * DisplayList produced by the returned Canvas. This enables the reuse of select C++ 91 * objects as a speed optimization. 92 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 93 determined based on Properties::getRenderPipelineType(). 94 * 95 */ 96 static WARN_UNUSED_RESULT Canvas* create_recording_canvas( 97 int width, int height, uirenderer::RenderNode* renderNode = nullptr); 98 99 /** 100 * Create a new Canvas object which delegates to an SkCanvas. 101 * 102 * @param skiaCanvas Must not be NULL. All drawing calls will be 103 * delegated to this object. This function will call ref() on the 104 * SkCanvas, and the returned Canvas will unref() it upon 105 * destruction. 106 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 107 * determined based on Properties::getRenderPipelineType(). 108 */ 109 static Canvas* create_canvas(SkCanvas* skiaCanvas); 110 111 /** 112 * Sets the target SDK version used to build the app. 113 * 114 * @param apiLevel API level 115 * 116 */ 117 static void setCompatibilityVersion(int apiLevel); 118 119 virtual void setBitmap(const SkBitmap& bitmap) = 0; 120 121 virtual bool isOpaque() = 0; 122 virtual int width() = 0; 123 virtual int height() = 0; 124 125 // ---------------------------------------------------------------------------- 126 // View System operations (not exposed in public Canvas API) 127 // ---------------------------------------------------------------------------- 128 129 virtual void resetRecording(int width, int height, 130 uirenderer::RenderNode* renderNode = nullptr) = 0; 131 virtual void finishRecording(uirenderer::RenderNode* destination) = 0; 132 virtual void enableZ(bool enableZ) = 0; 133 isHighContrastText()134 bool isHighContrastText() const { return uirenderer::Properties::enableHighContrastText; } 135 136 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 137 uirenderer::CanvasPropertyPrimitive* top, 138 uirenderer::CanvasPropertyPrimitive* right, 139 uirenderer::CanvasPropertyPrimitive* bottom, 140 uirenderer::CanvasPropertyPrimitive* rx, 141 uirenderer::CanvasPropertyPrimitive* ry, 142 uirenderer::CanvasPropertyPaint* paint) = 0; 143 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 144 uirenderer::CanvasPropertyPrimitive* y, 145 uirenderer::CanvasPropertyPrimitive* radius, 146 uirenderer::CanvasPropertyPaint* paint) = 0; 147 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) = 0; 148 149 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0; 150 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0; 151 drawWebViewFunctor(int)152 virtual void drawWebViewFunctor(int /*functor*/) { 153 LOG_ALWAYS_FATAL("Not supported"); 154 } 155 156 virtual void punchHole(const SkRRect& rect, float alpha) = 0; 157 158 // ---------------------------------------------------------------------------- 159 // Canvas state operations 160 // ---------------------------------------------------------------------------- 161 162 // Save (layer) 163 virtual int getSaveCount() const = 0; 164 virtual int save(SaveFlags::Flags flags) = 0; 165 virtual void restore() = 0; 166 virtual void restoreToCount(int saveCount) = 0; 167 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) = 0; 168 169 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) = 0; 170 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) = 0; 171 virtual int saveUnclippedLayer(int, int, int, int) = 0; 172 173 // Matrix 174 virtual void getMatrix(SkMatrix* outMatrix) const = 0; 175 virtual void setMatrix(const SkMatrix& matrix) = 0; 176 177 virtual void concat(const SkMatrix& matrix) = 0; 178 virtual void rotate(float degrees) = 0; 179 virtual void scale(float sx, float sy) = 0; 180 virtual void skew(float sx, float sy) = 0; 181 virtual void translate(float dx, float dy) = 0; 182 183 // clip 184 virtual bool getClipBounds(SkRect* outRect) const = 0; 185 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; 186 virtual bool quickRejectPath(const SkPath& path) const = 0; 187 188 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) = 0; 189 virtual bool clipPath(const SkPath* path, SkClipOp op) = 0; 190 // Resets clip to wide open, used to emulate the now-removed SkClipOp::kReplace on 191 // apps with compatibility < P. Canvases for version P and later are restricted to 192 // intersect and difference at the Java level, matching SkClipOp's definition. 193 // NOTE: These functions are deprecated and will be removed in a future release 194 virtual bool replaceClipRect_deprecated(float left, float top, float right, float bottom) = 0; 195 virtual bool replaceClipPath_deprecated(const SkPath* path) = 0; 196 197 // filters 198 virtual PaintFilter* getPaintFilter() = 0; 199 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) = 0; 200 201 // WebView only captureCanvasState()202 virtual SkCanvasState* captureCanvasState() const { return nullptr; } 203 204 // ---------------------------------------------------------------------------- 205 // Canvas draw operations 206 // ---------------------------------------------------------------------------- 207 virtual void drawColor(int color, SkBlendMode mode) = 0; 208 virtual void drawPaint(const Paint& paint) = 0; 209 210 // Geometry 211 virtual void drawPoint(float x, float y, const Paint& paint) = 0; 212 virtual void drawPoints(const float* points, int floatCount, const Paint& paint) = 0; 213 virtual void drawLine(float startX, float startY, float stopX, float stopY, 214 const Paint& paint) = 0; 215 virtual void drawLines(const float* points, int floatCount, const Paint& paint) = 0; 216 virtual void drawRect(float left, float top, float right, float bottom, 217 const Paint& paint) = 0; 218 virtual void drawRegion(const SkRegion& region, const Paint& paint) = 0; 219 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 220 const Paint& paint) = 0; 221 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner, 222 const Paint& paint) = 0; 223 virtual void drawCircle(float x, float y, float radius, const Paint& paint) = 0; 224 virtual void drawOval(float left, float top, float right, float bottom, 225 const Paint& paint) = 0; 226 virtual void drawArc(float left, float top, float right, float bottom, float startAngle, 227 float sweepAngle, bool useCenter, const Paint& paint) = 0; 228 virtual void drawPath(const SkPath& path, const Paint& paint) = 0; 229 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) = 0; 230 virtual void drawMesh(const Mesh& mesh, sk_sp<SkBlender>, const Paint& paint) = 0; 231 232 // Bitmap-based 233 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) = 0; 234 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) = 0; 235 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, 236 float srcBottom, float dstLeft, float dstTop, float dstRight, 237 float dstBottom, const Paint* paint) = 0; 238 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 239 const float* vertices, const int* colors, const Paint* paint) = 0; 240 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, 241 float dstTop, float dstRight, float dstBottom, 242 const Paint* paint) = 0; 243 244 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) = 0; 245 virtual void drawPicture(const SkPicture& picture) = 0; 246 247 /** 248 * Draws a VectorDrawable onto the canvas. 249 */ 250 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0; 251 252 void drawGlyphs(const minikin::Font& font, const int* glyphIds, const float* positions, 253 int glyphCount, const Paint& paint); 254 255 /** 256 * Converts utf16 text to glyphs, calculating position and boundary, 257 * and delegating the final draw to virtual drawGlyphs method. 258 */ 259 void drawText(const uint16_t* text, int textSize, int start, int count, int contextStart, 260 int contextCount, float x, float y, minikin::Bidi bidiFlags, 261 const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt); 262 263 void drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags, 264 const SkPath& path, float hOffset, float vOffset, const Paint& paint, 265 const Typeface* typeface); 266 267 void drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight, 268 float outerBottom, float outerRx, float outerRy, float innerLeft, 269 float innerTop, float innerRight, float innerBottom, float innerRx, 270 float innerRy, const Paint& paint); 271 272 void drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight, 273 float outerBottom, const float* outerRadii, float innerLeft, 274 float innerTop, float innerRight, float innerBottom, 275 const float* innerRadii, const Paint& paint); 276 GetApiLevel()277 static int GetApiLevel() { return sApiLevel; } 278 279 protected: 280 void drawTextDecorations(float x, float y, float length, const Paint& paint); 281 282 /** 283 * glyphFunc: valid only for the duration of the call and should not be cached. 284 * drawText: count is of glyphs 285 * totalAdvance: used to define width of text decorations (underlines, strikethroughs). 286 */ 287 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x, 288 float y,float totalAdvance) = 0; 289 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 290 const Paint& paint, const SkPath& path, size_t start, 291 size_t end) = 0; 292 static int sApiLevel; 293 294 friend class DrawTextFunctor; 295 friend class DrawTextOnPathFunctor; 296 }; 297 298 } // namespace android 299