Home | History | Annotate | Download | only in graphics
      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 "SkBitmap.h"
     21 #include "SkCanvas.h"
     22 #include "SkMatrix.h"
     23 
     24 namespace android {
     25 
     26 class Canvas {
     27 public:
     28     virtual ~Canvas() {};
     29 
     30     static Canvas* create_canvas(SkBitmap* bitmap);
     31     static Canvas* create_canvas(SkCanvas* skiaCanvas);
     32 
     33     // TODO: enable HWUI to either create similar canvas wrapper or subclass
     34     //       directly from Canvas
     35     //static Canvas* create_canvas(uirenderer::Renderer* renderer);
     36 
     37     // TODO: this is a temporary affordance until all necessary logic can be
     38     //       moved within this interface! Further, the return value should
     39     //       NOT be unref'd and is valid until this canvas is destroyed or a
     40     //       new bitmap is set.
     41     virtual SkCanvas* getSkCanvas() = 0;
     42 
     43     virtual void setBitmap(SkBitmap* bitmap, bool copyState) = 0;
     44 
     45     virtual bool isOpaque() = 0;
     46     virtual int width() = 0;
     47     virtual int height() = 0;
     48 
     49 // ----------------------------------------------------------------------------
     50 // Canvas state operations
     51 // ----------------------------------------------------------------------------
     52     // Save (layer)
     53     virtual int getSaveCount() const = 0;
     54     virtual int save(SkCanvas::SaveFlags flags) = 0;
     55     virtual void restore() = 0;
     56     virtual void restoreToCount(int saveCount) = 0;
     57 
     58     virtual int saveLayer(float left, float top, float right, float bottom,
     59                 const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
     60     virtual int saveLayerAlpha(float left, float top, float right, float bottom,
     61             int alpha, SkCanvas::SaveFlags flags) = 0;
     62 
     63     // Matrix
     64     virtual void getMatrix(SkMatrix* outMatrix) const = 0;
     65     virtual void setMatrix(const SkMatrix& matrix) = 0;
     66 
     67     virtual void concat(const SkMatrix& matrix) = 0;
     68     virtual void rotate(float degrees) = 0;
     69     virtual void scale(float sx, float sy) = 0;
     70     virtual void skew(float sx, float sy) = 0;
     71     virtual void translate(float dx, float dy) = 0;
     72 
     73     // clip
     74     virtual bool getClipBounds(SkRect* outRect) const = 0;
     75     virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
     76     virtual bool quickRejectPath(const SkPath& path) const = 0;
     77 
     78     virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
     79     virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
     80     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
     81 
     82     // filters
     83     virtual SkDrawFilter* getDrawFilter() = 0;
     84     virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
     85 
     86 // ----------------------------------------------------------------------------
     87 // Canvas draw operations
     88 // ----------------------------------------------------------------------------
     89     virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
     90     virtual void drawPaint(const SkPaint& paint) = 0;
     91 
     92     // Geometry
     93     virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
     94     virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
     95     virtual void drawLine(float startX, float startY, float stopX, float stopY,
     96                 const SkPaint& paint) = 0;
     97     virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
     98     virtual void drawRect(float left, float top, float right, float bottom,
     99             const SkPaint& paint) = 0;
    100     virtual void drawRoundRect(float left, float top, float right, float bottom,
    101             float rx, float ry, const SkPaint& paint) = 0;
    102     virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
    103     virtual void drawOval(float left, float top, float right, float bottom,
    104             const SkPaint& paint) = 0;
    105     virtual void drawArc(float left, float top, float right, float bottom,
    106             float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
    107     virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
    108     virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
    109                               const float* verts, const float* tex, const int* colors,
    110                               const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
    111 
    112     // Bitmap-based
    113     virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
    114             const SkPaint* paint) = 0;
    115     virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
    116             const SkPaint* paint) = 0;
    117     virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
    118             float srcRight, float srcBottom, float dstLeft, float dstTop,
    119             float dstRight, float dstBottom, const SkPaint* paint) = 0;
    120     virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
    121             const float* vertices, const int* colors, const SkPaint* paint) = 0;
    122 
    123     // Text
    124     virtual void drawText(const uint16_t* text, const float* positions, int count,
    125             const SkPaint& paint, float x, float y,
    126             float boundsLeft, float boundsTop, float boundsRight, float boundsBottom) = 0;
    127     virtual void drawPosText(const uint16_t* text, const float* positions, int count,
    128             int posCount, const SkPaint& paint) = 0;
    129     virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
    130             float hOffset, float vOffset, const SkPaint& paint) = 0;
    131 
    132     /*
    133      * Specifies if the positions passed to ::drawText are absolute or relative
    134      * to the (x,y) value provided.
    135      *
    136      * If true the (x,y) values are ignored. Otherwise, those (x,y) values need
    137      * to be added to each glyph's position to get its absolute position.
    138      */
    139     virtual bool drawTextAbsolutePos() const = 0;
    140 };
    141 
    142 }; // namespace android
    143 #endif // ANDROID_GRAPHICS_CANVAS_H
    144