1 /* 2 * Copyright 2025 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 PipelineCallbackHandler_DEFINED 9 #define PipelineCallbackHandler_DEFINED 10 11 #include "include/core/SkData.h" 12 #include "include/core/SkRefCnt.h" 13 #include "src/base/SkSpinlock.h" 14 #include "src/core/SkChecksum.h" 15 #include "src/core/SkTHash.h" 16 17 #include <vector> 18 19 class SkData; 20 21 namespace skiatools::graphite { 22 23 24 // This is intended to be an example of a Precompilation Callback handler. For DM it collects 25 // all the Android-style keys that are used by a given source (e.g., gm, or skp) and uses 26 // them in resetAndRecreatePipelines to recreate the Pipelines. 27 class PipelineCallBackHandler { 28 public: CallBack(void * data,sk_sp<SkData> androidStyleKey)29 static void CallBack(void* data, sk_sp<SkData> androidStyleKey) { 30 PipelineCallBackHandler* handler = reinterpret_cast<PipelineCallBackHandler*>(data); 31 32 handler->add(std::move(androidStyleKey)); 33 } 34 35 // Add an Android-style key to the map 36 void add(sk_sp<SkData> androidStyleKey) SK_EXCLUDES(fSpinLock); 37 38 // Retrieve all the unique collected keys 39 void retrieve(std::vector<sk_sp<SkData>>*) SK_EXCLUDES(fSpinLock); 40 41 void reset() SK_EXCLUDES(fSpinLock); 42 numKeys()43 int numKeys() const SK_EXCLUDES(fSpinLock) { 44 SkAutoSpinlock lock{ fSpinLock }; 45 return fMap.count(); 46 } 47 48 private: 49 mutable SkSpinlock fSpinLock; 50 51 struct SkDataKey { GetKeySkDataKey52 static SkDataKey GetKey(sk_sp<SkData>& e) { return { e.get() }; } HashSkDataKey53 static uint32_t Hash(const SkDataKey& k) { return k.hash(); } 54 55 bool operator==(const SkDataKey& other) const { return fData->equals(other.fData); } hashSkDataKey56 uint32_t hash() const { return SkChecksum::Hash32(fData->data(), fData->size()); } 57 58 const SkData* fData; 59 }; 60 61 skia_private::THashTable<sk_sp<SkData>, SkDataKey, SkDataKey> fMap SK_GUARDED_BY(fSpinLock); 62 }; 63 64 } // namespace skiatools::graphite 65 66 #endif // PipelineCallbackHandler_DEFINED 67