| 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 "TestSceneBase.h" |
| 18 | |
| 19 | class ReadbackFromHardware; |
| 20 | |
| 21 | static TestScene::Registrar _SaveLayer(TestScene::Info{ |
| 22 | "readbackFromHBitmap", "Allocates hardware bitmap and readback data from it.", |
| 23 | TestScene::simpleCreateScene<ReadbackFromHardware>}); |
| 24 | |
| 25 | class ReadbackFromHardware : public TestScene { |
| 26 | public: |
| 27 | static sk_sp<Bitmap> createHardwareBitmap() { |
| 28 | SkBitmap skBitmap; |
| 29 | SkImageInfo info = SkImageInfo::Make(400, 400, kN32_SkColorType, kPremul_SkAlphaType); |
| 30 | skBitmap.allocPixels(info); |
| 31 | skBitmap.eraseColor(Color::Red_500); |
| 32 | SkCanvas canvas(skBitmap); |
| 33 | SkPaint paint; |
| 34 | paint.setColor(Color::Blue_500); |
| 35 | canvas.drawRect(SkRect::MakeXYWH(30, 30, 30, 150), paint); |
| 36 | canvas.drawRect(SkRect::MakeXYWH(30, 30, 100, 30), paint); |
| 37 | canvas.drawRect(SkRect::MakeXYWH(30, 100, 70, 30), paint); |
| 38 | return Bitmap::allocateHardwareBitmap(skBitmap); |
| 39 | } |
| 40 | |
| 41 | void createContent(int width, int height, Canvas& canvas) override { |
| 42 | canvas.drawColor(Color::White, SkBlendMode::kSrcOver); // background |
| 43 | |
| 44 | sk_sp<Bitmap> hardwareBitmap(createHardwareBitmap()); |
| 45 | |
| 46 | SkBitmap readback; |
| 47 | hardwareBitmap->getSkBitmap(&readback); |
| 48 | |
| 49 | SkBitmap canvasBitmap; |
| 50 | sk_sp<Bitmap> heapBitmap(TestUtils::createBitmap(hardwareBitmap->width(), |
| 51 | hardwareBitmap->height(), &canvasBitmap)); |
| 52 | |
| 53 | SkCanvas skCanvas(canvasBitmap); |
| 54 | skCanvas.drawImage(readback.asImage(), 0, 0); |
| 55 | canvas.drawBitmap(*heapBitmap, 0, 0, nullptr); |
| 56 | |
| 57 | canvas.drawBitmap(*hardwareBitmap, 0, 500, nullptr); |
| 58 | } |
| 59 | |
| 60 | void doFrame(int frameNr) override {} |
| 61 | }; |