skia2/src/pdf/SkBitmapKey.h
Hal Canary f50ff39f47 SkPDF: subset drawImageRect while still deduping
- Replace SkImageBitmap with SkImageSubset

  - SkBitmapKey becomes trivial for simplicity.

  - SkPDFCanvas::onDraw(Bitmap|Image)Rect now clip and call
    SkCanvas::onDraw(Bitmap|Image)Rect.

  - SkPDFDevice::draw(Bitmap|BitmapRect|Sprite) now convert bitmap
    into SkImageSubset via make_image_subset function.

  - SkPDFDevice::draw(Image|Bitmap)Rect now implemented again.

  - SkPDFDevice::internalDrawImage now performs image subsetting
    as needed, while still deduping properly.

BUG=633528

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2785

Change-Id: I063346d12b0e9c6b6c0c4943ee25400c88aa1a44
Reviewed-on: https://skia-review.googlesource.com/2785
Reviewed-by: Ben Wagner <bungeman@google.com>
2016-09-30 15:22:06 +00:00

78 lines
2.0 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkBitmapKey_DEFINED
#define SkBitmapKey_DEFINED
#include "SkBitmap.h"
#include "SkImage.h"
#include "SkCanvas.h"
struct SkBitmapKey {
SkIRect fSubset;
uint32_t fID;
bool operator==(const SkBitmapKey& rhs) const {
return fID == rhs.fID && fSubset == rhs.fSubset;
}
bool operator!=(const SkBitmapKey& rhs) const { return !(*this == rhs); }
};
/**
This class has all the advantages of SkBitmaps and SkImages.
*/
class SkImageSubset {
public:
SkImageSubset(sk_sp<SkImage> i, SkIRect subset = {0, 0, 0, 0})
: fImage(std::move(i)) {
if (!fImage) {
fSubset = {0, 0, 0, 0};
fID = 0;
return;
}
fID = fImage->uniqueID();
if (subset.isEmpty()) {
fSubset = fImage->bounds();
// SkImage always has a non-zero dimensions.
SkASSERT(!fSubset.isEmpty());
} else {
fSubset = subset;
if (!fSubset.intersect(fImage->bounds())) {
fImage = nullptr;
fSubset = {0, 0, 0, 0};
fID = 0;
}
}
}
void setID(uint32_t id) { fID = id; }
bool isValid() const { return fImage != nullptr; }
SkIRect bounds() const { return SkIRect::MakeSize(this->dimensions()); }
SkISize dimensions() const { return fSubset.size(); }
sk_sp<SkImage> makeImage() const {
return fSubset == fImage->bounds() ? fImage : fImage->makeSubset(fSubset);
}
SkBitmapKey getKey() const { return SkBitmapKey{fSubset, fID}; }
void draw(SkCanvas* canvas, SkPaint* paint) const {
SkASSERT(this->isValid());
SkRect src = SkRect::Make(fSubset),
dst = SkRect::Make(this->bounds());
canvas->drawImageRect(fImage.get(), src, dst, paint);
}
private:
SkIRect fSubset;
sk_sp<SkImage> fImage;
uint32_t fID;
};
#endif // SkBitmapKey_DEFINED