1 /* 2 * Copyright 2019 Google Inc. 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_StrikeForGPU_DEFINED 9 #define sktext_StrikeForGPU_DEFINED 10 11 #include "include/core/SkImageInfo.h" 12 #include "include/core/SkPoint.h" 13 #include "include/core/SkSpan.h" 14 #include "include/core/SkTypes.h" 15 #include "src/core/SkGlyph.h" 16 17 #include <memory> 18 #include <optional> 19 #include <variant> 20 21 class SkDescriptor; 22 class SkDrawableGlyphBuffer; 23 class SkReadBuffer; 24 class SkSourceGlyphBuffer; 25 class SkStrike; 26 class SkStrikeCache; 27 class SkStrikeClient; 28 class SkStrikeSpec; 29 class SkWriteBuffer; 30 struct SkGlyphPositionRoundingSpec; 31 struct SkScalerContextEffects; 32 33 namespace sktext { 34 // -- SkStrikePromise ------------------------------------------------------------------------------ 35 // SkStrikePromise produces an SkStrike when needed by GPU glyph rendering. In ordinary 36 // operation, it just wraps an SkStrike. When used for remote glyph cache operation, the promise is 37 // serialized to an SkDescriptor. When SkStrikePromise is deserialized, it uses the descriptor to 38 // look up the SkStrike. 39 // 40 // When deserializing some care must be taken; if the needed SkStrike is removed from the cache, 41 // then looking up using the descriptor will fail resulting in a deserialization failure. The 42 // Renderer/GPU system solves this problem by pinning all the strikes needed into the cache. 43 class SkStrikePromise { 44 public: 45 SkStrikePromise() = delete; 46 SkStrikePromise(const SkStrikePromise&) = delete; 47 SkStrikePromise& operator=(const SkStrikePromise&) = delete; 48 SkStrikePromise(SkStrikePromise&&); 49 SkStrikePromise& operator=(SkStrikePromise&&); 50 51 explicit SkStrikePromise(sk_sp<SkStrike>&& strike); 52 explicit SkStrikePromise(const SkStrikeSpec& spec); 53 54 static std::optional<SkStrikePromise> MakeFromBuffer(SkReadBuffer& buffer, 55 const SkStrikeClient* client, 56 SkStrikeCache* strikeCache); 57 void flatten(SkWriteBuffer& buffer) const; 58 59 // Do what is needed to return a strike. 60 SkStrike* strike(); 61 62 // Reset the sk_sp<SkStrike> to nullptr. 63 void resetStrike(); 64 65 // Return a descriptor used to look up the SkStrike. 66 const SkDescriptor& descriptor() const; 67 68 private: 69 std::variant<sk_sp<SkStrike>, std::unique_ptr<SkStrikeSpec>> fStrikeOrSpec; 70 }; 71 72 // -- StrikeForGPU --------------------------------------------------------------------------------- 73 class StrikeForGPU : public SkRefCnt { 74 public: 75 virtual void lock() = 0; 76 virtual void unlock() = 0; 77 78 // Generate a digest for a given packed glyph ID as drawn using the give action type. 79 virtual SkGlyphDigest digestFor(skglyph::ActionType, SkPackedGlyphID) = 0; 80 81 // Prepare the glyph to draw an image, and return if the image exists. 82 virtual bool prepareForImage(SkGlyph*) = 0; 83 84 // Prepare the glyph to draw a path, and return if the path exists. 85 virtual bool prepareForPath(SkGlyph*) = 0; 86 87 // Prepare the glyph to draw a drawable, and return if the drawable exists. 88 virtual bool prepareForDrawable(SkGlyph*) = 0; 89 90 91 virtual const SkDescriptor& getDescriptor() const = 0; 92 93 virtual const SkGlyphPositionRoundingSpec& roundingSpec() const = 0; 94 95 // Return a strike promise. 96 virtual SkStrikePromise strikePromise() = 0; 97 }; 98 99 // prepareForPathDrawing uses this union to convert glyph ids to paths. 100 union IDOrPath { IDOrPath()101 IDOrPath() {} 102 103 // PathOpSubmitter takes care of destroying the paths. ~IDOrPath()104 ~IDOrPath() {} 105 SkGlyphID fGlyphID; 106 SkPath fPath; 107 }; 108 109 // prepareForDrawableDrawing uses this union to convert glyph ids to drawables. 110 union IDOrDrawable { 111 SkGlyphID fGlyphID; 112 SkDrawable* fDrawable; 113 }; 114 115 // -- StrikeMutationMonitor ------------------------------------------------------------------------ 116 class StrikeMutationMonitor { 117 public: 118 StrikeMutationMonitor(StrikeForGPU* strike); 119 ~StrikeMutationMonitor(); 120 121 private: 122 StrikeForGPU* fStrike; 123 }; 124 125 // -- StrikeForGPUCacheInterface ------------------------------------------------------------------- 126 class StrikeForGPUCacheInterface { 127 public: 128 virtual ~StrikeForGPUCacheInterface() = default; 129 virtual sk_sp<StrikeForGPU> findOrCreateScopedStrike(const SkStrikeSpec& strikeSpec) = 0; 130 }; 131 } // namespace sktext 132 #endif // sktext_StrikeForGPU_DEFINED 133