Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "tests/common/TestUtils.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <RecordingCanvas.h> |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 21 | #include <SkBlurDrawLooper.h> |
Matt Sarett | 44dc270 | 2017-04-13 09:33:18 -0400 | [diff] [blame] | 22 | #include <SkCanvasStateUtils.h> |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 23 | #include <SkPicture.h> |
| 24 | #include <SkPictureRecorder.h> |
| 25 | |
| 26 | using namespace android; |
| 27 | using namespace android::uirenderer; |
| 28 | |
| 29 | /** |
| 30 | * Verify that we get the same culling bounds for text for (1) drawing glyphs |
| 31 | * directly to a Canvas or (2) going through a SkPicture as an intermediate step. |
| 32 | */ |
Greg Daniel | 98c78da | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 33 | OPENGL_PIPELINE_TEST(SkiaCanvasProxy, drawGlyphsViaPicture) { |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 34 | auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) { |
| 35 | // setup test variables |
| 36 | SkPaint paint; |
| 37 | paint.setAntiAlias(true); |
| 38 | paint.setTextSize(20); |
Derek Sollenberger | 58691b6 | 2016-03-28 16:17:36 -0400 | [diff] [blame] | 39 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 40 | static const char* text = "testing text bounds"; |
| 41 | |
| 42 | // draw text directly into Recording canvas |
| 43 | TestUtils::drawUtf8ToCanvas(&canvas, text, paint, 25, 25); |
| 44 | |
| 45 | // record the same text draw into a SkPicture and replay it into a Recording canvas |
| 46 | SkPictureRecorder recorder; |
| 47 | SkCanvas* skCanvas = recorder.beginRecording(200, 200, NULL, 0); |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 48 | std::unique_ptr<Canvas> pictCanvas(Canvas::create_canvas(skCanvas, |
| 49 | Canvas::XformToSRGB::kDefer)); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 50 | TestUtils::drawUtf8ToCanvas(pictCanvas.get(), text, paint, 25, 25); |
Derek Sollenberger | 050bb6a | 2016-10-26 12:05:24 -0400 | [diff] [blame] | 51 | sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 52 | |
| 53 | canvas.asSkCanvas()->drawPicture(picture); |
| 54 | }); |
| 55 | |
| 56 | // verify that the text bounds and matrices match |
| 57 | ASSERT_EQ(2U, dl->getOps().size()); |
| 58 | auto directOp = dl->getOps()[0]; |
| 59 | auto pictureOp = dl->getOps()[1]; |
| 60 | ASSERT_EQ(RecordedOpId::TextOp, directOp->opId); |
| 61 | EXPECT_EQ(directOp->opId, pictureOp->opId); |
| 62 | EXPECT_EQ(directOp->unmappedBounds, pictureOp->unmappedBounds); |
| 63 | EXPECT_EQ(directOp->localMatrix, pictureOp->localMatrix); |
| 64 | } |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 65 | |
| 66 | TEST(SkiaCanvas, drawShadowLayer) { |
| 67 | auto surface = SkSurface::MakeRasterN32Premul(10, 10); |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 68 | SkiaCanvas canvas(surface->getCanvas(), Canvas::XformToSRGB::kDefer); |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 69 | |
| 70 | // clear to white |
| 71 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrc); |
| 72 | |
| 73 | SkPaint paint; |
| 74 | // it is transparent to ensure that we still draw the rect since it has a looper |
| 75 | paint.setColor(SK_ColorTRANSPARENT); |
| 76 | // this is how view's shadow layers are implemented |
| 77 | paint.setLooper(SkBlurDrawLooper::Make(0xF0000000, 6.0f, 0, 10)); |
| 78 | canvas.drawRect(3, 3, 7, 7, paint); |
| 79 | |
| 80 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE); |
| 81 | ASSERT_NE(TestUtils::getColor(surface, 5, 5), SK_ColorWHITE); |
| 82 | } |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 83 | |
| 84 | TEST(SkiaCanvas, colorSpaceXform) { |
| 85 | sk_sp<SkColorSpace> adobe = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 86 | SkColorSpace::kAdobeRGB_Gamut); |
| 87 | |
| 88 | SkImageInfo adobeInfo = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType, adobe); |
| 89 | sk_sp<Bitmap> adobeBitmap = Bitmap::allocateHeapBitmap(adobeInfo); |
| 90 | SkBitmap adobeSkBitmap; |
| 91 | adobeBitmap->getSkBitmap(&adobeSkBitmap); |
| 92 | adobeSkBitmap.lockPixels(); |
| 93 | *adobeSkBitmap.getAddr32(0, 0) = 0xFF0000F0; // Opaque, almost fully-red |
| 94 | |
| 95 | SkImageInfo info = adobeInfo.makeColorSpace(nullptr); |
| 96 | sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info); |
| 97 | SkBitmap skBitmap; |
| 98 | bitmap->getSkBitmap(&skBitmap); |
| 99 | |
| 100 | // Create a software canvas. |
| 101 | SkiaCanvas canvas(skBitmap); |
| 102 | canvas.drawBitmap(*adobeBitmap, 0, 0, nullptr); |
| 103 | // The result should be fully red, since we convert to sRGB at draw time. |
| 104 | skBitmap.lockPixels(); |
| 105 | ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0)); |
| 106 | |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 107 | // Create a software canvas with an Adobe color space. |
| 108 | SkiaCanvas adobeSkCanvas(adobeSkBitmap); |
| 109 | adobeSkCanvas.drawBitmap(*bitmap, 0, 0, nullptr); |
| 110 | // The result should be less than fully red, since we convert to Adobe RGB at draw time. |
| 111 | ASSERT_EQ(0xFF0000DC, *adobeSkBitmap.getAddr32(0, 0)); |
| 112 | |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 113 | // Now try in kDefer mode. This is a little strange given that, in practice, all software |
| 114 | // canvases are kImmediate. |
| 115 | SkCanvas skCanvas(skBitmap); |
| 116 | SkiaCanvas deferCanvas(&skCanvas, Canvas::XformToSRGB::kDefer); |
| 117 | deferCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr); |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 118 | // The result should be as before, since we deferred the conversion to sRGB. |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 119 | skBitmap.lockPixels(); |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 120 | ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0)); |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 121 | |
| 122 | // Test picture recording. We will kDefer the xform at recording time, but handle it when |
| 123 | // we playback to the software canvas. |
| 124 | SkPictureRecorder recorder; |
| 125 | SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0); |
| 126 | SkiaCanvas picCanvas(skPicCanvas, Canvas::XformToSRGB::kDefer); |
| 127 | picCanvas.drawBitmap(*adobeBitmap, 0, 0, nullptr); |
| 128 | sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); |
| 129 | |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 130 | // Playback to a deferred canvas. The result should be as before. |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 131 | deferCanvas.asSkCanvas()->drawPicture(picture); |
Matt Sarett | ca9b703 | 2017-04-13 12:18:47 -0400 | [diff] [blame] | 132 | ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0)); |
Matt Sarett | ea70d22 | 2017-03-29 16:25:10 -0400 | [diff] [blame] | 133 | |
| 134 | // Playback to an immediate canvas. The result should be fully red. |
| 135 | canvas.asSkCanvas()->drawPicture(picture); |
| 136 | ASSERT_EQ(0xFF0000FF, *skBitmap.getAddr32(0, 0)); |
| 137 | } |
Matt Sarett | 44dc270 | 2017-04-13 09:33:18 -0400 | [diff] [blame] | 138 | |
| 139 | TEST(SkiaCanvas, captureCanvasState) { |
| 140 | // Create a software canvas. |
| 141 | SkImageInfo info = SkImageInfo::Make(1, 1, kN32_SkColorType, kOpaque_SkAlphaType); |
| 142 | sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(info); |
| 143 | SkBitmap skBitmap; |
| 144 | bitmap->getSkBitmap(&skBitmap); |
| 145 | skBitmap.eraseColor(0); |
| 146 | SkiaCanvas canvas(skBitmap); |
| 147 | |
| 148 | // Translate, then capture and verify the CanvasState. |
| 149 | canvas.translate(1.0f, 1.0f); |
| 150 | SkCanvasState* state = canvas.captureCanvasState(); |
| 151 | ASSERT_NE(state, nullptr); |
| 152 | std::unique_ptr<SkCanvas> newCanvas = SkCanvasStateUtils::MakeFromCanvasState(state); |
| 153 | ASSERT_NE(newCanvas.get(), nullptr); |
| 154 | newCanvas->translate(-1.0f, -1.0f); |
| 155 | ASSERT_TRUE(newCanvas->getTotalMatrix().isIdentity()); |
| 156 | SkCanvasStateUtils::ReleaseCanvasState(state); |
| 157 | |
| 158 | // Create a picture canvas. |
| 159 | SkPictureRecorder recorder; |
| 160 | SkCanvas* skPicCanvas = recorder.beginRecording(1, 1, NULL, 0); |
| 161 | SkiaCanvas picCanvas(skPicCanvas, Canvas::XformToSRGB::kDefer); |
| 162 | state = picCanvas.captureCanvasState(); |
| 163 | |
| 164 | // Verify that we cannot get the CanvasState. |
| 165 | ASSERT_EQ(state, nullptr); |
| 166 | } |