summaryrefslogtreecommitdiff
path: root/vcl/skia/salbmp.cxx
blob: b066569e1a4570da37ce91de46209cba52e47076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <skia/salbmp.hxx>

#include <o3tl/safeint.hxx>
#include <tools/helpers.hxx>

#include <salgdi.hxx>

#include <SkCanvas.h>
#include <SkImage.h>
#include <SkPixelRef.h>

#ifdef DBG_UTIL
#include <fstream>
#define CANARY "skia-canary"
#endif

SkiaSalBitmap::SkiaSalBitmap() {}

SkiaSalBitmap::~SkiaSalBitmap() {}

static bool isValidBitCount(sal_uInt16 nBitCount)
{
    return (nBitCount == 1) || (nBitCount == 4) || (nBitCount == 8) || (nBitCount == 16)
           || (nBitCount == 24) || (nBitCount == 32);
}

SkiaSalBitmap::SkiaSalBitmap(const SkImage& image)
{
    if (Create(Size(image.width(), image.height()), 32, BitmapPalette()))
    {
        SkCanvas canvas(mBitmap);
        // TODO makeNonTextureImage() ?
        canvas.drawImage(&image, 0, 0);
    }
}

bool SkiaSalBitmap::Create(const Size& rSize, sal_uInt16 nBitCount, const BitmapPalette& rPal)
{
    Destroy();
    if (!isValidBitCount(nBitCount))
        return false;
    // Skia only supports 8bit gray, 16bit and 32bit formats (e.g. 24bpp is actually stored as 32bpp).
    // But some of our code accessing the bitmap assumes that when it asked for 24bpp, the format
    // really will be 24bpp (e.g. the png loader).
    // TODO what is the performance impact of handling 24bpp ourselves instead of in Skia?
    SkColorType colorType = kUnknown_SkColorType;
    switch (nBitCount)
    {
        case 8:
            if (rPal.IsGreyPalette()) // see GetAlphaSkBitmap()
                colorType = kGray_8_SkColorType;
            break;
        case 16:
            colorType = kRGB_565_SkColorType;
            break;
        case 32:
            colorType = kN32_SkColorType;
            break;
        default:
            break;
    }
    if (colorType != kUnknown_SkColorType)
    {
        if (!mBitmap.tryAllocPixels(
                SkImageInfo::Make(rSize.Width(), rSize.Height(), colorType, kPremul_SkAlphaType)))
        {
            return false;
        }
    }
    else
    {
        // Image formats not supported by Skia are stored in a buffer and converted as necessary.
        int bitScanlineWidth;
        if (o3tl::checked_multiply<int>(rSize.Width(), nBitCount, bitScanlineWidth))
        {
            SAL_WARN("vcl.gdi", "checked multiply failed");
            return false;
        }
        mScanlineSize = AlignedWidth4Bytes(bitScanlineWidth);
        sal_uInt8* buffer = nullptr;
        if (mScanlineSize != 0 && rSize.Height() != 0)
        {
            size_t allocate = mScanlineSize * rSize.Height();
#ifdef DBG_UTIL
            allocate += sizeof(CANARY);
#endif
            buffer = new sal_uInt8[allocate];
#if OSL_DEBUG_LEVEL > 0
            memcpy(buffer + allocate - sizeof(CANARY), CANARY, sizeof(CANARY));
#endif
        }
        mBuffer.reset(buffer);
    }
    mPalette = rPal;
    mBitCount = nBitCount;
    mSize = rSize;
    return true;
}

bool SkiaSalBitmap::Create(const SalBitmap& rSalBmp)
{
    return Create(rSalBmp, rSalBmp.GetBitCount());
}

bool SkiaSalBitmap::Create(const SalBitmap& rSalBmp, SalGraphics* pGraphics)
{
    return Create(rSalBmp, pGraphics ? pGraphics->GetBitCount() : rSalBmp.GetBitCount());
}

bool SkiaSalBitmap::Create(const SalBitmap& rSalBmp, sal_uInt16 nNewBitCount)
{
    const SkiaSalBitmap& src = static_cast<const SkiaSalBitmap&>(rSalBmp);
    if (nNewBitCount == src.GetBitCount())
    {
        mBitmap = src.mBitmap; // TODO unshare?
        mPalette = src.mPalette;
        mBitCount = src.mBitCount;
        mSize = src.mSize;
        if (src.mBuffer != nullptr)
        {
            sal_uInt32 allocate = src.mScanlineSize * src.mSize.Height();
#ifdef DBG_UTIL
            assert(memcmp(src.mBuffer.get() + allocate, CANARY, sizeof(CANARY)) == 0);
            allocate += sizeof(CANARY);
#endif
            sal_uInt8* newBuffer = new sal_uInt8[allocate];
            memcpy(newBuffer, src.mBuffer.get(), allocate);
            mBuffer.reset(newBuffer);
            mScanlineSize = src.mScanlineSize;
        }
        return true;
    }
    if (!Create(src.mSize, src.mBitCount, src.mPalette))
        return false;
    // TODO copy data
    abort();
    return true;
}

bool SkiaSalBitmap::Create(const css::uno::Reference<css::rendering::XBitmapCanvas>& rBitmapCanvas,
                           Size& rSize, bool bMask)
{
    (void)rBitmapCanvas;
    (void)rSize;
    (void)bMask;
    return false;
}

void SkiaSalBitmap::Destroy()
{
    mBitmap.reset();
    mBuffer.reset();
}

Size SkiaSalBitmap::GetSize() const { return mSize; }

sal_uInt16 SkiaSalBitmap::GetBitCount() const { return mBitCount; }

BitmapBuffer* SkiaSalBitmap::AcquireBuffer(BitmapAccessMode nMode)
{
    (void)nMode; // TODO
    if (mBitmap.drawsNothing() && !mBuffer)
        return nullptr;
    BitmapBuffer* buffer = new BitmapBuffer;
    buffer->mnWidth = mSize.Width();
    buffer->mnHeight = mSize.Height();
    buffer->mnBitCount = mBitCount;
    buffer->maPalette = mPalette;
    if (mBuffer)
    {
        buffer->mpBits = mBuffer.get();
        buffer->mnScanlineSize = mScanlineSize;
    }
    else
    {
        buffer->mpBits = static_cast<sal_uInt8*>(mBitmap.getPixels());
        buffer->mnScanlineSize = mBitmap.rowBytes();
    }
    switch (mBitCount)
    {
        case 1:
            buffer->mnFormat = ScanlineFormat::N1BitMsbPal;
            break;
        case 4:
            buffer->mnFormat = ScanlineFormat::N4BitMsnPal;
            break;
        case 8:
            buffer->mnFormat = ScanlineFormat::N8BitPal;
            break;
        case 16:
        {
            buffer->mnFormat = ScanlineFormat::N16BitTcMsbMask;
            ColorMaskElement aRedMask(0x0000f800);
            aRedMask.CalcMaskShift();
            ColorMaskElement aGreenMask(0x000007e0);
            aGreenMask.CalcMaskShift();
            ColorMaskElement aBlueMask(0x0000001f);
            aBlueMask.CalcMaskShift();
            buffer->maColorMask = ColorMask(aRedMask, aGreenMask, aBlueMask);
            break;
        }
        case 24:
        {
// Make the RGB/BGR format match the default Skia 32bpp format, to allow
// easy conversion later.
// Use a macro to hide an unreachable code warning.
#define GET_FORMAT                                                                                 \
    (kN32_SkColorType == kBGRA_8888_SkColorType ? ScanlineFormat::N24BitTcBgr                      \
                                                : ScanlineFormat::N24BitTcRgb)
            buffer->mnFormat = GET_FORMAT;
#undef GET_FORMAT
            break;
        }
        case 32:
            // TODO are these correct?
            buffer->mnFormat = mBitmap.colorType() == kRGBA_8888_SkColorType
                                   ? ScanlineFormat::N32BitTcRgba
                                   : ScanlineFormat::N32BitTcBgra;
            break;
        default:
            abort();
    }
    buffer->mnFormat |= ScanlineFormat::TopDown;
    return buffer;
}

void SkiaSalBitmap::ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode)
{
    if (nMode == BitmapAccessMode::Write) // TODO something more?
    {
        mPalette = pBuffer->maPalette;
        ResetCachedBitmap();
    }
    // Are there any more ground movements underneath us ?
    assert(pBuffer->mnWidth == mSize.Width());
    assert(pBuffer->mnHeight == mSize.Height());
    assert(pBuffer->mnBitCount == mBitCount);
    verify();
    delete pBuffer;
}

bool SkiaSalBitmap::GetSystemData(BitmapSystemData& rData)
{
    (void)rData;
    return false;
}

bool SkiaSalBitmap::ScalingSupported() const { return false; }

bool SkiaSalBitmap::Scale(const double& rScaleX, const double& rScaleY, BmpScaleFlag nScaleFlag)
{
    (void)rScaleX;
    (void)rScaleY;
    (void)nScaleFlag;
    return false;
}

bool SkiaSalBitmap::Replace(const Color& rSearchColor, const Color& rReplaceColor, sal_uInt8 nTol)
{
    (void)rSearchColor;
    (void)rReplaceColor;
    (void)nTol;
    return false;
}

bool SkiaSalBitmap::ConvertToGreyscale() { return false; }

const SkBitmap& SkiaSalBitmap::GetSkBitmap() const
{
    if (mBuffer && mBitmap.drawsNothing())
    {
        if (mBitCount == 24)
        {
            // Convert 24bpp RGB/BGR to 32bpp RGBA/BGRA.
            std::unique_ptr<sal_uInt8[]> data(new sal_uInt8[mSize.Height() * mSize.Width() * 4]);
            sal_uInt8* dest = data.get();
            for (int y = 0; y < mSize.Height(); ++y)
            {
                const sal_uInt8* src = mBuffer.get() + mScanlineSize * y;
                for (int x = 0; x < mSize.Width(); ++x)
                {
                    *dest++ = *src++;
                    *dest++ = *src++;
                    *dest++ = *src++;
                    *dest++ = 0xff;
                }
            }
            if (!const_cast<SkBitmap&>(mBitmap).installPixels(
                    SkImageInfo::MakeS32(mSize.Width(), mSize.Height(), kOpaque_SkAlphaType),
                    data.release(), mSize.Width() * 4,
                    [](void* addr, void*) { delete[] static_cast<sal_uInt8*>(addr); }, nullptr))
                abort();
        }
        else
        {
// Use a macro to hide an unreachable code warning.
#define GET_FORMAT                                                                                 \
    (kN32_SkColorType == kBGRA_8888_SkColorType ? BitConvert::BGRA : BitConvert::RGBA)
            std::unique_ptr<sal_uInt8[]> data
                = convertDataBitCount(mBuffer.get(), mSize.Width(), mSize.Height(), mBitCount,
                                      mScanlineSize, mPalette, GET_FORMAT);
#undef GET_FORMAT
            if (!const_cast<SkBitmap&>(mBitmap).installPixels(
                    SkImageInfo::MakeS32(mSize.Width(), mSize.Height(), kOpaque_SkAlphaType),
                    data.release(), mSize.Width() * 4,
                    [](void* addr, void*) { delete[] static_cast<sal_uInt8*>(addr); }, nullptr))
                abort();
        }
    }
    return mBitmap;
}

const SkBitmap& SkiaSalBitmap::GetAlphaSkBitmap() const
{
    assert(mBitCount <= 8);
    if (mAlphaBitmap.drawsNothing())
    {
        if (mBuffer)
        {
            assert(mBuffer.get());
            verify();
            std::unique_ptr<sal_uInt8[]> data
                = convertDataBitCount(mBuffer.get(), mSize.Width(), mSize.Height(), mBitCount,
                                      mScanlineSize, mPalette, BitConvert::A8);
            if (!const_cast<SkBitmap&>(mAlphaBitmap)
                     .installPixels(
                         SkImageInfo::MakeA8(mSize.Width(), mSize.Height()), data.release(),
                         mSize.Width(),
                         [](void* addr, void*) { delete[] static_cast<sal_uInt8*>(addr); },
                         nullptr))
                abort();
        }
        else
        {
            assert(mBitmap.colorType() == kGray_8_SkColorType);
            // Skia uses a bitmap as an alpha channel only if it's set as kAlpha_8_SkColorType.
            // But in SalBitmap::Create() it's not quite clear if the 8-bit image will be used
            // as a mask or as a real bitmap. So mBitmap is always kGray_8_SkColorType
            // and mAlphaBitmap is kAlpha_8_SkColorType that can be used as a mask.
            // Make mAlphaBitmap share mBitmap's data.
            const_cast<SkBitmap&>(mAlphaBitmap)
                .setInfo(mBitmap.info().makeColorType(kAlpha_8_SkColorType), mBitmap.rowBytes());
            const_cast<SkBitmap&>(mAlphaBitmap)
                .setPixelRef(sk_ref_sp(mBitmap.pixelRef()), mBitmap.pixelRefOrigin().x(),
                             mBitmap.pixelRefOrigin().y());
            return mAlphaBitmap;
        }
    }
    return mAlphaBitmap;
}

// Reset the cached bitmap allocated in GetSkBitmap().
void SkiaSalBitmap::ResetCachedBitmap()
{
    mAlphaBitmap.reset();
    if (mBuffer)
        mBitmap.reset();
}

#ifdef DBG_UTIL
void SkiaSalBitmap::dump(const char* file) const
{
    sk_sp<SkImage> image = SkImage::MakeFromBitmap(GetSkBitmap());
    sk_sp<SkData> data = image->encodeToData();
    std::ofstream ostream(file, std::ios::binary);
    ostream.write(static_cast<const char*>(data->data()), data->size());
}

void SkiaSalBitmap::verify() const
{
    if (!mBuffer)
        return;
    size_t canary = mScanlineSize * mSize.Height();
    assert(memcmp(mBuffer.get() + canary, CANARY, sizeof(CANARY)) == 0);
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */