aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkCanvasDrawable.cpp
blob: 0065d430f900b5be411a167e1ee572185bcc66c5 (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
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkCanvas.h"
#include "SkCanvasDrawable.h"
#include "SkThread.h"

static int32_t next_generation_id() {
    static int32_t gCanvasDrawableGenerationID;

    // do a loop in case our global wraps around, as we never want to
    // return a 0
    int32_t genID;
    do {
        genID = sk_atomic_inc(&gCanvasDrawableGenerationID) + 1;
    } while (0 == genID);
    return genID;
}

SkCanvasDrawable::SkCanvasDrawable() : fGenerationID(0) {}

void SkCanvasDrawable::draw(SkCanvas* canvas) {
    SkAutoCanvasRestore acr(canvas, true);
    this->onDraw(canvas);
}

SkPicture* SkCanvasDrawable::newPictureSnapshot(SkBBHFactory* bbhFactory, uint32_t recordFlags) {
    return this->onNewPictureSnapshot(bbhFactory, recordFlags);
}

uint32_t SkCanvasDrawable::getGenerationID() {
    if (0 == fGenerationID) {
        fGenerationID = next_generation_id();
    }
    return fGenerationID;
}

SkRect SkCanvasDrawable::getBounds() {
    return this->onGetBounds();
}

void SkCanvasDrawable::notifyDrawingChanged() {
    fGenerationID = 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

#include "SkPictureRecorder.h"

SkPicture* SkCanvasDrawable::onNewPictureSnapshot(SkBBHFactory* bbhFactory, uint32_t recordFlags) {
    const SkRect bounds = this->getBounds();
    SkPictureRecorder recorder;
    this->draw(recorder.beginRecording(bounds.width(), bounds.height(), bbhFactory, recordFlags));
    return recorder.endRecording();
}