1c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org/* 2c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * Copyright 2014 Google Inc. 3c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * 4c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be 5c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org * found in the LICENSE file. 6c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org */ 7c4b21e6c03a6cdb03e116b9f510eb10cf8daedb1commit-bot@chromium.org 8e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org#ifndef SkRecord_DEFINED 9e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org#define SkRecord_DEFINED 10e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 114304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby#include "SkArenaAlloc.h" 12e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org#include "SkRecords.h" 1308bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org#include "SkTLogic.h" 14e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org#include "SkTemplates.h" 15e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 1629b1afc169576cf5e708e46b74313b5666e66249mtklein// SkRecord represents a sequence of SkCanvas calls, saved for future use. 17e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// These future uses may include: replay, optimization, serialization, or combinations of those. 18e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// 19e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// Though an enterprising user may find calling alloc(), append(), visit(), and mutate() enough to 20e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// work with SkRecord, you probably want to look at SkRecorder which presents an SkCanvas interface 21e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into another SkCanvas. 22e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// 23e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// SkRecord often looks like it's compatible with any type T, but really it's compatible with any 24e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// type T which has a static const SkRecords::Type kType. That is to say, SkRecord is compatible 25e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// only with SkRecords::* structs defined in SkRecords.h. Your compiler will helpfully yell if you 26e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org// get this wrong. 27e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 28cd25df9c560f51b39c993902d1bafe895c1ef217Mike Kleinclass SkRecord : public SkRefCnt { 29e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.orgpublic: 304304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby SkRecord() = default; 31f98862c39b7e79a35f1907cb94240f5d0545fe7bmtklein ~SkRecord(); 32506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 3388c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Returns the number of canvas commands in this SkRecord. 34c6ad06acefa096716f8dabed5342f9b89dc43dfemtklein int count() const { return fCount; } 35e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 3688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Visit the i-th canvas command with a functor matching this interface: 37e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // template <typename T> 38c71da1f6ed3a5e1a3eb2dd860b8129e1b423f9f4commit-bot@chromium.org // R operator()(const T& record) { ... } 3988c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // This operator() must be defined for at least all SkRecords::*. 40343a63d082bda969d7e8a4e09ba850e931185269mtklein template <typename F> 41343a63d082bda969d7e8a4e09ba850e931185269mtklein auto visit(int i, F&& f) const -> decltype(f(SkRecords::NoOp())) { 42343a63d082bda969d7e8a4e09ba850e931185269mtklein return fRecords[i].visit(f); 43506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org } 44506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org 4588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Mutate the i-th canvas command with a functor matching this interface: 46e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // template <typename T> 47c71da1f6ed3a5e1a3eb2dd860b8129e1b423f9f4commit-bot@chromium.org // R operator()(T* record) { ... } 4888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // This operator() must be defined for at least all SkRecords::*. 49343a63d082bda969d7e8a4e09ba850e931185269mtklein template <typename F> 50343a63d082bda969d7e8a4e09ba850e931185269mtklein auto mutate(int i, F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) { 51343a63d082bda969d7e8a4e09ba850e931185269mtklein return fRecords[i].mutate(f); 52506db0b7d2905c6bedba9fc5d4aeaf231a9a34eacommit-bot@chromium.org } 5329b1afc169576cf5e708e46b74313b5666e66249mtklein 5488c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Allocate contiguous space for count Ts, to be freed when the SkRecord is destroyed. 5588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Here T can be any class, not just those from SkRecords. Throws on failure. 56e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org template <typename T> 572347b624678fedf1d2f7ab1d79b9ad70087c3392reed T* alloc(size_t count = 1) { 584304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby struct RawBytes { 594304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby alignas(T) char data[sizeof(T)]; 604304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby }; 614304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby fApproxBytesAllocated += count * sizeof(T) + alignof(T); 624304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby return (T*)fAlloc.makeArrayDefault<RawBytes>(count); 63e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 64e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 6588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Add a new command of type T to the end of this SkRecord. 6688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // You are expected to placement new an object of type T onto this pointer. 67e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org template <typename T> 68e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org T* append() { 69e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org if (fCount == fReserved) { 70f98862c39b7e79a35f1907cb94240f5d0545fe7bmtklein this->grow(); 71e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 7208bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org return fRecords[fCount++].set(this->allocCommand<T>()); 73e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 74e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 7588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Replace the i-th command with a new command of type T. 7688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // You are expected to placement new an object of type T onto this pointer. 77f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org // References to the original command are invalidated. 7888c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org template <typename T> 79c6ad06acefa096716f8dabed5342f9b89dc43dfemtklein T* replace(int i) { 8088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org SkASSERT(i < this->count()); 81f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org 82f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org Destroyer destroyer; 83343a63d082bda969d7e8a4e09ba850e931185269mtklein this->mutate(i, destroyer); 84f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org 8508bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org return fRecords[i].set(this->allocCommand<T>()); 8688c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org } 8788c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 88f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org // Replace the i-th command with a new command of type T. 89f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org // You are expected to placement new an object of type T onto this pointer. 90f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org // You must show proof that you've already adopted the existing command. 91f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org template <typename T, typename Existing> 92c6ad06acefa096716f8dabed5342f9b89dc43dfemtklein T* replace(int i, const SkRecords::Adopted<Existing>& proofOfAdoption) { 93f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org SkASSERT(i < this->count()); 94f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org 9529b1afc169576cf5e708e46b74313b5666e66249mtklein SkASSERT(Existing::kType == fRecords[i].type()); 9629b1afc169576cf5e708e46b74313b5666e66249mtklein SkASSERT(proofOfAdoption == fRecords[i].ptr()); 97f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org 9808bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org return fRecords[i].set(this->allocCommand<T>()); 99f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org } 10088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org 101158fcaa031d105dc999d9813fee8927db56a871ctomhudson // Does not return the bytes in any pointers embedded in the Records; callers 102158fcaa031d105dc999d9813fee8927db56a871ctomhudson // need to iterate with a visitor to measure those they care for. 103f98862c39b7e79a35f1907cb94240f5d0545fe7bmtklein size_t bytesUsed() const; 104158fcaa031d105dc999d9813fee8927db56a871ctomhudson 105c3c6194ba2b90fde57d8d0bc1d6302656f0dae27mtklein // Rearrange and resize this record to eliminate any NoOps. 106c3c6194ba2b90fde57d8d0bc1d6302656f0dae27mtklein // May change count() and the indices of ops, but preserves their order. 107c3c6194ba2b90fde57d8d0bc1d6302656f0dae27mtklein void defrag(); 108c3c6194ba2b90fde57d8d0bc1d6302656f0dae27mtklein 109e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.orgprivate: 11029b1afc169576cf5e708e46b74313b5666e66249mtklein // An SkRecord is structured as an array of pointers into a big chunk of memory where 111e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // records representing each canvas draw call are stored: 112e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // 113e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // fRecords: [*][*][*]... 114e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // | | | 115e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // | | | 116e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // | | +---------------------------------------+ 117e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // | +-----------------+ | 118e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // | | | 119e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // v v v 120e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // fAlloc: [SkRecords::DrawRect][SkRecords::DrawPosTextH][SkRecords::DrawRect]... 121e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // 12229b1afc169576cf5e708e46b74313b5666e66249mtklein // We store the types of each of the pointers alongside the pointer. 12329b1afc169576cf5e708e46b74313b5666e66249mtklein // The cost to append a T to this structure is 8 + sizeof(T) bytes. 124e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 125f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org // A mutator that can be used with replace to destroy canvas commands. 126f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org struct Destroyer { 127f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org template <typename T> 128f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org void operator()(T* record) { record->~T(); } 129f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org }; 130e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 13108bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org template <typename T> 1320e101667d604e4902eb24e0c0939d4f5b12c96ecbungeman SK_WHEN(std::is_empty<T>::value, T*) allocCommand() { 13350ca12be56f5c3803b60d6a6a2eafed45316bf09commit-bot@chromium.org static T singleton = {}; 13408bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org return &singleton; 13508bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org } 13608bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org 13708bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org template <typename T> 1380e101667d604e4902eb24e0c0939d4f5b12c96ecbungeman SK_WHEN(!std::is_empty<T>::value, T*) allocCommand() { return this->alloc<T>(); } 13908bf86c56495b6779001b5756839fc2c73decba3commit-bot@chromium.org 140f98862c39b7e79a35f1907cb94240f5d0545fe7bmtklein void grow(); 141f98862c39b7e79a35f1907cb94240f5d0545fe7bmtklein 14229b1afc169576cf5e708e46b74313b5666e66249mtklein // A typed pointer to some bytes in fAlloc. visit() and mutate() allow polymorphic dispatch. 143e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org struct Record { 14429b1afc169576cf5e708e46b74313b5666e66249mtklein // On 32-bit machines we store type in 4 bytes, followed by a pointer. Simple. 14529b1afc169576cf5e708e46b74313b5666e66249mtklein // On 64-bit machines we store a pointer with the type slotted into two top (unused) bytes. 14629b1afc169576cf5e708e46b74313b5666e66249mtklein // FWIW, SkRecords::Type is tiny. It can easily fit in one byte. 14729b1afc169576cf5e708e46b74313b5666e66249mtklein uint64_t fTypeAndPtr; 14829b1afc169576cf5e708e46b74313b5666e66249mtklein static const int kTypeShift = sizeof(void*) == 4 ? 32 : 48; 14929b1afc169576cf5e708e46b74313b5666e66249mtklein 15088c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org // Point this record to its data in fAlloc. Returns ptr for convenience. 151e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org template <typename T> 15288c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org T* set(T* ptr) { 15329b1afc169576cf5e708e46b74313b5666e66249mtklein fTypeAndPtr = ((uint64_t)T::kType) << kTypeShift | (uintptr_t)ptr; 15429b1afc169576cf5e708e46b74313b5666e66249mtklein SkASSERT(this->ptr() == ptr && this->type() == T::kType); 15588c3e279ab79125e5741b0b0b3175291e2e2bbeecommit-bot@chromium.org return ptr; 156e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 157e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 15829b1afc169576cf5e708e46b74313b5666e66249mtklein SkRecords::Type type() const { return (SkRecords::Type)(fTypeAndPtr >> kTypeShift); } 15929b1afc169576cf5e708e46b74313b5666e66249mtklein void* ptr() const { return (void*)(fTypeAndPtr & ((1ull<<kTypeShift)-1)); } 160f0ae5e471b759f18b614a8e0928c9151947de04ccommit-bot@chromium.org 16129b1afc169576cf5e708e46b74313b5666e66249mtklein // Visit this record with functor F (see public API above). 162343a63d082bda969d7e8a4e09ba850e931185269mtklein template <typename F> 163343a63d082bda969d7e8a4e09ba850e931185269mtklein auto visit(F&& f) const -> decltype(f(SkRecords::NoOp())) { 16429b1afc169576cf5e708e46b74313b5666e66249mtklein #define CASE(T) case SkRecords::T##_Type: return f(*(const SkRecords::T*)this->ptr()); 16529b1afc169576cf5e708e46b74313b5666e66249mtklein switch(this->type()) { SK_RECORD_TYPES(CASE) } 166e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org #undef CASE 167c71da1f6ed3a5e1a3eb2dd860b8129e1b423f9f4commit-bot@chromium.org SkDEBUGFAIL("Unreachable"); 1689f183505acc94a587e71b485761f47781d6f5030Mike Klein static const SkRecords::NoOp noop{}; 1699f183505acc94a587e71b485761f47781d6f5030Mike Klein return f(noop); 170e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 171e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 17229b1afc169576cf5e708e46b74313b5666e66249mtklein // Mutate this record with functor F (see public API above). 173343a63d082bda969d7e8a4e09ba850e931185269mtklein template <typename F> 174343a63d082bda969d7e8a4e09ba850e931185269mtklein auto mutate(F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) { 17529b1afc169576cf5e708e46b74313b5666e66249mtklein #define CASE(T) case SkRecords::T##_Type: return f((SkRecords::T*)this->ptr()); 17629b1afc169576cf5e708e46b74313b5666e66249mtklein switch(this->type()) { SK_RECORD_TYPES(CASE) } 177e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org #undef CASE 178c71da1f6ed3a5e1a3eb2dd860b8129e1b423f9f4commit-bot@chromium.org SkDEBUGFAIL("Unreachable"); 1799f183505acc94a587e71b485761f47781d6f5030Mike Klein static const SkRecords::NoOp noop{}; 1809f183505acc94a587e71b485761f47781d6f5030Mike Klein return f(const_cast<SkRecords::NoOp*>(&noop)); 181e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org } 18235f55764b81390a085fb90f624082c196fbd6229mtklein }; 183e2dd9408cd711777afaa9410427fb0d761ab004amtklein 18429b1afc169576cf5e708e46b74313b5666e66249mtklein // fRecords needs to be a data structure that can append fixed length data, and need to 18529b1afc169576cf5e708e46b74313b5666e66249mtklein // support efficient random access and forward iteration. (It doesn't need to be contiguous.) 1864304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby int fCount{0}, 1874304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby fReserved{0}; 1880fa156fcfba3b430801b5448ddfc254732bf7386Mike Klein SkAutoTMalloc<Record> fRecords; 18929b1afc169576cf5e708e46b74313b5666e66249mtklein 190e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // fAlloc needs to be a data structure which can append variable length data in contiguous 191e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org // chunks, returning a stable handle to that data for later retrieval. 1924304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby SkArenaAlloc fAlloc{256}; 1934304d11ada22ebfb6b64d87247ad24cde89c3a74Herb Derby size_t fApproxBytesAllocated{0}; 194e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org}; 195e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org 196e3ff558a4baf4cb924e7513a81c8073ddae385fccommit-bot@chromium.org#endif//SkRecord_DEFINED 197