blob: 9665a68b0e7738de354356dc81de2bd467977bb3 [file] [log] [blame]
Romain Guycf51a412013-04-08 19:40:31 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
Chris Craik96a5c4c2015-01-27 15:46:35 -080019#include "PixelBuffer.h"
Romain Guycf51a412013-04-08 19:40:31 -070020
Romain Guy9e6f3ac2013-06-20 16:31:35 -070021#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070022#include "Extensions.h"
Romain Guycf51a412013-04-08 19:40:31 -070023#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024#include "renderstate/RenderState.h"
25
26#include <utils/Log.h>
Romain Guycf51a412013-04-08 19:40:31 -070027
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// CPU pixel buffer
33///////////////////////////////////////////////////////////////////////////////
34
35class CpuPixelBuffer: public PixelBuffer {
36public:
37 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070038
Chris Craikd41c4d82015-01-05 15:51:13 -080039 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
40 void unmap() override;
Romain Guycf51a412013-04-08 19:40:31 -070041
Chris Craikd41c4d82015-01-05 15:51:13 -080042 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070043
Chris Craikd41c4d82015-01-05 15:51:13 -080044 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070045
46private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080047 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070048};
49
Chris Craik51d6a3d2014-12-22 17:16:56 -080050CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
51 : PixelBuffer(format, width, height)
52 , mBuffer(new uint8_t[width * height * formatSize(format)]) {
Romain Guycf51a412013-04-08 19:40:31 -070053}
54
55uint8_t* CpuPixelBuffer::map(AccessMode mode) {
56 if (mAccessMode == kAccessMode_None) {
57 mAccessMode = mode;
58 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080059 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070060}
61
62void CpuPixelBuffer::unmap() {
63 mAccessMode = kAccessMode_None;
64}
65
66uint8_t* CpuPixelBuffer::getMappedPointer() const {
Chris Craik51d6a3d2014-12-22 17:16:56 -080067 return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070068}
69
70void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
71 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
Chris Craik51d6a3d2014-12-22 17:16:56 -080072 mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070073}
74
75///////////////////////////////////////////////////////////////////////////////
76// GPU pixel buffer
77///////////////////////////////////////////////////////////////////////////////
78
79class GpuPixelBuffer: public PixelBuffer {
80public:
81 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
82 ~GpuPixelBuffer();
83
Chris Craikd41c4d82015-01-05 15:51:13 -080084 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
85 void unmap() override;
Romain Guycf51a412013-04-08 19:40:31 -070086
Chris Craikd41c4d82015-01-05 15:51:13 -080087 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070088
Chris Craikd41c4d82015-01-05 15:51:13 -080089 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070090
91private:
92 GLuint mBuffer;
93 uint8_t* mMappedPointer;
94 Caches& mCaches;
95};
96
Chris Craik96a5c4c2015-01-27 15:46:35 -080097GpuPixelBuffer::GpuPixelBuffer(GLenum format,
98 uint32_t width, uint32_t height)
Chris Craikd41c4d82015-01-05 15:51:13 -080099 : PixelBuffer(format, width, height)
100 , mMappedPointer(nullptr)
Chris Craik96a5c4c2015-01-27 15:46:35 -0800101 , mCaches(Caches::getInstance()){
Romain Guycf51a412013-04-08 19:40:31 -0700102 glGenBuffers(1, &mBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800103
Chris Craik44eb2c02015-01-29 09:45:09 -0800104 mCaches.pixelBufferState().bind(mBuffer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800105 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
Chris Craik44eb2c02015-01-29 09:45:09 -0800106 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700107}
108
109GpuPixelBuffer::~GpuPixelBuffer() {
110 glDeleteBuffers(1, &mBuffer);
111}
112
113uint8_t* GpuPixelBuffer::map(AccessMode mode) {
114 if (mAccessMode == kAccessMode_None) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800115 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700116 mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700117#if DEBUG_OPENGL
118 if (!mMappedPointer) {
119 GLenum status = GL_NO_ERROR;
120 while ((status = glGetError()) != GL_NO_ERROR) {
121 ALOGE("Could not map GPU pixel buffer: 0x%x", status);
122 }
123 }
124#endif
Romain Guycf51a412013-04-08 19:40:31 -0700125 mAccessMode = mode;
126 }
127
128 return mMappedPointer;
129}
130
131void GpuPixelBuffer::unmap() {
132 if (mAccessMode != kAccessMode_None) {
133 if (mMappedPointer) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800134 mCaches.pixelBufferState().bind(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700135 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
136 if (status == GL_FALSE) {
137 ALOGE("Corrupted GPU pixel buffer");
138 }
Romain Guycf51a412013-04-08 19:40:31 -0700139 }
140 mAccessMode = kAccessMode_None;
Chris Craikd41c4d82015-01-05 15:51:13 -0800141 mMappedPointer = nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700142 }
143}
144
145uint8_t* GpuPixelBuffer::getMappedPointer() const {
146 return mMappedPointer;
147}
148
149void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
150 // If the buffer is not mapped, unmap() will not bind it
Chris Craik44eb2c02015-01-29 09:45:09 -0800151 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700152 unmap();
153 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
Kévin PETIT73fc5582014-02-13 11:03:40 +0000154 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
Romain Guycf51a412013-04-08 19:40:31 -0700155}
156
157///////////////////////////////////////////////////////////////////////////////
158// Factory
159///////////////////////////////////////////////////////////////////////////////
160
Chris Craik96a5c4c2015-01-27 15:46:35 -0800161PixelBuffer* PixelBuffer::create(GLenum format,
162 uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700163 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
164 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700165 }
166 return new CpuPixelBuffer(format, width, height);
167}
168
169}; // namespace uirenderer
170}; // namespace android