Home | History | Annotate | Download | only in docs
      1 #Topic Picture
      2 #Alias Pictures ##
      3 #Alias Picture_Reference ##
      4 
      5 #Class SkPicture
      6 
      7 #Code
      8 #Populate
      9 ##
     10 
     11 Picture records drawing commands made to Canvas. The command stream may be
     12 played in whole or in part at a later time.
     13 
     14 Picture is an abstract class. Picture may be generated by Picture_Recorder
     15 or Drawable, or from Picture previously saved to Data or Stream. 
     16 
     17 Picture may contain any Canvas drawing command, as well as one or more
     18 Canvas_Matrix or Canvas_Clip. Picture has a cull Rect, which is used as
     19 a bounding box hint. To limit Picture bounds, use Canvas_Clip when
     20 recording or drawing Picture.
     21 
     22 # ------------------------------------------------------------------------------
     23 
     24 #Class AbortCallback
     25 #Line # utility to stop picture playback ##
     26 
     27 #Code
     28     class AbortCallback {
     29     public:
     30         AbortCallback() {}
     31         virtual ~AbortCallback() {}
     32         virtual bool abort() = 0;
     33     };
     34 ##
     35 
     36 AbortCallback is an abstract class. An implementation of AbortCallback may
     37 passed as a parameter to SkPicture::playback, to stop it before all drawing
     38 commands have been processed.
     39 
     40 If AbortCallback::abort returns true, SkPicture::playback is interrupted.
     41 
     42 # ------------------------------------------------------------------------------
     43 
     44 #Method AbortCallback()
     45 #In Constructors
     46 #Line # defines default constructor ##
     47 #Populate
     48 
     49 #NoExample
     50 ##
     51 
     52 #SeeAlso playback
     53 
     54 #Method ##
     55 
     56 # ------------------------------------------------------------------------------
     57 
     58 #Method virtual ~AbortCallback()
     59 #In Constructors
     60 #Line # defines default destructor ##
     61 #Populate
     62 
     63 #NoExample
     64 ##
     65 
     66 #SeeAlso playback
     67 
     68 #Method ##
     69 
     70 # ------------------------------------------------------------------------------
     71 
     72 #Method virtual bool abort() = 0
     73 #In Utility
     74 #Line # aborts playback by callback ##
     75 #Populate
     76 
     77 #NoExample
     78 ##
     79 
     80 #SeeAlso playback
     81 
     82 #Method ##
     83 
     84 #Example
     85 #Description
     86 JustOneDraw allows the black rectangle to draw but stops playback before the
     87 white rectangle appears.
     88 ##
     89 #Function
     90 class JustOneDraw : public SkPicture::AbortCallback {
     91 public:
     92     bool abort() override { return fCalls++ > 0; }
     93 private:
     94     int fCalls = 0;
     95 };
     96 ##
     97 
     98 void draw(SkCanvas* canvas) {
     99     SkPictureRecorder recorder;
    100     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    101     SkPaint paint;
    102     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    103     paint.setColor(SK_ColorWHITE);
    104     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    105     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    106     JustOneDraw callback;
    107     picture->playback(canvas, &callback);
    108 }
    109 
    110 ##
    111 
    112 #Class AbortCallback ##
    113 
    114 # ------------------------------------------------------------------------------
    115 
    116 #Method static sk_sp<SkPicture> MakeFromStream(SkStream* stream,
    117                                            const SkDeserialProcs* procs = nullptr)
    118 #In Constructors
    119 #Line # constructs Picture from stream ##
    120 #Populate
    121 
    122 #Example
    123     SkPictureRecorder recorder;
    124     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    125     SkPaint paint;
    126     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    127     paint.setColor(SK_ColorWHITE);
    128     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    129     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    130     SkDynamicMemoryWStream writableStream;
    131     picture->serialize(&writableStream);
    132     std::unique_ptr<SkStreamAsset> readableStream = writableStream.detachAsStream();
    133     sk_sp<SkPicture> copy = SkPicture::MakeFromStream(readableStream.get());
    134     copy->playback(canvas);
    135 ##
    136 
    137 #SeeAlso MakeFromData SkPictureRecorder
    138 
    139 #Method ##
    140 
    141 # ------------------------------------------------------------------------------
    142 
    143 #Method static sk_sp<SkPicture> MakeFromData(const SkData* data,
    144                                          const SkDeserialProcs* procs = nullptr)
    145 #In Constructors
    146 #Line # constructs Picture from data ##
    147 #Populate
    148 
    149 #Example
    150     SkPictureRecorder recorder;
    151     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    152     SkPaint paint;
    153     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    154     paint.setColor(SK_ColorWHITE);
    155     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    156     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    157     SkDynamicMemoryWStream writableStream;
    158     picture->serialize(&writableStream);
    159     sk_sp<SkData> readableData = writableStream.detachAsData();
    160     sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData.get());
    161     copy->playback(canvas);
    162 ##
    163 
    164 #SeeAlso MakeFromStream SkPictureRecorder
    165 
    166 #Method ##
    167 
    168 # ------------------------------------------------------------------------------
    169 
    170 #Method static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
    171                                          const SkDeserialProcs* procs = nullptr)
    172 #Populate
    173 
    174 #Example
    175     SkPictureRecorder recorder;
    176     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    177     SkPaint paint;
    178     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    179     paint.setColor(SK_ColorWHITE);
    180     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    181     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    182     SkDynamicMemoryWStream writableStream;
    183     picture->serialize(&writableStream);
    184     sk_sp<SkData> readableData = writableStream.detachAsData();
    185     sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    186     copy->playback(canvas);
    187 ##
    188 
    189 #SeeAlso MakeFromStream SkPictureRecorder
    190 
    191 #Method ##
    192 
    193 # ------------------------------------------------------------------------------
    194 
    195 #Method virtual void playback(SkCanvas* canvas, AbortCallback* callback = nullptr) const = 0
    196 #In Action
    197 #Line # replays drawing commands on canvas ##
    198 #Populate
    199 
    200 #Example
    201     SkPictureRecorder recorder;
    202     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    203     SkPaint paint;
    204     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    205     paint.setColor(SK_ColorWHITE);
    206     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    207     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    208     picture->playback(canvas);
    209 ##
    210 
    211 #SeeAlso SkCanvas::drawPicture
    212 
    213 #Method ##
    214 
    215 # ------------------------------------------------------------------------------
    216 
    217 #Method virtual SkRect cullRect() const = 0
    218 #In Property
    219 #Line # returns bounds used to record Picture ##
    220 #Populate
    221 
    222 #Example
    223 #Description
    224 Picture recorded bounds are smaller than contents; contents outside recorded
    225 bounds may be drawn, and are drawn in this example.
    226 ##
    227     SkPictureRecorder recorder;
    228     SkCanvas* pictureCanvas = recorder.beginRecording({64, 64, 192, 192});
    229     SkPaint paint;
    230     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    231     paint.setColor(SK_ColorWHITE);
    232     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    233     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    234     picture->playback(canvas);
    235     paint.setBlendMode(SkBlendMode::kModulate);
    236     paint.setColor(0x40404040);
    237     canvas->drawRect(picture->cullRect(), paint);
    238 ##
    239 
    240 #SeeAlso SkCanvas::clipRect
    241 
    242 #Method ##
    243 
    244 # ------------------------------------------------------------------------------
    245 
    246 #Method uint32_t uniqueID() const
    247 #In Property
    248 #Line # returns identifier for Picture ##
    249 #Populate
    250 
    251 #Example
    252     SkPictureRecorder recorder;
    253     recorder.beginRecording({0, 0, 0, 0});
    254     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    255     SkDebugf("empty picture id = %d\n", picture->uniqueID());
    256     sk_sp<SkPicture> placeholder = SkPicture::MakePlaceholder({0, 0, 0, 0});
    257     SkDebugf("placeholder id = %d\n", placeholder->uniqueID());
    258 #StdOut
    259 empty picture id = 1
    260 placeholder id = 2
    261 ##
    262 ##
    263 
    264 #SeeAlso SkRefCnt
    265 
    266 #Method ##
    267 
    268 # ------------------------------------------------------------------------------
    269 
    270 #Method sk_sp<SkData> serialize(const SkSerialProcs* procs = nullptr) const
    271 #In Utility
    272 #Line # writes Picture to Data ##
    273 #Populate
    274 
    275 #Example
    276     SkPictureRecorder recorder;
    277     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    278     SkPaint paint;
    279     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    280     paint.setColor(SK_ColorWHITE);
    281     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    282     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    283     sk_sp<SkData> readableData = picture->serialize();
    284     sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    285     copy->playback(canvas);
    286 ##
    287 
    288 #SeeAlso MakeFromData SkData SkSerialProcs
    289 
    290 #Method ##
    291 
    292 # ------------------------------------------------------------------------------
    293 
    294 #Method void serialize(SkWStream* stream, const SkSerialProcs* procs = nullptr) const
    295 #Populate
    296 
    297 #Example
    298     SkPictureRecorder recorder;
    299     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    300     SkPaint paint;
    301     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    302     paint.setColor(SK_ColorWHITE);
    303     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    304     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    305     SkDynamicMemoryWStream writableStream;
    306     picture->serialize(&writableStream);
    307     sk_sp<SkData> readableData = writableStream.detachAsData();
    308     sk_sp<SkPicture> copy = SkPicture::MakeFromData(readableData->data(), readableData->size());
    309     copy->playback(canvas);
    310 ##
    311 
    312 #SeeAlso MakeFromStream SkWStream SkSerialProcs
    313 
    314 #Method ##
    315 
    316 # ------------------------------------------------------------------------------
    317 
    318 #Method static sk_sp<SkPicture> MakePlaceholder(SkRect cull)
    319 #In Constructors
    320 #Line # constructs placeholder with unique identifier ##
    321 #Populate
    322 
    323 #Example
    324 #Function
    325 class MyCanvas : public SkCanvas {
    326 public:
    327     MyCanvas(SkCanvas* c) : canvas(c) {}
    328         void onDrawPicture(const SkPicture* picture, const SkMatrix* ,
    329                                const SkPaint* ) override {
    330         const SkRect rect = picture->cullRect();
    331         SkPaint redPaint;
    332         redPaint.setColor(SK_ColorRED);
    333         canvas->drawRect(rect, redPaint);
    334    }
    335 
    336    SkCanvas* canvas;
    337 };
    338 ##
    339 SkPictureRecorder recorder;
    340 SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    341 sk_sp<SkPicture> placeholder = SkPicture::MakePlaceholder({10, 40, 80, 110});
    342 pictureCanvas->drawPicture(placeholder);
    343 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    344 MyCanvas myCanvas(canvas);
    345 myCanvas.drawPicture(picture);
    346 ##
    347 
    348 #SeeAlso MakeFromStream MakeFromData uniqueID
    349 
    350 #Method ##
    351 
    352 # ------------------------------------------------------------------------------
    353 
    354 #Method virtual int approximateOpCount() const = 0
    355 #In Property
    356 #Line # returns approximate operation count ##
    357 #Populate
    358 
    359 #Example
    360     SkPictureRecorder recorder;
    361     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    362     SkPaint paint;
    363     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    364     paint.setColor(SK_ColorWHITE);
    365     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    366     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    367     picture->playback(canvas);
    368     std::string opCount = "approximate op count: " + std::to_string(picture->approximateOpCount());
    369     canvas->drawString(opCount.c_str(), 50, 220, SkPaint());
    370 ##
    371 
    372 #SeeAlso approximateBytesUsed
    373 
    374 #Method ##
    375 
    376 # ------------------------------------------------------------------------------
    377 
    378 #Method virtual size_t approximateBytesUsed() const = 0
    379 #In Property
    380 #Line # returns approximate size ##
    381 #Populate
    382 
    383 #Example
    384     SkPictureRecorder recorder;
    385     SkCanvas* pictureCanvas = recorder.beginRecording({0, 0, 256, 256});
    386     SkPaint paint;
    387     pictureCanvas->drawRect(SkRect::MakeWH(200, 200), paint);
    388     paint.setColor(SK_ColorWHITE);
    389     pictureCanvas->drawRect(SkRect::MakeLTRB(20, 20, 180, 180), paint);
    390     sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
    391     picture->playback(canvas);
    392     std::string opCount = "approximate bytes used: " + std::to_string(picture->approximateBytesUsed());
    393     canvas->drawString(opCount.c_str(), 20, 220, SkPaint());
    394 ##
    395 
    396 #SeeAlso approximateOpCount
    397 
    398 #Method ##
    399 
    400 #Class SkPicture ##
    401 
    402 #Topic Picture ##
    403