Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkCanvasStateUtils.cpp
Go to the documentation of this file.
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
9
18#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
21#include "src/core/SkDevice.h"
22#include "src/core/SkWriter32.h"
24
25#include <cstddef>
26#include <cstdint>
27#include <utility>
28
29/*
30 * WARNING: The structs below are part of a stable ABI and as such we explicitly
31 * use unambigious primitives (e.g. int32_t instead of an enum).
32 *
33 * ANY CHANGES TO THE STRUCTS BELOW THAT IMPACT THE ABI SHOULD RESULT IN A NEW
34 * NEW SUBCLASS OF SkCanvasState. SUCH CHANGES SHOULD ONLY BE MADE IF ABSOLUTELY
35 * NECESSARY!
36 *
37 * In order to test changes, run the CanvasState tests. gyp/canvas_state_lib.gyp
38 * describes how to create a library to pass to the CanvasState tests. The tests
39 * should succeed when building the library with your changes and passing that to
40 * the tests running in the unchanged Skia.
41 */
47typedef int32_t RasterConfig;
48
55typedef int32_t CanvasBackend;
56
57struct ClipRect {
58 int32_t left, top, right, bottom;
59};
60
61struct SkMCState {
62 float matrix[9];
63 // NOTE: this only works for non-antialiased clips
66};
67
68// NOTE: If you add more members, create a new subclass of SkCanvasState with a
69// new CanvasState::version.
72 int32_t x, y;
73 int32_t width;
74 int32_t height;
75
77
78 union {
79 struct {
80 RasterConfig config; // pixel format: a value from RasterConfigs.
81 uint64_t rowBytes; // Number of bytes from start of one line to next.
82 void* pixels; // The pixels, all (height * rowBytes) of them.
84 struct {
85 int32_t textureID;
86 } gpu;
87 };
88};
89
91public:
92 SkCanvasState(int32_t version, SkCanvas* canvas) {
93 SkASSERT(canvas);
94 this->version = version;
95 width = canvas->getBaseLayerSize().width();
96 height = canvas->getBaseLayerSize().height();
97
98 }
99
100 /**
101 * The version this struct was built with. This field must always appear
102 * first in the struct so that when the versions don't match (and the
103 * remaining contents and size are potentially different) we can still
104 * compare the version numbers.
105 */
106 int32_t version;
107 int32_t width;
108 int32_t height;
110};
111
113public:
114 static const int32_t kVersion = 1;
115
117 layerCount = 0;
118 layers = nullptr;
120 mcState.clipRects = nullptr;
121 originalCanvas = canvas;
122 }
123
125 // loop through the layers and free the data allocated to the clipRects.
126 // See setup_MC_state, clipRects is only allocated when the clip isn't empty; and empty
127 // is implicitly represented as clipRectCount == 0.
128 for (int i = 0; i < layerCount; ++i) {
129 if (layers[i].mcState.clipRectCount > 0) {
131 }
132 }
133
134 if (mcState.clipRectCount > 0) {
136 }
137
138 // layers is always allocated, even if it's with sk_malloc(0), so this is safe.
140 }
141
143
144 int32_t layerCount;
146private:
147 SkCanvas* originalCanvas;
148 using INHERITED = SkCanvasState;
149};
150
151////////////////////////////////////////////////////////////////////////////////
152
153static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkIRect& clip) {
154 // initialize the struct
155 state->clipRectCount = 0;
156
157 // capture the matrix
158 for (int i = 0; i < 9; i++) {
159 state->matrix[i] = matrix.get(i);
160 }
161
162 /*
163 * We only support a single clipRect, so we take the clip's bounds. Clients have long made
164 * this assumption anyway, so this restriction is fine.
165 */
166 SkSWriter32<sizeof(ClipRect)> clipWriter;
167
168 if (!clip.isEmpty()) {
169 state->clipRectCount = 1;
170 state->clipRects = (ClipRect*)sk_malloc_throw(sizeof(ClipRect));
171 state->clipRects->left = clip.fLeft;
172 state->clipRects->top = clip.fTop;
173 state->clipRects->right = clip.fRight;
174 state->clipRects->bottom = clip.fBottom;
175 }
176}
177
179 SkASSERT(canvas);
180
181 // Check the clip can be decomposed into rectangles (i.e. no soft clips).
182 if (canvas->androidFramework_isClipAA()) {
183 return nullptr;
184 }
185
186 std::unique_ptr<SkCanvasState_v1> canvasState(new SkCanvasState_v1(canvas));
187
188 setup_MC_state(&canvasState->mcState, canvas->getTotalMatrix(), canvas->getDeviceClipBounds());
189
190 // Historically, the canvas state could report multiple top-level layers because SkCanvas
191 // supported unclipped layers. With that feature removed, all required information is contained
192 // by the canvas' top-most device.
193 SkDevice* device = canvas->topDevice();
195
196 SkSWriter32<sizeof(SkCanvasLayerState)> layerWriter;
197 // we currently only work for bitmap backed devices
198 SkPixmap pmap;
199 if (!device->accessPixels(&pmap) || 0 == pmap.width() || 0 == pmap.height()) {
200 return nullptr;
201 }
202 // and for axis-aligned devices (so not transformed for an image filter)
203 if (!device->isPixelAlignedToGlobal()) {
204 return nullptr;
205 }
206
207 SkIPoint origin = device->getOrigin(); // safe since it's pixel aligned
208
209 SkCanvasLayerState* layerState =
210 (SkCanvasLayerState*) layerWriter.reserve(sizeof(SkCanvasLayerState));
211 layerState->type = kRaster_CanvasBackend;
212 layerState->x = origin.x();
213 layerState->y = origin.y();
214 layerState->width = pmap.width();
215 layerState->height = pmap.height();
216
217 switch (pmap.colorType()) {
218 case kN32_SkColorType:
220 break;
222 layerState->raster.config = kRGB_565_RasterConfig;
223 break;
224 default:
225 return nullptr;
226 }
227 layerState->raster.rowBytes = pmap.rowBytes();
228 layerState->raster.pixels = pmap.writable_addr();
229
230 setup_MC_state(&layerState->mcState, device->localToDevice(), device->devClipBounds());
231
232 // allocate memory for the layers and then and copy them to the struct
233 SkASSERT(layerWriter.bytesWritten() == sizeof(SkCanvasLayerState));
234 canvasState->layerCount = 1;
235 canvasState->layers = (SkCanvasLayerState*) sk_malloc_throw(layerWriter.bytesWritten());
236 layerWriter.flatten(canvasState->layers);
237
238 return canvasState.release();
239}
240
241////////////////////////////////////////////////////////////////////////////////
242
244 // reconstruct the matrix
245 SkMatrix matrix;
246 for (int i = 0; i < 9; i++) {
247 matrix.set(i, state.matrix[i]);
248 }
249
250 // only realy support 1 rect, so if the caller (legacy?) sent us more, we just take the bounds
251 // of what they sent.
252 SkIRect bounds = SkIRect::MakeEmpty();
253 if (state.clipRectCount > 0) {
254 bounds.setLTRB(state.clipRects[0].left,
255 state.clipRects[0].top,
256 state.clipRects[0].right,
257 state.clipRects[0].bottom);
258 for (int i = 1; i < state.clipRectCount; ++i) {
259 bounds.join({state.clipRects[i].left,
260 state.clipRects[i].top,
261 state.clipRects[i].right,
262 state.clipRects[i].bottom});
263 }
264 }
265
266 canvas->clipRect(SkRect::Make(bounds));
267 canvas->concat(matrix);
268}
269
270static std::unique_ptr<SkCanvas>
272 SkASSERT(kRaster_CanvasBackend == layerState.type);
273
276 layerState.raster.config == kARGB_8888_RasterConfig ? kN32_SkColorType :
279
281 return nullptr;
282 }
283
284 bitmap.installPixels(SkImageInfo::Make(layerState.width, layerState.height,
286 layerState.raster.pixels, (size_t) layerState.raster.rowBytes);
287
288 SkASSERT(!bitmap.empty());
289 SkASSERT(!bitmap.isNull());
290
291 std::unique_ptr<SkCanvas> canvas(new SkCanvas(bitmap));
292
293 // setup the matrix and clip
294 setup_canvas_from_MC_state(layerState.mcState, canvas.get());
295
296 return canvas;
297}
298
301 // Currently there is only one possible version.
303
304 const SkCanvasState_v1* state_v1 = static_cast<const SkCanvasState_v1*>(state);
305
306 if (state_v1->layerCount < 1) {
307 return nullptr;
308 }
309
310 std::unique_ptr<SkCanvasStack> canvas(new SkCanvasStack(state->width, state->height));
311
312 // setup the matrix and clip on the n-way canvas
313 setup_canvas_from_MC_state(state_v1->mcState, canvas.get());
314
315 // Iterate over the layers and add them to the n-way canvas. New clients will only send one
316 // layer since unclipped layers are no longer supported, but old canvas clients may still
317 // create them.
318 for (int i = state_v1->layerCount - 1; i >= 0; --i) {
319 std::unique_ptr<SkCanvas> canvasLayer = make_canvas_from_canvas_layer(state_v1->layers[i]);
320 if (!canvasLayer) {
321 return nullptr;
322 }
323 canvas->pushCanvas(std::move(canvasLayer), SkIPoint::Make(state_v1->layers[i].x,
324 state_v1->layers[i].y));
325 }
326
327 return canvas;
328}
329
330////////////////////////////////////////////////////////////////////////////////
331
334 // Upcast to the correct version of SkCanvasState. This avoids having a virtual destructor on
335 // SkCanvasState. That would be strange since SkCanvasState has no other virtual functions, and
336 // instead uses the field "version" to determine how to behave.
337 delete static_cast<SkCanvasState_v1*>(state);
338}
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
static std::unique_ptr< SkCanvas > make_canvas_from_canvas_layer(const SkCanvasLayerState &layerState)
@ kGPU_CanvasBackend
@ kRaster_CanvasBackend
@ kPDF_CanvasBackend
@ kUnknown_CanvasBackend
int32_t RasterConfig
static void setup_canvas_from_MC_state(const SkMCState &state, SkCanvas *canvas)
@ kARGB_8888_RasterConfig
@ kUnknown_RasterConfig
@ kRGB_565_RasterConfig
static void setup_MC_state(SkMCState *state, const SkMatrix &matrix, const SkIRect &clip)
int32_t CanvasBackend
SkColorType
Definition SkColorType.h:19
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kUnknown_SkColorType
uninitialized
Definition SkColorType.h:20
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API void sk_free(void *)
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
static SkCanvasState * CaptureCanvasState(SkCanvas *canvas)
static std::unique_ptr< SkCanvas > MakeFromCanvasState(const SkCanvasState *state)
static void ReleaseCanvasState(SkCanvasState *state)
SkCanvasState_v1(SkCanvas *canvas)
static const int32_t kVersion
SkCanvasLayerState * layers
SkCanvasState(int32_t version, SkCanvas *canvas)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
virtual SkISize getBaseLayerSize() const
Definition SkCanvas.cpp:373
SkMatrix getTotalMatrix() const
SkIRect getDeviceClipBounds() const
void concat(const SkMatrix &matrix)
bool isEmpty() const
Definition SkPath.cpp:406
VkDevice device
Definition main.cc:53
AtkStateType state
struct SkCanvasLayerState::@429::@432 gpu
struct SkCanvasLayerState::@429::@431 raster
constexpr int32_t y() const
static constexpr SkIPoint Make(int32_t x, int32_t y)
constexpr int32_t x() const
static constexpr SkIRect MakeEmpty()
Definition SkRect.h:45
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
ClipRect * clipRects
static SkRect Make(const SkISize &size)
Definition SkRect.h:669