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