calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / vcl / inc / skia / salbmp.hxx
blob8a974f08566cf8ba4e603db949a68def0cb0b233
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
23 #include <salbmp.hxx>
24 #include <vcl/bitmap.hxx>
26 #include <boost/shared_ptr.hpp>
28 #include <skia/utils.hxx>
30 #include <SkImage.h>
32 class VCL_PLUGIN_PUBLIC SkiaSalBitmap final : public SalBitmap
34 public:
35 SkiaSalBitmap();
36 SkiaSalBitmap(const sk_sp<SkImage>& image);
37 virtual ~SkiaSalBitmap() override;
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;
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 #if defined MACOSX || defined IOS
68 virtual CGImageRef CreateWithMask(const SalBitmap& rMask, int nX, int nY, int nWidth,
69 int nHeight) const override;
70 virtual CGImageRef CreateColorMask(int nX, int nY, int nWidth, int nHeight,
71 Color nMaskColor) const override;
72 virtual CGImageRef CreateCroppedImage(int nX, int nY, int nWidth, int nHeight) const override;
73 #endif
75 const BitmapPalette& Palette() const { return mPalette; }
77 // True if GetSkShader() should be preferred to GetSkImage() (or the Alpha variants).
78 bool PreferSkShader() const;
80 // Direct image means direct access to the stored SkImage, without checking
81 // if its size is up to date. This should be used only in special cases with care.
82 using DirectImage = SkiaHelper::DirectImage;
83 // Returns the contents as SkImage (possibly GPU-backed).
84 const sk_sp<SkImage>& GetSkImage(DirectImage direct = DirectImage::No) const;
85 sk_sp<SkShader> GetSkShader(const SkSamplingOptions& samplingOptions,
86 DirectImage direct = DirectImage::No) const;
87 // Returns the contents as alpha SkImage (possibly GPU-backed)
88 const sk_sp<SkImage>& GetAlphaSkImage(DirectImage direct = DirectImage::No) const;
89 sk_sp<SkShader> GetAlphaSkShader(const SkSamplingOptions& samplingOptions,
90 DirectImage direct = DirectImage::No) const;
92 // Key for caching/hashing.
93 OString GetImageKey(DirectImage direct = DirectImage::No) const;
94 OString GetAlphaImageKey(DirectImage direct = DirectImage::No) const;
96 // Returns true if it is known that this bitmap can be ignored if it's to be used
97 // as an alpha bitmap. An optimization, not guaranteed to return true for all such cases.
98 bool IsFullyOpaqueAsAlpha() const;
99 // Alpha type best suitable for the content.
100 SkAlphaType alphaType() const;
102 // Tries to create direct GetAlphaSkImage() from direct GetSkImage().
103 void TryDirectConvertToAlphaNoScaling();
105 // Dump contents to a file for debugging.
106 void dump(const char* file) const;
108 // These are to be used only by unittests.
109 bool unittestHasBuffer() const { return mBuffer.get(); }
110 bool unittestHasImage() const { return mImage.get(); }
111 bool unittestHasAlphaImage() const { return mAlphaImage.get(); }
112 bool unittestHasEraseColor() const { return mEraseColorSet; }
113 bool unittestHasPendingScale() const { return mSize != mPixelsSize; }
114 const sal_uInt8* unittestGetBuffer() const { return mBuffer.get(); }
115 const SkImage* unittestGetImage() const { return mImage.get(); }
116 const SkImage* unittestGetAlphaImage() const { return mAlphaImage.get(); }
117 void unittestResetToImage() { ResetToSkImage(GetSkImage()); }
119 private:
120 // This should be called whenever the contents have (possibly) changed.
121 // It may reset some cached data such as the checksum.
122 void DataChanged();
123 // Reset the state to pixel data (resets cached images allocated in GetSkImage()/GetAlphaSkImage()).
124 void ResetToBuffer();
125 // Sets the data only as SkImage (will be converted as needed).
126 void ResetToSkImage(sk_sp<SkImage> image);
127 // Resets all data (buffer and images).
128 void ResetAllData();
129 // Call to ensure mBuffer has data (will convert from mImage if necessary).
130 void EnsureBitmapData();
131 void EnsureBitmapData() const { return const_cast<SkiaSalBitmap*>(this)->EnsureBitmapData(); }
132 // Like EnsureBitmapData(), but will also make any shared data unique.
133 // Call before changing the data.
134 void EnsureBitmapUniqueData();
135 // Allocate mBuffer (with uninitialized contents).
136 void CreateBitmapData();
137 // Should be called whenever mPixelsSize or mBitCount is set/changed.
138 bool ComputeScanlineSize();
139 // Resets information about pending scaling. To be called when mBuffer is resized or created.
140 void ResetPendingScaling();
141 // Sets bitmap to be erased on demand.
142 void EraseInternal(const Color& color);
143 // Sets pixels to the erase color.
144 void PerformErase();
145 // Try to find out if the content is completely black. Used for optimizations,
146 // not guaranteed to always return true for such bitmaps.
147 bool IsAllBlack() const;
148 void ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode, bool dontChangeToErase);
149 SkBitmap GetAsSkBitmap() const;
150 bool ConserveMemory() const;
151 void verify() const
152 #ifdef DBG_UTIL
154 #else
157 #endif
159 template <typename charT, typename traits>
160 friend std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& stream,
161 const SkiaSalBitmap* bitmap)
163 if (bitmap == nullptr)
164 return stream << "(null)";
165 // p - has (non-trivial) palette
166 // I/i - has SkImage (on GPU/CPU),
167 // A/a - has alpha SkImage (on GPU/CPU)
168 // E - has erase color
169 // B - has pixel buffer
170 // (wxh) - has pending scaling (after each item)
171 stream << static_cast<const void*>(bitmap) << " " << bitmap->GetSize() << "x"
172 << bitmap->GetBitCount();
173 if (bitmap->GetBitCount() <= 8 && !bitmap->Palette().IsGreyPalette8Bit())
174 stream << "p";
175 stream << "/";
176 if (bitmap->mImage)
178 stream << (bitmap->mImage->isTextureBacked() ? "I" : "i");
179 if (SkiaHelper::imageSize(bitmap->mImage) != bitmap->mSize)
180 stream << "(" << SkiaHelper::imageSize(bitmap->mImage) << ")";
182 if (bitmap->mAlphaImage)
184 stream << (bitmap->mAlphaImage->isTextureBacked() ? "A" : "a");
185 if (SkiaHelper::imageSize(bitmap->mAlphaImage) != bitmap->mSize)
186 stream << "(" << SkiaHelper::imageSize(bitmap->mAlphaImage) << ")";
188 if (bitmap->mEraseColorSet)
189 stream << "E" << bitmap->mEraseColor;
190 if (bitmap->mBuffer)
192 stream << "B";
193 if (bitmap->mSize != bitmap->mPixelsSize)
194 stream << "(" << bitmap->mPixelsSize << ")";
196 return stream;
199 BitmapPalette mPalette;
200 int mBitCount = 0; // bpp
201 Size mSize;
202 // The contents of the bitmap may be stored in several different ways:
203 // As mBuffer buffer, which normally stores pixels in the given format.
204 // As SkImage, as cached GPU-backed data, but sometimes also a result of some operation.
205 // There is no "master" storage that the other would be derived from. The usual
206 // mode of operation is that mBuffer holds the data, mImage is created
207 // on demand as GPU-backed cached data by calling GetSkImage(), and the cached mImage
208 // is reset by ResetCachedImage(). But sometimes only mImage will be set and in that case
209 // mBuffer must be filled from it on demand if necessary by EnsureBitmapData().
210 boost::shared_ptr<sal_uInt8[]> mBuffer;
211 int mScanlineSize; // size of one row in mBuffer (based on mPixelsSize)
212 sk_sp<SkImage> mImage; // possibly GPU-backed
213 sk_sp<SkImage> mAlphaImage; // cached contents as alpha image, possibly GPU-backed
214 // Actual scaling triggered by scale() is done on-demand. This is the size of the pixel
215 // data in mBuffer, if it differs from mSize, then there is a scaling operation pending.
216 Size mPixelsSize;
217 BmpScaleFlag mScaleQuality = BmpScaleFlag::BestQuality; // quality for on-demand scaling
218 // Erase() is delayed, just sets these two instead of filling the buffer.
219 bool mEraseColorSet = false;
220 Color mEraseColor;
221 int mReadAccessCount = 0; // number of read AcquireAccess() that have not been released
222 #ifdef DBG_UTIL
223 int mWriteAccessCount = 0; // number of write AcquireAccess() that have not been released
224 #endif
227 #endif // INCLUDED_VCL_INC_SKIA_SALBMP_H
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */