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