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