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 24class HwBitmapInCompositeShader; 25 26static TestScene::Registrar _HwBitmapInCompositeShader(TestScene::Info{ 27 "hwbitmapcompositeshader", "Draws composite shader with hardware bitmap", 28 TestScene::simpleCreateScene<HwBitmapInCompositeShader>}); 29 30class HwBitmapInCompositeShader : public TestScene { 31public: 32 sp<RenderNode> card; 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)); 54 sk_sp<SkShader> hardwareShader(createBitmapShader(*hardwareBitmap)); 55 56 SkPoint center; 57 center.set(50, 50); 58 SkColor colors[2]; 59 colors[0] = Color::Black; 60 colors[1] = Color::White; 61 sk_sp<SkShader> gradientShader = SkGradientShader::MakeRadial( 62 center, 50, colors, nullptr, 2, SkShader::TileMode::kRepeat_TileMode); 63 64 sk_sp<SkShader> compositeShader( 65 SkShader::MakeComposeShader(hardwareShader, gradientShader, SkBlendMode::kDstATop)); 66 67 SkPaint paint; 68 paint.setShader(std::move(compositeShader)); 69 canvas.drawRoundRect(0, 0, 400, 200, 10.0f, 10.0f, paint); 70 } 71 72 void doFrame(int frameNr) override {} 73 74 sk_sp<SkShader> createBitmapShader(Bitmap& bitmap) { 75 sk_sp<SkColorFilter> colorFilter; 76 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter); 77 return image->makeShader(SkShader::TileMode::kClamp_TileMode, 78 SkShader::TileMode::kClamp_TileMode); 79 } 80}; 81