Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
1 /*
2 * Copyright 2011 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 */
8 #ifndef SkPictureRecord_DEFINED
9 #define SkPictureRecord_DEFINED
11 #include "SkCanvas.h"
12 #include "SkFlattenable.h"
13 #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
14 #include "SkMatrixClipStateMgr.h"
15 #endif
16 #include "SkPathHeap.h"
17 #include "SkPicture.h"
18 #include "SkPictureFlat.h"
19 #include "SkTemplates.h"
20 #include "SkWriter32.h"
22 class SkBBoxHierarchy;
23 class SkOffsetTable;
24 class SkPictureStateTree;
26 // These macros help with packing and unpacking a single byte value and
27 // a 3 byte value into/out of a uint32_t
28 #define MASK_24 0x00FFFFFF
29 #define UNPACK_8_24(combined, small, large) \
30 small = (combined >> 24) & 0xFF; \
31 large = combined & MASK_24;
32 #define PACK_8_24(small, large) ((small << 24) | large)
35 class SkPictureRecord : public SkCanvas {
36 public:
37 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
38 virtual ~SkPictureRecord();
40 virtual void clear(SkColor) SK_OVERRIDE;
41 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
42 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
43 const SkPaint&) SK_OVERRIDE;
44 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
45 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
46 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
47 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
48 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
49 const SkPaint*) SK_OVERRIDE;
50 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
51 const SkRect& dst, const SkPaint* paint,
52 DrawBitmapRectFlags flags) SK_OVERRIDE;
53 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
54 const SkPaint*) SK_OVERRIDE;
55 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
56 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
57 virtual void drawSprite(const SkBitmap&, int left, int top,
58 const SkPaint*) SK_OVERRIDE;
59 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
60 SkScalar y, const SkPaint&) SK_OVERRIDE;
61 virtual void drawPosText(const void* text, size_t byteLength,
62 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
63 virtual void drawPosTextH(const void* text, size_t byteLength,
64 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
65 virtual void drawTextOnPath(const void* text, size_t byteLength,
66 const SkPath& path, const SkMatrix* matrix,
67 const SkPaint&) SK_OVERRIDE;
68 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
69 virtual void drawVertices(VertexMode, int vertexCount,
70 const SkPoint vertices[], const SkPoint texs[],
71 const SkColor colors[], SkXfermode*,
72 const uint16_t indices[], int indexCount,
73 const SkPaint&) SK_OVERRIDE;
74 virtual void drawData(const void*, size_t) SK_OVERRIDE;
75 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
76 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
77 virtual void endCommentGroup() SK_OVERRIDE;
78 virtual bool isDrawingToLayer() const SK_OVERRIDE;
80 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
81 SkScalar minY, SkScalar maxY);
83 const SkTDArray<SkPicture* >& getPictureRefs() const {
84 return fPictureRefs;
85 }
87 void setFlags(uint32_t recordFlags) {
88 fRecordFlags = recordFlags;
89 }
91 const SkWriter32& writeStream() const {
92 return fWriter;
93 }
95 void beginRecording();
96 void endRecording();
98 void internalOnly_EnableOpts(bool optsEnabled) {
99 fOptsEnabled = optsEnabled;
100 }
102 private:
103 void handleOptimization(int opt);
104 int recordRestoreOffsetPlaceholder(SkRegion::Op);
105 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
107 #ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
108 SkTDArray<int32_t> fRestoreOffsetStack;
109 int fFirstSavedLayerIndex;
110 enum {
111 kNoSavedLayerIndex = -1
112 };
113 #endif
115 SkTDArray<uint32_t> fCullOffsetStack;
117 /*
118 * Write the 'drawType' operation and chunk size to the skp. 'size'
119 * can potentially be increased if the chunk size needs its own storage
120 * location (i.e., it overflows 24 bits).
121 * Returns the start offset of the chunk. This is the location at which
122 * the opcode & size are stored.
123 * TODO: since we are handing the size into here we could call reserve
124 * and then return a pointer to the memory storage. This could decrease
125 * allocation overhead but could lead to more wasted space (the tail
126 * end of blocks could go unused). Possibly add a second addDraw that
127 * operates in this manner.
128 */
129 size_t addDraw(DrawType drawType, uint32_t* size) {
130 size_t offset = fWriter.bytesWritten();
132 this->predrawNotify();
134 #ifdef SK_DEBUG_TRACE
135 SkDebugf("add %s\n", DrawTypeToString(drawType));
136 #endif
138 SkASSERT(0 != *size);
139 SkASSERT(((uint8_t) drawType) == drawType);
141 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
142 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
143 *size += 1;
144 fWriter.writeInt(*size);
145 } else {
146 fWriter.writeInt(PACK_8_24(drawType, *size));
147 }
149 return offset;
150 }
152 void addInt(int value) {
153 fWriter.writeInt(value);
154 }
155 void addScalar(SkScalar scalar) {
156 fWriter.writeScalar(scalar);
157 }
159 // The command at 'offset' in the skp uses the specified bitmap
160 void trackBitmapUse(int bitmapID, size_t offset);
161 int addBitmap(const SkBitmap& bitmap);
162 void addMatrix(const SkMatrix& matrix);
163 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
164 const SkFlatData* addPaintPtr(const SkPaint* paint);
165 void addFlatPaint(const SkFlatData* flatPaint);
166 void addPath(const SkPath& path);
167 void addPicture(SkPicture& picture);
168 void addPoint(const SkPoint& point);
169 void addPoints(const SkPoint pts[], int count);
170 void addRect(const SkRect& rect);
171 void addRectPtr(const SkRect* rect);
172 void addIRect(const SkIRect& rect);
173 void addIRectPtr(const SkIRect* rect);
174 void addRRect(const SkRRect&);
175 void addRegion(const SkRegion& region);
176 void addText(const void* text, size_t byteLength);
178 int find(const SkBitmap& bitmap);
180 #ifdef SK_DEBUG_DUMP
181 public:
182 void dumpMatrices();
183 void dumpPaints();
184 #endif
186 #ifdef SK_DEBUG_SIZE
187 public:
188 size_t size() const;
189 int bitmaps(size_t* size) const;
190 int matrices(size_t* size) const;
191 int paints(size_t* size) const;
192 int paths(size_t* size) const;
193 int regions(size_t* size) const;
194 size_t streamlen() const;
196 size_t fPointBytes, fRectBytes, fTextBytes;
197 int fPointWrites, fRectWrites, fTextWrites;
198 #endif
200 #ifdef SK_DEBUG_VALIDATE
201 public:
202 void validate(size_t initialOffset, uint32_t size) const;
203 private:
204 void validateBitmaps() const;
205 void validateMatrices() const;
206 void validatePaints() const;
207 void validatePaths() const;
208 void validateRegions() const;
209 #else
210 public:
211 void validate(size_t initialOffset, uint32_t size) const {
212 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
213 }
214 #endif
216 protected:
217 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
218 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
219 return NULL;
220 }
222 virtual void willSave(SaveFlags) SK_OVERRIDE;
223 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
224 virtual void willRestore() SK_OVERRIDE;
226 virtual void didTranslate(SkScalar, SkScalar) SK_OVERRIDE;
227 virtual void didScale(SkScalar, SkScalar) SK_OVERRIDE;
228 virtual void didRotate(SkScalar) SK_OVERRIDE;
229 virtual void didSkew(SkScalar, SkScalar) SK_OVERRIDE;
230 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
231 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
233 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
234 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
235 virtual void onPopCull() SK_OVERRIDE;
237 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
238 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
239 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
240 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
242 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
243 // tweaked by paint.computeFastBounds().
244 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
246 // Make sure that flat has fTopBot written.
247 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
248 if (!flat.isTopBotWritten()) {
249 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
250 SkASSERT(flat.isTopBotWritten());
251 }
252 }
253 // Will return a cached version when possible.
254 const SkFlatData* getFlatPaintData(const SkPaint& paint);
255 /**
256 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
257 * then it calls this, using the extra parameter, to avoid duplication.
258 */
259 void drawPosTextHImpl(const void* text, size_t byteLength,
260 const SkScalar xpos[], SkScalar constY,
261 const SkPaint& paint, const SkFlatData* flatPaintData);
263 int addPathToHeap(const SkPath& path); // does not write to ops stream
265 // These entry points allow the writing of matrices, clips, saves &
266 // restores to be deferred (e.g., if the MC state is being collapsed and
267 // only written out as needed).
268 void recordConcat(const SkMatrix& matrix);
269 int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
270 int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
271 int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
272 int recordClipRegion(const SkRegion& region, SkRegion::Op op);
273 void recordSave(SaveFlags flags);
274 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
275 void recordRestore(bool fillInSkips = true);
277 // These are set to NULL in our constructor, but may be changed by
278 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
279 SkBBoxHierarchy* fBoundingHierarchy;
280 SkPictureStateTree* fStateTree;
282 // Allocated in the constructor and managed by this class.
283 SkBitmapHeap* fBitmapHeap;
285 private:
286 friend class MatrixClipState; // for access to *Impl methods
287 friend class SkMatrixClipStateMgr; // for access to *Impl methods
289 SkChunkFlatController fFlattenableHeap;
291 SkPaintDictionary fPaints;
293 SkPathHeap* fPathHeap; // reference counted
294 SkWriter32 fWriter;
296 // we ref each item in these arrays
297 SkTDArray<SkPicture*> fPictureRefs;
299 uint32_t fRecordFlags;
300 bool fOptsEnabled;
301 int fInitialSaveCount;
303 SkAutoTUnref<SkOffsetTable> fBitmapUseOffsets;
305 friend class SkPicturePlayback;
306 friend class SkPictureTester; // for unit testing
308 #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
309 SkMatrixClipStateMgr fMCMgr;
310 #endif
312 typedef SkCanvas INHERITED;
313 };
315 #endif