• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef sktext_gpu_Slug_DEFINED
9 #define sktext_gpu_Slug_DEFINED
10 
11 #include "include/core/SkData.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkRefCnt.h"
14 
15 class SkCanvas;
16 class SkMatrix;
17 class SkPaint;
18 class SkTextBlob;
19 class SkReadBuffer;
20 class SkStrikeClient;
21 class SkWriteBuffer;
22 
23 namespace sktext::gpu {
24 
25 // You can use Slug to simulate drawTextBlob by defining the following at compile time.
26 //    SK_EXPERIMENTAL_SIMULATE_DRAWGLYPHRUNLIST_WITH_SLUG
27 // You can use Slug serialization to simulate drawTextBlob by defining the following:
28 //    SK_EXPERIMENTAL_SIMULATE_DRAWGLYPHRUNLIST_WITH_SLUG_SERIALIZE
29 // For Skia, add this to your args.gn file.
30 //    extra_cflags = ["-D", "SK_EXPERIMENTAL_SIMULATE_DRAWGLYPHRUNLIST_WITH_SLUG"]
31 
32 // Slug encapsulates an SkTextBlob at a specific origin, using a specific paint. It can be
33 // manipulated using matrix and clip changes to the canvas. If the canvas is transformed, then
34 // the Slug will also transform with smaller glyphs using bi-linear interpolation to render. You
35 // can think of a Slug as making a rubber stamp out of a SkTextBlob.
36 class SK_API Slug : public SkRefCnt {
37 public:
38     // Return nullptr if the blob would not draw. This is not because of clipping, but because of
39     // some paint optimization. The Slug is captured as if drawn using drawTextBlob.
40     static sk_sp<Slug> ConvertBlob(
41             SkCanvas* canvas, const SkTextBlob& blob, SkPoint origin, const SkPaint& paint);
42 
43     // Serialize the slug.
44     sk_sp<SkData> serialize() const;
45     size_t serialize(void* buffer, size_t size) const;
46 
47     // Set the client parameter to the appropriate SkStrikeClient when typeface ID translation
48     // is needed.
49     static sk_sp<Slug> Deserialize(
50             const void* data, size_t size, const SkStrikeClient* client = nullptr);
51     static sk_sp<Slug> MakeFromBuffer(SkReadBuffer& buffer);
52 
53 
54     // Draw the Slug obeying the canvas's mapping and clipping.
55     void draw(SkCanvas* canvas) const;
56 
57     virtual SkRect sourceBounds() const = 0;
58     virtual SkRect sourceBoundsWithOrigin () const = 0;
59 
60     // The paint passed into ConvertBlob; this paint is used instead of the paint resulting from
61     // the call to aboutToDraw because when we call draw(), the initial paint is needed to call
62     // aboutToDraw again to get the layer right.
63     virtual const SkPaint& initialPaint() const = 0;
64 
65     virtual void doFlatten(SkWriteBuffer&) const = 0;
66 
uniqueID()67     uint32_t uniqueID() const { return fUniqueID; }
68 
69 private:
70     static uint32_t NextUniqueID();
71     const uint32_t  fUniqueID{NextUniqueID()};
72 };
73 
74 }  // namespace sktext::gpu
75 
76 #endif  // sktext_gpu_Slug_DEFINED
77