SkValidatingReadBuffer.h revision ba6e954140e45e251d67934ed6ad752149fcf72f
1/*
2 * Copyright 2013 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#ifndef SkValidatingReadBuffer_DEFINED
9#define SkValidatingReadBuffer_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkBitmapHeap.h"
13#include "SkFlattenableBuffers.h"
14#include "SkPath.h"
15#include "SkPicture.h"
16#include "SkReader32.h"
17
18class SkBitmap;
19
20#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_MAC)
21    #define DEBUG_NON_DETERMINISTIC_ASSERT
22#endif
23
24class SkValidatingReadBuffer : public SkFlattenableReadBuffer {
25public:
26    SkValidatingReadBuffer();
27    SkValidatingReadBuffer(const void* data, size_t size);
28    SkValidatingReadBuffer(SkStream* stream);
29    virtual ~SkValidatingReadBuffer();
30
31    SkReader32* getReader32() { return &fReader; }
32
33    uint32_t size() { return fReader.size(); }
34    uint32_t offset() { return fReader.offset(); }
35    bool eof() { return fReader.eof(); }
36    const void* skip(size_t size);
37
38    // primitives
39    virtual bool readBool() SK_OVERRIDE;
40    virtual SkColor readColor() SK_OVERRIDE;
41    virtual SkFixed readFixed() SK_OVERRIDE;
42    virtual int32_t readInt() SK_OVERRIDE;
43    virtual SkScalar readScalar() SK_OVERRIDE;
44    virtual uint32_t readUInt() SK_OVERRIDE;
45    virtual int32_t read32() SK_OVERRIDE;
46
47    // strings -- the caller is responsible for freeing the string contents
48    virtual void readString(SkString* string) SK_OVERRIDE;
49    virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) SK_OVERRIDE;
50
51    // common data structures
52    virtual SkFlattenable* readFlattenable() SK_OVERRIDE;
53    virtual void readPoint(SkPoint* point) SK_OVERRIDE;
54    virtual void readMatrix(SkMatrix* matrix) SK_OVERRIDE;
55    virtual void readIRect(SkIRect* rect) SK_OVERRIDE;
56    virtual void readRect(SkRect* rect) SK_OVERRIDE;
57    virtual void readRegion(SkRegion* region) SK_OVERRIDE;
58    virtual void readPath(SkPath* path) SK_OVERRIDE;
59
60    // binary data and arrays
61    virtual uint32_t readByteArray(void* value) SK_OVERRIDE;
62    virtual uint32_t readColorArray(SkColor* colors) SK_OVERRIDE;
63    virtual uint32_t readIntArray(int32_t* values) SK_OVERRIDE;
64    virtual uint32_t readPointArray(SkPoint* points) SK_OVERRIDE;
65    virtual uint32_t readScalarArray(SkScalar* values) SK_OVERRIDE;
66
67    // helpers to get info about arrays and binary data
68    virtual uint32_t getArrayCount() SK_OVERRIDE;
69
70    virtual void readBitmap(SkBitmap* bitmap) SK_OVERRIDE;
71    virtual SkTypeface* readTypeface() SK_OVERRIDE;
72
73    void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) {
74        SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage);
75    }
76
77    void setTypefaceArray(SkTypeface* array[], int count) {
78        fTFArray = array;
79        fTFCount = count;
80    }
81
82    /**
83     *  Call this with a pre-loaded array of Factories, in the same order as
84     *  were created/written by the writer. SkPicture uses this.
85     */
86    void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
87        fFactoryTDArray = NULL;
88        fFactoryArray = array;
89        fFactoryCount = count;
90    }
91
92    /**
93     *  Call this with an initially empty array, so the reader can cache each
94     *  factory it sees by name. Used by the pipe code in conjunction with
95     *  SkOrderedWriteBuffer::setNamedFactoryRecorder.
96     */
97    void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) {
98        fFactoryTDArray = array;
99        fFactoryArray = NULL;
100        fFactoryCount = 0;
101    }
102
103    /**
104     *  Provide a function to decode an SkBitmap from encoded data. Only used if the writer
105     *  encoded the SkBitmap. If the proper decoder cannot be used, a red bitmap with the
106     *  appropriate size will be used.
107     */
108    void setBitmapDecoder(SkPicture::InstallPixelRefProc bitmapDecoder) {
109        fBitmapDecoder = bitmapDecoder;
110    }
111
112private:
113    void setMemory(const void* data, size_t size);
114
115    static bool ptr_align_4(const void* ptr) {
116        return (((const char*)ptr - (const char*)NULL) & 3) == 0;
117    }
118
119    SkReader32 fReader;
120    void* fMemoryPtr;
121
122    SkBitmapHeapReader* fBitmapStorage;
123    SkTypeface** fTFArray;
124    int        fTFCount;
125
126    SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
127    SkFlattenable::Factory* fFactoryArray;
128    int                     fFactoryCount;
129
130    SkPicture::InstallPixelRefProc fBitmapDecoder;
131
132#ifdef DEBUG_NON_DETERMINISTIC_ASSERT
133    // Debugging counter to keep track of how many bitmaps we
134    // have decoded.
135    int fDecodedBitmapIndex;
136#endif // DEBUG_NON_DETERMINISTIC_ASSERT
137
138    typedef SkFlattenableReadBuffer INHERITED;
139};
140
141#endif // SkValidatingReadBuffer_DEFINED
142