michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkDrawLooper.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkMatrix.h" michael@0: #include "SkPaint.h" michael@0: #include "SkRect.h" michael@0: #include "SkSmallAllocator.h" michael@0: michael@0: bool SkDrawLooper::canComputeFastBounds(const SkPaint& paint) const { michael@0: SkCanvas canvas; michael@0: SkSmallAllocator<1, 32> allocator; michael@0: void* buffer = allocator.reserveT(this->contextSize()); michael@0: michael@0: SkDrawLooper::Context* context = this->createContext(&canvas, buffer); michael@0: for (;;) { michael@0: SkPaint p(paint); michael@0: if (context->next(&canvas, &p)) { michael@0: p.setLooper(NULL); michael@0: if (!p.canComputeFastBounds()) { michael@0: return false; michael@0: } michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void SkDrawLooper::computeFastBounds(const SkPaint& paint, const SkRect& src, michael@0: SkRect* dst) const { michael@0: SkCanvas canvas; michael@0: SkSmallAllocator<1, 32> allocator; michael@0: void* buffer = allocator.reserveT(this->contextSize()); michael@0: michael@0: *dst = src; // catch case where there are no loops michael@0: SkDrawLooper::Context* context = this->createContext(&canvas, buffer); michael@0: for (bool firstTime = true;; firstTime = false) { michael@0: SkPaint p(paint); michael@0: if (context->next(&canvas, &p)) { michael@0: SkRect r(src); michael@0: michael@0: p.setLooper(NULL); michael@0: p.computeFastBounds(r, &r); michael@0: canvas.getTotalMatrix().mapRect(&r); michael@0: michael@0: if (firstTime) { michael@0: *dst = r; michael@0: } else { michael@0: dst->join(r); michael@0: } michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: }