Canvas.h revision 260ab726486317496bc12a57d599ea96dcde3284
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 22#include "GlFunctorLifecycleListener.h" 23#include "utils/Macros.h" 24#include "utils/NinePatch.h" 25 26#include <SkBitmap.h> 27#include <SkCanvas.h> 28#include <SkMatrix.h> 29 30namespace minikin { 31 class Layout; 32} 33 34namespace android { 35 36namespace uirenderer { 37 class CanvasPropertyPaint; 38 class CanvasPropertyPrimitive; 39 class DeferredLayerUpdater; 40 class DisplayList; 41 class RenderNode; 42} 43 44namespace SaveFlags { 45 46// These must match the corresponding Canvas API constants. 47enum { 48 Matrix = 0x01, 49 Clip = 0x02, 50 HasAlphaLayer = 0x04, 51 ClipToLayer = 0x10, 52 53 // Helper constant 54 MatrixClip = Matrix | Clip, 55}; 56typedef uint32_t Flags; 57 58} // namespace SaveFlags 59 60namespace uirenderer { 61class SkiaCanvasProxy; 62namespace VectorDrawable { 63class Tree; 64}; 65}; 66typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 67 68class Paint; 69struct Typeface; 70 71class ANDROID_API Canvas { 72public: 73 virtual ~Canvas() {}; 74 75 static Canvas* create_canvas(const SkBitmap& bitmap); 76 77 static WARN_UNUSED_RESULT Canvas* create_recording_canvas(int width, int height); 78 79 /** 80 * Create a new Canvas object which delegates to an SkCanvas. 81 * 82 * @param skiaCanvas Must not be NULL. All drawing calls will be 83 * delegated to this object. This function will call ref() on the 84 * SkCanvas, and the returned Canvas will unref() it upon 85 * destruction. 86 * @return new Canvas object. Will not return NULL. 87 */ 88 static Canvas* create_canvas(SkCanvas* skiaCanvas); 89 90 /** 91 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas. 92 * It is useful for testing and clients (e.g. Picture/Movie) that expect to 93 * draw their contents into an SkCanvas. 94 * 95 * The SkCanvas returned is *only* valid until another Canvas call is made 96 * that would change state (e.g. matrix or clip). Clients of asSkCanvas() 97 * are responsible for *not* persisting this pointer. 98 * 99 * Further, the returned SkCanvas should NOT be unref'd and is valid until 100 * this canvas is destroyed or a new bitmap is set. 101 */ 102 virtual SkCanvas* asSkCanvas() = 0; 103 104 105 virtual void setBitmap(const SkBitmap& bitmap) = 0; 106 107 virtual bool isOpaque() = 0; 108 virtual int width() = 0; 109 virtual int height() = 0; 110 111// ---------------------------------------------------------------------------- 112// View System operations (not exposed in public Canvas API) 113// ---------------------------------------------------------------------------- 114 115 virtual void resetRecording(int width, int height) = 0; 116 virtual uirenderer::DisplayList* finishRecording() = 0; 117 virtual void insertReorderBarrier(bool enableReorder) = 0; 118 119 virtual void setHighContrastText(bool highContrastText) = 0; 120 virtual bool isHighContrastText() = 0; 121 122 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 123 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, 124 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, 125 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0; 126 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 127 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius, 128 uirenderer::CanvasPropertyPaint* paint) = 0; 129 130 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0; 131 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0; 132 virtual void callDrawGLFunction(Functor* functor, 133 uirenderer::GlFunctorLifecycleListener* listener) = 0; 134 135// ---------------------------------------------------------------------------- 136// Canvas state operations 137// ---------------------------------------------------------------------------- 138 139 // Save (layer) 140 virtual int getSaveCount() const = 0; 141 virtual int save(SaveFlags::Flags flags) = 0; 142 virtual void restore() = 0; 143 virtual void restoreToCount(int saveCount) = 0; 144 145 virtual int saveLayer(float left, float top, float right, float bottom, 146 const SkPaint* paint, SaveFlags::Flags flags) = 0; 147 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 148 int alpha, SaveFlags::Flags flags) = 0; 149 150 // Matrix 151 virtual void getMatrix(SkMatrix* outMatrix) const = 0; 152 virtual void setMatrix(const SkMatrix& matrix) = 0; 153 154 virtual void concat(const SkMatrix& matrix) = 0; 155 virtual void rotate(float degrees) = 0; 156 virtual void scale(float sx, float sy) = 0; 157 virtual void skew(float sx, float sy) = 0; 158 virtual void translate(float dx, float dy) = 0; 159 160 // clip 161 virtual bool getClipBounds(SkRect* outRect) const = 0; 162 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; 163 virtual bool quickRejectPath(const SkPath& path) const = 0; 164 165 virtual bool clipRect(float left, float top, float right, float bottom, 166 SkRegion::Op op = SkRegion::kIntersect_Op) = 0; 167 virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0; 168 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0; 169 170 // filters 171 virtual SkDrawFilter* getDrawFilter() = 0; 172 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0; 173 174// ---------------------------------------------------------------------------- 175// Canvas draw operations 176// ---------------------------------------------------------------------------- 177 virtual void drawColor(int color, SkBlendMode mode) = 0; 178 virtual void drawPaint(const SkPaint& paint) = 0; 179 180 // Geometry 181 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0; 182 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0; 183 virtual void drawLine(float startX, float startY, float stopX, float stopY, 184 const SkPaint& paint) = 0; 185 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0; 186 virtual void drawRect(float left, float top, float right, float bottom, 187 const SkPaint& paint) = 0; 188 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0; 189 virtual void drawRoundRect(float left, float top, float right, float bottom, 190 float rx, float ry, const SkPaint& paint) = 0; 191 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0; 192 virtual void drawOval(float left, float top, float right, float bottom, 193 const SkPaint& paint) = 0; 194 virtual void drawArc(float left, float top, float right, float bottom, 195 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0; 196 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0; 197 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 198 const float* verts, const float* tex, const int* colors, 199 const uint16_t* indices, int indexCount, const SkPaint& paint) = 0; 200 201 // Bitmap-based 202 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, 203 const SkPaint* paint) = 0; 204 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, 205 const SkPaint* paint) = 0; 206 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 207 float srcRight, float srcBottom, float dstLeft, float dstTop, 208 float dstRight, float dstBottom, const SkPaint* paint) = 0; 209 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 210 const float* vertices, const int* colors, const SkPaint* paint) = 0; 211 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk, 212 float dstLeft, float dstTop, float dstRight, float dstBottom, 213 const SkPaint* paint) = 0; 214 215 /** 216 * Specifies if the positions passed to ::drawText are absolute or relative 217 * to the (x,y) value provided. 218 * 219 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need 220 * to be added to each glyph's position to get its absolute position. 221 */ 222 virtual bool drawTextAbsolutePos() const = 0; 223 224 /** 225 * Draws a VectorDrawable onto the canvas. 226 */ 227 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0; 228 229 /** 230 * Converts utf16 text to glyphs, calculating position and boundary, 231 * and delegating the final draw to virtual drawGlyphs method. 232 */ 233 void drawText(const uint16_t* text, int start, int count, int contextCount, 234 float x, float y, int bidiFlags, const Paint& origPaint, Typeface* typeface); 235 236 void drawTextOnPath(const uint16_t* text, int count, int bidiFlags, const SkPath& path, 237 float hOffset, float vOffset, const Paint& paint, Typeface* typeface); 238 239protected: 240 void drawTextDecorations(float x, float y, float length, const SkPaint& paint); 241 242 /** 243 * drawText: count is of glyphs 244 * totalAdvance: used to define width of text decorations (underlines, strikethroughs). 245 */ 246 virtual void drawGlyphs(const uint16_t* glyphs, const float* positions, int count, 247 const SkPaint& paint, float x, float y, 248 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 249 float totalAdvance) = 0; 250 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 251 const SkPaint& paint, const SkPath& path, size_t start, size_t end) = 0; 252 friend class DrawTextFunctor; 253 friend class DrawTextOnPathFunctor; 254 friend class uirenderer::SkiaCanvasProxy; 255}; 256 257}; // namespace android 258