LibreOffice Module vcl (master) 1
skia/salbmp.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_VCL_INC_SKIA_SALBMP_H
21#define INCLUDED_VCL_INC_SKIA_SALBMP_H
22
23#include <salbmp.hxx>
24#include <vcl/bitmap.hxx>
25
26#include <boost/shared_ptr.hpp>
27
28#include <skia/utils.hxx>
29
30#include <SkImage.h>
31
33{
34public:
36 SkiaSalBitmap(const sk_sp<SkImage>& image);
37 virtual ~SkiaSalBitmap() override;
38
39 // SalBitmap methods
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;
47
48 virtual void Destroy() final override;
49
50 virtual Size GetSize() const override;
51 virtual sal_uInt16 GetBitCount() const override;
52
53 virtual BitmapBuffer* AcquireBuffer(BitmapAccessMode nMode) override;
54 virtual void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode) override;
55
56 virtual bool GetSystemData(BitmapSystemData& rData) override;
57
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;
74#endif
75
76 const BitmapPalette& Palette() const { return mPalette; }
77
78 // True if GetSkShader() should be preferred to GetSkImage() (or the Alpha variants).
79 bool PreferSkShader() const;
80
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.
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;
92
93 // Key for caching/hashing.
94 OString GetImageKey(DirectImage direct = DirectImage::No) const;
96
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.
100 // Alpha type best suitable for the content.
101 SkAlphaType alphaType() const;
102
103 // Tries to create direct GetAlphaSkImage() from direct GetSkImage().
105
106 // Dump contents to a file for debugging.
107 void dump(const char* file) const;
108
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()); }
119
120private:
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()).
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).
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.
136 // Allocate mBuffer (with uninitialized contents).
138 // Should be called whenever mPixelsSize or mBitCount is set/changed.
140 // Resets information about pending scaling. To be called when mBuffer is resized or created.
142 // Sets bitmap to be erased on demand.
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;
152 void verify() const
153#ifdef DBG_UTIL
154 ;
155#else
156 {
157 }
158#endif
159
160 template <typename charT, typename traits>
161 friend std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
162 const SkiaSalBitmap* bitmap)
163 {
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())
175 stream << "p";
176 stream << "/";
177 if (bitmap->mImage)
178 {
179 stream << (bitmap->mImage->isTextureBacked() ? "I" : "i");
180 if (SkiaHelper::imageSize(bitmap->mImage) != bitmap->mSize)
181 stream << "(" << SkiaHelper::imageSize(bitmap->mImage) << ")";
182 }
183 if (bitmap->mAlphaImage)
184 {
185 stream << (bitmap->mAlphaImage->isTextureBacked() ? "A" : "a");
186 if (SkiaHelper::imageSize(bitmap->mAlphaImage) != bitmap->mSize)
187 stream << "(" << SkiaHelper::imageSize(bitmap->mAlphaImage) << ")";
188 }
189 if (bitmap->mEraseColorSet)
190 stream << "E" << bitmap->mEraseColor;
191 if (bitmap->mBuffer)
192 {
193 stream << "B";
194 if (bitmap->mSize != bitmap->mPixelsSize)
195 stream << "(" << bitmap->mPixelsSize << ")";
196 }
197 return stream;
198 }
199
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 sk_sp<SkImage> mAlphaImage; // cached contents as alpha image, possibly GPU-backed
215 // Actual scaling triggered by scale() is done on-demand. This is the size of the pixel
216 // data in mBuffer, if it differs from mSize, then there is a scaling operation pending.
218 BmpScaleFlag mScaleQuality = BmpScaleFlag::BestQuality; // quality for on-demand scaling
219 // Erase() is delayed, just sets these two instead of filling the buffer.
220 bool mEraseColorSet = false;
222 int mReadAccessCount = 0; // number of read AcquireAccess() that have not been released
223#ifdef DBG_UTIL
224 int mWriteAccessCount = 0; // number of write AcquireAccess() that have not been released
225#endif
226};
227
228#endif // INCLUDED_VCL_INC_SKIA_SALBMP_H
229
230/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
BitmapAccessMode
bool IsGreyPalette8Bit() const
Returns true if the palette is 8-bit grey palette.
const SkImage * unittestGetImage() const
boost::shared_ptr< sal_uInt8[]> mBuffer
void ResetToSkImage(sk_sp< SkImage > image)
virtual sal_uInt16 GetBitCount() const override
void unittestResetToImage()
const SkImage * unittestGetAlphaImage() const
const sk_sp< SkImage > & GetSkImage(DirectImage direct=DirectImage::No) const
bool PreferSkShader() const
bool ComputeScanlineSize()
void ResetToBuffer()
virtual ~SkiaSalBitmap() override
bool unittestHasBuffer() const
virtual Size GetSize() const override
sk_sp< SkImage > mImage
bool unittestHasAlphaImage() const
bool IsAllBlack() const
const sk_sp< SkImage > & GetAlphaSkImage(DirectImage direct=DirectImage::No) const
virtual bool Create(const css::uno::Reference< css::rendering::XBitmapCanvas > &rBitmapCanvas, Size &rSize, bool bMask=false) override
SkAlphaType alphaType() const
SkiaSalBitmap(const sk_sp< SkImage > &image)
void PerformErase()
sk_sp< SkShader > GetAlphaSkShader(const SkSamplingOptions &samplingOptions, DirectImage direct=DirectImage::No) const
virtual bool Create(const SalBitmap &rSalBmp) override
friend std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &stream, const SkiaSalBitmap *bitmap)
const BitmapPalette & Palette() const
Definition: skia/salbmp.hxx:76
SkBitmap GetAsSkBitmap() const
void dump(const char *file) const
void ResetPendingScaling()
void verify() const
void DataChanged()
void EnsureBitmapData()
bool unittestHasEraseColor() const
sk_sp< SkShader > GetSkShader(const SkSamplingOptions &samplingOptions, DirectImage direct=DirectImage::No) const
const sal_uInt8 * unittestGetBuffer() const
OString GetImageKey(DirectImage direct=DirectImage::No) const
bool IsFullyOpaqueAsAlpha() const
BitmapPalette mPalette
sk_sp< SkImage > mAlphaImage
bool unittestHasImage() const
void TryDirectConvertToAlphaNoScaling()
void EnsureBitmapUniqueData()
void ResetAllData()
void EraseInternal(const Color &color)
void CreateBitmapData()
virtual bool Create(const Size &rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette &rPal) override
OString GetAlphaImageKey(DirectImage direct=DirectImage::No) const
virtual bool Create(const SalBitmap &rSalBmp, SalGraphics *pGraphics) override
virtual bool Create(const SalBitmap &rSalBmp, vcl::PixelFormat eNewPixelFormat) override
void ReleaseBuffer(BitmapBuffer *pBuffer, BitmapAccessMode nMode, bool dontChangeToErase)
virtual void Destroy() final override
void EnsureBitmapData() const
bool ConserveMemory() const
bool unittestHasPendingScale() const
#define VCL_PLUGIN_PUBLIC
Definition: dllapi.h:40
Reference< XOutputStream > stream
Size imageSize(const sk_sp< SkImage > &image)
Definition: utils.hxx:84
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:20
unsigned char sal_uInt8