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> 21#include <SkBlurDrawLooper.h> 22#include <SkCanvasStateUtils.h> 23#include <SkPicture.h> 24#include <SkPictureRecorder.h> 25 26using namespace android; 27using 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 */ 33OPENGL_PIPELINE_TEST(SkiaCanvasProxy, drawGlyphsViaPicture) { 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); 39 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 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); 48 std::unique_ptr<Canvas> pictCanvas(Canvas::create_canvas(skCanvas, 49 Canvas::XformToSRGB::kDefer)); 50 TestUtils::drawUtf8ToCanvas(pictCanvas.get(), text, paint, 25, 25); 51 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); 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} 65 66TEST(SkiaCanvas, drawShadowLayer) { 67 auto surface = SkSurface::MakeRasterN32Premul(10, 10); 68 SkiaCanvas canvas(surface->getCanvas(), Canvas::XformToSRGB::kDefer); 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} 83 84TEST(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 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 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); 118 // The result should be as before, since we deferred the conversion to sRGB. 119 skBitmap.lockPixels(); 120 ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0)); 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 130 // Playback to a deferred canvas. The result should be as before. 131 deferCanvas.asSkCanvas()->drawPicture(picture); 132 ASSERT_EQ(0xFF0000DC, *skBitmap.getAddr32(0, 0)); 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} 138 139TEST(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} 167