blob: b78fd6fc1df803965c021297fc026478d273b2c5 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkEvent_DEFINED
9#define SkEvent_DEFINED
10
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkMetaData.h"
12#include "SkString.h"
13
Hal Canary20de6152017-02-23 13:24:49 -050014class SkDOM;
15struct SkDOMNode;
16
halcanary4dbbd042016-06-07 17:21:10 -070017#include "../private/SkLeanWindows.h"
18
reed@android.com8a1c16f2008-12-17 15:59:43 +000019/** 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*/
24typedef uint32_t SkEventSinkID;
25
reed@google.com87fac4a2011-08-04 13:50:17 +000026/**
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.com8a1c16f2008-12-17 15:59:43 +000033class SkEvent {
34public:
reed@google.com87fac4a2011-08-04 13:50:17 +000035 /**
36 * Function pointer that takes an event, returns true if it "handled" it.
37 */
38 typedef bool (*Proc)(const SkEvent& evt);
39
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkEvent();
reed@google.com87fac4a2011-08-04 13:50:17 +000041 explicit SkEvent(const SkString& type, SkEventSinkID = 0);
42 explicit SkEvent(const char type[], SkEventSinkID = 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkEvent(const SkEvent& src);
44 ~SkEvent();
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 /** Copy the event's type into the specified SkString parameter */
reed@google.com87fac4a2011-08-04 13:50:17 +000047 void getType(SkString* str) const;
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000050 bool isType(const SkString& str) const;
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 /** Returns true if the event's type matches exactly the specified type (case sensitive) */
reed@google.com87fac4a2011-08-04 13:50:17 +000053 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.com8a1c16f2008-12-17 15:59:43 +000064
reed@google.comc514dde2011-08-03 19:41:24 +000065 /**
66 * Return the target ID, or 0 if there is none.
reed@google.com87fac4a2011-08-04 13:50:17 +000067 *
68 * When an event is dispatched from the event queue, it is either sent to
69 * the eventsink matching the targetID (if not 0), or the target proc is
70 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000071 */
72 SkEventSinkID getTargetID() const { return fTargetID; }
73
74 /**
reed@google.com87fac4a2011-08-04 13:50:17 +000075 * Set the target ID for this event. 0 means none. Calling this will
76 * automatically clear the targetProc to null.
77 *
78 * When an event is dispatched from the event queue, it is either sent to
79 * the eventsink matching the targetID (if not 0), or the target proc is
80 * called (if not NULL).
reed@google.comc514dde2011-08-03 19:41:24 +000081 */
reed@google.com87fac4a2011-08-04 13:50:17 +000082 SkEvent* setTargetID(SkEventSinkID targetID) {
83 fTargetProc = NULL;
84 fTargetID = targetID;
85 return this;
86 }
reed@google.comc514dde2011-08-03 19:41:24 +000087
reed@google.com87fac4a2011-08-04 13:50:17 +000088 /**
89 * Return the target proc, or NULL if it has none.
90 *
91 * When an event is dispatched from the event queue, it is either sent to
92 * the eventsink matching the targetID (if not 0), or the target proc is
93 * called (if not NULL).
94 */
95 Proc getTargetProc() const { return fTargetProc; }
96
97 /**
98 * Set the target ID for this event. NULL means none. Calling this will
99 * automatically clear the targetID to 0.
100 *
101 * When an event is dispatched from the event queue, it is either sent to
102 * the eventsink matching the targetID (if not 0), or the target proc is
103 * called (if not NULL).
104 */
105 SkEvent* setTargetProc(Proc proc) {
106 fTargetID = 0;
107 fTargetProc = proc;
108 return this;
109 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000110
reed@google.com87fac4a2011-08-04 13:50:17 +0000111 /**
112 * Return the event's unnamed 32bit field. Default value is 0
113 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 uint32_t getFast32() const { return f32; }
reed@google.com87fac4a2011-08-04 13:50:17 +0000115
116 /**
117 * Set the event's unnamed 32bit field.
118 */
119 void setFast32(uint32_t x) { f32 = x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120
121 /** Return true if the event contains the named 32bit field, and return the field
122 in value (if value is non-null). If there is no matching named field, return false
123 and ignore the value parameter.
124 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000125 bool findS32(const char name[], int32_t* value = NULL) const { return fMeta.findS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 /** Return true if the event contains the named SkScalar field, and return the field
127 in value (if value is non-null). If there is no matching named field, return false
128 and ignore the value parameter.
129 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000130 bool findScalar(const char name[], SkScalar* value = NULL) const { return fMeta.findScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 /** Return true if the event contains the named SkScalar field, and return the fields
132 in value[] (if value is non-null), and return the number of SkScalars in count (if count is non-null).
133 If there is no matching named field, return false and ignore the value and count parameters.
134 */
135 const SkScalar* findScalars(const char name[], int* count, SkScalar values[] = NULL) const { return fMeta.findScalars(name, count, values); }
136 /** Return the value of the named string field, or if no matching named field exists, return null.
137 */
138 const char* findString(const char name[]) const { return fMeta.findString(name); }
139 /** Return true if the event contains the named pointer field, and return the field
140 in value (if value is non-null). If there is no matching named field, return false
141 and ignore the value parameter.
142 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000143 bool findPtr(const char name[], void** value) const { return fMeta.findPtr(name, value); }
144 bool findBool(const char name[], bool* value) const { return fMeta.findBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000145 const void* findData(const char name[], size_t* byteCount = NULL) const {
146 return fMeta.findData(name, byteCount);
147 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
149 /** Returns true if ethe event contains the named 32bit field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000150 bool hasS32(const char name[], int32_t value) const { return fMeta.hasS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Returns true if ethe event contains the named SkScalar field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000152 bool hasScalar(const char name[], SkScalar value) const { return fMeta.hasScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 /** Returns true if ethe event contains the named string field, and if it equals (using strcmp) the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000154 bool hasString(const char name[], const char value[]) const { return fMeta.hasString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 /** Returns true if ethe event contains the named pointer field, and if it equals the specified value */
reed@google.com87fac4a2011-08-04 13:50:17 +0000156 bool hasPtr(const char name[], void* value) const { return fMeta.hasPtr(name, value); }
157 bool hasBool(const char name[], bool value) const { return fMeta.hasBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000158 bool hasData(const char name[], const void* data, size_t byteCount) const {
159 return fMeta.hasData(name, data, byteCount);
160 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161
162 /** Add/replace the named 32bit field to the event. In XML use the subelement <data name=... s32=... /> */
reed@google.com87fac4a2011-08-04 13:50:17 +0000163 void setS32(const char name[], int32_t value) { fMeta.setS32(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 /** Add/replace the named SkScalar field to the event. In XML use the subelement <data name=... scalar=... /> */
reed@google.com87fac4a2011-08-04 13:50:17 +0000165 void setScalar(const char name[], SkScalar value) { fMeta.setScalar(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 /** Add/replace the named SkScalar[] field to the event. */
167 SkScalar* setScalars(const char name[], int count, const SkScalar values[] = NULL) { return fMeta.setScalars(name, count, values); }
168 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
reed@google.com87fac4a2011-08-04 13:50:17 +0000169 void setString(const char name[], const SkString& value) { fMeta.setString(name, value.c_str()); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 /** Add/replace the named string field to the event. In XML use the subelement <data name=... string=... */
reed@google.com87fac4a2011-08-04 13:50:17 +0000171 void setString(const char name[], const char value[]) { fMeta.setString(name, value); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 /** Add/replace the named pointer field to the event. There is no XML equivalent for this call */
reed@google.com87fac4a2011-08-04 13:50:17 +0000173 void setPtr(const char name[], void* value) { fMeta.setPtr(name, value); }
174 void setBool(const char name[], bool value) { fMeta.setBool(name, value); }
reed@android.comf2b98d62010-12-20 18:26:13 +0000175 void setData(const char name[], const void* data, size_t byteCount) {
176 fMeta.setData(name, data, byteCount);
177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178
179 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000180 SkMetaData& getMetaData() { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 /** Return the underlying metadata object */
reed@google.com87fac4a2011-08-04 13:50:17 +0000182 const SkMetaData& getMetaData() const { return fMeta; }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183
184 /** Call this to initialize the event from the specified XML node */
Hal Canary20de6152017-02-23 13:24:49 -0500185 void inflate(const SkDOM&, const SkDOMNode*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186
187 SkDEBUGCODE(void dump(const char title[] = NULL);)
188
reed@google.com87fac4a2011-08-04 13:50:17 +0000189 ///////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
reed@google.comc514dde2011-08-03 19:41:24 +0000191 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000192 * Post to the event queue using the event's targetID or target-proc.
193 *
194 * The event must be dynamically allocated, as ownership is transferred to
195 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000196 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000197 void post() {
reed@google.comc514dde2011-08-03 19:41:24 +0000198 return this->postDelay(0);
199 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
reed@google.comc514dde2011-08-03 19:41:24 +0000201 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000202 * Post to the event queue using the event's targetID or target-proc and
203 * the specifed millisecond delay.
204 *
205 * The event must be dynamically allocated, as ownership is transferred to
206 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000207 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000208 void postDelay(SkMSec delay);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209
reed@google.comc514dde2011-08-03 19:41:24 +0000210 /**
reed@google.com87fac4a2011-08-04 13:50:17 +0000211 * Post to the event queue using the event's targetID or target-proc.
212 * The event will be delivered no sooner than the specified millisecond
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700213 * time, as measured by GetMSecsSinceStartup().
reed@google.com87fac4a2011-08-04 13:50:17 +0000214 *
215 * The event must be dynamically allocated, as ownership is transferred to
216 * the event queue. It cannot be allocated on the stack or in a global.
reed@google.comc514dde2011-08-03 19:41:24 +0000217 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000218 void postTime(SkMSec time);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700220 /**
221 * Returns ~zero the first time it's called, then returns the number of
222 * milliseconds since the first call. Behavior is undefined if the program
223 * runs more than ~25 days.
224 */
225 static SkMSec GetMSecsSinceStartup();
226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 ///////////////////////////////////////////////
228 /** Porting layer must call these functions **/
229 ///////////////////////////////////////////////
230
231 /** Global initialization function for the SkEvent system. Should be called exactly
232 once before any other event method is called, and should be called after the
233 call to SkGraphics::Init().
234 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000235 static void Init();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 /** Global cleanup function for the SkEvent system. Should be called exactly once after
mtkleinfe81e2d2015-09-09 07:35:42 -0700237 all event methods have been called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000239 static void Term();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240
241 /** Call this to process one event from the queue. If it returns true, there are more events
242 to process.
243 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000244 static bool ProcessEvent();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 /** Call this whenever the requested timer has expired (requested by a call to SetQueueTimer).
246 It will post any delayed events whose time as "expired" onto the event queue.
247 It may also call SignalQueueTimer() and SignalNonEmptyQueue().
248 */
reed@google.com87fac4a2011-08-04 13:50:17 +0000249 static void ServiceQueueTimer();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
reed@android.comf2b98d62010-12-20 18:26:13 +0000251 /** Return the number of queued events. note that this value may be obsolete
252 upon return, since another thread may have called ProcessEvent() or
253 Post() after the count was made.
254 */
255 static int CountEventsOnQueue();
256
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 ////////////////////////////////////////////////////
258 /** Porting layer must implement these functions **/
259 ////////////////////////////////////////////////////
260
261 /** Called whenever an SkEvent is posted to an empty queue, so that the OS
262 can be told to later call Dequeue().
263 */
264 static void SignalNonEmptyQueue();
265 /** Called whenever the delay until the next delayed event changes. If zero is
266 passed, then there are no more queued delay events.
267 */
268 static void SignalQueueTimer(SkMSec delay);
269
tfarina@chromium.org887760f2012-10-01 16:14:21 +0000270#if defined(SK_BUILD_FOR_WIN)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 static bool WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273
274private:
275 SkMetaData fMeta;
276 mutable char* fType; // may be characters with low bit set to know that it is not a pointer
277 uint32_t f32;
reed@google.com87fac4a2011-08-04 13:50:17 +0000278
279 // 'there can be only one' (non-zero) between target-id and target-proc
reed@google.comc514dde2011-08-03 19:41:24 +0000280 SkEventSinkID fTargetID;
reed@google.com87fac4a2011-08-04 13:50:17 +0000281 Proc fTargetProc;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282
283 // these are for our implementation of the event queue
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 SkMSec fTime;
285 SkEvent* fNextEvent; // either in the delay or normal event queue
reed@google.com87fac4a2011-08-04 13:50:17 +0000286
287 void initialize(const char* type, size_t typeLen, SkEventSinkID);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
289 static bool Enqueue(SkEvent* evt);
290 static SkMSec EnqueueTime(SkEvent* evt, SkMSec time);
reed@google.com87fac4a2011-08-04 13:50:17 +0000291 static SkEvent* Dequeue();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 static bool QHasEvents();
293};
294
295#endif