From 31f99ce7d2f3c2b61742d07ab92b6dac6a928ef9 Mon Sep 17 00:00:00 2001 From: Matt Sarett Date: Tue, 11 Apr 2017 08:46:01 -0400 Subject: [PATCH] SkCanvas constructor with legacy flag for Android framework Bug: skia: Change-Id: I5b268ad03dde075f2710196d5ad2bd19249557f7 Reviewed-on: https://skia-review.googlesource.com/11788 Commit-Queue: Matt Sarett Reviewed-by: Mike Reed --- include/core/SkCanvas.h | 13 +++++++++++++ src/core/SkCanvas.cpp | 15 +++++++++++++++ tests/CanvasTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index c44965e110..d73ceb7578 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -112,6 +112,19 @@ public: */ explicit SkCanvas(const SkBitmap& bitmap); +#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK + enum class ColorBehavior { + kLegacy, + }; + + /** + * Android framework only constructor. + * Allows the creation of a legacy SkCanvas even though the |bitmap| + * and its pixel ref may have an SkColorSpace. + */ + SkCanvas(const SkBitmap& bitmap, ColorBehavior); +#endif + /** Construct a canvas with the specified bitmap to draw into. @param bitmap Specifies a bitmap for the canvas to draw into. Its structure are copied to the canvas. diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index cd1600ad72..aa69f09f47 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -747,6 +747,21 @@ SkCanvas::SkCanvas(const SkBitmap& bitmap, std::unique_ptr(&tmp.info()) = tmp.info().makeColorSpace(nullptr); + sk_sp device(new SkBitmapDevice(tmp, fProps, nullptr)); + this->init(device.get(), kDefault_InitFlags); +} +#endif + SkCanvas::~SkCanvas() { // free up the contents of our deque this->restoreToCount(1); // restore everything but the last diff --git a/tests/CanvasTest.cpp b/tests/CanvasTest.cpp index 26228bacde..fabd19574a 100644 --- a/tests/CanvasTest.cpp +++ b/tests/CanvasTest.cpp @@ -887,3 +887,23 @@ DEF_TEST(CanvasClipType, r) { SkDynamicMemoryWStream stream; test_cliptype(SkDocument::MakePDF(&stream)->beginPage(100, 100), r); } + +#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK +DEF_TEST(Canvas_LegacyColorBehavior, r) { + sk_sp cs = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, + SkColorSpace::kAdobeRGB_Gamut); + + // Make a Adobe RGB bitmap. + SkBitmap bitmap; + bitmap.allocPixels(SkImageInfo::MakeN32(1, 1, kOpaque_SkAlphaType, cs)); + bitmap.eraseColor(0xFF000000); + + // Wrap it in a legacy canvas. Test that the canvas behaves like a legacy canvas. + SkCanvas canvas(bitmap, SkCanvas::ColorBehavior::kLegacy); + REPORTER_ASSERT(r, !canvas.imageInfo().colorSpace()); + SkPaint p; + p.setColor(SK_ColorRED); + canvas.drawIRect(SkIRect::MakeWH(1, 1), p); + REPORTER_ASSERT(r, SK_ColorRED == SkSwizzle_BGRA_to_PMColor(*bitmap.getAddr32(0, 0))); +} +#endif -- 2.34.1