blob: 8cdd1d96189ab4909cd495807b8ab2f2817a6da2 [file] [log] [blame]
rileya@google.com9f5898d2012-09-11 20:21:44 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkBBoxHierarchyRecord.h"
10#include "SkPictureStateTree.h"
rileya@google.com9f5898d2012-09-11 20:21:44 +000011
robertphillips0bdbea72014-06-11 11:37:55 -070012SkBBoxHierarchyRecord::SkBBoxHierarchyRecord(const SkISize& size,
commit-bot@chromium.org19fafef2014-02-17 15:28:00 +000013 uint32_t recordFlags,
14 SkBBoxHierarchy* h)
robertphillips0bdbea72014-06-11 11:37:55 -070015 : INHERITED(size, recordFlags) {
rileya@google.com9f5898d2012-09-11 20:21:44 +000016 fStateTree = SkNEW(SkPictureStateTree);
17 fBoundingHierarchy = h;
18 fBoundingHierarchy->ref();
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000019 fBoundingHierarchy->setClient(this);
rileya@google.com9f5898d2012-09-11 20:21:44 +000020}
21
22void SkBBoxHierarchyRecord::handleBBox(const SkRect& bounds) {
23 SkIRect r;
24 bounds.roundOut(&r);
reed@google.com44699382013-10-31 17:28:30 +000025 SkPictureStateTree::Draw* draw = fStateTree->appendDraw(this->writeStream().bytesWritten());
rileya@google.com9f5898d2012-09-11 20:21:44 +000026 fBoundingHierarchy->insert(draw, r, true);
27}
28
Florin Malita5f6102d2014-06-30 10:13:28 -040029void SkBBoxHierarchyRecord::willSave() {
rileya@google.com9f5898d2012-09-11 20:21:44 +000030 fStateTree->appendSave();
Florin Malita5f6102d2014-06-30 10:13:28 -040031 this->INHERITED::willSave();
rileya@google.com9f5898d2012-09-11 20:21:44 +000032}
33
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000034SkCanvas::SaveLayerStrategy SkBBoxHierarchyRecord::willSaveLayer(const SkRect* bounds,
35 const SkPaint* paint,
36 SaveFlags flags) {
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000037 // For now, assume all filters affect transparent black.
38 // FIXME: This could be made less conservative as an optimization.
39 bool paintAffectsTransparentBlack = NULL != paint &&
40 ((NULL != paint->getImageFilter()) ||
41 (NULL != paint->getColorFilter()));
42 SkRect drawBounds;
43 if (paintAffectsTransparentBlack) {
mtkleind910f542014-08-22 09:06:34 -070044 SkIRect deviceBounds;
45 this->getClipDeviceBounds(&deviceBounds);
46 drawBounds.set(deviceBounds);
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000047 }
reed@google.com44699382013-10-31 17:28:30 +000048 fStateTree->appendSaveLayer(this->writeStream().bytesWritten());
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000049 SkCanvas::SaveLayerStrategy strategy = this->INHERITED::willSaveLayer(bounds, paint, flags);
50 if (paintAffectsTransparentBlack) {
51 this->handleBBox(drawBounds);
52 this->addNoOp();
53 }
54 return strategy;
rileya@google.com9f5898d2012-09-11 20:21:44 +000055}
56
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000057void SkBBoxHierarchyRecord::willRestore() {
rileya@google.com9f5898d2012-09-11 20:21:44 +000058 fStateTree->appendRestore();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000059 this->INHERITED::willRestore();
rileya@google.com9f5898d2012-09-11 20:21:44 +000060}
61
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000062void SkBBoxHierarchyRecord::didConcat(const SkMatrix& matrix) {
rileya@google.com9f5898d2012-09-11 20:21:44 +000063 fStateTree->appendTransform(getTotalMatrix());
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000064 INHERITED::didConcat(matrix);
rileya@google.com9f5898d2012-09-11 20:21:44 +000065}
66
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000067void SkBBoxHierarchyRecord::didSetMatrix(const SkMatrix& matrix) {
rileya@google.com9f5898d2012-09-11 20:21:44 +000068 fStateTree->appendTransform(getTotalMatrix());
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000069 INHERITED::didSetMatrix(matrix);
rileya@google.com9f5898d2012-09-11 20:21:44 +000070}
71
robertphillips@google.com8f90a892014-02-28 18:19:39 +000072void SkBBoxHierarchyRecord::onClipRect(const SkRect& rect,
73 SkRegion::Op op,
74 ClipEdgeStyle edgeStyle) {
reed@google.com44699382013-10-31 17:28:30 +000075 fStateTree->appendClip(this->writeStream().bytesWritten());
robertphillips@google.com8f90a892014-02-28 18:19:39 +000076 this->INHERITED::onClipRect(rect, op, edgeStyle);
rileya@google.com9f5898d2012-09-11 20:21:44 +000077}
78
robertphillips@google.com8f90a892014-02-28 18:19:39 +000079void SkBBoxHierarchyRecord::onClipRegion(const SkRegion& region,
80 SkRegion::Op op) {
reed@google.com44699382013-10-31 17:28:30 +000081 fStateTree->appendClip(this->writeStream().bytesWritten());
robertphillips@google.com8f90a892014-02-28 18:19:39 +000082 this->INHERITED::onClipRegion(region, op);
rileya@google.com9f5898d2012-09-11 20:21:44 +000083}
84
robertphillips@google.com8f90a892014-02-28 18:19:39 +000085void SkBBoxHierarchyRecord::onClipPath(const SkPath& path,
86 SkRegion::Op op,
87 ClipEdgeStyle edgeStyle) {
reed@google.com44699382013-10-31 17:28:30 +000088 fStateTree->appendClip(this->writeStream().bytesWritten());
robertphillips@google.com8f90a892014-02-28 18:19:39 +000089 this->INHERITED::onClipPath(path, op, edgeStyle);
rileya@google.com9f5898d2012-09-11 20:21:44 +000090}
91
robertphillips@google.com8f90a892014-02-28 18:19:39 +000092void SkBBoxHierarchyRecord::onClipRRect(const SkRRect& rrect,
93 SkRegion::Op op,
94 ClipEdgeStyle edgeStyle) {
reed@google.com44699382013-10-31 17:28:30 +000095 fStateTree->appendClip(this->writeStream().bytesWritten());
robertphillips@google.com8f90a892014-02-28 18:19:39 +000096 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
junov@chromium.org675de162012-12-18 19:37:49 +000097}
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000098
99bool SkBBoxHierarchyRecord::shouldRewind(void* data) {
100 // SkBBoxHierarchy::rewindInserts is called by SkPicture after the
101 // SkPicture has rewound its command stream. To match that rewind in the
102 // BBH, we rewind all draws that reference commands that were recorded
103 // past the point to which the SkPicture has rewound, which is given by
reed@google.com44699382013-10-31 17:28:30 +0000104 // writeStream().bytesWritten().
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000105 SkPictureStateTree::Draw* draw = static_cast<SkPictureStateTree::Draw*>(data);
reed@google.com44699382013-10-31 17:28:30 +0000106 return draw->fOffset >= writeStream().bytesWritten();
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000107}