Flutter Engine
The Flutter Engine
SkImage_Picture.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
23#include "src/base/SkTLazy.h"
27
28#include <cstring>
29#include <memory>
30#include <utility>
31
32class SkPaint;
33struct SkISize;
34
36 const SkMatrix* matrix, const SkPaint* paint,
37 SkImages::BitDepth bitDepth, sk_sp<SkColorSpace> colorSpace,
38 SkSurfaceProps props) {
40 bitDepth, std::move(colorSpace), props);
41
43 SharedGenerator::Make(std::move(gen)), nullptr, nullptr);
44
45 return validator ? sk_make_sp<SkImage_Picture>(&validator) : nullptr;
46}
47
49 auto pictureIG = static_cast<SkPictureImageGenerator*>(this->generator()->fGenerator.get());
50 return &pictureIG->fProps;
51}
52
53void SkImage_Picture::replay(SkCanvas* canvas) const {
54 auto sharedGenerator = this->generator();
55 SkAutoMutexExclusive mutex(sharedGenerator->fMutex);
56
57 auto pictureIG = static_cast<SkPictureImageGenerator*>(sharedGenerator->fGenerator.get());
59 canvas->drawPicture(pictureIG->fPicture,
60 &pictureIG->fMatrix,
61 pictureIG->fPaint.getMaybeNull());
62}
63
65 uint32_t keyValues[SkTiledImageUtils::kNumImageKeyValues]) const {
66
67 auto sharedGenerator = this->generator();
68 SkAutoMutexExclusive mutex(sharedGenerator->fMutex);
69
70 auto pictureIG = static_cast<SkPictureImageGenerator*>(sharedGenerator->fGenerator.get());
71 if (pictureIG->fPaint.getMaybeNull()) {
72 // A full paint complicates the potential key too much.
73 return false;
74 }
75
76 const SkImageInfo& ii = sharedGenerator->getInfo();
77 if (!ii.colorSpace()->isSRGB()) {
78 // We only return key values if the colorSpace is sRGB.
79 return false;
80 }
81
82 const SkMatrix& m = pictureIG->fMatrix;
83 if (!m.isIdentity() && !m.isTranslate()) {
84 // To keep the key small we only cache simple (<= translation) matrices
85 return false;
86 }
87
88 bool isU8 = ii.colorType() != kRGBA_F16_SkColorType;
89 uint32_t pixelGeometry = this->props()->pixelGeometry();
90 uint32_t surfacePropFlags = this->props()->flags();
91 int width = ii.width();
92 int height = ii.height();
93 float transX = m.getTranslateX();
94 float transY = m.getTranslateY();
95
96 SkASSERT(pixelGeometry <= 4);
97 SkASSERT(surfacePropFlags < 8);
98 SkASSERT(SkTFitsIn<uint32_t>(width));
99 SkASSERT(SkTFitsIn<uint32_t>(height));
100 SkASSERT(sizeof(float) == sizeof(uint32_t));
101
102 // The 0th slot usually holds either the SkBitmap's ID or the image's. In those two cases
103 // slot #1 is zero so we can reuse the 0th slot here.
104 keyValues[0] = (isU8 ? 0x1 : 0x0) | // 1 bit
105 (pixelGeometry << 1) | // 3 bits
106 (surfacePropFlags << 4); // 3 bits
107 keyValues[1] = pictureIG->fPicture->uniqueID();
108 SkASSERT(keyValues[1] != 0); // Double check we don't collide w/ bitmap or image keys
109 keyValues[2] = width;
110 keyValues[3] = height;
111 memcpy(&keyValues[4], &transX, sizeof(uint32_t));
112 memcpy(&keyValues[5], &transY, sizeof(uint32_t));
113 return true;
114}
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
static sk_sp< SharedGenerator > Make(std::unique_ptr< SkImageGenerator > gen)
std::unique_ptr< SkImageGenerator > fGenerator
Definition: SkImage_Lazy.h:117
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawPicture(const SkPicture *picture)
Definition: SkCanvas.h:1961
bool isSRGB() const
sk_sp< SharedGenerator > generator() const
bool getImageKeyValues(uint32_t keyValues[SkTiledImageUtils::kNumImageKeyValues]) const
const SkSurfaceProps * props() const
void replay(SkCanvas *) const
static sk_sp< SkImage > Make(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, SkImages::BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
SkColorSpace * colorSpace() const
Definition: SkImage.cpp:156
SkISize dimensions() const
Definition: SkImage.h:297
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
uint32_t flags() const
SkPixelGeometry pixelGeometry() const
const Paint & paint
Definition: color_source.cc:38
constexpr SkColor4f kTransparent
Definition: SkColor.h:434
std::unique_ptr< SkImageGenerator > MakeFromPicture(const SkISize &, sk_sp< SkPicture >, const SkMatrix *, const SkPaint *, SkImages::BitDepth, sk_sp< SkColorSpace >, SkSurfaceProps props)
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
static constexpr int kNumImageKeyValues
Definition: gen.py:1
Definition: SkSize.h:16
SkColorSpace * colorSpace() const
int width() const
Definition: SkImageInfo.h:365
SkColorType colorType() const
Definition: SkImageInfo.h:373
int height() const
Definition: SkImageInfo.h:371