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 <android/graphics/bitmap.h>
20 #include <android/graphics/canvas.h>
21 #include <android/graphics/paint.h>
22 #include <android/native_window.h>
23 #include <log/log.h>
24
25 namespace android {
26
draw(sp<Surface> surface) const27 bool SpriteIcon::draw(sp<Surface> surface) const {
28 ANativeWindow_Buffer outBuffer;
29 status_t status = surface->lock(&outBuffer, NULL);
30 if (status) {
31 ALOGE("Error %d locking sprite surface before drawing.", status);
32 return false;
33 }
34
35 graphics::Paint paint;
36 paint.setBlendMode(ABLEND_MODE_SRC);
37 if (drawNativeDropShadow) {
38 paint.setImageFilter(AIMAGE_FILTER_DROP_SHADOW_FOR_POINTER_ICON);
39 }
40
41 graphics::Canvas canvas(outBuffer, (int32_t)surface->getBuffersDataSpace());
42 canvas.drawBitmap(bitmap, 0, 0, &paint);
43
44 const int iconWidth = width();
45 const int iconHeight = height();
46
47 if (outBuffer.width > iconWidth) {
48 paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
49 canvas.drawRect({iconWidth, 0, outBuffer.width, iconHeight}, paint);
50 }
51 if (outBuffer.height > iconHeight) {
52 paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
53 canvas.drawRect({0, iconHeight, outBuffer.width, outBuffer.height}, paint);
54 }
55
56 status = surface->unlockAndPost();
57 if (status) {
58 ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
59 }
60 return !status;
61 }
62
63 } // namespace android
64