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 #ifndef _UI_SPRITE_ICON_H
18 #define _UI_SPRITE_ICON_H
19 
20 #include <SkBitmap.h>
21 #include <gui/Surface.h>
22 
23 namespace android {
24 
25 /*
26  * Icon that a sprite displays, including its hotspot.
27  */
28 struct SpriteIcon {
SpriteIconSpriteIcon29     inline SpriteIcon() : style(0), hotSpotX(0), hotSpotY(0) { }
SpriteIconSpriteIcon30     inline SpriteIcon(const SkBitmap& bitmap, int32_t style, float hotSpotX, float hotSpotY) :
31             bitmap(bitmap), style(style), hotSpotX(hotSpotX), hotSpotY(hotSpotY) { }
32 
33     SkBitmap bitmap;
34     int32_t style;
35     float hotSpotX;
36     float hotSpotY;
37 
copySpriteIcon38     inline SpriteIcon copy() const {
39         SkBitmap bitmapCopy;
40         if (bitmapCopy.tryAllocPixels(bitmap.info().makeColorType(kN32_SkColorType))) {
41             bitmap.readPixels(bitmapCopy.info(), bitmapCopy.getPixels(), bitmapCopy.rowBytes(),
42                     0, 0);
43         }
44         return SpriteIcon(bitmapCopy, style, hotSpotX, hotSpotY);
45     }
46 
resetSpriteIcon47     inline void reset() {
48         bitmap.reset();
49         style = 0;
50         hotSpotX = 0;
51         hotSpotY = 0;
52     }
53 
isValidSpriteIcon54     inline bool isValid() const { return !bitmap.isNull() && !bitmap.empty(); }
55 
widthSpriteIcon56     inline int32_t width() const { return bitmap.width(); }
heightSpriteIcon57     inline int32_t height() const { return bitmap.height(); }
58 
59     // Draw the bitmap onto the given surface. Returns true if it's successful, or false otherwise.
60     // Note it doesn't set any metadata to the surface.
61     bool draw(const sp<Surface> surface) const;
62 };
63 
64 
65 } // namespace android
66 
67 #endif // _UI_SPRITE_ICON_H
68