1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_INC_SKIA_SALBMP_H
21 #define INCLUDED_VCL_INC_SKIA_SALBMP_H
24 #include <vcl/bitmap.hxx>
26 #include <boost/shared_ptr.hpp>
28 #include <skia/utils.hxx>
32 class VCL_PLUGIN_PUBLIC SkiaSalBitmap final
: public SalBitmap
36 SkiaSalBitmap(const sk_sp
<SkImage
>& image
);
37 virtual ~SkiaSalBitmap() override
;
40 virtual bool Create(const Size
& rSize
, vcl::PixelFormat ePixelFormat
,
41 const BitmapPalette
& rPal
) override
;
42 virtual bool Create(const SalBitmap
& rSalBmp
) override
;
43 virtual bool Create(const SalBitmap
& rSalBmp
, SalGraphics
* pGraphics
) override
;
44 virtual bool Create(const SalBitmap
& rSalBmp
, vcl::PixelFormat eNewPixelFormat
) override
;
45 virtual bool Create(const css::uno::Reference
<css::rendering::XBitmapCanvas
>& rBitmapCanvas
,
46 Size
& rSize
, bool bMask
= false) override
;
48 virtual void Destroy() final override
;
50 virtual Size
GetSize() const override
;
51 virtual sal_uInt16
GetBitCount() const override
;
53 virtual BitmapBuffer
* AcquireBuffer(BitmapAccessMode nMode
) override
;
54 virtual void ReleaseBuffer(BitmapBuffer
* pBuffer
, BitmapAccessMode nMode
) override
;
56 virtual bool GetSystemData(BitmapSystemData
& rData
) override
;
58 virtual bool ScalingSupported() const override
;
59 virtual bool Scale(const double& rScaleX
, const double& rScaleY
,
60 BmpScaleFlag nScaleFlag
) override
;
61 virtual bool Replace(const Color
& rSearchColor
, const Color
& rReplaceColor
,
62 sal_uInt8 nTol
) override
;
63 virtual bool InterpretAs8Bit() override
;
64 virtual bool ConvertToGreyscale() override
;
65 virtual bool Erase(const Color
& color
) override
;
66 virtual bool AlphaBlendWith(const SalBitmap
& rSalBmp
) override
;
67 virtual bool Invert() override
;
68 #if defined MACOSX || defined IOS
69 virtual CGImageRef
CreateWithMask(const SalBitmap
& rMask
, int nX
, int nY
, int nWidth
,
70 int nHeight
) const override
;
71 virtual CGImageRef
CreateColorMask(int nX
, int nY
, int nWidth
, int nHeight
,
72 Color nMaskColor
) const override
;
73 virtual CGImageRef
CreateCroppedImage(int nX
, int nY
, int nWidth
, int nHeight
) const override
;
76 const BitmapPalette
& Palette() const { return mPalette
; }
78 // True if GetSkShader() should be preferred to GetSkImage() (or the Alpha variants).
79 bool PreferSkShader() const;
81 // Direct image means direct access to the stored SkImage, without checking
82 // if its size is up to date. This should be used only in special cases with care.
83 using DirectImage
= SkiaHelper::DirectImage
;
84 // Returns the contents as SkImage (possibly GPU-backed).
85 const sk_sp
<SkImage
>& GetSkImage(DirectImage direct
= DirectImage::No
) const;
86 sk_sp
<SkShader
> GetSkShader(const SkSamplingOptions
& samplingOptions
,
87 DirectImage direct
= DirectImage::No
) const;
88 // Returns the contents as alpha SkImage (possibly GPU-backed)
89 const sk_sp
<SkImage
>& GetAlphaSkImage(DirectImage direct
= DirectImage::No
) const;
90 sk_sp
<SkShader
> GetAlphaSkShader(const SkSamplingOptions
& samplingOptions
,
91 DirectImage direct
= DirectImage::No
) const;
93 // Key for caching/hashing.
94 OString
GetImageKey(DirectImage direct
= DirectImage::No
) const;
95 OString
GetAlphaImageKey(DirectImage direct
= DirectImage::No
) const;
97 // Returns true if it is known that this bitmap can be ignored if it's to be used
98 // as an alpha bitmap. An optimization, not guaranteed to return true for all such cases.
99 bool IsFullyOpaqueAsAlpha() const;
100 // Alpha type best suitable for the content.
101 SkAlphaType
alphaType() const;
103 // Tries to create direct GetAlphaSkImage() from direct GetSkImage().
104 void TryDirectConvertToAlphaNoScaling();
106 // Dump contents to a file for debugging.
107 void dump(const char* file
) const;
109 // These are to be used only by unittests.
110 bool unittestHasBuffer() const { return mBuffer
.get(); }
111 bool unittestHasImage() const { return mImage
.get(); }
112 bool unittestHasAlphaImage() const { return mAlphaImage
.get(); }
113 bool unittestHasEraseColor() const { return mEraseColorSet
; }
114 bool unittestHasPendingScale() const { return mSize
!= mPixelsSize
; }
115 const sal_uInt8
* unittestGetBuffer() const { return mBuffer
.get(); }
116 const SkImage
* unittestGetImage() const { return mImage
.get(); }
117 const SkImage
* unittestGetAlphaImage() const { return mAlphaImage
.get(); }
118 void unittestResetToImage() { ResetToSkImage(GetSkImage()); }
121 // This should be called whenever the contents have (possibly) changed.
122 // It may reset some cached data such as the checksum.
124 // Reset the state to pixel data (resets cached images allocated in GetSkImage()/GetAlphaSkImage()).
125 void ResetToBuffer();
126 // Sets the data only as SkImage (will be converted as needed).
127 void ResetToSkImage(sk_sp
<SkImage
> image
);
128 // Resets all data (buffer and images).
130 // Call to ensure mBuffer has data (will convert from mImage if necessary).
131 void EnsureBitmapData();
132 void EnsureBitmapData() const { return const_cast<SkiaSalBitmap
*>(this)->EnsureBitmapData(); }
133 // Like EnsureBitmapData(), but will also make any shared data unique.
134 // Call before changing the data.
135 void EnsureBitmapUniqueData();
136 // Allocate mBuffer (with uninitialized contents).
137 void CreateBitmapData();
138 // Should be called whenever mPixelsSize or mBitCount is set/changed.
139 bool ComputeScanlineSize();
140 // Resets information about pending scaling. To be called when mBuffer is resized or created.
141 void ResetPendingScaling();
142 // Sets bitmap to be erased on demand.
143 void EraseInternal(const Color
& color
);
144 // Sets pixels to the erase color.
146 // Try to find out if the content is completely black. Used for optimizations,
147 // not guaranteed to always return true for such bitmaps.
148 bool IsAllBlack() const;
149 void ReleaseBuffer(BitmapBuffer
* pBuffer
, BitmapAccessMode nMode
, bool dontChangeToErase
);
150 SkBitmap
GetAsSkBitmap() const;
151 bool ConserveMemory() const;
160 template <typename charT
, typename traits
>
161 friend std::basic_ostream
<charT
, traits
>& operator<<(std::basic_ostream
<charT
, traits
>& stream
,
162 const SkiaSalBitmap
* bitmap
)
164 if (bitmap
== nullptr)
165 return stream
<< "(null)";
166 // p - has (non-trivial) palette
167 // I/i - has SkImage (on GPU/CPU),
168 // A/a - has alpha SkImage (on GPU/CPU)
169 // E - has erase color
170 // B - has pixel buffer
171 // (wxh) - has pending scaling (after each item)
172 stream
<< static_cast<const void*>(bitmap
) << " " << bitmap
->GetSize() << "x"
173 << bitmap
->GetBitCount();
174 if (bitmap
->GetBitCount() <= 8 && !bitmap
->Palette().IsGreyPalette8Bit())
179 stream
<< (bitmap
->mImage
->isTextureBacked() ? "I" : "i");
180 if (SkiaHelper::imageSize(bitmap
->mImage
) != bitmap
->mSize
)
181 stream
<< "(" << SkiaHelper::imageSize(bitmap
->mImage
) << ")";
183 if (bitmap
->mAlphaImage
)
185 stream
<< (bitmap
->mAlphaImage
->isTextureBacked() ? "A" : "a");
186 if (SkiaHelper::imageSize(bitmap
->mAlphaImage
) != bitmap
->mSize
)
187 stream
<< "(" << SkiaHelper::imageSize(bitmap
->mAlphaImage
) << ")";
189 if (bitmap
->mEraseColorSet
)
190 stream
<< "E" << bitmap
->mEraseColor
;
194 if (bitmap
->mSize
!= bitmap
->mPixelsSize
)
195 stream
<< "(" << bitmap
->mPixelsSize
<< ")";
200 BitmapPalette mPalette
;
201 int mBitCount
= 0; // bpp
203 // The contents of the bitmap may be stored in several different ways:
204 // As mBuffer buffer, which normally stores pixels in the given format.
205 // As SkImage, as cached GPU-backed data, but sometimes also a result of some operation.
206 // There is no "master" storage that the other would be derived from. The usual
207 // mode of operation is that mBuffer holds the data, mImage is created
208 // on demand as GPU-backed cached data by calling GetSkImage(), and the cached mImage
209 // is reset by ResetCachedImage(). But sometimes only mImage will be set and in that case
210 // mBuffer must be filled from it on demand if necessary by EnsureBitmapData().
211 boost::shared_ptr
<sal_uInt8
[]> mBuffer
;
212 int mScanlineSize
; // size of one row in mBuffer (based on mPixelsSize)
213 sk_sp
<SkImage
> mImage
; // possibly GPU-backed
214 bool mImageImmutable
= false;
215 sk_sp
<SkImage
> mAlphaImage
; // cached contents as alpha image, possibly GPU-backed
216 // Actual scaling triggered by scale() is done on-demand. This is the size of the pixel
217 // data in mBuffer, if it differs from mSize, then there is a scaling operation pending.
219 BmpScaleFlag mScaleQuality
= BmpScaleFlag::BestQuality
; // quality for on-demand scaling
220 // Erase() is delayed, just sets these two instead of filling the buffer.
221 bool mEraseColorSet
= false;
223 int mReadAccessCount
= 0; // number of read AcquireAccess() that have not been released
225 int mWriteAccessCount
= 0; // number of write AcquireAccess() that have not been released
229 #endif // INCLUDED_VCL_INC_SKIA_SALBMP_H
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */