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 "TestSceneBase.h"
18 #include "utils/Color.h"
19 
20 #include <SkBlendMode.h>
21 #include <SkColorSpace.h>
22 #include <SkGradientShader.h>
23 #include <SkImagePriv.h>
24 #include <ui/PixelFormat.h>
25 
26 class HwBitmapInCompositeShader;
27 
28 static TestScene::Registrar _HwBitmapInCompositeShader(TestScene::Info{
29         "hwbitmapcompositeshader", "Draws composite shader with hardware bitmap",
30         TestScene::simpleCreateScene<HwBitmapInCompositeShader>});
31 
32 class HwBitmapInCompositeShader : public TestScene {
33 public:
34     sp<RenderNode> card;
createContent(int width,int height,Canvas & canvas)35     void createContent(int width, int height, Canvas& canvas) override {
36         canvas.drawColor(Color::Red_500, SkBlendMode::kSrcOver);
37 
38         uint32_t usage = GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_READ_NEVER |
39                          GRALLOC_USAGE_SW_WRITE_RARELY;
40 
41         sp<GraphicBuffer> buffer = new GraphicBuffer(400, 200, PIXEL_FORMAT_RGBA_8888, usage);
42 
43         unsigned char* pixels = nullptr;
44         buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, ((void**)&pixels));
45         size_t size =
46                 bytesPerPixel(buffer->getPixelFormat()) * buffer->getStride() * buffer->getHeight();
47         memset(pixels, 0, size);
48         for (int i = 0; i < 6000; i++) {
49             pixels[4000 + 4 * i + 0] = 255;
50             pixels[4000 + 4 * i + 1] = 255;
51             pixels[4000 + 4 * i + 2] = 0;
52             pixels[4000 + 4 * i + 3] = 255;
53         }
54         buffer->unlock();
55         sk_sp<Bitmap> hardwareBitmap(Bitmap::createFrom(buffer->toAHardwareBuffer(),
56                                                         SkColorSpace::MakeSRGB()));
57         sk_sp<SkShader> hardwareShader(createBitmapShader(*hardwareBitmap));
58 
59         SkPoint center;
60         center.set(50, 50);
61         SkColor colors[2];
62         colors[0] = Color::Black;
63         colors[1] = Color::White;
64         sk_sp<SkShader> gradientShader = SkGradientShader::MakeRadial(
65                 center, 50, colors, nullptr, 2, SkTileMode::kRepeat);
66 
67         sk_sp<SkShader> compositeShader(
68                 SkShaders::Blend(SkBlendMode::kDstATop, hardwareShader, gradientShader));
69 
70         Paint paint;
71         paint.setShader(std::move(compositeShader));
72         canvas.drawRoundRect(0, 0, 400, 200, 10.0f, 10.0f, paint);
73     }
74 
doFrame(int frameNr)75     void doFrame(int frameNr) override {}
76 
createBitmapShader(Bitmap & bitmap)77     sk_sp<SkShader> createBitmapShader(Bitmap& bitmap) {
78         sk_sp<SkImage> image = bitmap.makeImage();
79         return image->makeShader(SkSamplingOptions());
80     }
81 };
82