| 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 |
|
| 8 |
#ifndef SkTiledImageUtils_DEFINED |
| 9 |
#define SkTiledImageUtils_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkSamplingOptions.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/private/base/SkAPI.h" |
| 18 |
|
| 19 |
#include <cstdint> |
| 20 |
|
| 21 |
class SkPaint; |
| 22 |
|
| 23 |
/** \namespace SkTiledImageUtils |
| 24 |
SkTiledImageUtils' DrawImage/DrawImageRect methods are intended to be direct replacements |
| 25 |
for their SkCanvas equivalents. The SkTiledImageUtils calls will break SkBitmap-backed |
| 26 |
SkImages into smaller tiles and draw them if the original image is too large to be |
| 27 |
uploaded to the GPU. If the original image doesn't need tiling or is already gpu-backed |
| 28 |
the DrawImage/DrawImageRect calls will fall through to the matching SkCanvas call. |
| 29 |
*/ |
| 30 |
namespace SkTiledImageUtils { |
| 31 |
|
| 32 |
SK_API void DrawImageRect(SkCanvas* canvas, |
| 33 |
const SkImage* image, |
| 34 |
const SkRect& src, |
| 35 |
const SkRect& dst, |
| 36 |
const SkSamplingOptions& sampling = {}, |
| 37 |
const SkPaint* paint = nullptr, |
| 38 |
SkCanvas::SrcRectConstraint constraint = |
| 39 |
SkCanvas::kFast_SrcRectConstraint); |
| 40 |
|
| 41 |
inline void DrawImageRect(SkCanvas* canvas, |
| 42 |
const sk_sp<SkImage>& image, |
| 43 |
const SkRect& src, |
| 44 |
const SkRect& dst, |
| 45 |
const SkSamplingOptions& sampling = {}, |
| 46 |
const SkPaint* paint = nullptr, |
| 47 |
SkCanvas::SrcRectConstraint constraint = |
| 48 |
SkCanvas::kFast_SrcRectConstraint) { |
| 49 |
DrawImageRect(canvas, image.get(), src, dst, sampling, paint, constraint); |
| 50 |
} |
| 51 |
|
| 52 |
inline void DrawImageRect(SkCanvas* canvas, |
| 53 |
const SkImage* image, |
| 54 |
const SkRect& dst, |
| 55 |
const SkSamplingOptions& sampling = {}, |
| 56 |
const SkPaint* paint = nullptr, |
| 57 |
SkCanvas::SrcRectConstraint constraint = |
| 58 |
SkCanvas::kFast_SrcRectConstraint) { |
| 59 |
if (!image) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
SkRect src = SkRect::MakeIWH(image->width(), image->height()); |
| 64 |
|
| 65 |
DrawImageRect(canvas, image, src, dst, sampling, paint, constraint); |
| 66 |
} |
| 67 |
|
| 68 |
inline void DrawImageRect(SkCanvas* canvas, |
| 69 |
const sk_sp<SkImage>& image, |
| 70 |
const SkRect& dst, |
| 71 |
const SkSamplingOptions& sampling = {}, |
| 72 |
const SkPaint* paint = nullptr, |
| 73 |
SkCanvas::SrcRectConstraint constraint = |
| 74 |
SkCanvas::kFast_SrcRectConstraint) { |
| 75 |
DrawImageRect(canvas, image.get(), dst, sampling, paint, constraint); |
| 76 |
} |
| 77 |
|
| 78 |
inline void DrawImage(SkCanvas* canvas, |
| 79 |
const SkImage* image, |
| 80 |
SkScalar x, SkScalar y, |
| 81 |
const SkSamplingOptions& sampling = {}, |
| 82 |
const SkPaint* paint = nullptr, |
| 83 |
SkCanvas::SrcRectConstraint constraint = |
| 84 |
SkCanvas::kFast_SrcRectConstraint) { |
| 85 |
if (!image) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
SkRect src = SkRect::MakeIWH(image->width(), image->height()); |
| 90 |
SkRect dst = SkRect::MakeXYWH(x, y, image->width(), image->height()); |
| 91 |
|
| 92 |
DrawImageRect(canvas, image, src, dst, sampling, paint, constraint); |
| 93 |
} |
| 94 |
|
| 95 |
inline void DrawImage(SkCanvas* canvas, |
| 96 |
const sk_sp<SkImage>& image, |
| 97 |
SkScalar x, SkScalar y, |
| 98 |
const SkSamplingOptions& sampling = {}, |
| 99 |
const SkPaint* paint = nullptr, |
| 100 |
SkCanvas::SrcRectConstraint constraint = |
| 101 |
SkCanvas::kFast_SrcRectConstraint) { |
| 102 |
DrawImage(canvas, image.get(), x, y, sampling, paint, constraint); |
| 103 |
} |
| 104 |
|
| 105 |
static constexpr int kNumImageKeyValues = 6; |
| 106 |
|
| 107 |
/** Retrieves a set of values that can be used as part of a cache key for the provided image. |
| 108 |
|
| 109 |
Unfortunately, SkImage::uniqueID isn't sufficient as an SkImage cache key. In particular, |
| 110 |
SkBitmap-backed SkImages can share a single SkBitmap and refer to different subsets of it. |
| 111 |
In this situation the optimal key is based on the SkBitmap's generation ID and the subset |
| 112 |
rectangle. |
| 113 |
For Picture-backed images this method will attempt to generate a concise internally-based |
| 114 |
key (i.e., containing picture ID, matrix translation, width and height, etc.). For complicated |
| 115 |
Picture-backed images (i.e., those w/ a paint or a full matrix) it will fall back to |
| 116 |
using 'image's unique key. |
| 117 |
|
| 118 |
@param image The image for which key values are desired |
| 119 |
@param keyValues The resulting key values |
| 120 |
*/ |
| 121 |
SK_API void GetImageKeyValues(const SkImage* image, uint32_t keyValues[kNumImageKeyValues]); |
| 122 |
|
| 123 |
} // namespace SkTiledImageUtils |
| 124 |
|
| 125 |
#endif // SkTiledImageUtils_DEFINED |