1
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkBBoxRecord_DEFINED
10#define SkBBoxRecord_DEFINED
11
12#include "SkPictureRecord.h"
13#include "SkTDArray.h"
14
15/**
16  * This is an abstract SkPictureRecord subclass that intercepts draw calls and computes an
17  * axis-aligned bounding box for each draw that it sees, subclasses implement handleBBox()
18  * which will be called every time we get a new bounding box.
19  */
20class SkBBoxRecord : public SkPictureRecord {
21public:
22
23    SkBBoxRecord(const SkISize& size, uint32_t recordFlags)
24        : INHERITED(size, recordFlags) {
25    }
26    virtual ~SkBBoxRecord();
27
28    /**
29     * This is called each time we get a bounding box, it will be axis-aligned,
30     * in device coordinates, and expanded to include stroking, shadows, etc.
31     */
32    virtual void handleBBox(const SkRect& bbox) = 0;
33
34    virtual void drawOval(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
35    virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
36    virtual void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
37    virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
38    virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
39                            const SkPaint& paint) SK_OVERRIDE;
40    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
41    virtual void clear(SkColor) SK_OVERRIDE;
42    virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
43                            const SkPaint* paint = NULL) SK_OVERRIDE;
44    virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
45                                      const SkRect& dst, const SkPaint* paint,
46                                      DrawBitmapRectFlags flags) SK_OVERRIDE;
47    virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& mat,
48                                  const SkPaint* paint) SK_OVERRIDE;
49    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
50                                const SkRect& dst, const SkPaint* paint) SK_OVERRIDE;
51    virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
52                            const SkPaint* paint) SK_OVERRIDE;
53    virtual void drawVertices(VertexMode mode, int vertexCount,
54                              const SkPoint vertices[], const SkPoint texs[],
55                              const SkColor colors[], SkXfermode* xfer,
56                              const uint16_t indices[], int indexCount,
57                              const SkPaint& paint) SK_OVERRIDE;
58
59protected:
60    virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
61    virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
62                            const SkPaint&) SK_OVERRIDE;
63    virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
64                               const SkPaint&) SK_OVERRIDE;
65    virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
66                                SkScalar constY, const SkPaint&) SK_OVERRIDE;
67    virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
68                                  const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
69    virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
70                                const SkPaint& paint) SK_OVERRIDE;
71    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
72                             const SkPoint texCoords[4], SkXfermode* xmode,
73                             const SkPaint& paint) SK_OVERRIDE;
74    virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
75    virtual void willSave() SK_OVERRIDE;
76    virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
77    virtual void willRestore() SK_OVERRIDE;
78
79private:
80    /**
81     * Takes a bounding box in current canvas view space, accounts for stroking and effects, and
82     * computes an axis-aligned bounding box in device coordinates, then passes it to handleBBox()
83     * returns false if the draw is completely clipped out, and may safely be ignored.
84     **/
85    bool transformBounds(const SkRect& bounds, const SkPaint* paint);
86
87    /**
88     * Paints from currently-active saveLayers that need to be applied to bounding boxes of all
89     * primitives drawn inside them. We own these pointers.
90     **/
91    SkTDArray<const SkPaint*> fSaveStack;
92
93    typedef SkPictureRecord INHERITED;
94};
95
96#endif
97