Flutter Engine
The Flutter Engine
SkPixmapDraw.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 * This file contains implementations of SkPixmap methods which require the CPU backend.
8 */
9
18#include "include/core/SkRect.h"
24
25#include <utility>
26
28
29bool SkPixmap::scalePixels(const SkPixmap& actualDst, const SkSamplingOptions& sampling) const {
30 // We may need to tweak how we interpret these just a little below, so we make copies.
31 SkPixmap src = *this,
32 dst = actualDst;
33
34 // Can't do anthing with empty src or dst
35 if (src.width() <= 0 || src.height() <= 0 ||
36 dst.width() <= 0 || dst.height() <= 0) {
37 return false;
38 }
39
40 // no scaling involved?
41 if (src.width() == dst.width() && src.height() == dst.height()) {
42 return src.readPixels(dst);
43 }
44
45 // If src and dst are both unpremul, we'll fake the source out to appear as if premul,
46 // and mark the destination as opaque. This odd combination allows us to scale unpremul
47 // pixels without ever premultiplying them (perhaps losing information in the color channels).
48 // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM.
49 bool clampAsIfUnpremul = false;
50 if (src.alphaType() == kUnpremul_SkAlphaType &&
51 dst.alphaType() == kUnpremul_SkAlphaType) {
52 src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes());
53 dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes());
54
55 // We'll need to tell the image shader to clamp to [0,1] instead of the
56 // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality).
57 clampAsIfUnpremul = true;
58 }
59
61 if (!bitmap.installPixels(src)) {
62 return false;
63 }
64 bitmap.setImmutable(); // Don't copy when we create an image.
65
67
72 &scale,
73 clampAsIfUnpremul);
74
76 SkSurfaces::WrapPixels(dst.info(), dst.writable_addr(), dst.rowBytes());
77 if (!shader || !surface) {
78 return false;
79 }
80
82 paint.setBlendMode(SkBlendMode::kSrc);
83 paint.setShader(std::move(shader));
84 surface->getCanvas()->drawPaint(paint);
85 return true;
86}
kUnpremul_SkAlphaType
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
static sk_sp< SkShader > Make(sk_sp< SkImage >, SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix, bool clampAsIfUnpremul=false)
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition: SkMatrix.h:157
bool scalePixels(const SkPixmap &dst, const SkSamplingOptions &) const
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
Definition: bitmap.py:1
dst
Definition: cp.py:12
const Scalar scale
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669