xref: /aosp_15_r20/frameworks/base/libs/input/SpriteIcon.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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     LOG_ALWAYS_FATAL_IF(!isValid(), "Cannot draw SpriteIcon: not valid");
29 
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     graphics::Paint paint;
38     paint.setBlendMode(ABLEND_MODE_SRC);
39     if (drawNativeDropShadow) {
40         paint.setImageFilter(AIMAGE_FILTER_DROP_SHADOW_FOR_POINTER_ICON);
41     }
42 
43     graphics::Canvas canvas(outBuffer, (int32_t)surface->getBuffersDataSpace());
44     canvas.drawBitmap(bitmap, 0, 0, &paint);
45 
46     const int iconWidth = width();
47     const int iconHeight = height();
48 
49     if (outBuffer.width > iconWidth) {
50         paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
51         canvas.drawRect({iconWidth, 0, outBuffer.width, iconHeight}, paint);
52     }
53     if (outBuffer.height > iconHeight) {
54         paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
55         canvas.drawRect({0, iconHeight, outBuffer.width, outBuffer.height}, 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
66