diff options
author | reed <reed@google.com> | 2015-12-02 14:19:47 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-02 14:19:47 -0800 |
commit | 7850eb2f357c215b2e2c50bf16d6c6df38c7967f (patch) | |
tree | a67b456a7b21c9ee3dbec07772882dd117672b52 /src/core | |
parent | dc5685ac3752e90dd68179e9f1675ff6f15ed600 (diff) |
API to support native scaling by image-generator
BUG=skia:
Review URL: https://codereview.chromium.org/1396323007
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkImageGenerator.cpp | 24 | ||||
-rw-r--r-- | src/core/SkPictureImageGenerator.cpp | 44 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/core/SkImageGenerator.cpp b/src/core/SkImageGenerator.cpp index 5e401920cd..dcd3ad00b1 100644 --- a/src/core/SkImageGenerator.cpp +++ b/src/core/SkImageGenerator.cpp @@ -111,6 +111,30 @@ GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkIRect* subs return this->onGenerateTexture(ctx, subset); } +bool SkImageGenerator::computeScaledDimensions(SkScalar scale, SupportedSizes* sizes) { + if (scale > 0 && scale <= 1) { + return this->onComputeScaledDimensions(scale, sizes); + } + return false; +} + +bool SkImageGenerator::generateScaledPixels(const SkISize& scaledSize, + const SkIPoint& subsetOrigin, + const SkPixmap& subsetPixels) { + if (scaledSize.width() <= 0 || scaledSize.height() <= 0) { + return false; + } + if (subsetPixels.width() <= 0 || subsetPixels.height() <= 0) { + return false; + } + const SkIRect subset = SkIRect::MakeXYWH(subsetOrigin.x(), subsetOrigin.y(), + subsetPixels.width(), subsetPixels.height()); + if (!SkIRect::MakeWH(scaledSize.width(), scaledSize.height()).contains(subset)) { + return false; + } + return this->onGenerateScaledPixels(scaledSize, subsetOrigin, subsetPixels); +} + ///////////////////////////////////////////////////////////////////////////////////////////// SkData* SkImageGenerator::onRefEncodedData() { diff --git a/src/core/SkPictureImageGenerator.cpp b/src/core/SkPictureImageGenerator.cpp index 556015d99e..39caa55d0c 100644 --- a/src/core/SkPictureImageGenerator.cpp +++ b/src/core/SkPictureImageGenerator.cpp @@ -21,6 +21,9 @@ public: protected: bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], int* ctableCount) override; + bool onComputeScaledDimensions(SkScalar scale, SupportedSizes*) override; + bool onGenerateScaledPixels(const SkISize&, const SkIPoint&, const SkPixmap&) override; + #if SK_SUPPORT_GPU GrTexture* onGenerateTexture(GrContext*, const SkIRect*) override; #endif @@ -78,6 +81,47 @@ bool SkPictureImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, return true; } +bool SkPictureImageGenerator::onComputeScaledDimensions(SkScalar scale, + SupportedSizes* sizes) { + SkASSERT(scale > 0 && scale <= 1); + const int w = this->getInfo().width(); + const int h = this->getInfo().height(); + const int sw = SkScalarRoundToInt(scale * w); + const int sh = SkScalarRoundToInt(scale * h); + if (sw > 0 && sh > 0) { + sizes->fSizes[0].set(sw, sh); + sizes->fSizes[1].set(sw, sh); + return true; + } + return false; +} + +bool SkPictureImageGenerator::onGenerateScaledPixels(const SkISize& scaledSize, + const SkIPoint& scaledOrigin, + const SkPixmap& scaledPixels) { + int w = scaledSize.width(); + int h = scaledSize.height(); + + const SkScalar scaleX = SkIntToScalar(w) / this->getInfo().width(); + const SkScalar scaleY = SkIntToScalar(h) / this->getInfo().height(); + SkMatrix matrix = SkMatrix::MakeScale(scaleX, scaleY); + matrix.postTranslate(-SkIntToScalar(scaledOrigin.x()), -SkIntToScalar(scaledOrigin.y())); + + SkBitmap bitmap; + if (!bitmap.installPixels(scaledPixels.info(), scaledPixels.writable_addr(), + scaledPixels.rowBytes())) { + return false; + } + + bitmap.eraseColor(SK_ColorTRANSPARENT); + SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); + matrix.preConcat(fMatrix); + canvas.drawPicture(fPicture, &matrix, fPaint.getMaybeNull()); + return true; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + SkImageGenerator* SkImageGenerator::NewFromPicture(const SkISize& size, const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { return SkPictureImageGenerator::Create(size, picture, matrix, paint); |