| 1 |
/* |
| 2 |
* Copyright 2016 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 |
|
| 8 |
#include "src/core/SkSpecialImage.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkShader.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "src/core/SkNextID.h" |
| 18 |
#include "src/image/SkImage_Base.h" |
| 19 |
#include "src/shaders/SkImageShader.h" |
| 20 |
|
| 21 |
SkSpecialImage::SkSpecialImage(const SkIRect& subset, |
| 22 |
uint32_t uniqueID, |
| 23 |
const SkColorInfo& colorInfo, |
| 24 |
const SkSurfaceProps& props) |
| 25 |
: fSubset(subset) |
| 26 |
, fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) |
| 27 |
, fColorInfo(colorInfo) |
| 28 |
, fProps(props) { |
| 29 |
} |
| 30 |
|
| 31 |
void SkSpecialImage::draw(SkCanvas* canvas, |
| 32 |
SkScalar x, SkScalar y, |
| 33 |
const SkSamplingOptions& sampling, |
| 34 |
const SkPaint* paint, bool strict) const { |
| 35 |
SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); |
| 36 |
|
| 37 |
canvas->drawImageRect(this->asImage(), SkRect::Make(this->subset()), dst, |
| 38 |
sampling, paint, strict ? SkCanvas::kStrict_SrcRectConstraint |
| 39 |
: SkCanvas::kFast_SrcRectConstraint); |
| 40 |
} |
| 41 |
|
| 42 |
// TODO(skbug.com/40043877): Once bitmap images work with SkImageShader::MakeSubset(), this does not |
| 43 |
// need to be virtual anymore. |
| 44 |
sk_sp<SkShader> SkSpecialImage::asShader(SkTileMode tileMode, |
| 45 |
const SkSamplingOptions& sampling, |
| 46 |
const SkMatrix& lm, |
| 47 |
bool strict) const { |
| 48 |
// The special image's logical (0,0) is at its subset's topLeft() so we need to account for |
| 49 |
// that in the local matrix used when sampling. |
| 50 |
SkMatrix subsetOrigin = SkMatrix::Translate(-this->subset().topLeft()); |
| 51 |
subsetOrigin.postConcat(lm); |
| 52 |
|
| 53 |
if (strict) { |
| 54 |
// However, we don't need to modify the subset itself since that is defined with respect |
| 55 |
// to the base image, and the local matrix is applied before any tiling/clamping. |
| 56 |
const SkRect subset = SkRect::Make(this->subset()); |
| 57 |
|
| 58 |
// asImage() w/o a subset makes no copy; create the SkImageShader directly to remember |
| 59 |
// the subset used to access the image. |
| 60 |
return SkImageShader::MakeSubset( |
| 61 |
this->asImage(), subset, tileMode, tileMode, sampling, &subsetOrigin); |
| 62 |
} else { |
| 63 |
// Ignore 'subset' other than its origin translation applied to the local matrix. |
| 64 |
return this->asImage()->makeShader(tileMode, tileMode, sampling, subsetOrigin); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
class SkSpecialImage_Raster final : public SkSpecialImage { |
| 69 |
public: |
| 70 |
SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps& props) |
| 71 |
: SkSpecialImage(subset, bm.getGenerationID(), bm.info().colorInfo(), props) |
| 72 |
, fBitmap(bm) { |
| 73 |
SkASSERT(bm.pixelRef()); |
| 74 |
SkASSERT(fBitmap.getPixels()); |
| 75 |
} |
| 76 |
|
| 77 |
bool getROPixels(SkBitmap* bm) const { |
| 78 |
return fBitmap.extractSubset(bm, this->subset()); |
| 79 |
} |
| 80 |
|
| 81 |
SkISize backingStoreDimensions() const override { return fBitmap.dimensions(); } |
| 82 |
|
| 83 |
size_t getSize() const override { return fBitmap.computeByteSize(); } |
| 84 |
|
| 85 |
sk_sp<SkImage> asImage() const override { return fBitmap.asImage(); } |
| 86 |
|
| 87 |
sk_sp<SkSpecialImage> onMakeBackingStoreSubset(const SkIRect& subset) const override { |
| 88 |
// No need to extract subset, onGetROPixels handles that when needed |
| 89 |
return SkSpecialImages::MakeFromRaster(subset, fBitmap, this->props()); |
| 90 |
} |
| 91 |
|
| 92 |
sk_sp<SkShader> asShader(SkTileMode tileMode, |
| 93 |
const SkSamplingOptions& sampling, |
| 94 |
const SkMatrix& lm, |
| 95 |
bool strict) const override { |
| 96 |
if (strict) { |
| 97 |
// TODO(skbug.com/40043877): SkImage::makeShader() doesn't support a subset yet, but |
| 98 |
// SkBitmap supports subset views so create the shader from the subset bitmap instead of |
| 99 |
// fBitmap. |
| 100 |
SkBitmap subsetBM; |
| 101 |
if (!this->getROPixels(&subsetBM)) { |
| 102 |
return nullptr; |
| 103 |
} |
| 104 |
return subsetBM.makeShader(tileMode, tileMode, sampling, lm); |
| 105 |
} else { |
| 106 |
// The special image's logical (0,0) is at its subset's topLeft() so we need to |
| 107 |
// account for that in the local matrix used when sampling. |
| 108 |
SkMatrix subsetOrigin = SkMatrix::Translate(-this->subset().topLeft()); |
| 109 |
subsetOrigin.postConcat(lm); |
| 110 |
return fBitmap.makeShader(tileMode, tileMode, sampling, subsetOrigin); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private: |
| 115 |
SkBitmap fBitmap; |
| 116 |
}; |
| 117 |
|
| 118 |
namespace SkSpecialImages { |
| 119 |
|
| 120 |
sk_sp<SkSpecialImage> MakeFromRaster(const SkIRect& subset, |
| 121 |
const SkBitmap& bm, |
| 122 |
const SkSurfaceProps& props) { |
| 123 |
SkASSERT(bm.bounds().contains(subset)); |
| 124 |
|
| 125 |
if (!bm.pixelRef()) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
return sk_make_sp<SkSpecialImage_Raster>(subset, bm, props); |
| 129 |
} |
| 130 |
|
| 131 |
sk_sp<SkSpecialImage> CopyFromRaster(const SkIRect& subset, |
| 132 |
const SkBitmap& bm, |
| 133 |
const SkSurfaceProps& props) { |
| 134 |
SkASSERT(bm.bounds().contains(subset)); |
| 135 |
|
| 136 |
if (!bm.pixelRef()) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
SkBitmap tmp; |
| 140 |
SkImageInfo info = bm.info().makeDimensions(subset.size()); |
| 141 |
if (!tmp.tryAllocPixels(info)) { |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
if (!bm.readPixels(tmp.info(), tmp.getPixels(), tmp.rowBytes(), subset.x(), subset.y())) { |
| 145 |
return nullptr; |
| 146 |
} |
| 147 |
// Since we're making a copy of the raster, the resulting special image is the exact size |
| 148 |
// of the requested subset of the original and no longer needs to be offset by subset's left |
| 149 |
// and top, since those were relative to the original's buffer. |
| 150 |
return sk_make_sp<SkSpecialImage_Raster>( |
| 151 |
SkIRect::MakeWH(subset.width(), subset.height()), tmp, props); |
| 152 |
} |
| 153 |
|
| 154 |
sk_sp<SkSpecialImage> MakeFromRaster(const SkIRect& subset, |
| 155 |
sk_sp<SkImage> image, |
| 156 |
const SkSurfaceProps& props) { |
| 157 |
if (!image || subset.isEmpty()) { |
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
SkASSERT(image->bounds().contains(subset)); |
| 162 |
SkASSERT(!image->isTextureBacked()); |
| 163 |
|
| 164 |
// This will not work if the image is uploaded to a GPU render target. |
| 165 |
SkBitmap bm; |
| 166 |
if (as_IB(image)->getROPixels(nullptr, &bm)) { |
| 167 |
return MakeFromRaster(subset, bm, props); |
| 168 |
} |
| 169 |
return nullptr; |
| 170 |
} |
| 171 |
|
| 172 |
bool AsBitmap(const SkSpecialImage* img, SkBitmap* result) { |
| 173 |
if (!img || img->isGaneshBacked() || img->isGraphiteBacked()) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
auto rasterImg = static_cast<const SkSpecialImage_Raster*>(img); |
| 177 |
return rasterImg->getROPixels(result); |
| 178 |
} |
| 179 |
|
| 180 |
} // namespace SkSpecialImages |