• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38     graphics::Canvas canvas(outBuffer, (int32_t)surface->getBuffersDataSpace());
39     canvas.drawBitmap(bitmap, 0, 0, &paint);
40 
41     const int iconWidth = width();
42     const int iconHeight = height();
43 
44     if (outBuffer.width > iconWidth) {
45         paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
46         canvas.drawRect({iconWidth, 0, outBuffer.width, iconHeight}, paint);
47     }
48     if (outBuffer.height > iconHeight) {
49         paint.setBlendMode(ABLEND_MODE_CLEAR); // clear to transparent
50         canvas.drawRect({0, iconHeight, outBuffer.width, outBuffer.height}, paint);
51     }
52 
53     status = surface->unlockAndPost();
54     if (status) {
55         ALOGE("Error %d unlocking and posting sprite surface after drawing.", status);
56     }
57     return !status;
58 }
59 
60 } // namespace android
61