aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/SkPicture_Reference.bmh
blob: 64561de50ed983c44ee66d8c2f37a790132dbc24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#Topic Picture
#Alias Pictures ##
#Alias Picture_Reference ##

#Class SkPicture

An SkPicture records drawing commands made to a canvas to be played back at a later time.
This base class handles serialization and a few other miscellany.

#Subtopic Overview
#Populate
##

#Subtopic Class
#Populate
##

#Subtopic Member_Function
#Populate
##

# ------------------------------------------------------------------------------

#Class AbortCallback
#Line # utility to stop picture playback ##

#Code
    class AbortCallback {
    public:
        AbortCallback() {}
        virtual ~AbortCallback() {}
        virtual bool abort() = 0;
    };
##

Subclasses of this can be passed to playback(). During the playback
of the picture, this callback will periodically be invoked. If its
abort() returns true, then picture playback will be interrupted.
The resulting drawing is undefined, as there is no guarantee how often the
callback will be invoked. If the abort happens inside some level of nested
calls to save(), restore will automatically be called to return the state
to the same level it was before the playback call was made.

# ------------------------------------------------------------------------------

#Method AbortCallback()
#In Constructor
#Line # defines default constructor ##
Has no effect.

#Return abstract class cannot be instantiated ##

#NoExample
##

#SeeAlso playback

#Method ##

# ------------------------------------------------------------------------------

#Method virtual ~AbortCallback()
#In Constructor
#Line # defines default destructor ##
Has no effect.

#NoExample
##

#SeeAlso playback

#Method ##

# ------------------------------------------------------------------------------

#Method virtual bool abort() = 0
#In Utility
#Line # aborts playback by callback ##

Stops Picture playback when some condition is met. A subclass of
AbortCallback provides an override for abort() that can stop playback() from
drawing the entire picture.

#Return true to stop playback ##

#NoExample
##

#SeeAlso playback

#Method ##

#Example
#Description
JustOneDraw allows the black rectangle to draw but stops playback before the
white rectangle appears.
##
#Function
class JustOneDraw : public SkPicture::AbortCallback {
public:
    bool abort() override { return fCalls++ > 0; }
private:
    int fCalls = 0;
};
##

void draw(SkCanvas* canvas) {
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    JustOneDraw callback;
    picture->playback(canvas, &callback);
}

##

#Class AbortCallback ##

# ------------------------------------------------------------------------------

#Method static sk_sp<SkPicture> MakeFromStream(SkStream* stream,
                                           const SkDeserialProcs* procs = nullptr)
#In Constructor
#Line # constructs Picture from stream ##

Recreates a picture that was serialized into a stream.

#Param stream  container for serial data ##
#Param procs  custom serial data decoders; may be nullptr ##

#Return Picture constructed from stream data ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    SkDynamicMemoryWStream writableStream;
    picture->serialize(&writableStream);
    std::unique_ptr<SkStreamAsset> readableStream = writableStream.detachAsStream();
    sk_sp<SkPicture> copy = SkPicture::MakeFromStream(readableStream.get());
    copy->playback(canvas);
##

#SeeAlso MakeFromData SkPictureRecorder

#Method ##

# ------------------------------------------------------------------------------

#Method static sk_sp<SkPicture> MakeFromData(const SkData* data,
                                         const SkDeserialProcs* procs = nullptr)
#In Constructor
#Line # constructs Picture from data ##

Recreates a picture that was serialized into data.

#Param data  container for serial data ##
#Param procs  custom serial data decoders; may be nullptr ##

#Return Picture constructed from data ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    SkDynamicMemoryWStream writableStream;
    picture->serialize(&writableStream);
    sk_sp<SkData> readableData = writableStream.detachAsData();
    sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData.get());
    copy->playback(canvas);
##

#SeeAlso MakeFromStream SkPictureRecorder

#Method ##

# ------------------------------------------------------------------------------

#Method static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
                                         const SkDeserialProcs* procs = nullptr)

#Param data  pointer to serial data ##
#Param size  size of data ##
#Param procs  custom serial data decoders; may be nullptr ##

Recreates a picture that was serialized into data.

#Return Picture constructed from data ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    SkDynamicMemoryWStream writableStream;
    picture->serialize(&writableStream);
    sk_sp<SkData> readableData = writableStream.detachAsData();
    sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    copy->playback(canvas);
##

#SeeAlso MakeFromStream SkPictureRecorder

#Method ##

# ------------------------------------------------------------------------------

#Method virtual void playback(SkCanvas* canvas, AbortCallback* callback = nullptr) const = 0
#In Action
#Line # replays drawing commands on canvas ##

Replays the drawing commands on the specified canvas. Note that
this has the effect of unfurling this picture into the destination
canvas. Using the SkCanvas::drawPicture entry point gives the destination
canvas the option of just taking a ref.

#Param canvas  receiver of drawing commands ##
#Param callback  allows interruption of playback ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    picture->playback(canvas);
##

#SeeAlso SkCanvas::drawPicture

#Method ##

# ------------------------------------------------------------------------------

#Method virtual SkRect cullRect() const = 0
#In Property
#Line # returns bounds used to record Picture ##

Returns cull Rect for this picture.
Ops recorded into this picture that attempt to draw outside the cull might not be drawn.

#Return bounds passed when Picture was created ##

#Example
#Description
Picture recorded bounds are smaller than contents; contents outside recorded
bounds may be drawn, and are drawn in this example.
##
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({64, 64, 192, 192});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    picture->playback(canvas);
    paint.setBlendMode(SkBlendMode::kModulate);
    paint.setColor(0x40404040);
    canvas->drawRect(picture->cullRect(), paint);
##

#SeeAlso SkCanvas::clipRect

#Method ##

# ------------------------------------------------------------------------------

#Method uint32_t uniqueID() const
#In Property
#Line # returns identifier for Picture ##

Returns a non-zero value unique among all pictures.

#Return identifier for Picture ##

#Example
    SkPictureRecorder recorder;
    recorder.beginRecording({0, 0, 0, 0});
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    SkDebugf("empty picture id = %d\n", picture->uniqueID());
    sk_sp<SkPicture> placeholder = SkPicture::MakePlaceholder({0, 0, 0, 0});
    SkDebugf("placeholder id = %d\n", placeholder->uniqueID());
#StdOut
empty picture id = 1
placeholder id = 2
##
##

#SeeAlso SkRefCnt

#Method ##

# ------------------------------------------------------------------------------

#Method sk_sp<SkData> serialize(const SkSerialProcs* procs = nullptr) const
#In Utility
#Line # writes Picture to data ##
Returns storage containing data describing Picture, using optional custom encoders.

#Param procs  custom serial data encoders; may be nullptr ##

#Return storage containing serialized Picture ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    sk_sp<SkData> readableData = picture->serialize();
    sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    copy->playback(canvas);
##

#SeeAlso MakeFromData SkData SkSerialProcs

#Method ##

# ------------------------------------------------------------------------------

#Method void serialize(SkWStream* stream, const SkSerialProcs* procs = nullptr) const
Writes picture to stream, using optional custom encoders.

#Param stream  writable serial data stream ##
#Param procs  custom serial data encoders; may be nullptr ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    SkDynamicMemoryWStream writableStream;
    picture->serialize(&writableStream);
    sk_sp<SkData> readableData = writableStream.detachAsData();
    sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    copy->playback(canvas);
##

#SeeAlso MakeFromStream SkWStream SkSerialProcs

#Method ##

# ------------------------------------------------------------------------------

#Method static sk_sp<SkPicture> MakePlaceholder(SkRect cull)
#In Constructor
#Line # constructs placeholder with unique identifier ##

Returns a placeholder SkPicture.
This placeholder does not draw anything itself.  It has a distinct uniqueID()
(just like all Pictures) and will always be visible to SkSerialProcs.

#Param cull  placeholder dimensions
##

#Return placeholder with unique identifier ##

#Example
sk_sp<SkPicture> pict1 = SkPicture::MakePlaceholder({10, 40, 80, 110});
sk_sp<SkPicture> pict2 = SkPicture::MakePlaceholder({10, 40, 80, 110});
for (auto pict : { pict1, pict2 } ) {
    SkRect bounds = pict->cullRect();
    SkDebugf("id:%d bounds:{%g, %g, %g, %g}\n", pict->uniqueID(), 
             bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
}
#StdOut
id:1 bounds:{10, 40, 80, 110}
id:2 bounds:{10, 40, 80, 110}
##
##

#SeeAlso MakeFromStream MakeFromData

#Method ##

# ------------------------------------------------------------------------------

#Method virtual int approximateOpCount() const = 0
#In Property
#Line # returns approximate operation count ##

Returns the approximate number of operations in this picture.  This
number may be greater or less than the number of SkCanvas calls
recorded: some calls may be recorded as more than one operation, or some
calls may be optimized away.

#Return approximate operation count ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    picture->playback(canvas);
    std::string opCount = "approximate op count: " + std::to_string(picture->approximateOpCount());
    canvas->drawString(opCount.c_str(), 50, 220, SkPaint());
##

#SeeAlso approximateBytesUsed

#Method ##

# ------------------------------------------------------------------------------

#Method virtual size_t approximateBytesUsed() const = 0
#In Property
#Line # returns approximate size ##

Returns the approximate byte size of Picture. Does not include large objects
referenced Picture.

#Return approximate size ##

#Example
    SkPictureRecorder recorder;
    SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    SkPaint paint;
    pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    paint.setColor(SK_ColorWHITE);
    pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    picture->playback(canvas);
    std::string opCount = "approximate bytes used: " + std::to_string(picture->approximateBytesUsed());
    canvas->drawString(opCount.c_str(), 20, 220, SkPaint());
##

#SeeAlso approximateOpCount

#Method ##

#Class SkPicture ##

#Topic Picture ##