forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRectTextureImage.mm
184 lines (159 loc) · 5.82 KB
/
RectTextureImage.mm
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
/* -*- Mode: objc; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "RectTextureImage.h"
#include "gfxUtils.h"
#include "GLContextCGL.h"
#include "mozilla/layers/GLManager.h"
#include "mozilla/gfx/MacIOSurface.h"
#include "OGLShaderProgram.h"
#include "ScopedGLHelpers.h"
namespace mozilla {
namespace widget {
RectTextureImage::RectTextureImage()
: mGLContext(nullptr)
, mTexture(0)
, mInUpdate(false)
{
}
RectTextureImage::~RectTextureImage()
{
DeleteTexture();
}
already_AddRefed<gfx::DrawTarget>
RectTextureImage::BeginUpdate(const LayoutDeviceIntSize& aNewSize,
const LayoutDeviceIntRegion& aDirtyRegion)
{
MOZ_ASSERT(!mInUpdate, "Beginning update during update!");
mUpdateRegion = aDirtyRegion;
bool needRecreate = false;
if (aNewSize != mBufferSize) {
mBufferSize = aNewSize;
mUpdateRegion =
LayoutDeviceIntRect(LayoutDeviceIntPoint(0, 0), aNewSize);
needRecreate = true;
}
if (mUpdateRegion.IsEmpty()) {
return nullptr;
}
if (!mIOSurface || needRecreate) {
DeleteTexture();
mIOSurface = MacIOSurface::CreateIOSurface(mBufferSize.width,
mBufferSize.height);
if (!mIOSurface) {
return nullptr;
}
}
mInUpdate = true;
mIOSurface->Lock(false);
unsigned char* ioData = (unsigned char*)mIOSurface->GetBaseAddress();
gfx::IntSize size(mBufferSize.width, mBufferSize.height);
int32_t stride = mIOSurface->GetBytesPerRow();
gfx::SurfaceFormat format = gfx::SurfaceFormat::B8G8R8A8;
RefPtr<gfx::DrawTarget> drawTarget =
gfx::Factory::CreateDrawTargetForData(gfx::BackendType::SKIA,
ioData, size,
stride, format);
return drawTarget.forget();
}
void
RectTextureImage::EndUpdate()
{
MOZ_ASSERT(mInUpdate, "Ending update while not in update");
mIOSurface->Unlock(false);
mInUpdate = false;
}
void
RectTextureImage::UpdateFromCGContext(const LayoutDeviceIntSize& aNewSize,
const LayoutDeviceIntRegion& aDirtyRegion,
CGContextRef aCGContext)
{
gfx::IntSize size = gfx::IntSize(CGBitmapContextGetWidth(aCGContext),
CGBitmapContextGetHeight(aCGContext));
RefPtr<gfx::DrawTarget> dt = BeginUpdate(aNewSize, aDirtyRegion);
if (dt) {
gfx::Rect rect(0, 0, size.width, size.height);
gfxUtils::ClipToRegion(dt, GetUpdateRegion().ToUnknownRegion());
RefPtr<gfx::SourceSurface> sourceSurface =
dt->CreateSourceSurfaceFromData(static_cast<uint8_t *>(CGBitmapContextGetData(aCGContext)),
size,
CGBitmapContextGetBytesPerRow(aCGContext),
gfx::SurfaceFormat::B8G8R8A8);
dt->DrawSurface(sourceSurface, rect, rect,
gfx::DrawSurfaceOptions(),
gfx::DrawOptions(1.0, gfx::CompositionOp::OP_SOURCE));
dt->PopClip();
EndUpdate();
}
}
void
RectTextureImage::Draw(layers::GLManager* aManager,
const LayoutDeviceIntPoint& aLocation,
const gfx::Matrix4x4& aTransform)
{
gl::GLContext* gl = aManager->gl();
bool bound = BindIOSurfaceToTexture(gl);
if (!bound) {
return;
}
layers::ShaderProgramOGL* program =
aManager->GetProgram(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
gfx::SurfaceFormat::R8G8B8A8);
gl->fActiveTexture(LOCAL_GL_TEXTURE0);
gl::ScopedBindTexture texture(gl, mTexture, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
aManager->ActivateProgram(program);
program->SetProjectionMatrix(aManager->GetProjMatrix());
program->SetLayerTransform(gfx::Matrix4x4(aTransform).PostTranslate(aLocation.x, aLocation.y, 0));
program->SetTextureTransform(gfx::Matrix4x4());
program->SetRenderOffset(nsIntPoint(0, 0));
program->SetTexCoordMultiplier(mBufferSize.width, mBufferSize.height);
program->SetTextureUnit(0);
aManager->BindAndDrawQuad(program,
gfx::Rect(0.0, 0.0, mBufferSize.width, mBufferSize.height),
gfx::Rect(0.0, 0.0, 1.0f, 1.0f));
}
void
RectTextureImage::DeleteTexture()
{
if (mTexture) {
MOZ_ASSERT(mGLContext);
mGLContext->MakeCurrent();
mGLContext->fDeleteTextures(1, &mTexture);
mTexture = 0;
}
}
bool
RectTextureImage::BindIOSurfaceToTexture(gl::GLContext* aGL)
{
if (!mIOSurface) {
// If our size is zero or MacIOSurface::CreateIOSurface failed for some
// other reason, there's nothing we can bind.
return false;
}
if (!mTexture) {
MOZ_ASSERT(aGL);
aGL->fGenTextures(1, &mTexture);
aGL->fActiveTexture(LOCAL_GL_TEXTURE0);
gl::ScopedBindTexture texture(aGL, mTexture, LOCAL_GL_TEXTURE_RECTANGLE_ARB);
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
LOCAL_GL_TEXTURE_MIN_FILTER,
LOCAL_GL_LINEAR);
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
LOCAL_GL_TEXTURE_MAG_FILTER,
LOCAL_GL_LINEAR);
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
LOCAL_GL_TEXTURE_WRAP_T,
LOCAL_GL_CLAMP_TO_EDGE);
aGL->fTexParameteri(LOCAL_GL_TEXTURE_RECTANGLE_ARB,
LOCAL_GL_TEXTURE_WRAP_S,
LOCAL_GL_CLAMP_TO_EDGE);
mIOSurface->CGLTexImageIOSurface2D(aGL,
gl::GLContextCGL::Cast(aGL)->GetCGLContext(),
0);
mGLContext = aGL;
}
return true;
}
} // namespace widget
} // namespace mozilla