Home | History | Annotate | Download | only in ext
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef SKIA_EXT_ANALYSIS_CANVAS_H_
      6 #define SKIA_EXT_ANALYSIS_CANVAS_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "third_party/skia/include/core/SkCanvas.h"
     10 #include "third_party/skia/include/core/SkDevice.h"
     11 #include "third_party/skia/include/core/SkPicture.h"
     12 
     13 namespace skia {
     14 
     15 class AnalysisDevice;
     16 
     17 // Does not render anything, but gathers statistics about a region
     18 // (specified as a clip rectangle) of an SkPicture as the picture is
     19 // played back through it.
     20 // To use: create a SkBitmap with kNo_Config, create an AnalysisDevice
     21 // using that bitmap, and create an AnalysisCanvas using the device.
     22 // Play a picture into the canvas, and then check result.
     23 class SK_API AnalysisCanvas : public SkCanvas, public SkDrawPictureCallback {
     24  public:
     25   AnalysisCanvas(AnalysisDevice*);
     26   virtual ~AnalysisCanvas();
     27 
     28   // Returns true when a SkColor can be used to represent result.
     29   bool GetColorIfSolid(SkColor* color) const;
     30   bool HasText() const;
     31 
     32   // SkDrawPictureCallback override.
     33   virtual bool abortDrawing() OVERRIDE;
     34 
     35   // SkCanvas overrides.
     36   virtual bool clipRect(const SkRect& rect,
     37                         SkRegion::Op op = SkRegion::kIntersect_Op,
     38                         bool do_anti_alias = false) OVERRIDE;
     39   virtual bool clipPath(const SkPath& path,
     40                         SkRegion::Op op = SkRegion::kIntersect_Op,
     41                         bool do_anti_alias = false) OVERRIDE;
     42   virtual bool clipRRect(const SkRRect& rrect,
     43                          SkRegion::Op op = SkRegion::kIntersect_Op,
     44                          bool do_anti_alias = false) OVERRIDE;
     45 
     46   virtual int saveLayer(const SkRect* bounds,
     47                         const SkPaint* paint,
     48                         SkCanvas::SaveFlags flags) OVERRIDE;
     49   virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) OVERRIDE;
     50 
     51   virtual void restore() OVERRIDE;
     52 
     53  private:
     54   typedef SkCanvas INHERITED;
     55 
     56   int saved_stack_size_;
     57   int force_not_solid_stack_level_;
     58   int force_not_transparent_stack_level_;
     59 };
     60 
     61 class SK_API AnalysisDevice : public SkDevice {
     62  public:
     63   AnalysisDevice(const SkBitmap& bitmap);
     64   virtual ~AnalysisDevice();
     65 
     66   bool GetColorIfSolid(SkColor* color) const;
     67   bool HasText() const;
     68 
     69   void SetForceNotSolid(bool flag);
     70   void SetForceNotTransparent(bool flag);
     71 
     72  protected:
     73   // SkDevice overrides.
     74   virtual void clear(SkColor color) OVERRIDE;
     75   virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) OVERRIDE;
     76   virtual void drawPoints(const SkDraw& draw,
     77                           SkCanvas::PointMode mode,
     78                           size_t count,
     79                           const SkPoint points[],
     80                           const SkPaint& paint) OVERRIDE;
     81   virtual void drawRect(const SkDraw& draw,
     82                         const SkRect& rect,
     83                         const SkPaint& paint) OVERRIDE;
     84   virtual void drawRRect(const SkDraw& draw,
     85                          const SkRRect& rr,
     86                          const SkPaint& paint) OVERRIDE;
     87   virtual void drawOval(const SkDraw& draw,
     88                         const SkRect& oval,
     89                         const SkPaint& paint) OVERRIDE;
     90   virtual void drawPath(const SkDraw& draw,
     91                         const SkPath& path,
     92                         const SkPaint& paint,
     93                         const SkMatrix* pre_path_matrix = NULL,
     94                         bool path_is_mutable = false) OVERRIDE;
     95   virtual void drawBitmap(const SkDraw& draw,
     96                           const SkBitmap& bitmap,
     97                           const SkMatrix& matrix,
     98                           const SkPaint& paint) OVERRIDE;
     99   virtual void drawSprite(const SkDraw& draw,
    100                           const SkBitmap& bitmap,
    101                           int x,
    102                           int y,
    103                           const SkPaint& paint) OVERRIDE;
    104   virtual void drawBitmapRect(const SkDraw& draw,
    105                               const SkBitmap& bitmap,
    106                               const SkRect* src_or_null,
    107                               const SkRect& dst,
    108                               const SkPaint& paint) OVERRIDE;
    109   virtual void drawText(const SkDraw& draw,
    110                         const void* text,
    111                         size_t len,
    112                         SkScalar x,
    113                         SkScalar y,
    114                         const SkPaint& paint) OVERRIDE;
    115   virtual void drawPosText(const SkDraw& draw,
    116                            const void* text,
    117                            size_t len,
    118                            const SkScalar pos[],
    119                            SkScalar const_y,
    120                            int scalars_per_pos,
    121                            const SkPaint& paint) OVERRIDE;
    122   virtual void drawTextOnPath(const SkDraw& draw,
    123                               const void* text,
    124                               size_t len,
    125                               const SkPath& path,
    126                               const SkMatrix* matrix,
    127                               const SkPaint& paint) OVERRIDE;
    128 #ifdef SK_BUILD_FOR_ANDROID
    129   virtual void drawPosTextOnPath(const SkDraw& draw,
    130                                  const void* text,
    131                                  size_t len,
    132                                  const SkPoint pos[],
    133                                  const SkPaint& paint,
    134                                  const SkPath& path,
    135                                  const SkMatrix* matrix) OVERRIDE;
    136 #endif
    137   virtual void drawVertices(const SkDraw& draw,
    138                             SkCanvas::VertexMode vertex_mode,
    139                             int vertex_count,
    140                             const SkPoint verts[],
    141                             const SkPoint texs[],
    142                             const SkColor colors[],
    143                             SkXfermode* xmode,
    144                             const uint16_t indices[],
    145                             int index_count,
    146                             const SkPaint& paint) OVERRIDE;
    147   virtual void drawDevice(const SkDraw& draw,
    148                           SkDevice* device,
    149                           int x,
    150                           int y,
    151                           const SkPaint& paint) OVERRIDE;
    152 
    153  private:
    154   typedef SkDevice INHERITED;
    155 
    156   bool is_forced_not_solid_;
    157   bool is_forced_not_transparent_;
    158   bool is_solid_color_;
    159   SkColor color_;
    160   bool is_transparent_;
    161   bool has_text_;
    162 };
    163 
    164 }  // namespace skia
    165 
    166 #endif  // SKIA_EXT_ANALYSIS_CANVAS_H_
    167 
    168