blob: 0bff589f485371d2a8b11468a60aa66c4c303070 [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.com3443fd82013-11-13 19:09:13 +000011#include "SkImageInfo.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.combba65d92012-07-25 14:42:15 +000026/**
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.org96447be2013-04-18 13:28:19 +000036class SK_API SkImage : public SkRefCnt {
reed@google.combba65d92012-07-25 14:42:15 +000037public:
reed@google.comfd875e82012-08-28 12:43:54 +000038 SK_DECLARE_INST_COUNT(SkImage)
robertphillips@google.coma22e2112012-08-16 14:58:06 +000039
reed@google.com2bd8b812013-11-01 13:46:54 +000040#ifdef SK_SUPPORT_LEGACY_COLORTYPE
41 typedef SkColorType ColorType;
skia.committer@gmail.com572a8652013-09-21 07:01:53 +000042
reed@google.com2bd8b812013-11-01 13:46:54 +000043 static const SkColorType kAlpha_8_ColorType = kAlpha_8_SkColorType;
reed@google.com9230ea22013-12-09 22:01:03 +000044 static const SkColorType kARGB_4444_ColorType = kARGB_4444_SkColorType;
reed@google.com2bd8b812013-11-01 13:46:54 +000045 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.comd28ba802013-09-20 19:33:52 +000050#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.com2bd8b812013-11-01 13:46:54 +000052#ifdef SK_SUPPORT_LEGACY_ALPHATYPE
robertphillips@google.com485e1c02013-09-22 21:57:03 +000053 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.com2bd8b812013-11-01 13:46:54 +000059#endif
reed@google.combba65d92012-07-25 14:42:15 +000060
reed@google.com2bd8b812013-11-01 13:46:54 +000061 typedef SkImageInfo Info;
reed@google.combba65d92012-07-25 14:42:15 +000062
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000063 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
64 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
reed@google.combba65d92012-07-25 14:42:15 +000065 static SkImage* NewEncodedData(SkData*);
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000066
67 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000068 * GrTexture is a more logical parameter for this factory, but its
commit-bot@chromium.org4d24b742013-07-25 23:29:40 +000069 * 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.combba65d92012-07-25 14:42:15 +000073
reed@google.comf6627b72012-07-27 18:02:50 +000074 int width() const { return fWidth; }
75 int height() const { return fHeight; }
76 uint32_t uniqueID() const { return fUniqueID; }
77
junov@chromium.orgda904742013-05-01 22:38:16 +000078 /**
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.combba65d92012-07-25 14:42:15 +000085 SkShader* newShaderClamp() const;
86 SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const;
reed@google.comf6627b72012-07-27 18:02:50 +000087
88 void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
89
reed@google.com4b0757b2013-05-20 16:33:41 +000090 /**
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000091 * 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.orgdfec28d2013-07-23 15:52:16 +000094 *
95 * See SkCanvas::drawBitmapRectToRect for similar behavior.
96 */
97 void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000098
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000099 /**
reed@google.com4b0757b2013-05-20 16:33:41 +0000100 * 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.com7def5e12013-05-31 14:00:10 +0000106 SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type,
107 int quality = 80) const;
reed@google.com4b0757b2013-05-20 16:33:41 +0000108
reed@google.comf6627b72012-07-27 18:02:50 +0000109protected:
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
119private:
120 const int fWidth;
121 const int fHeight;
122 const uint32_t fUniqueID;
123
124 static uint32_t NextUniqueID();
skia.committer@gmail.coma27096b2012-08-30 14:38:00 +0000125
reed@google.com7edfb492012-08-28 12:48:35 +0000126 typedef SkRefCnt INHERITED;
reed@google.combba65d92012-07-25 14:42:15 +0000127};
128
reed@google.combba65d92012-07-25 14:42:15 +0000129#endif