Garfield Tan | 58c44f3 | 2020-05-28 14:31:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 "SpriteIcon.h" |
| 18 | |
| 19 | #include <SkBitmap.h> |
| 20 | #include <SkCanvas.h> |
| 21 | #include <SkColor.h> |
| 22 | #include <SkPaint.h> |
| 23 | |
| 24 | #include <android/native_window.h> |
| 25 | #include <log/log.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | bool SpriteIcon::draw(sp<Surface> surface) const { |
| 30 | ANativeWindow_Buffer outBuffer; |
| 31 | status_t status = surface->lock(&outBuffer, NULL); |
| 32 | if (status) { |
| 33 | ALOGE("Error %d locking sprite surface before drawing.", status); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | SkBitmap surfaceBitmap; |
| 38 | ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); |
| 39 | surfaceBitmap.installPixels(SkImageInfo::MakeN32Premul(outBuffer.width, outBuffer.height), |
| 40 | outBuffer.bits, bpr); |
| 41 | |
| 42 | SkCanvas surfaceCanvas(surfaceBitmap); |
| 43 | |
| 44 | SkPaint paint; |
| 45 | paint.setBlendMode(SkBlendMode::kSrc); |
| 46 | surfaceCanvas.drawBitmap(bitmap, 0, 0, &paint); |
| 47 | |
| 48 | if (outBuffer.width > width()) { |
| 49 | paint.setColor(0); // transparent fill color |
| 50 | surfaceCanvas.drawRect(SkRect::MakeLTRB(width(), 0, outBuffer.width, height()), paint); |
| 51 | } |
| 52 | if (outBuffer.height > height()) { |
| 53 | paint.setColor(0); // transparent fill color |
| 54 | surfaceCanvas.drawRect(SkRect::MakeLTRB(0, height(), outBuffer.width, outBuffer.height), |
| 55 | paint); |
| 56 | } |
| 57 | |
| 58 | status = surface->unlockAndPost(); |
| 59 | if (status) { |
| 60 | ALOGE("Error %d unlocking and posting sprite surface after drawing.", status); |
| 61 | } |
| 62 | return !status; |
| 63 | } |
| 64 | |
| 65 | } // namespace android |