• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkCanvasPriv.h"
9 
10 #include "src/core/SkAutoMalloc.h"
11 #include "src/core/SkDevice.h"
12 #include "src/core/SkReadBuffer.h"
13 #include "src/core/SkWriter32.h"
14 
15 #include <locale>
16 
SkAutoCanvasMatrixPaint(SkCanvas * canvas,const SkMatrix * matrix,const SkPaint * paint,const SkRect & bounds)17 SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix,
18                                                  const SkPaint* paint, const SkRect& bounds)
19         : fCanvas(canvas)
20         , fSaveCount(canvas->getSaveCount()) {
21     if (paint) {
22         SkRect newBounds = bounds;
23         if (matrix) {
24             matrix->mapRect(&newBounds);
25         }
26         canvas->saveLayer(&newBounds, paint);
27     } else if (matrix) {
28         canvas->save();
29     }
30 
31     if (matrix) {
32         canvas->concat(*matrix);
33     }
34 }
35 
~SkAutoCanvasMatrixPaint()36 SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() {
37     fCanvas->restoreToCount(fSaveCount);
38 }
39 
40 ///////////////////////////////////////////////////////////////////////////////////////////////////
41 
ReadLattice(SkReadBuffer & buffer,SkCanvas::Lattice * lattice)42 bool SkCanvasPriv::ReadLattice(SkReadBuffer& buffer, SkCanvas::Lattice* lattice) {
43     lattice->fXCount = buffer.readInt();
44     lattice->fXDivs = buffer.skipT<int32_t>(lattice->fXCount);
45     lattice->fYCount = buffer.readInt();
46     lattice->fYDivs = buffer.skipT<int32_t>(lattice->fYCount);
47     int flagCount = buffer.readInt();
48     lattice->fRectTypes = nullptr;
49     lattice->fColors = nullptr;
50     if (flagCount) {
51         lattice->fRectTypes = buffer.skipT<SkCanvas::Lattice::RectType>(flagCount);
52         lattice->fColors = buffer.skipT<SkColor>(flagCount);
53     }
54     lattice->fBounds = buffer.skipT<SkIRect>();
55     return buffer.isValid();
56 }
57 
WriteLattice(void * buffer,const SkCanvas::Lattice & lattice)58 size_t SkCanvasPriv::WriteLattice(void* buffer, const SkCanvas::Lattice& lattice) {
59     int flagCount = lattice.fRectTypes ? (lattice.fXCount + 1) * (lattice.fYCount + 1) : 0;
60 
61     const size_t size = (1 + lattice.fXCount + 1 + lattice.fYCount + 1) * sizeof(int32_t) +
62                         SkAlign4(flagCount * sizeof(SkCanvas::Lattice::RectType)) +
63                         SkAlign4(flagCount * sizeof(SkColor)) +
64                         sizeof(SkIRect);
65 
66     if (buffer) {
67         SkWriter32 writer(buffer, size);
68         writer.write32(lattice.fXCount);
69         writer.write(lattice.fXDivs, lattice.fXCount * sizeof(uint32_t));
70         writer.write32(lattice.fYCount);
71         writer.write(lattice.fYDivs, lattice.fYCount * sizeof(uint32_t));
72         writer.write32(flagCount);
73         writer.writePad(lattice.fRectTypes, flagCount * sizeof(uint8_t));
74         writer.write(lattice.fColors, flagCount * sizeof(SkColor));
75         SkASSERT(lattice.fBounds);
76         writer.write(lattice.fBounds, sizeof(SkIRect));
77         SkASSERT(writer.bytesWritten() == size);
78     }
79     return size;
80 };
81 
WriteLattice(SkWriteBuffer & buffer,const SkCanvas::Lattice & lattice)82 void SkCanvasPriv::WriteLattice(SkWriteBuffer& buffer, const SkCanvas::Lattice& lattice) {
83     const size_t size = WriteLattice(nullptr, lattice);
84     SkAutoSMalloc<1024> storage(size);
85     WriteLattice(storage.get(), lattice);
86     buffer.writePad32(storage.get(), size);
87 }
88 
GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[],int count,int * totalDstClipCount,int * totalMatrixCount)89 void SkCanvasPriv::GetDstClipAndMatrixCounts(const SkCanvas::ImageSetEntry set[], int count,
90                                              int* totalDstClipCount, int* totalMatrixCount) {
91     int dstClipCount = 0;
92     int maxMatrixIndex = -1;
93     for (int i = 0; i < count; ++i) {
94         dstClipCount += 4 * set[i].fHasClip;
95         if (set[i].fMatrixIndex > maxMatrixIndex) {
96             maxMatrixIndex = set[i].fMatrixIndex;
97         }
98     }
99 
100     *totalDstClipCount = dstClipCount;
101     *totalMatrixCount = maxMatrixIndex + 1;
102 }
103 
ValidateMarker(const char * name)104 bool SkCanvasPriv::ValidateMarker(const char* name) {
105     if (!name) {
106         return false;
107     }
108 
109     std::locale loc(std::locale::classic());
110     if (!std::isalpha(*name, loc)) {
111         return false;
112     }
113     while (*(++name)) {
114         if (!std::isalnum(*name, loc) && *name != '_') {
115             return false;
116         }
117     }
118     return true;
119 }
120 
121 #if GR_TEST_UTILS
122 
123 #if SK_SUPPORT_GPU
124 #include "src/gpu/BaseDevice.h"
125 
126 #if SK_GPU_V1
TopDeviceSurfaceDrawContext(SkCanvas * canvas)127 skgpu::v1::SurfaceDrawContext* SkCanvasPriv::TopDeviceSurfaceDrawContext(SkCanvas* canvas) {
128     if (auto gpuDevice = canvas->topDevice()->asGpuDevice()) {
129         return gpuDevice->surfaceDrawContext();
130     }
131 
132     return nullptr;
133 }
134 #endif // SK_GPU_V1
135 
TopDeviceSurfaceFillContext(SkCanvas * canvas)136 skgpu::SurfaceFillContext* SkCanvasPriv::TopDeviceSurfaceFillContext(SkCanvas* canvas) {
137     if (auto gpuDevice = canvas->topDevice()->asGpuDevice()) {
138         return gpuDevice->surfaceFillContext();
139     }
140 
141     return nullptr;
142 }
143 
144 #else // SK_SUPPORT_GPU
145 
146 #if SK_GPU_V1
TopDeviceSurfaceDrawContext(SkCanvas * canvas)147 skgpu::v1::SurfaceDrawContext* SkCanvasPriv::TopDeviceSurfaceDrawContext(SkCanvas* canvas) {
148     return nullptr;
149 }
150 #endif // SK_GPU_V1
151 
TopDeviceSurfaceFillContext(SkCanvas * canvas)152 skgpu::SurfaceFillContext* SkCanvasPriv::TopDeviceSurfaceFillContext(SkCanvas* canvas) {
153     return nullptr;
154 }
155 
156 #endif // SK_SUPPORT_GPU
157 
158 #endif // GR_TEST_UTILS
159 
160 #if SK_SUPPORT_GPU
161 #include "src/gpu/BaseDevice.h"
162 
TopDeviceTargetProxy(SkCanvas * canvas)163 GrRenderTargetProxy* SkCanvasPriv::TopDeviceTargetProxy(SkCanvas* canvas) {
164     if (auto gpuDevice = canvas->topDevice()->asGpuDevice()) {
165         return gpuDevice->targetProxy();
166     }
167 
168     return nullptr;
169 }
170 
171 #else // SK_SUPPORT_GPU
172 
TopDeviceTargetProxy(SkCanvas * canvas)173 GrRenderTargetProxy* SkCanvasPriv::TopDeviceTargetProxy(SkCanvas* canvas) {
174     return nullptr;
175 }
176 
177 #endif // SK_SUPPORT_GPU
178