mtklein | 29b1afc | 2015-04-09 07:46:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 8 | #include "SkRecord.h" |
mtklein | c3c6194 | 2015-11-19 07:23:49 -0800 | [diff] [blame] | 9 | #include <algorithm> |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 10 | |
| 11 | SkRecord::~SkRecord() { |
| 12 | Destroyer destroyer; |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 13 | for (int i = 0; i < this->count(); i++) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 14 | this->mutate(i, destroyer); |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 15 | } |
| 16 | } |
| 17 | |
| 18 | void SkRecord::grow() { |
| 19 | SkASSERT(fCount == fReserved); |
mtklein | 29b1afc | 2015-04-09 07:46:41 -0700 | [diff] [blame] | 20 | SkASSERT(fReserved > 0); |
| 21 | fReserved *= 2; |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 22 | fRecords.realloc(fReserved); |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | size_t SkRecord::bytesUsed() const { |
mtklein | 1dda219 | 2015-04-13 12:17:02 -0700 | [diff] [blame] | 26 | size_t bytes = fAlloc.approxBytesAllocated() + sizeof(SkRecord); |
| 27 | // If fReserved <= kInlineRecords, we've already accounted for fRecords with sizeof(SkRecord). |
| 28 | // When we go over that limit, they're allocated on the heap (and the inline space is wasted). |
| 29 | if (fReserved > kInlineRecords) { |
| 30 | bytes += fReserved * sizeof(Record); |
| 31 | } |
| 32 | return bytes; |
mtklein | f98862c | 2014-11-24 14:45:47 -0800 | [diff] [blame] | 33 | } |
mtklein | c3c6194 | 2015-11-19 07:23:49 -0800 | [diff] [blame] | 34 | |
| 35 | void SkRecord::defrag() { |
| 36 | // Remove all the NoOps, preserving the order of other ops, e.g. |
| 37 | // Save, ClipRect, NoOp, DrawRect, NoOp, NoOp, Restore |
| 38 | // -> Save, ClipRect, DrawRect, Restore |
| 39 | Record* noops = std::remove_if(fRecords.get(), fRecords.get() + fCount, |
| 40 | [](Record op) { return op.type() == SkRecords::NoOp_Type; }); |
| 41 | fCount = noops - fRecords.get(); |
| 42 | } |