blob: d39b021db4c2146edf03fd84477dcc7e2d6de78e [file] [log] [blame]
bungeman@google.come8f05922012-08-16 16:13:40 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkDWriteFontFileStream_DEFINED
9#define SkDWriteFontFileStream_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
bungeman@google.come8f05922012-08-16 16:13:40 +000012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkStream.h"
14#include "include/private/SkMutex.h"
Ben Wagner6cb6a072019-08-12 18:30:27 -040015#include "src/utils/win/SkObjBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/utils/win/SkTScopedComPtr.h"
bungeman@google.come8f05922012-08-16 16:13:40 +000017
18#include <dwrite.h>
19
20/**
21 * An SkStream backed by an IDWriteFontFileStream.
22 * This allows Skia code to read an IDWriteFontFileStream.
23 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +000024class SkDWriteFontFileStream : public SkStreamMemory {
bungeman@google.come8f05922012-08-16 16:13:40 +000025public:
26 explicit SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream);
Chris Dalton1ef80942017-12-04 12:01:30 -070027 ~SkDWriteFontFileStream() override;
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
mtklein36352bf2015-03-25 18:17:31 -070029 size_t read(void* buffer, size_t size) override;
30 bool isAtEnd() const override;
31 bool rewind() override;
mtklein36352bf2015-03-25 18:17:31 -070032 size_t getPosition() const override;
33 bool seek(size_t position) override;
34 bool move(long offset) override;
mtklein36352bf2015-03-25 18:17:31 -070035 size_t getLength() const override;
36 const void* getMemoryBase() override;
bungeman@google.come8f05922012-08-16 16:13:40 +000037
Mike Reed98c5d922017-09-15 21:39:47 -040038 std::unique_ptr<SkDWriteFontFileStream> duplicate() const {
39 return std::unique_ptr<SkDWriteFontFileStream>(this->onDuplicate());
40 }
41 std::unique_ptr<SkDWriteFontFileStream> fork() const {
42 return std::unique_ptr<SkDWriteFontFileStream>(this->onFork());
43 }
Mike Reed98c5d922017-09-15 21:39:47 -040044
bungeman@google.come8f05922012-08-16 16:13:40 +000045private:
Mike Reed98c5d922017-09-15 21:39:47 -040046 SkDWriteFontFileStream* onDuplicate() const override;
47 SkDWriteFontFileStream* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -040048
bungeman@google.come8f05922012-08-16 16:13:40 +000049 SkTScopedComPtr<IDWriteFontFileStream> fFontFileStream;
50 size_t fPos;
51 const void* fLockedMemory;
52 void* fFragmentLock;
53};
54
55/**
56 * An IDWriteFontFileStream backed by an SkStream.
57 * This allows DirectWrite to read an SkStream.
58 */
59class SkDWriteFontFileStreamWrapper : public IDWriteFontFileStream {
60public:
61 // IUnknown methods
Ben Wagner6cb6a072019-08-12 18:30:27 -040062 SK_STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject) override;
63 SK_STDMETHODIMP_(ULONG) AddRef() override;
64 SK_STDMETHODIMP_(ULONG) Release() override;
bungeman@google.come8f05922012-08-16 16:13:40 +000065
66 // IDWriteFontFileStream methods
Ben Wagner6cb6a072019-08-12 18:30:27 -040067 SK_STDMETHODIMP ReadFileFragment(
bungeman@google.come8f05922012-08-16 16:13:40 +000068 void const** fragmentStart,
69 UINT64 fileOffset,
70 UINT64 fragmentSize,
Ben Wagner6cb6a072019-08-12 18:30:27 -040071 void** fragmentContext) override;
bungeman@google.come8f05922012-08-16 16:13:40 +000072
Ben Wagner6cb6a072019-08-12 18:30:27 -040073 SK_STDMETHODIMP_(void) ReleaseFileFragment(void* fragmentContext) override;
74 SK_STDMETHODIMP GetFileSize(UINT64* fileSize) override;
75 SK_STDMETHODIMP GetLastWriteTime(UINT64* lastWriteTime) override;
bungeman@google.come8f05922012-08-16 16:13:40 +000076
bungeman0babd3c2015-02-18 11:01:05 -080077 static HRESULT Create(SkStreamAsset* stream,
78 SkDWriteFontFileStreamWrapper** streamFontFileStream);
bungeman@google.come8f05922012-08-16 16:13:40 +000079
80private:
bungeman0babd3c2015-02-18 11:01:05 -080081 explicit SkDWriteFontFileStreamWrapper(SkStreamAsset* stream);
bungemand7f846b2014-06-19 11:26:59 -070082 virtual ~SkDWriteFontFileStreamWrapper() { }
bungeman@google.come8f05922012-08-16 16:13:40 +000083
84 ULONG fRefCount;
Ben Wagner145dbcd2016-11-03 14:40:50 -040085 std::unique_ptr<SkStreamAsset> fStream;
bungeman@google.come8f05922012-08-16 16:13:40 +000086 SkMutex fStreamMutex;
87};
88#endif