1 // Copyright 2017 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 CC_PAINT_RECORD_PAINT_CANVAS_H_
6 #define CC_PAINT_RECORD_PAINT_CANVAS_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/optional.h"
13 #include "build/build_config.h"
14 #include "cc/paint/paint_canvas.h"
15 #include "cc/paint/paint_flags.h"
16 #include "cc/paint/paint_record.h"
17 #include "third_party/skia/include/utils/SkNoDrawCanvas.h"
18 
19 namespace cc {
20 
21 class DisplayItemList;
22 class PaintFlags;
23 
24 class CC_PAINT_EXPORT RecordPaintCanvas : public PaintCanvas {
25  public:
26   RecordPaintCanvas(DisplayItemList* list, const SkRect& bounds);
27   RecordPaintCanvas(const RecordPaintCanvas&) = delete;
28   ~RecordPaintCanvas() override;
29 
30   RecordPaintCanvas& operator=(const RecordPaintCanvas&) = delete;
31 
32   SkImageInfo imageInfo() const override;
33 
34   void* accessTopLayerPixels(SkImageInfo* info,
35                              size_t* rowBytes,
36                              SkIPoint* origin = nullptr) override;
37 
38   void flush() override;
39 
40   int save() override;
41   int saveLayer(const SkRect* bounds, const PaintFlags* flags) override;
42   int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) override;
43 
44   void restore() override;
45   int getSaveCount() const override;
46   void restoreToCount(int save_count) override;
47   void translate(SkScalar dx, SkScalar dy) override;
48   void scale(SkScalar sx, SkScalar sy) override;
49   void rotate(SkScalar degrees) override;
50   void concat(const SkMatrix& matrix) override;
51   void setMatrix(const SkMatrix& matrix) override;
52 
53   void clipRect(const SkRect& rect, SkClipOp op, bool antialias) override;
54   void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) override;
55   void clipPath(const SkPath& path, SkClipOp op, bool antialias) override;
56   SkRect getLocalClipBounds() const override;
57   bool getLocalClipBounds(SkRect* bounds) const override;
58   SkIRect getDeviceClipBounds() const override;
59   bool getDeviceClipBounds(SkIRect* bounds) const override;
60   void drawColor(SkColor color, SkBlendMode mode) override;
61   void clear(SkColor color) override;
62 
63   void drawLine(SkScalar x0,
64                 SkScalar y0,
65                 SkScalar x1,
66                 SkScalar y1,
67                 const PaintFlags& flags) override;
68   void drawRect(const SkRect& rect, const PaintFlags& flags) override;
69   void drawIRect(const SkIRect& rect, const PaintFlags& flags) override;
70   void drawOval(const SkRect& oval, const PaintFlags& flags) override;
71   void drawRRect(const SkRRect& rrect, const PaintFlags& flags) override;
72   void drawDRRect(const SkRRect& outer,
73                   const SkRRect& inner,
74                   const PaintFlags& flags) override;
75   void drawRoundRect(const SkRect& rect,
76                      SkScalar rx,
77                      SkScalar ry,
78                      const PaintFlags& flags) override;
79   void drawPath(const SkPath& path, const PaintFlags& flags) override;
80   void drawImage(const PaintImage& image,
81                  SkScalar left,
82                  SkScalar top,
83                  const PaintFlags* flags) override;
84   void drawImageRect(const PaintImage& image,
85                      const SkRect& src,
86                      const SkRect& dst,
87                      const PaintFlags* flags,
88                      SrcRectConstraint constraint) override;
89   void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
90                    const SkRect& dst,
91                    float t) override;
92   void drawTextBlob(sk_sp<SkTextBlob> blob,
93                     SkScalar x,
94                     SkScalar y,
95                     const PaintFlags& flags) override;
96   void drawTextBlob(sk_sp<SkTextBlob> blob,
97                     SkScalar x,
98                     SkScalar y,
99                     NodeId node_id,
100                     const PaintFlags& flags) override;
101 
102   void drawPicture(sk_sp<const PaintRecord> record) override;
103 
104   bool isClipEmpty() const override;
105   SkMatrix getTotalMatrix() const override;
106 
107   void Annotate(AnnotationType type,
108                 const SkRect& rect,
109                 sk_sp<SkData> data) override;
110   void recordCustomData(uint32_t id) override;
111   void setNodeId(int) override;
112 
113   // Don't shadow non-virtual helper functions.
114   using PaintCanvas::clipRect;
115   using PaintCanvas::clipRRect;
116   using PaintCanvas::clipPath;
117   using PaintCanvas::drawColor;
118   using PaintCanvas::drawImage;
119   using PaintCanvas::drawPicture;
120 
121  private:
122   const SkNoDrawCanvas* GetCanvas() const;
123   SkNoDrawCanvas* GetCanvas();
124 
125   bool InitializedWithRecordingBounds() const;
126 
127   DisplayItemList* list_;
128 
129   // TODO(enne): Although RecordPaintCanvas is mostly a write-only interface
130   // where paint commands are stored, occasionally users of PaintCanvas want
131   // to ask stateful questions mid-stream of clip and transform state.
132   // To avoid duplicating all this code (for now?), just forward to an SkCanvas
133   // that's not backed by anything but can answer these questions.
134   //
135   // This is mutable so that const functions (e.g. quickReject) that may
136   // lazy initialize the canvas can still be const.
137   mutable base::Optional<SkNoDrawCanvas> canvas_;
138   SkRect recording_bounds_;
139 };
140 
141 }  // namespace cc
142 
143 #endif  // CC_PAINT_RECORD_PAINT_CANVAS_H_
144