Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkImage.h
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 
11 #include "SkFilterQuality.h"
12 #include "SkImageInfo.h"
13 #include "SkImageEncoder.h"
14 #include "SkRefCnt.h"
15 #include "SkScalar.h"
16 #include "SkShader.h"
17 
18 class SkData;
19 class SkCanvas;
20 class SkColorTable;
21 class SkImageGenerator;
22 class SkPaint;
23 class SkPicture;
24 class SkPixelSerializer;
25 class SkString;
26 class SkSurface;
27 class GrBackendTexture;
28 class GrContext;
30 class GrTexture;
31 
32 struct AHardwareBuffer;
33 
48 class SK_API SkImage : public SkRefCnt {
49 public:
50  typedef SkImageInfo Info;
51  typedef void* ReleaseContext;
52 
53  static sk_sp<SkImage> MakeRasterCopy(const SkPixmap&);
54  static sk_sp<SkImage> MakeRasterData(const Info&, sk_sp<SkData> pixels, size_t rowBytes);
55 
56  typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext);
57 
65  static sk_sp<SkImage> MakeFromRaster(const SkPixmap&, RasterReleaseProc, ReleaseContext);
66 
71  static sk_sp<SkImage> MakeFromBitmap(const SkBitmap&);
72 
79  static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator>,
80  const SkIRect* subset = nullptr);
81 
88  static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr);
89 
100  return MakeFromTexture(ctx, desc, kPremul_SkAlphaType, nullptr, nullptr, nullptr);
101  }
102 
103  static sk_sp<SkImage> MakeFromTexture(GrContext* ctx, const GrBackendTextureDesc& de,
104  SkAlphaType at) {
105  return MakeFromTexture(ctx, de, at, nullptr, nullptr, nullptr);
106  }
107 
108  typedef void (*TextureReleaseProc)(ReleaseContext);
109 
121  SkAlphaType at, TextureReleaseProc trp,
122  ReleaseContext rc) {
123  return MakeFromTexture(ctx, desc, at, nullptr, trp, rc);
124  }
125 
136  static sk_sp<SkImage> MakeFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType,
137  sk_sp<SkColorSpace>, TextureReleaseProc, ReleaseContext);
138 
146  const GrBackendTexture& tex, GrSurfaceOrigin origin,
147  SkAlphaType at, sk_sp<SkColorSpace> cs) {
148  return MakeFromTexture(ctx, tex, origin, at, cs, nullptr, nullptr);
149  }
150 
158  static sk_sp<SkImage> MakeFromTexture(GrContext*,
159  const GrBackendTexture&, GrSurfaceOrigin origin,
160  SkAlphaType, sk_sp<SkColorSpace>,
161  TextureReleaseProc, ReleaseContext);
162 
176  static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext*, sk_sp<SkData>, bool buildMips,
177  SkColorSpace* dstColorSpace);
178 
188  static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&,
189  SkAlphaType = kPremul_SkAlphaType,
190  sk_sp<SkColorSpace> = nullptr);
191 
198  static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext*,
199  const GrBackendTexture&, GrSurfaceOrigin,
200  SkAlphaType = kPremul_SkAlphaType,
201  sk_sp<SkColorSpace> = nullptr);
202 
208  static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext*, SkYUVColorSpace,
209  const GrBackendObject yuvTextureHandles[3],
210  const SkISize yuvSizes[3],
211  GrSurfaceOrigin,
212  sk_sp<SkColorSpace> = nullptr);
213 
219  static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext*, SkYUVColorSpace,
220  const GrBackendObject nv12TextureHandles[2],
221  const SkISize nv12Sizes[2], GrSurfaceOrigin,
222  sk_sp<SkColorSpace> = nullptr);
223 
224  enum class BitDepth {
225  kU8,
226  kF16,
227  };
228 
233  static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture>, const SkISize& dimensions,
234  const SkMatrix*, const SkPaint*, BitDepth,
236 
237 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
238 
242  static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer*,
243  SkAlphaType = kPremul_SkAlphaType,
244  sk_sp<SkColorSpace> = nullptr);
245 #endif
246 
248 
249  int width() const { return fWidth; }
250  int height() const { return fHeight; }
251  SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
252  SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
253  uint32_t uniqueID() const { return fUniqueID; }
254  SkAlphaType alphaType() const;
255 
263  SkColorSpace* colorSpace() const;
264  sk_sp<SkColorSpace> refColorSpace() const;
265 
269  bool isAlphaOnly() const;
270  bool isOpaque() const { return SkAlphaTypeIsOpaque(this->alphaType()); }
271 
273  const SkMatrix* localMatrix = nullptr) const;
277  sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const {
278  return this->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, localMatrix);
279  }
280 
288  bool peekPixels(SkPixmap* pixmap) const;
289 
290  // DEPRECATED - currently used by Canvas2DLayerBridge in Chromium.
291  GrTexture* getTexture() const;
292 
296  bool isTextureBacked() const;
297 
306  bool isValid(GrContext* context) const;
307 
315  GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
316  GrSurfaceOrigin* origin = nullptr) const;
317 
323  enum CachingHint {
324  kAllow_CachingHint,
325  kDisallow_CachingHint,
326  };
327 
346  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
347  int srcX, int srcY, CachingHint = kAllow_CachingHint) const;
348 
349  bool readPixels(const SkPixmap& dst, int srcX, int srcY,
350  CachingHint = kAllow_CachingHint) const;
351 
359  bool scalePixels(const SkPixmap& dst, SkFilterQuality, CachingHint = kAllow_CachingHint) const;
360 
371  SkData* encode(SkEncodedImageFormat, int quality) const;
372 
387  SkData* encode(SkPixelSerializer* = nullptr) const;
388 
398  SkData* refEncoded() const;
399 
400  const char* toString(SkString*) const;
401 
409  sk_sp<SkImage> makeSubset(const SkIRect& subset) const;
410 
417  sk_sp<SkImage> makeTextureImage(GrContext*, SkColorSpace* dstColorSpace) const;
418 
423  sk_sp<SkImage> makeNonTextureImage() const;
441  sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
442  const SkIRect& clipBounds, SkIRect* outSubset,
443  SkIPoint* offset) const;
444 
447  DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality,
448  int preScaleMipLevel)
449  : fMatrix(matrix), fQuality(quality), fPreScaleMipLevel(preScaleMipLevel) {}
450  SkMatrix fMatrix;
451  SkFilterQuality fQuality;
452  int fPreScaleMipLevel;
453  };
454 
484  size_t getDeferredTextureImageData(const GrContextThreadSafeProxy&,
486  int paramCnt,
487  void* buffer,
488  SkColorSpace* dstColorSpace = nullptr,
489  SkColorType dstColorType = kN32_SkColorType) const;
490 
496  static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext*, const void*, SkBudgeted);
497 
498  // Helper functions to convert to SkBitmap
499 
500  enum LegacyBitmapMode {
501  kRO_LegacyBitmapMode,
502  kRW_LegacyBitmapMode,
503  };
504 
514  bool asLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
515 
520  bool isLazyGenerated() const;
521 
535  sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
536  SkTransferFunctionBehavior premulBehavior) const;
537 
538 private:
539  SkImage(int width, int height, uint32_t uniqueID);
540  friend class SkImage_Base;
541 
542  static sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&,
543  const GrMipLevel* texels, int mipLevelCount,
544  SkBudgeted, SkDestinationSurfaceColorMode);
545 
546  const int fWidth;
547  const int fHeight;
548  const uint32_t fUniqueID;
549 
550  typedef SkRefCnt INHERITED;
551 };
552 
553 #endif
CachingHint
Hints to image calls where the system might cache computed intermediates (e.g.
Definition: SkImage.h:323
A Canvas encapsulates all of the state about drawing into a device (bitmap).
Definition: SkCanvas.h:59
Definition: GrTypes.h:679
Definition: SkColorSpace.h:59
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 8-bit bitmaps...
Definition: SkColorTable.h:25
static sk_sp< SkImage > MakeFromTexture(GrContext *ctx, const GrBackendTexture &tex, GrSurfaceOrigin origin, SkAlphaType at, sk_sp< SkColorSpace > cs)
Create a new image from the specified descriptor.
Definition: SkImage.h:145
Definition: SkImageGenerator.h:27
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
static sk_sp< SkImage > MakeFromTexture(GrContext *ctx, const GrBackendTextureDesc &desc)
Create a new image from the specified descriptor.
Definition: SkImage.h:99
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:28
SkSurface is responsible for managing the pixels that a canvas draws into.
Definition: SkSurface.h:32
Definition: SkRefCnt.h:125
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:45
Definition: GrBackendSurface.h:18
An SkPicture records drawing commands made to a canvas to be played back at a later time...
Definition: SkPicture.h:38
SkData holds an immutable data buffer.
Definition: SkData.h:22
static sk_sp< SkImage > MakeFromTexture(GrContext *ctx, const GrBackendTextureDesc &desc, SkAlphaType at, TextureReleaseProc trp, ReleaseContext rc)
Create a new image from the specified descriptor.
Definition: SkImage.h:120
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:37
Base class for image filters.
Definition: SkImageFilter.h:36
Definition: GrTypes.h:581
Definition: GrContext.h:47
Definition: SkSize.h:13
Interface for serializing pixels, e.g.
Definition: SkPixelSerializer.h:19
TileMode
Definition: SkShader.h:43
replicate the edge color if the shader draws outside of its original bounds
Definition: SkShader.h:47
sk_sp< SkShader > makeShader(const SkMatrix *localMatrix=nullptr) const
Helper version of makeShader() that specifies Clamp tilemode.
Definition: SkImage.h:277
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
Definition: GrTexture.h:19
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:48
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
Drawing params for which a deferred texture image data should be optimized.
Definition: SkImage.h:446
Light weight class for managing strings.
Definition: SkString.h:121
Can be used to perform actions related to the generating GrContext in a thread safe manner...
Definition: GrContext.h:361