| 1 | /* |
| 2 | * Copyright (C) 2015 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 <SkImagePriv.h> |
| 18 | #include "hwui/Paint.h" |
| 19 | #include "TestSceneBase.h" |
| 20 | #include "tests/common/BitmapAllocationTestUtils.h" |
| 21 | #include "utils/Color.h" |
| 22 | |
| 23 | class BitmapShaders; |
| 24 | |
| 25 | static bool _BitmapShaders(BitmapAllocationTestUtils::registerBitmapAllocationScene<BitmapShaders>( |
| 26 | "bitmapShader", "Draws bitmap shaders with repeat and mirror modes.")); |
| 27 | |
| 28 | class BitmapShaders : public TestScene { |
| 29 | public: |
| 30 | explicit BitmapShaders(BitmapAllocationTestUtils::BitmapAllocator allocator) |
| 31 | : TestScene(), mAllocator(allocator) {} |
| 32 | |
| 33 | sp<RenderNode> card; |
| 34 | void createContent(int width, int height, Canvas& canvas) override { |
| 35 | canvas.drawColor(Color::Grey_200, SkBlendMode::kSrcOver); |
| 36 | sk_sp<Bitmap> hwuiBitmap = |
| 37 | mAllocator(200, 200, kRGBA_8888_SkColorType, [](SkBitmap& skBitmap) { |
| 38 | skBitmap.eraseColor(Color::White); |
| 39 | SkCanvas skCanvas(skBitmap); |
| 40 | SkPaint skPaint; |
| 41 | skPaint.setColor(Color::Red_500); |
| 42 | skCanvas.drawRect(SkRect::MakeWH(100, 100), skPaint); |
| 43 | skPaint.setColor(Color::Blue_500); |
| 44 | skCanvas.drawRect(SkRect::MakeXYWH(100, 100, 100, 100), skPaint); |
| 45 | }); |
| 46 | |
| 47 | SkSamplingOptions sampling; |
| 48 | Paint paint; |
| 49 | sk_sp<SkImage> image = hwuiBitmap->makeImage(); |
| 50 | sk_sp<SkShader> repeatShader = |
| 51 | image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, sampling); |
| 52 | paint.setShader(std::move(repeatShader)); |
| 53 | canvas.drawRoundRect(0, 0, 500, 500, 50.0f, 50.0f, paint); |
| 54 | |
| 55 | sk_sp<SkShader> mirrorShader = |
| 56 | image->makeShader(SkTileMode::kMirror, SkTileMode::kMirror, sampling); |
| 57 | paint.setShader(std::move(mirrorShader)); |
| 58 | canvas.drawRoundRect(0, 600, 500, 1100, 50.0f, 50.0f, paint); |
| 59 | } |
| 60 | |
| 61 | void doFrame(int frameNr) override {} |
| 62 | |
| 63 | BitmapAllocationTestUtils::BitmapAllocator mAllocator; |
| 64 | }; |