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 <ui/PixelFormat.h> 21 #include <SkGradientShader.h> 22 #include <SkImagePriv.h> 23 24 class HwBitmapInCompositeShader; 25 26 static TestScene::Registrar _HwBitmapInCompositeShader(TestScene::Info{ 27 "hwbitmapcompositeshader", 28 "Draws composite shader with hardware bitmap", 29 TestScene::simpleCreateScene<HwBitmapInCompositeShader> 30 }); 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 39 | GraphicBuffer::USAGE_SW_READ_NEVER 40 | GRALLOC_USAGE_SW_WRITE_RARELY; 41 42 sp<GraphicBuffer> buffer = new GraphicBuffer(400, 200, PIXEL_FORMAT_RGBA_8888, usage); 43 44 unsigned char* pixels = nullptr; 45 buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, ((void**)&pixels)); 46 size_t size = bytesPerPixel(buffer->getPixelFormat()) * buffer->getStride() 47 * buffer->getHeight(); 48 memset(pixels, 0, size); 49 for (int i = 0; i < 6000; i++) { 50 pixels[4000 + 4 * i + 0] = 255; 51 pixels[4000 + 4 * i + 1] = 255; 52 pixels[4000 + 4 * i + 2] = 0; 53 pixels[4000 + 4 * i + 3] = 255; 54 } 55 buffer->unlock(); 56 sk_sp<Bitmap> hardwareBitmap(Bitmap::createFrom(buffer)); 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(center, 50, colors, nullptr, 65 2, SkShader::TileMode::kRepeat_TileMode); 66 67 sk_sp<SkShader> compositeShader( 68 SkShader::MakeComposeShader(hardwareShader, gradientShader, SkBlendMode::kDstATop)); 69 70 SkPaint 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(SkShader::TileMode::kClamp_TileMode, 80 SkShader::TileMode::kClamp_TileMode); 81 } 82 }; 83