1b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips/*
2b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * Copyright 2016 Google Inc.
3b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips *
4b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * Use of this source code is governed by a BSD-style license that can be
5b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * found in the LICENSE file
6b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips */
7b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
8b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#ifndef SkSpecialImage_DEFINED
9b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#define SkSpecialImage_DEFINED
10b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
113b087f4010327b304242aaf27ef3689150dfc226robertphillips#include "SkNextID.h"
12b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#include "SkRefCnt.h"
13898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman#include "SkSurfaceProps.h"
14b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
15eed6b0e1d865a1f93143c09961debba0aca592cabrianosman#include "SkImageFilter.h" // for OutputProperties
16eed6b0e1d865a1f93143c09961debba0aca592cabrianosman#include "SkImageInfo.h"   // for SkAlphaType
1783c17fa56b23159166394cb3feb431ffafbbab48robertphillips
1883c17fa56b23159166394cb3feb431ffafbbab48robertphillipsclass GrContext;
193743013f755d23c215d852af7d829c3cd74f34a2Robert Phillipsclass GrSurfaceProxy;
20b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass GrTexture;
218bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillipsclass GrTextureProxy;
22b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkBitmap;
23b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkCanvas;
24b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkImage;
25b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsstruct SkImageInfo;
26b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkPaint;
2783c17fa56b23159166394cb3feb431ffafbbab48robertphillipsclass SkPixmap;
28b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkSpecialSurface;
29b4bd11e66519f545282f914c67b54bf17cecab84robertphillipsclass SkSurface;
30b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
313b087f4010327b304242aaf27ef3689150dfc226robertphillipsenum {
323b087f4010327b304242aaf27ef3689150dfc226robertphillips    kNeedNewImageUniqueID_SpecialImage = 0
333b087f4010327b304242aaf27ef3689150dfc226robertphillips};
343b087f4010327b304242aaf27ef3689150dfc226robertphillips
35b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips/**
36b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * This is a restricted form of SkImage solely intended for internal use. It
37b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * differs from SkImage in that:
38b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips *      - it can only be backed by raster or gpu (no generators)
392c6d2bfced6d20703d52ab14a598c76d926f52fbRobert Phillips *      - it can be backed by a GrTextureProxy larger than its nominal bounds
40b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips *      - it can't be drawn tiled
41b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips *      - it can't be drawn with MIPMAPs
42b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * It is similar to SkImage in that it abstracts how the pixels are stored/represented.
43b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips *
44b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips * Note: the contents of the backing storage outside of the subset rect are undefined.
45b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips */
46b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsclass SkSpecialImage : public SkRefCnt {
47b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipspublic:
4837bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips    typedef void* ReleaseContext;
4937bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips    typedef void(*RasterReleaseProc)(void* pixels, ReleaseContext);
5037bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips
51898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman    const SkSurfaceProps& props() const { return fProps; }
52898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman
53b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    int width() const { return fSubset.width(); }
54b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    int height() const { return fSubset.height(); }
55df7bb471e5455dece2784a970d9ae50d3ab0ca75robertphillips    const SkIRect& subset() const { return fSubset; }
56afbf71dd924c7bb46ccdac49e7408b4b088563ffbrianosman    SkColorSpace* getColorSpace() const;
57df7bb471e5455dece2784a970d9ae50d3ab0ca75robertphillips
583b087f4010327b304242aaf27ef3689150dfc226robertphillips    uint32_t uniqueID() const { return fUniqueID; }
5980e96088bc608d6935b24ff623b9025b38da576ebrianosman    virtual SkAlphaType alphaType() const = 0;
603b087f4010327b304242aaf27ef3689150dfc226robertphillips    virtual size_t getSize() const = 0;
61b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
62b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    /**
6383c17fa56b23159166394cb3feb431ffafbbab48robertphillips     *  Ensures that a special image is backed by a texture (when GrContext is non-null). If no
6483c17fa56b23159166394cb3feb431ffafbbab48robertphillips     *  transformation is required, the returned image may be the same as this special image.
6583c17fa56b23159166394cb3feb431ffafbbab48robertphillips     *  If this special image is from a different GrContext, this will fail.
6683c17fa56b23159166394cb3feb431ffafbbab48robertphillips     */
673e302275b324172c845627cbd00cee8a06571bafrobertphillips    sk_sp<SkSpecialImage> makeTextureImage(GrContext*);
6883c17fa56b23159166394cb3feb431ffafbbab48robertphillips
6983c17fa56b23159166394cb3feb431ffafbbab48robertphillips    /**
70b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips     *  Draw this SpecialImage into the canvas.
71b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips     */
72e8c34974c42b302d78c5cc8c7dcfb36956256baarobertphillips    void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const;
73b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
743e302275b324172c845627cbd00cee8a06571bafrobertphillips    static sk_sp<SkSpecialImage> MakeFromImage(const SkIRect& subset,
75898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman                                               sk_sp<SkImage>,
7661624f0c716b576706659750d87b6956f4c15722Brian Osman                                               SkColorSpace* dstColorSpace,
77898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman                                               const SkSurfaceProps* = nullptr);
783e302275b324172c845627cbd00cee8a06571bafrobertphillips    static sk_sp<SkSpecialImage> MakeFromRaster(const SkIRect& subset,
79898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman                                                const SkBitmap&,
80898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman                                                const SkSurfaceProps* = nullptr);
81c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips#if SK_SUPPORT_GPU
828bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips    static sk_sp<SkSpecialImage> MakeDeferredFromGpu(GrContext*,
838bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                     const SkIRect& subset,
848bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                     uint32_t uniqueID,
8563c67461ed07b5fca35182ac62657b2cb16afbb4Robert Phillips                                                     sk_sp<GrTextureProxy>,
868bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                     sk_sp<SkColorSpace>,
878bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                     const SkSurfaceProps* = nullptr,
888bc06d07f57ede17ccabfa38f1d7e31bbf311ab5Robert Phillips                                                     SkAlphaType at = kPremul_SkAlphaType);
89c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips#endif
90b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
91b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    /**
92b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     *  Create a new special surface with a backend that is compatible with this special image.
93b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips     */
94eed6b0e1d865a1f93143c09961debba0aca592cabrianosman    sk_sp<SkSpecialSurface> makeSurface(const SkImageFilter::OutputProperties& outProps,
95eed6b0e1d865a1f93143c09961debba0aca592cabrianosman                                        const SkISize& size,
96eed6b0e1d865a1f93143c09961debba0aca592cabrianosman                                        SkAlphaType at = kPremul_SkAlphaType) const;
97b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
98c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    /**
99b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     * Create a new surface with a backend that is compatible with this special image.
100b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     * TODO: switch this to makeSurface once we resolved the naming issue
101b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     */
102eed6b0e1d865a1f93143c09961debba0aca592cabrianosman    sk_sp<SkSurface> makeTightSurface(const SkImageFilter::OutputProperties& outProps,
103eed6b0e1d865a1f93143c09961debba0aca592cabrianosman                                      const SkISize& size,
104eed6b0e1d865a1f93143c09961debba0aca592cabrianosman                                      SkAlphaType at = kPremul_SkAlphaType) const;
105b4bd11e66519f545282f914c67b54bf17cecab84robertphillips
106b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    /**
107c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     * Extract a subset of this special image and return it as a special image.
108c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     * It may or may not point to the same backing memory.
109c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     */
11037bd7c3aca66697fff2db79c21771a0b3cbe3b4crobertphillips    sk_sp<SkSpecialImage> makeSubset(const SkIRect& subset) const;
111c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
112b4bd11e66519f545282f914c67b54bf17cecab84robertphillips    /**
113a5fdc974a996dca79be8388e61db68043001760bRobert Phillips     * Create an SkImage from the contents of this special image optionally extracting a subset.
114b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     * It may or may not point to the same backing memory.
115a5fdc974a996dca79be8388e61db68043001760bRobert Phillips     * Note: when no 'subset' parameter is specified the the entire SkSpecialImage will be
116a5fdc974a996dca79be8388e61db68043001760bRobert Phillips     * returned - including whatever extra padding may have resulted from a loose fit!
117a5fdc974a996dca79be8388e61db68043001760bRobert Phillips     * When the 'subset' parameter is specified the returned image will be tight even if that
118a5fdc974a996dca79be8388e61db68043001760bRobert Phillips     * entails a copy!
119b4bd11e66519f545282f914c67b54bf17cecab84robertphillips     */
120a5fdc974a996dca79be8388e61db68043001760bRobert Phillips    sk_sp<SkImage> asImage(const SkIRect* subset = nullptr) const;
121b4bd11e66519f545282f914c67b54bf17cecab84robertphillips
1224418dbac3386f26c8da62ab242be9c178961eb18robertphillips    // TODO: hide this when GrLayerHoister uses SkSpecialImages more fully (see skbug.com/5063)
1234418dbac3386f26c8da62ab242be9c178961eb18robertphillips    /**
124646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips     *  If the SpecialImage is backed by a gpu texture, return true.
125646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips     */
126646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    bool isTextureBacked() const;
127646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips
128646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    /**
129646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips     * Return the GrContext if the SkSpecialImage is GrTexture-backed
130646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips     */
131646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    GrContext* getContext() const;
132646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips
133c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips#if SK_SUPPORT_GPU
134646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    /**
1358e1c4e672553ecae2745168514240705f3516773Robert Phillips     *  Regardless of the underlying backing store, return the contents as a GrTextureProxy.
1364418dbac3386f26c8da62ab242be9c178961eb18robertphillips     *  The active portion of the texture can be retrieved via 'subset'.
1374418dbac3386f26c8da62ab242be9c178961eb18robertphillips     */
1388e1c4e672553ecae2745168514240705f3516773Robert Phillips    sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*) const;
139c91fd3447e1d3452d5e43e70e371896c80645b61robertphillips#endif
1404418dbac3386f26c8da62ab242be9c178961eb18robertphillips
141c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    // TODO: hide this whe the imagefilter all have a consistent draw path (see skbug.com/5063)
142c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips    /**
143646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips     *  Regardless of the underlying backing store, return the contents as an SkBitmap
144c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     *
145c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     *  The returned ImageInfo represents the backing memory. Use 'subset'
146c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     *  to get the active portion's dimensions.
147c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips     */
148646125114b42b24e5ada3c9f8fac53a85f9ad2a0robertphillips    bool getROPixels(SkBitmap*) const;
149c5035e70cc3fb290f95fd1c052c637aa0dbaf9earobertphillips
150b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsprotected:
1513e302275b324172c845627cbd00cee8a06571bafrobertphillips    SkSpecialImage(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps*);
1523b087f4010327b304242aaf27ef3689150dfc226robertphillips
153b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillipsprivate:
154898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman    const SkSurfaceProps fProps;
155898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman    const SkIRect        fSubset;
156898235c4864df66aa7f6d32bc2a8b8551040ce1ebrianosman    const uint32_t       fUniqueID;
1573b087f4010327b304242aaf27ef3689150dfc226robertphillips
158b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips    typedef SkRefCnt INHERITED;
159b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips};
160b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips
161b6c65e99956b867e24bd5bd68ae37673a9fd4b27robertphillips#endif
162