blob: ae8bbf5d50d5c9eeddd6ce0743449c92b1b2187b [file] [log] [blame]
reed@google.combba65d92012-07-25 14:42:15 +00001/*
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.comd28ba802013-09-20 19:33:52 +000011#include "SkAlpha.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000012#include "SkImageEncoder.h"
reed@google.comf6627b72012-07-27 18:02:50 +000013#include "SkRefCnt.h"
14#include "SkScalar.h"
15
16class SkData;
17class SkCanvas;
18class SkPaint;
19class SkShader;
reed@google.com5d4ba882012-07-31 15:45:27 +000020class GrContext;
robertphillips@google.com72ba6682012-10-31 14:58:16 +000021class GrTexture;
reed@google.comf6627b72012-07-27 18:02:50 +000022
23// need for TileMode
24#include "SkShader.h"
reed@google.combba65d92012-07-25 14:42:15 +000025
reed@google.com2bd8b812013-11-01 13:46:54 +000026enum SkColorType {
27 kAlpha_8_SkColorType,
28 kRGB_565_SkColorType,
29 kRGBA_8888_SkColorType,
30 kBGRA_8888_SkColorType,
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +000031
reed@google.com2bd8b812013-11-01 13:46:54 +000032#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
43struct SkImageInfo {
44 int fWidth;
45 int fHeight;
46 SkColorType fColorType;
47 SkAlphaType fAlphaType;
48};
49
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000050bool operator==(const SkImageInfo& lhs, const SkImageInfo& rhs);
51bool operator!=(const SkImageInfo& lhs, const SkImageInfo& rhs);
52
reed@google.combba65d92012-07-25 14:42:15 +000053/**
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.org96447be2013-04-18 13:28:19 +000063class SK_API SkImage : public SkRefCnt {
reed@google.combba65d92012-07-25 14:42:15 +000064public:
reed@google.comfd875e82012-08-28 12:43:54 +000065 SK_DECLARE_INST_COUNT(SkImage)
robertphillips@google.coma22e2112012-08-16 14:58:06 +000066
reed@google.com2bd8b812013-11-01 13:46:54 +000067#ifdef SK_SUPPORT_LEGACY_COLORTYPE
68 typedef SkColorType ColorType;
skia.committer@gmail.com572a8652013-09-21 07:01:53 +000069
reed@google.com2bd8b812013-11-01 13:46:54 +000070 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.comd28ba802013-09-20 19:33:52 +000076#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
reed@google.com2bd8b812013-11-01 13:46:54 +000078#ifdef SK_SUPPORT_LEGACY_ALPHATYPE
robertphillips@google.com485e1c02013-09-22 21:57:03 +000079 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.com2bd8b812013-11-01 13:46:54 +000085#endif
reed@google.combba65d92012-07-25 14:42:15 +000086
reed@google.com2bd8b812013-11-01 13:46:54 +000087 typedef SkImageInfo Info;
reed@google.combba65d92012-07-25 14:42:15 +000088
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000089 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
90 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
reed@google.combba65d92012-07-25 14:42:15 +000091 static SkImage* NewEncodedData(SkData*);
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000092
93 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000094 * GrTexture is a more logical parameter for this factory, but its
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000095 * 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.combba65d92012-07-25 14:42:15 +000099
reed@google.comf6627b72012-07-27 18:02:50 +0000100 int width() const { return fWidth; }
101 int height() const { return fHeight; }
102 uint32_t uniqueID() const { return fUniqueID; }
103
junov@chromium.orgda904742013-05-01 22:38:16 +0000104 /**
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.combba65d92012-07-25 14:42:15 +0000111 SkShader* newShaderClamp() const;
112 SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const;
reed@google.comf6627b72012-07-27 18:02:50 +0000113
114 void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
115
reed@google.com4b0757b2013-05-20 16:33:41 +0000116 /**
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000117 * 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.orgdfec28d2013-07-23 15:52:16 +0000120 *
121 * See SkCanvas::drawBitmapRectToRect for similar behavior.
122 */
123 void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000124
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000125 /**
reed@google.com4b0757b2013-05-20 16:33:41 +0000126 * 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.com7def5e12013-05-31 14:00:10 +0000132 SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type,
133 int quality = 80) const;
reed@google.com4b0757b2013-05-20 16:33:41 +0000134
reed@google.comf6627b72012-07-27 18:02:50 +0000135protected:
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
145private:
146 const int fWidth;
147 const int fHeight;
148 const uint32_t fUniqueID;
149
150 static uint32_t NextUniqueID();
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000151
reed@google.com7edfb492012-08-28 12:48:35 +0000152 typedef SkRefCnt INHERITED;
reed@google.combba65d92012-07-25 14:42:15 +0000153};
154
reed@google.combba65d92012-07-25 14:42:15 +0000155#endif