blob: 910a9889db1fc8ad2f24a22b2aa76d1e097a163b [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080017#include "PixelBuffer.h"
Romain Guycf51a412013-04-08 19:40:31 -070018
Romain Guy9e6f3ac2013-06-20 16:31:35 -070019#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070020#include "Extensions.h"
Romain Guycf51a412013-04-08 19:40:31 -070021#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080022#include "renderstate/RenderState.h"
John Reck2de77712016-01-20 11:09:53 -080023#include "utils/GLUtils.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
25#include <utils/Log.h>
Romain Guycf51a412013-04-08 19:40:31 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// CPU pixel buffer
32///////////////////////////////////////////////////////////////////////////////
33
John Reck1bcacfd2017-11-03 10:12:19 -070034class CpuPixelBuffer : public PixelBuffer {
Romain Guycf51a412013-04-08 19:40:31 -070035public:
36 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070037
Chris Craikd41c4d82015-01-05 15:51:13 -080038 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070039
Chris Craikd41c4d82015-01-05 15:51:13 -080040 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070041
John Reckf3ad3242016-02-24 15:36:35 -080042protected:
43 void unmap() override;
44
Romain Guycf51a412013-04-08 19:40:31 -070045private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080046 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070047};
48
Chris Craik51d6a3d2014-12-22 17:16:56 -080049CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
50 : PixelBuffer(format, width, height)
John Reck1bcacfd2017-11-03 10:12:19 -070051 , mBuffer(new uint8_t[width * height * formatSize(format)]) {}
Romain Guycf51a412013-04-08 19:40:31 -070052
53uint8_t* CpuPixelBuffer::map(AccessMode mode) {
54 if (mAccessMode == kAccessMode_None) {
55 mAccessMode = mode;
56 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080057 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070058}
59
60void CpuPixelBuffer::unmap() {
61 mAccessMode = kAccessMode_None;
62}
63
Romain Guycf51a412013-04-08 19:40:31 -070064void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
John Reck1bcacfd2017-11-03 10:12:19 -070065 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE,
66 &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070067}
68
69///////////////////////////////////////////////////////////////////////////////
70// GPU pixel buffer
71///////////////////////////////////////////////////////////////////////////////
72
John Reck1bcacfd2017-11-03 10:12:19 -070073class GpuPixelBuffer : public PixelBuffer {
Romain Guycf51a412013-04-08 19:40:31 -070074public:
75 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
76 ~GpuPixelBuffer();
77
Chris Craikd41c4d82015-01-05 15:51:13 -080078 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070079
Chris Craikd41c4d82015-01-05 15:51:13 -080080 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070081
John Reckf3ad3242016-02-24 15:36:35 -080082protected:
83 void unmap() override;
84
Romain Guycf51a412013-04-08 19:40:31 -070085private:
86 GLuint mBuffer;
87 uint8_t* mMappedPointer;
88 Caches& mCaches;
89};
90
John Reck1bcacfd2017-11-03 10:12:19 -070091GpuPixelBuffer::GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
Chris Craikd41c4d82015-01-05 15:51:13 -080092 : PixelBuffer(format, width, height)
93 , mMappedPointer(nullptr)
John Reck1bcacfd2017-11-03 10:12:19 -070094 , mCaches(Caches::getInstance()) {
Romain Guycf51a412013-04-08 19:40:31 -070095 glGenBuffers(1, &mBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080096
Chris Craik44eb2c02015-01-29 09:45:09 -080097 mCaches.pixelBufferState().bind(mBuffer);
Chris Craikd41c4d82015-01-05 15:51:13 -080098 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
Chris Craik44eb2c02015-01-29 09:45:09 -080099 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700100}
101
102GpuPixelBuffer::~GpuPixelBuffer() {
103 glDeleteBuffers(1, &mBuffer);
104}
105
106uint8_t* GpuPixelBuffer::map(AccessMode mode) {
107 if (mAccessMode == kAccessMode_None) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800108 mCaches.pixelBufferState().bind(mBuffer);
John Reck1bcacfd2017-11-03 10:12:19 -0700109 mMappedPointer = (uint8_t*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
John Reck2de77712016-01-20 11:09:53 -0800110 if (CC_UNLIKELY(!mMappedPointer)) {
111 GLUtils::dumpGLErrors();
112 LOG_ALWAYS_FATAL("Failed to map PBO");
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700113 }
Romain Guycf51a412013-04-08 19:40:31 -0700114 mAccessMode = mode;
John Reckf3ad3242016-02-24 15:36:35 -0800115 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700116 }
117
118 return mMappedPointer;
119}
120
121void GpuPixelBuffer::unmap() {
122 if (mAccessMode != kAccessMode_None) {
123 if (mMappedPointer) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800124 mCaches.pixelBufferState().bind(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700125 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
126 if (status == GL_FALSE) {
127 ALOGE("Corrupted GPU pixel buffer");
128 }
Romain Guycf51a412013-04-08 19:40:31 -0700129 }
130 mAccessMode = kAccessMode_None;
Chris Craikd41c4d82015-01-05 15:51:13 -0800131 mMappedPointer = nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700132 }
133}
134
Romain Guycf51a412013-04-08 19:40:31 -0700135void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
136 // If the buffer is not mapped, unmap() will not bind it
Chris Craik44eb2c02015-01-29 09:45:09 -0800137 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700138 unmap();
John Reck1bcacfd2017-11-03 10:12:19 -0700139 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE,
140 reinterpret_cast<void*>(offset));
John Reckf3ad3242016-02-24 15:36:35 -0800141 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700142}
143
144///////////////////////////////////////////////////////////////////////////////
145// Factory
146///////////////////////////////////////////////////////////////////////////////
147
John Reck1bcacfd2017-11-03 10:12:19 -0700148PixelBuffer* PixelBuffer::create(GLenum format, uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700149 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
150 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700151 }
152 return new CpuPixelBuffer(format, width, height);
153}
154
John Reck1bcacfd2017-11-03 10:12:19 -0700155}; // namespace uirenderer
156}; // namespace android