reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkEvent_DEFINED |
| 9 | #define SkEvent_DEFINED |
| 10 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkMetaData.h" |
| 12 | #include "SkString.h" |
| 13 | |
Hal Canary | 20de615 | 2017-02-23 13:24:49 -0500 | [diff] [blame] | 14 | class SkDOM; |
| 15 | struct SkDOMNode; |
| 16 | |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 17 | #include "../private/SkLeanWindows.h" |
| 18 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | /** Unique 32bit id used to identify an instance of SkEventSink. When events are |
| 20 | posted, they are posted to a specific sinkID. When it is time to dispatch the |
| 21 | event, the sinkID is used to find the specific SkEventSink object. If it is found, |
| 22 | its doEvent() method is called with the event. |
| 23 | */ |
| 24 | typedef uint32_t SkEventSinkID; |
| 25 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 26 | /** |
| 27 | * \class SkEvent |
| 28 | * |
| 29 | * When an event is dispatched from the event queue, it is either sent to |
| 30 | * the eventsink matching the target ID (if not 0), or the target proc is |
| 31 | * called (if not NULL). |
| 32 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | class SkEvent { |
| 34 | public: |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Function pointer that takes an event, returns true if it "handled" it. |
| 37 | */ |
| 38 | typedef bool (*Proc)(const SkEvent& evt); |
| 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | SkEvent(); |
Brian Osman | 4f99e58 | 2017-11-22 13:23:35 -0500 | [diff] [blame^] | 41 | explicit SkEvent(const SkString& type); |
| 42 | explicit SkEvent(const char type[]); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | SkEvent(const SkEvent& src); |
| 44 | ~SkEvent(); |
| 45 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | /** Copy the event's type into the specified SkString parameter */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 47 | void getType(SkString* str) const; |
| 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | /** Returns true if the event's type matches exactly the specified type (case sensitive) */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 50 | bool isType(const SkString& str) const; |
| 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | /** Returns true if the event's type matches exactly the specified type (case sensitive) */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 53 | bool isType(const char type[], size_t len = 0) const; |
| 54 | |
| 55 | /** |
| 56 | * Set the event's type to the specified string. |
| 57 | */ |
| 58 | void setType(const SkString&); |
| 59 | |
| 60 | /** |
| 61 | * Set the event's type to the specified string. |
| 62 | */ |
| 63 | void setType(const char type[], size_t len = 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame] | 65 | /** |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 66 | * Return the event's unnamed 32bit field. Default value is 0 |
| 67 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | uint32_t getFast32() const { return f32; } |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Set the event's unnamed 32bit field. |
| 72 | */ |
| 73 | void setFast32(uint32_t x) { f32 = x; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
| 75 | /** Return true if the event contains the named 32bit field, and return the field |
| 76 | in value (if value is non-null). If there is no matching named field, return false |
| 77 | and ignore the value parameter. |
| 78 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 79 | bool findS32(const char name[], int32_t* value = nullptr) const { |
| 80 | return fMeta.findS32(name, value); |
| 81 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | /** Return true if the event contains the named SkScalar field, and return the field |
| 83 | in value (if value is non-null). If there is no matching named field, return false |
| 84 | and ignore the value parameter. |
| 85 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 86 | bool findScalar(const char name[], SkScalar* value = nullptr) const { |
| 87 | return fMeta.findScalar(name, value); |
| 88 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | /** Return true if the event contains the named SkScalar field, and return the fields |
| 90 | in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null). |
| 91 | If there is no matching named field, return false and ignore the value and count parameters. |
| 92 | */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 93 | const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = nullptr) const { |
| 94 | return fMeta.findScalars(name, count, values); |
| 95 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | /** Return the value of the named string field, or if no matching named field exists, return null. |
| 97 | */ |
| 98 | const char* findString(const char name[]) const { return fMeta.findString(name); } |
| 99 | /** Return true if the event contains the named pointer field, and return the field |
| 100 | in value (if value is non-null). If there is no matching named field, return false |
| 101 | and ignore the value parameter. |
| 102 | */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 103 | bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); } |
| 104 | bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); } |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 105 | const void* findData(const char name[], size_t* byteCount = nullptr) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 106 | return fMeta.findData(name, byteCount); |
| 107 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | |
| 109 | /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 110 | bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 112 | bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 114 | bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 116 | bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); } |
| 117 | bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 118 | bool hasData(const char name[], const void* data, size_t byteCount) const { |
| 119 | return fMeta.hasData(name, data, byteCount); |
| 120 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
| 122 | /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 123 | void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 125 | void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | /** Add/replace the named SkScalar[] field to the event. */ |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 127 | SkScalar* setScalars(const char name[], int count, const SkScalar values[] = nullptr) { |
| 128 | return fMeta.setScalars(name, count, values); |
| 129 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 131 | void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 133 | void setString(const char name[], const char value[]) { fMeta.setString(name, value); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 135 | void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); } |
| 136 | void setBool(const char name[], bool value) { fMeta.setBool(name, value); } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 137 | void setData(const char name[], const void* data, size_t byteCount) { |
| 138 | fMeta.setData(name, data, byteCount); |
| 139 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | |
| 141 | /** Return the underlying metadata object */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 142 | SkMetaData& getMetaData() { return fMeta; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | /** Return the underlying metadata object */ |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 144 | const SkMetaData& getMetaData() const { return fMeta; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | |
| 146 | /** Call this to initialize the event from the specified XML node */ |
Hal Canary | 20de615 | 2017-02-23 13:24:49 -0500 | [diff] [blame] | 147 | void inflate(const SkDOM&, const SkDOMNode*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 149 | SkDEBUGCODE(void dump(const char title[] = nullptr);) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 151 | /////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | private: |
| 154 | SkMetaData fMeta; |
| 155 | mutable char* fType; // may be characters with low bit set to know that it is not a pointer |
| 156 | uint32_t f32; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 157 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | // these are for our implementation of the event queue |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | SkMSec fTime; |
| 160 | SkEvent* fNextEvent; // either in the delay or normal event queue |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 161 | |
Brian Osman | 4f99e58 | 2017-11-22 13:23:35 -0500 | [diff] [blame^] | 162 | void initialize(const char* type, size_t typeLen); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | #endif |