blob: 8f65b3eb557d4d57328b97c8bc4c417fb211d78d [file] [log] [blame]
reed@google.comc9062042012-07-30 18:06:00 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkSurface_Base.h"
9#include "SkCanvas.h"
10#include "SkImagePriv.h"
11#include "SkPicture.h"
12
13/**
14 * What does it mean to ask for more than one canvas from a picture?
15 * How do we return an Image and then "continue" recording?
16 */
17class SkSurface_Picture : public SkSurface_Base {
18public:
19 SkSurface_Picture(int width, int height);
20 virtual ~SkSurface_Picture();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000021
reed@google.comc9062042012-07-30 18:06:00 +000022 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
reed@google.com2bd8b812013-11-01 13:46:54 +000023 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
junov@chromium.org5ee449a2013-04-12 20:20:50 +000024 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
reed@google.comc9062042012-07-30 18:06:00 +000025 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
26 const SkPaint*) SK_OVERRIDE;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +000027 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000028 virtual void onDiscard() SK_OVERRIDE;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
reed@google.comc9062042012-07-30 18:06:00 +000030private:
31 SkPicture* fPicture;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032
reed@google.comc9062042012-07-30 18:06:00 +000033 typedef SkSurface_Base INHERITED;
34};
35
36///////////////////////////////////////////////////////////////////////////////
37
38SkSurface_Picture::SkSurface_Picture(int width, int height) : INHERITED(width, height) {
39 fPicture = NULL;
40}
41
42SkSurface_Picture::~SkSurface_Picture() {
43 SkSafeUnref(fPicture);
44}
45
46SkCanvas* SkSurface_Picture::onNewCanvas() {
47 if (!fPicture) {
48 fPicture = SkNEW(SkPicture);
49 }
50 SkCanvas* canvas = fPicture->beginRecording(this->width(), this->height());
51 canvas->ref(); // our caller will call unref()
52 return canvas;
53}
54
reed@google.com2bd8b812013-11-01 13:46:54 +000055SkSurface* SkSurface_Picture::onNewSurface(const SkImageInfo& info) {
reed@google.comc9062042012-07-30 18:06:00 +000056 return SkSurface::NewPicture(info.fWidth, info.fHeight);
57}
58
junov@chromium.org5ee449a2013-04-12 20:20:50 +000059SkImage* SkSurface_Picture::onNewImageSnapshot() {
reed@google.comc9062042012-07-30 18:06:00 +000060 if (fPicture) {
61 return SkNewImageFromPicture(fPicture);
62 } else {
reed@google.com2bd8b812013-11-01 13:46:54 +000063 SkImageInfo info;
reed@google.comc9062042012-07-30 18:06:00 +000064 info.fWidth = info.fHeight = 0;
commit-bot@chromium.org757ebd22014-04-10 22:36:34 +000065 info.fColorType = kPMColor_SkColorType;
reed@google.comd28ba802013-09-20 19:33:52 +000066 info.fAlphaType = kOpaque_SkAlphaType;
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000067 return SkImage::NewRasterCopy(info, NULL, 0);
reed@google.comc9062042012-07-30 18:06:00 +000068 }
69}
70
71void SkSurface_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
72 const SkPaint* paint) {
73 if (!fPicture) {
74 return;
75 }
76 SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
77}
78
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000079void SkSurface_Picture::onCopyOnWrite(ContentChangeMode mode) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +000080 // We always spawn a copy of the recording picture when we
robertphillips@google.com97b6b072012-10-31 14:48:39 +000081 // are asked for a snapshot, so we never need to do anything here.
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000082 if (kDiscard_ContentChangeMode == mode) {
83 this->SkSurface_Picture::onDiscard();
84 }
85}
86
87void SkSurface_Picture::onDiscard() {
88 if (NULL != fPicture) {
89 fPicture->beginRecording(this->width(), this->height());
90 }
robertphillips@google.com97b6b072012-10-31 14:48:39 +000091}
92
reed@google.comc9062042012-07-30 18:06:00 +000093///////////////////////////////////////////////////////////////////////////////
94
95
96SkSurface* SkSurface::NewPicture(int width, int height) {
97 if ((width | height) < 0) {
98 return NULL;
99 }
100
101 return SkNEW_ARGS(SkSurface_Picture, (width, height));
102}