reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkImage_DEFINED |
| 9 | #define SkImage_DEFINED |
| 10 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 11 | #include "SkAlpha.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | #include "SkScalar.h" |
| 15 | |
| 16 | class SkData; |
| 17 | class SkCanvas; |
| 18 | class SkPaint; |
| 19 | class SkShader; |
reed@google.com | 5d4ba88 | 2012-07-31 15:45:27 +0000 | [diff] [blame] | 20 | class GrContext; |
robertphillips@google.com | 72ba668 | 2012-10-31 14:58:16 +0000 | [diff] [blame] | 21 | class GrTexture; |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 22 | |
| 23 | // need for TileMode |
| 24 | #include "SkShader.h" |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 25 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 26 | enum SkColorType { |
| 27 | kAlpha_8_SkColorType, |
| 28 | kRGB_565_SkColorType, |
| 29 | kRGBA_8888_SkColorType, |
| 30 | kBGRA_8888_SkColorType, |
skia.committer@gmail.com | f54ad6f | 2013-11-02 07:02:02 +0000 | [diff] [blame] | 31 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 32 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
| 33 | kPMColor_SkColorType = kBGRA_8888_SkColorType, |
| 34 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
| 35 | kPMColor_SkColorType = kRGBA_8888_SkColorType, |
| 36 | #else |
| 37 | #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" |
| 38 | #endif |
| 39 | |
| 40 | kLastEnum_SkColorType = kBGRA_8888_SkColorType |
| 41 | }; |
| 42 | |
| 43 | struct SkImageInfo { |
| 44 | int fWidth; |
| 45 | int fHeight; |
| 46 | SkColorType fColorType; |
| 47 | SkAlphaType fAlphaType; |
| 48 | }; |
| 49 | |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame^] | 50 | bool operator==(const SkImageInfo& lhs, const SkImageInfo& rhs); |
| 51 | bool operator!=(const SkImageInfo& lhs, const SkImageInfo& rhs); |
| 52 | |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 53 | /** |
| 54 | * SkImage is an abstraction for drawing a rectagle of pixels, though the |
| 55 | * particular type of image could be actually storing its data on the GPU, or |
| 56 | * as drawing commands (picture or PDF or otherwise), ready to be played back |
| 57 | * into another canvas. |
| 58 | * |
| 59 | * The content of SkImage is always immutable, though the actual storage may |
| 60 | * change, if for example that image can be re-created via encoded data or |
| 61 | * other means. |
| 62 | */ |
junov@chromium.org | 96447be | 2013-04-18 13:28:19 +0000 | [diff] [blame] | 63 | class SK_API SkImage : public SkRefCnt { |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 64 | public: |
reed@google.com | fd875e8 | 2012-08-28 12:43:54 +0000 | [diff] [blame] | 65 | SK_DECLARE_INST_COUNT(SkImage) |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 66 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 67 | #ifdef SK_SUPPORT_LEGACY_COLORTYPE |
| 68 | typedef SkColorType ColorType; |
skia.committer@gmail.com | 572a865 | 2013-09-21 07:01:53 +0000 | [diff] [blame] | 69 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 70 | static const SkColorType kAlpha_8_ColorType = kAlpha_8_SkColorType; |
| 71 | static const SkColorType kRGB_565_ColorType = kRGB_565_SkColorType; |
| 72 | static const SkColorType kRGBA_8888_ColorType = kRGBA_8888_SkColorType; |
| 73 | static const SkColorType kBGRA_8888_ColorType = kBGRA_8888_SkColorType; |
| 74 | static const SkColorType kPMColor_ColorType = kPMColor_SkColorType; |
| 75 | static const SkColorType kLastEnum_ColorType = kLastEnum_SkColorType; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 76 | #endif |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 77 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 78 | #ifdef SK_SUPPORT_LEGACY_ALPHATYPE |
robertphillips@google.com | 485e1c0 | 2013-09-22 21:57:03 +0000 | [diff] [blame] | 79 | typedef SkAlphaType AlphaType; |
| 80 | |
| 81 | static const SkAlphaType kIgnore_AlphaType = kIgnore_SkAlphaType; |
| 82 | static const SkAlphaType kOpaque_AlphaType = kOpaque_SkAlphaType; |
| 83 | static const SkAlphaType kPremul_AlphaType = kPremul_SkAlphaType; |
| 84 | static const SkAlphaType kUnpremul_AlphaType = kUnpremul_SkAlphaType; |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 85 | #endif |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 86 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 87 | typedef SkImageInfo Info; |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 88 | |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 89 | static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes); |
| 90 | static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 91 | static SkImage* NewEncodedData(SkData*); |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
skia.committer@gmail.com | 956b310 | 2013-07-26 07:00:58 +0000 | [diff] [blame] | 94 | * GrTexture is a more logical parameter for this factory, but its |
commit-bot@chromium.org | 4d24b74 | 2013-07-25 23:29:40 +0000 | [diff] [blame] | 95 | * interactions with scratch cache still has issues, so for now we take |
| 96 | * SkBitmap instead. This will be changed in the future. skbug.com/1449 |
| 97 | */ |
| 98 | static SkImage* NewTexture(const SkBitmap&); |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 99 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 100 | int width() const { return fWidth; } |
| 101 | int height() const { return fHeight; } |
| 102 | uint32_t uniqueID() const { return fUniqueID; } |
| 103 | |
junov@chromium.org | da90474 | 2013-05-01 22:38:16 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Return the GrTexture that stores the image pixels. Calling getTexture |
| 106 | * does not affect the reference count of the GrTexture object. |
| 107 | * Will return NULL if the image does not use a texture. |
| 108 | */ |
| 109 | GrTexture* getTexture(); |
| 110 | |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 111 | SkShader* newShaderClamp() const; |
| 112 | SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const; |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 113 | |
| 114 | void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); |
| 115 | |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 116 | /** |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 117 | * Draw the image, cropped to the src rect, to the dst rect of a canvas. |
| 118 | * If src is larger than the bounds of the image, the rest of the image is |
| 119 | * filled with transparent black pixels. |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 120 | * |
| 121 | * See SkCanvas::drawBitmapRectToRect for similar behavior. |
| 122 | */ |
| 123 | void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*); |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 124 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 125 | /** |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 126 | * Encode the image's pixels and return the result as a new SkData, which |
| 127 | * the caller must manage (i.e. call unref() when they are done). |
| 128 | * |
| 129 | * If the image type cannot be encoded, or the requested encoder type is |
| 130 | * not supported, this will return NULL. |
| 131 | */ |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 132 | SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type, |
| 133 | int quality = 80) const; |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 134 | |
reed@google.com | f6627b7 | 2012-07-27 18:02:50 +0000 | [diff] [blame] | 135 | protected: |
| 136 | SkImage(int width, int height) : |
| 137 | fWidth(width), |
| 138 | fHeight(height), |
| 139 | fUniqueID(NextUniqueID()) { |
| 140 | |
| 141 | SkASSERT(width >= 0); |
| 142 | SkASSERT(height >= 0); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const int fWidth; |
| 147 | const int fHeight; |
| 148 | const uint32_t fUniqueID; |
| 149 | |
| 150 | static uint32_t NextUniqueID(); |
skia.committer@gmail.com | a27096b | 2012-08-30 14:38:00 +0000 | [diff] [blame] | 151 | |
reed@google.com | 7edfb49 | 2012-08-28 12:48:35 +0000 | [diff] [blame] | 152 | typedef SkRefCnt INHERITED; |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
reed@google.com | bba65d9 | 2012-07-25 14:42:15 +0000 | [diff] [blame] | 155 | #endif |