Flutter Engine
The Flutter Engine
SkTiledImageUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
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
16#include "src/core/SkDevice.h"
19
20#include <string.h>
21
22namespace SkTiledImageUtils {
23
25 const SkImage* image,
26 const SkRect& src,
27 const SkRect& dst,
29 const SkPaint* paint,
30 SkCanvas::SrcRectConstraint constraint) {
31 if (!image || !canvas) {
32 return;
33 }
34
35 SkPaint p;
36 if (paint) {
37 p = *paint;
38 }
39 if (!SkCanvasPriv::TopDevice(canvas)->drawAsTiledImageRect(
40 canvas, image, &src, dst, sampling, p, constraint)) {
41 // Either the image didn't require tiling or this is a raster-backed
42 // canvas. In either case fall back to a default draw.
43 canvas->drawImageRect(image, src, dst, sampling, paint, constraint);
44 }
45}
46
47void GetImageKeyValues(const SkImage* image, uint32_t keyValues[kNumImageKeyValues]) {
48 if (!image || !keyValues) {
49 if (keyValues) {
50 memset(keyValues, 0, kNumImageKeyValues * sizeof(uint32_t));
51 }
52 return;
53 }
54
55 const SkImage_Base* imageBase = as_IB(image);
56 if (const SkBitmap* bm = imageBase->onPeekBitmap()) {
57 keyValues[0] = bm->pixelRef()->getGenerationID();
58 SkIRect subset = image->bounds();
59 subset.offset(bm->pixelRefOrigin());
60
61 SkASSERT(SkTFitsIn<uint32_t>(subset.fLeft));
62 SkASSERT(SkTFitsIn<uint32_t>(subset.fTop));
63 SkASSERT(SkTFitsIn<uint32_t>(subset.fRight));
64 SkASSERT(SkTFitsIn<uint32_t>(subset.fBottom));
65
66 keyValues[1] = 0; // This empty slot is to disambiguate picture IDs
67 keyValues[2] = subset.fLeft;
68 keyValues[3] = subset.fTop;
69 keyValues[4] = subset.fRight;
70 keyValues[5] = subset.fBottom;
71 return;
72 }
73
74 if (imageBase->type() == SkImage_Base::Type::kLazyPicture) {
75 const SkImage_Picture* pictureImage = static_cast<const SkImage_Picture*>(imageBase);
76 if (pictureImage->getImageKeyValues(keyValues)) {
77 return;
78 }
79 }
80
81 keyValues[0] = image->uniqueID();
82 memset(&keyValues[1], 0, (kNumImageKeyValues-1) * sizeof(uint32_t));
83}
84
85} // namespace SkTiledImageUtils
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkImage_Base * as_IB(SkImage *image)
Definition: SkImage_Base.h:201
static SkDevice * TopDevice(const SkCanvas *canvas)
Definition: SkCanvasPriv.h:65
SrcRectConstraint
Definition: SkCanvas.h:1541
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
virtual Type type() const =0
virtual const SkBitmap * onPeekBitmap() const
Definition: SkImage_Base.h:64
bool getImageKeyValues(uint32_t keyValues[SkTiledImageUtils::kNumImageKeyValues]) const
uint32_t uniqueID() const
Definition: SkImage.h:311
SkIRect bounds() const
Definition: SkImage.h:303
const Paint & paint
Definition: color_source.cc:38
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SkSamplingOptions sampling
Definition: SkRecords.h:337
static constexpr int kNumImageKeyValues
SK_API void DrawImageRect(SkCanvas *canvas, const SkImage *image, const SkRect &src, const SkRect &dst, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
SK_API void GetImageKeyValues(const SkImage *image, uint32_t keyValues[kNumImageKeyValues])
dst
Definition: cp.py:12
Definition: SkRect.h:32
int32_t fBottom
larger y-axis bounds
Definition: SkRect.h:36
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
void offset(int32_t dx, int32_t dy)
Definition: SkRect.h:367
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35