1 /*
2 * Copyright 2018 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 #include "src/core/SkAutoMalloc.h"
9 #include "src/core/SkCanvasPriv.h"
10 #include "src/core/SkReadBuffer.h"
11 #include "src/core/SkWriter32.h"
12
SkAutoCanvasMatrixPaint(SkCanvas * canvas,const SkMatrix * matrix,const SkPaint * paint,const SkRect & bounds)13 SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix,
14 const SkPaint* paint, const SkRect& bounds)
15 : fCanvas(canvas)
16 , fSaveCount(canvas->getSaveCount())
17 {
18 if (paint) {
19 SkRect newBounds = bounds;
20 if (matrix) {
21 matrix->mapRect(&newBounds);
22 }
23 canvas->saveLayer(&newBounds, paint);
24 } else if (matrix) {
25 canvas->save();
26 }
27
28 if (matrix) {
29 canvas->concat(*matrix);
30 }
31 }
32
~SkAutoCanvasMatrixPaint()33 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
34 fCanvas->restoreToCount(fSaveCount);
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////////////////////////
38
ReadLattice(SkReadBuffer & buffer,SkCanvas::Lattice * lattice)39 bool SkCanvasPriv::ReadLattice(SkReadBuffer& buffer, SkCanvas::Lattice* lattice) {
40 lattice->fXCount = buffer.readInt();
41 lattice->fXDivs = buffer.skipT<int32_t>(lattice->fXCount);
42 lattice->fYCount = buffer.readInt();
43 lattice->fYDivs = buffer.skipT<int32_t>(lattice->fYCount);
44 int flagCount = buffer.readInt();
45 lattice->fRectTypes = nullptr;
46 lattice->fColors = nullptr;
47 if (flagCount) {
48 lattice->fRectTypes = buffer.skipT<SkCanvas::Lattice::RectType>(flagCount);
49 lattice->fColors = buffer.skipT<SkColor>(flagCount);
50 }
51 lattice->fBounds = buffer.skipT<SkIRect>();
52 return buffer.isValid();
53 }
54
WriteLattice(void * buffer,const SkCanvas::Lattice & lattice)55 size_t SkCanvasPriv::WriteLattice(void* buffer, const SkCanvas::Lattice& lattice) {
56 int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
57
58 const size_t size = (1 + lattice.fXCount + 1 + lattice.fYCount + 1) * sizeof(int32_t) +
59 SkAlign4(flagCount * sizeof(SkCanvas::Lattice::RectType)) +
60 SkAlign4(flagCount * sizeof(SkColor)) +
61 sizeof(SkIRect);
62
63 if (buffer) {
64 SkWriter32 writer(buffer, size);
65 writer.write32(lattice.fXCount);
66 writer.write(lattice.fXDivs, lattice.fXCount * sizeof(uint32_t));
67 writer.write32(lattice.fYCount);
68 writer.write(lattice.fYDivs, lattice.fYCount * sizeof(uint32_t));
69 writer.write32(flagCount);
70 writer.writePad(lattice.fRectTypes, flagCount * sizeof(uint8_t));
71 writer.write(lattice.fColors, flagCount * sizeof(SkColor));
72 SkASSERT(lattice.fBounds);
73 writer.write(lattice.fBounds, sizeof(SkIRect));
74 SkASSERT(writer.bytesWritten() == size);
75 }
76 return size;
77 };
78
WriteLattice(SkWriteBuffer & buffer,const SkCanvas::Lattice & lattice)79 void SkCanvasPriv::WriteLattice(SkWriteBuffer& buffer, const SkCanvas::Lattice& lattice) {
80 const size_t size = WriteLattice(nullptr, lattice);
81 SkAutoSMalloc<1024> storage(size);
82 WriteLattice(storage.get(), lattice);
83 buffer.writePad32(storage.get(), size);
84 }
85
GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[],int count,int * totalDstClipCount,int * totalMatrixCount)86 void SkCanvasPriv::GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[], int count,
87 int* totalDstClipCount, int* totalMatrixCount) {
88 int dstClipCount = 0;
89 int maxMatrixIndex = -1;
90 for (int i = 0; i < count; ++i) {
91 dstClipCount += 4 * set[i].fHasClip;
92 if (set[i].fMatrixIndex > maxMatrixIndex) {
93 maxMatrixIndex = set[i].fMatrixIndex;
94 }
95 }
96
97 *totalDstClipCount = dstClipCount;
98 *totalMatrixCount = maxMatrixIndex + 1;
99 }
100