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