summaryrefslogtreecommitdiffstats
path: root/image/SurfacePipe.cpp
blob: 2e4a86daefd1f25452bdb8e73a491f43bc73854b (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "SurfacePipe.h"

#include <algorithm>  // for min

#include "Decoder.h"

namespace mozilla {
namespace image {

using namespace gfx;

using std::min;

Maybe<SurfaceInvalidRect> AbstractSurfaceSink::TakeInvalidRect() {
  if (mInvalidRect.IsEmpty()) {
    return Nothing();
  }

  SurfaceInvalidRect invalidRect;
  invalidRect.mInputSpaceRect = invalidRect.mOutputSpaceRect = mInvalidRect;

  // Forget about the invalid rect we're returning.
  mInvalidRect = OrientedIntRect();

  return Some(invalidRect);
}

uint8_t* AbstractSurfaceSink::DoResetToFirstRow() {
  mRow = 0;
  return GetRowPointer();
}

uint8_t* SurfaceSink::DoAdvanceRowFromBuffer(const uint8_t* aInputRow) {
  CopyInputRow(aInputRow);
  return DoAdvanceRow();
}

uint8_t* SurfaceSink::DoAdvanceRow() {
  if (mRow >= uint32_t(InputSize().height)) {
    return nullptr;
  }

  // If we're vertically flipping the output, we need to flip the invalid rect.
  // Since we're dealing with an axis-aligned rect, only the y coordinate needs
  // to change.
  int32_t invalidY = mFlipVertically ? InputSize().height - (mRow + 1) : mRow;
  mInvalidRect.UnionRect(mInvalidRect,
                         OrientedIntRect(0, invalidY, InputSize().width, 1));

  mRow = min(uint32_t(InputSize().height), mRow + 1);

  return mRow < uint32_t(InputSize().height) ? GetRowPointer() : nullptr;
}

nsresult SurfaceSink::Configure(const SurfaceConfig& aConfig) {
  IntSize surfaceSize = aConfig.mOutputSize;

  // Allocate the frame.
  // XXX(seth): Once every Decoder subclass uses SurfacePipe, we probably want
  // to allocate the frame directly here and get rid of Decoder::AllocateFrame
  // altogether.
  nsresult rv = aConfig.mDecoder->AllocateFrame(surfaceSize, aConfig.mFormat,
                                                aConfig.mAnimParams);
  if (NS_FAILED(rv)) {
    return rv;
  }

  mImageData = aConfig.mDecoder->mImageData;
  mImageDataLength = aConfig.mDecoder->mImageDataLength;
  mFlipVertically = aConfig.mFlipVertically;

  MOZ_ASSERT(mImageData);
  MOZ_ASSERT(uint64_t(mImageDataLength) == uint64_t(surfaceSize.width) *
                                               uint64_t(surfaceSize.height) *
                                               sizeof(uint32_t));

  ConfigureFilter(surfaceSize, sizeof(uint32_t));
  return NS_OK;
}

uint8_t* SurfaceSink::GetRowPointer() const {
  // If we're flipping vertically, reverse the order in which we traverse the
  // rows.
  uint32_t row = mFlipVertically ? InputSize().height - (mRow + 1) : mRow;

  uint8_t* rowPtr = mImageData + row * InputSize().width * sizeof(uint32_t);

  MOZ_ASSERT(rowPtr >= mImageData);
  MOZ_ASSERT(rowPtr < mImageData + mImageDataLength);
  MOZ_ASSERT(rowPtr + InputSize().width * sizeof(uint32_t) <=
             mImageData + mImageDataLength);

  return rowPtr;
}

uint8_t* ReorientSurfaceSink::DoAdvanceRowFromBuffer(const uint8_t* aInputRow) {
  if (mRow >= uint32_t(InputSize().height)) {
    return nullptr;
  }

  IntRect dirty = mReorientFn(aInputRow, mRow, mImageData, mSurfaceSize,
                              mSurfaceSize.width * sizeof(uint32_t));
  auto orientedDirty = OrientedIntRect::FromUnknownRect(dirty);
  mInvalidRect.UnionRect(mInvalidRect, orientedDirty);

  mRow = min(uint32_t(InputSize().height), mRow + 1);

  return mRow < uint32_t(InputSize().height) ? GetRowPointer() : nullptr;
}

uint8_t* ReorientSurfaceSink::DoAdvanceRow() {
  return DoAdvanceRowFromBuffer(mBuffer.get());
}

nsresult ReorientSurfaceSink::Configure(const ReorientSurfaceConfig& aConfig) {
  mSurfaceSize = aConfig.mOutputSize.ToUnknownSize();

  // Allocate the frame.
  // XXX(seth): Once every Decoder subclass uses SurfacePipe, we probably want
  // to allocate the frame directly here and get rid of Decoder::AllocateFrame
  // altogether.
  nsresult rv =
      aConfig.mDecoder->AllocateFrame(mSurfaceSize, aConfig.mFormat, Nothing());
  if (NS_FAILED(rv)) {
    return rv;
  }

  // The filters above us need the unoriented size as the input.
  auto inputSize =
      aConfig.mOrientation.ToUnoriented(aConfig.mOutputSize).ToUnknownSize();
  mBuffer.reset(new (fallible) uint8_t[inputSize.width * sizeof(uint32_t)]);
  if (MOZ_UNLIKELY(!mBuffer)) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  memset(mBuffer.get(), 0xFF, inputSize.width * sizeof(uint32_t));

  mReorientFn = ReorientRow(aConfig.mOrientation);
  MOZ_ASSERT(mReorientFn);

  mImageData = aConfig.mDecoder->mImageData;
  mImageDataLength = aConfig.mDecoder->mImageDataLength;

  MOZ_ASSERT(mImageData);
  MOZ_ASSERT(uint64_t(mImageDataLength) == uint64_t(mSurfaceSize.width) *
                                               uint64_t(mSurfaceSize.height) *
                                               sizeof(uint32_t));

  ConfigureFilter(inputSize, sizeof(uint32_t));
  return NS_OK;
}

uint8_t* ReorientSurfaceSink::GetRowPointer() const { return mBuffer.get(); }

}  // namespace image
}  // namespace mozilla