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 #ifndef ANDROID_GRAPHICS_CANVAS_H
     18 #define ANDROID_GRAPHICS_CANVAS_H
     19 
     20 #include <cutils/compiler.h>
     21 
     22 #include <SkBitmap.h>
     23 #include <SkCanvas.h>
     24 #include <SkMatrix.h>
     25 
     26 namespace android {
     27 
     28 class ANDROID_API Canvas {
     29 public:
     30     virtual ~Canvas() {};
     31 
     32     static Canvas* create_canvas(const SkBitmap& bitmap);
     33 
     34     /**
     35      *  Create a new Canvas object which delegates to an SkCanvas.
     36      *
     37      *  @param skiaCanvas Must not be NULL. All drawing calls will be
     38      *      delegated to this object. This function will call ref() on the
     39      *      SkCanvas, and the returned Canvas will unref() it upon
     40      *      destruction.
     41      *  @return new Canvas object. Will not return NULL.
     42      */
     43     static Canvas* create_canvas(SkCanvas* skiaCanvas);
     44 
     45     /**
     46      *  Provides a Skia SkCanvas interface that acts as a proxy to this Canvas.
     47      *  It is useful for testing and clients (e.g. Picture/Movie) that expect to
     48      *  draw their contents into an SkCanvas.
     49      *
     50      *  The SkCanvas returned is *only* valid until another Canvas call is made
     51      *  that would change state (e.g. matrix or clip). Clients of asSkCanvas()
     52      *  are responsible for *not* persisting this pointer.
     53      *
     54      *  Further, the returned SkCanvas should NOT be unref'd and is valid until
     55      *  this canvas is destroyed or a new bitmap is set.
     56      */
     57     virtual SkCanvas* asSkCanvas() = 0;
     58 
     59     virtual void setBitmap(const SkBitmap& bitmap) = 0;
     60 
     61     virtual bool isOpaque() = 0;
     62     virtual int width() = 0;
     63     virtual int height() = 0;
     64 
     65 // ----------------------------------------------------------------------------
     66 // Canvas state operations
     67 // ----------------------------------------------------------------------------
     68     // Save (layer)
     69     virtual int getSaveCount() const = 0;
     70     virtual int save(SkCanvas::SaveFlags flags) = 0;
     71     virtual void restore() = 0;
     72     virtual void restoreToCount(int saveCount) = 0;
     73 
     74     virtual int saveLayer(float left, float top, float right, float bottom,
     75                 const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
     76     virtual int saveLayerAlpha(float left, float top, float right, float bottom,
     77             int alpha, SkCanvas::SaveFlags flags) = 0;
     78 
     79     // Matrix
     80     virtual void getMatrix(SkMatrix* outMatrix) const = 0;
     81     virtual void setMatrix(const SkMatrix& matrix) = 0;
     82 
     83     /// Like setMatrix(), but to be translated into local / view-relative coordinates
     84     /// rather than executed in global / device coordinates at rendering time.
     85     virtual void setLocalMatrix(const SkMatrix& matrix) = 0;
     86 
     87     virtual void concat(const SkMatrix& matrix) = 0;
     88     virtual void rotate(float degrees) = 0;
     89     virtual void scale(float sx, float sy) = 0;
     90     virtual void skew(float sx, float sy) = 0;
     91     virtual void translate(float dx, float dy) = 0;
     92 
     93     // clip
     94     virtual bool getClipBounds(SkRect* outRect) const = 0;
     95     virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
     96     virtual bool quickRejectPath(const SkPath& path) const = 0;
     97 
     98     virtual bool clipRect(float left, float top, float right, float bottom,
     99             SkRegion::Op op = SkRegion::kIntersect_Op) = 0;
    100     virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
    101     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
    102 
    103     // filters
    104     virtual SkDrawFilter* getDrawFilter() = 0;
    105     virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
    106 
    107 // ----------------------------------------------------------------------------
    108 // Canvas draw operations
    109 // ----------------------------------------------------------------------------
    110     virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
    111     virtual void drawPaint(const SkPaint& paint) = 0;
    112 
    113     // Geometry
    114     virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
    115     virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
    116     virtual void drawLine(float startX, float startY, float stopX, float stopY,
    117                 const SkPaint& paint) = 0;
    118     virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
    119     virtual void drawRect(float left, float top, float right, float bottom,
    120             const SkPaint& paint) = 0;
    121     virtual void drawRoundRect(float left, float top, float right, float bottom,
    122             float rx, float ry, const SkPaint& paint) = 0;
    123     virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
    124     virtual void drawOval(float left, float top, float right, float bottom,
    125             const SkPaint& paint) = 0;
    126     virtual void drawArc(float left, float top, float right, float bottom,
    127             float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
    128     virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
    129     virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
    130                               const float* verts, const float* tex, const int* colors,
    131                               const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
    132 
    133     // Bitmap-based
    134     virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
    135             const SkPaint* paint) = 0;
    136     virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
    137             const SkPaint* paint) = 0;
    138     virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
    139             float srcRight, float srcBottom, float dstLeft, float dstTop,
    140             float dstRight, float dstBottom, const SkPaint* paint) = 0;
    141     virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
    142             const float* vertices, const int* colors, const SkPaint* paint) = 0;
    143 
    144     // Text
    145     /**
    146      * drawText: count is of glyphs
    147      * totalAdvance is ignored in software renderering, used by hardware renderer for
    148      * text decorations (underlines, strikethroughs).
    149      */
    150     virtual void drawText(const uint16_t* glyphs, const float* positions, int count,
    151             const SkPaint& paint, float x, float y,
    152             float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
    153             float totalAdvance) = 0;
    154     /** drawPosText: count is of UTF16 characters, posCount is floats (2 * glyphs) */
    155     virtual void drawPosText(const uint16_t* text, const float* positions, int count,
    156             int posCount, const SkPaint& paint) = 0;
    157     /** drawTextOnPath: count is of glyphs */
    158     virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
    159             float hOffset, float vOffset, const SkPaint& paint) = 0;
    160 
    161     /**
    162      * Specifies if the positions passed to ::drawText are absolute or relative
    163      * to the (x,y) value provided.
    164      *
    165      * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
    166      * to be added to each glyph's position to get its absolute position.
    167      */
    168     virtual bool drawTextAbsolutePos() const = 0;
    169 };
    170 
    171 }; // namespace android
    172 #endif // ANDROID_GRAPHICS_CANVAS_H
    173