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